NIHVIVO-3467 Restructure SparqlQueryRunner to handle both Select and Construct queries.
This commit is contained in:
parent
78ad75cc6d
commit
4a917603f2
5 changed files with 70 additions and 51 deletions
|
@ -111,8 +111,8 @@ public class UserAccountsSelector {
|
|||
.replace("%offset%", offset());
|
||||
log.debug("main query: " + qString);
|
||||
|
||||
List<UserAccount> accounts = new SparqlQueryRunner<List<UserAccount>>(
|
||||
model, new MainQueryParser()).executeQuery(qString);
|
||||
List<UserAccount> accounts = new SparqlQueryRunner(model)
|
||||
.executeSelect(new MainQueryParser(), qString);
|
||||
log.debug("query returns: " + accounts);
|
||||
return accounts;
|
||||
}
|
||||
|
@ -126,8 +126,8 @@ public class UserAccountsSelector {
|
|||
.replace("%filterClauses%", filterClauses());
|
||||
log.debug("count query: " + qString);
|
||||
|
||||
int count = new SparqlQueryRunner<Integer>(model,
|
||||
new CountQueryParser()).executeQuery(qString);
|
||||
int count = new SparqlQueryRunner(model).executeSelect(
|
||||
new CountQueryParser(), qString);
|
||||
log.debug("result count: " + count);
|
||||
return count;
|
||||
}
|
||||
|
@ -139,8 +139,8 @@ public class UserAccountsSelector {
|
|||
PREFIX_LINES).replace("%uri%", uri);
|
||||
log.debug("permissions query: " + qString);
|
||||
|
||||
Set<String> permissions = new SparqlQueryRunner<Set<String>>(model,
|
||||
new PermissionsQueryParser()).executeQuery(qString);
|
||||
Set<String> permissions = new SparqlQueryRunner(model)
|
||||
.executeSelect(new PermissionsQueryParser(), qString);
|
||||
log.debug("permissions for '" + uri + "': " + permissions);
|
||||
account.setPermissionSetUris(permissions);
|
||||
}
|
||||
|
|
|
@ -94,8 +94,8 @@ public class ProxyRelationshipSelector {
|
|||
PREFIX_LINES);
|
||||
qString = replaceFilterClauses(qString);
|
||||
|
||||
int count = new SparqlQueryRunner<Integer>(context.userAccountsModel,
|
||||
new CountQueryParser()).executeQuery(qString);
|
||||
int count = new SparqlQueryRunner(context.userAccountsModel)
|
||||
.executeSelect(new CountQueryParser(), qString);
|
||||
|
||||
log.debug("result count: " + count);
|
||||
builder.count = count;
|
||||
|
@ -107,8 +107,8 @@ public class ProxyRelationshipSelector {
|
|||
return q.replace("%filterClause%", "");
|
||||
} else {
|
||||
String clean = SparqlQueryUtils.escapeForRegex(searchTerm);
|
||||
return q.replace("%filterClause%",
|
||||
"FILTER (REGEX(str(?label), '^" + clean + "', 'i'))");
|
||||
return q.replace("%filterClause%", "FILTER (REGEX(str(?label), '^"
|
||||
+ clean + "', 'i'))");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -136,9 +136,9 @@ public class ProxyRelationshipSelector {
|
|||
.replace("%offset%", offset());
|
||||
qString = replaceFilterClauses(qString);
|
||||
|
||||
List<Relationship> relationships = new SparqlQueryRunner<List<Relationship>>(
|
||||
context.userAccountsModel, new ProxyBasicsParser())
|
||||
.executeQuery(qString);
|
||||
List<Relationship> relationships = new SparqlQueryRunner(
|
||||
context.userAccountsModel).executeSelect(
|
||||
new ProxyBasicsParser(), qString);
|
||||
log.debug("getProxyBasics returns: " + relationships);
|
||||
builder.relationships.addAll(relationships);
|
||||
}
|
||||
|
@ -177,9 +177,8 @@ public class ProxyRelationshipSelector {
|
|||
.replace("%matchingProperty%", context.matchingProperty)
|
||||
.replace("%externalAuthId%", proxy.externalAuthId);
|
||||
|
||||
ItemInfo expansion = new SparqlQueryRunner<ItemInfo>(
|
||||
context.unionModel, new ExpandProxyParser())
|
||||
.executeQuery(qString);
|
||||
ItemInfo expansion = new SparqlQueryRunner(context.unionModel)
|
||||
.executeSelect(new ExpandProxyParser(), qString);
|
||||
proxy.classLabel = expansion.classLabel;
|
||||
proxy.imageUrl = expansion.imageUrl;
|
||||
}
|
||||
|
@ -200,9 +199,9 @@ public class ProxyRelationshipSelector {
|
|||
String qString = QUERY_RELATIONSHIPS.replace("%prefixes%",
|
||||
PREFIX_LINES).replace("%proxyUri%", proxy.uri);
|
||||
|
||||
List<String> profileUris = new SparqlQueryRunner<List<String>>(
|
||||
context.userAccountsModel, new RelationshipsParser())
|
||||
.executeQuery(qString);
|
||||
List<String> profileUris = new SparqlQueryRunner(
|
||||
context.userAccountsModel).executeSelect(
|
||||
new RelationshipsParser(), qString);
|
||||
|
||||
for (String profileUri : profileUris) {
|
||||
r.profileInfos
|
||||
|
@ -236,9 +235,8 @@ public class ProxyRelationshipSelector {
|
|||
String qString = QUERY_EXPAND_PROFILE.replace("%prefixes%",
|
||||
PREFIX_LINES).replace("%profileUri%", profile.uri);
|
||||
|
||||
ItemInfo expansion = new SparqlQueryRunner<ItemInfo>(
|
||||
context.unionModel, new ExpandProfileParser())
|
||||
.executeQuery(qString);
|
||||
ItemInfo expansion = new SparqlQueryRunner(context.unionModel)
|
||||
.executeSelect(new ExpandProfileParser(), qString);
|
||||
profile.label = expansion.label;
|
||||
profile.classLabel = expansion.classLabel;
|
||||
profile.imageUrl = expansion.imageUrl;
|
||||
|
|
|
@ -20,9 +20,6 @@ import com.hp.hpl.jena.query.QuerySolution;
|
|||
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.ajax.AbstractAjaxResponder;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.ImageUtil;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryUtils;
|
||||
|
||||
|
@ -50,7 +47,6 @@ public class BasicProfilesGetter extends AbstractAjaxResponder {
|
|||
|
||||
private final String term;
|
||||
private final OntModel fullModel;
|
||||
private final String placeholderImageUrl;
|
||||
|
||||
public BasicProfilesGetter(HttpServlet servlet, VitroRequest vreq,
|
||||
HttpServletResponse resp) {
|
||||
|
@ -58,9 +54,6 @@ public class BasicProfilesGetter extends AbstractAjaxResponder {
|
|||
fullModel = vreq.getJenaOntModel();
|
||||
|
||||
term = getStringParameter(PARAMETER_SEARCH_TERM, "");
|
||||
|
||||
placeholderImageUrl = UrlBuilder.getUrl(ImageUtil
|
||||
.getPlaceholderImagePathForType(VitroVocabulary.USERACCOUNT));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -73,8 +66,8 @@ public class BasicProfilesGetter extends AbstractAjaxResponder {
|
|||
String queryStr = QUERY_BASIC_PROFILES.replace("%typesUnion%",
|
||||
buildTypeClause()).replace("%term%", cleanTerm);
|
||||
|
||||
JSONArray jsonArray = new SparqlQueryRunner<JSONArray>(fullModel,
|
||||
new BasicProfileInfoParser()).executeQuery(queryStr);
|
||||
JSONArray jsonArray = new SparqlQueryRunner(fullModel)
|
||||
.executeSelect(new BasicProfileInfoParser(), queryStr);
|
||||
|
||||
String response = jsonArray.toString();
|
||||
log.debug(response);
|
||||
|
@ -91,11 +84,10 @@ public class BasicProfilesGetter extends AbstractAjaxResponder {
|
|||
for (int i = 1; i < types.length; i++) {
|
||||
typeClause += " UNION { ?uri rdf:type <" + types[i].trim() + "> }";
|
||||
}
|
||||
|
||||
|
||||
return typeClause;
|
||||
}
|
||||
|
||||
|
||||
/** Parse a query row into a map of keys and values. */
|
||||
private static class BasicProfileInfoParser extends JsonArrayParser {
|
||||
@Override
|
||||
|
|
|
@ -77,9 +77,10 @@ public class BasicProxiesGetter extends AbstractAjaxResponder {
|
|||
String cleanTerm = SparqlQueryUtils.escapeForRegex(term);
|
||||
String queryStr = QUERY_BASIC_PROXIES.replace("%term%", cleanTerm);
|
||||
|
||||
JSONArray jsonArray = new SparqlQueryRunner<JSONArray>(
|
||||
userAccountsModel, new BasicProxyInfoParser(
|
||||
placeholderImageUrl)).executeQuery(queryStr);
|
||||
JSONArray jsonArray = new SparqlQueryRunner(userAccountsModel)
|
||||
.executeSelect(
|
||||
new BasicProxyInfoParser(placeholderImageUrl),
|
||||
queryStr);
|
||||
|
||||
String response = jsonArray.toString();
|
||||
log.debug(response);
|
||||
|
|
|
@ -14,43 +14,50 @@ import com.hp.hpl.jena.query.QuerySolution;
|
|||
import com.hp.hpl.jena.query.ResultSet;
|
||||
import com.hp.hpl.jena.query.Syntax;
|
||||
import com.hp.hpl.jena.rdf.model.Literal;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
|
||||
/**
|
||||
* Execute a SPARQL query.
|
||||
* Execute SPARQL queries against a model.
|
||||
*
|
||||
* Take the model and a parser in the constructor. Then execute as many queries
|
||||
* as desired, with the query contained in a String.
|
||||
*
|
||||
* If there is an exception while parsing the query, executing the query, or
|
||||
* parsing the results, log the exception and return the parser's default value.
|
||||
* The query enbvironment is closed properly in any case.
|
||||
* Take the model in the constructor. Then execute as many queries as desired,
|
||||
* with the query contained in a String. Exceptions are handled in a tidy
|
||||
* manner, and the query environment is closed properly in any case.
|
||||
*/
|
||||
public class SparqlQueryRunner<T> {
|
||||
public class SparqlQueryRunner {
|
||||
private static final Log log = LogFactory.getLog(SparqlQueryRunner.class);
|
||||
|
||||
private static final Syntax SYNTAX = Syntax.syntaxARQ;
|
||||
|
||||
private final OntModel model;
|
||||
private final QueryParser<T> parser;
|
||||
|
||||
public SparqlQueryRunner(OntModel model, QueryParser<T> parser) {
|
||||
public SparqlQueryRunner(OntModel model) {
|
||||
if (model == null) {
|
||||
throw new NullPointerException("model may not be null.");
|
||||
}
|
||||
this.model = model;
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the query and parse the results, closing and cleaning up
|
||||
* Execute the SELECT query and parse the results, closing and cleaning up
|
||||
* afterward. If an exception occurs, return the parser's default value.
|
||||
*/
|
||||
public T executeQuery(String queryStr) {
|
||||
log.debug("query is: '" + queryStr + "'");
|
||||
public <T> T executeSelect(QueryParser<T> parser, String queryStr) {
|
||||
if (parser == null) {
|
||||
throw new NullPointerException("parser may not be null.");
|
||||
}
|
||||
if (queryStr == null) {
|
||||
throw new NullPointerException("queryStr may not be null.");
|
||||
}
|
||||
|
||||
log.debug("select query is: '" + queryStr + "'");
|
||||
QueryExecution qe = null;
|
||||
try {
|
||||
Query query = QueryFactory.create(queryStr, SYNTAX);
|
||||
qe = QueryExecutionFactory.create(query, model);
|
||||
return parser.parseResults(queryStr, qe.execSelect());
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to execute the query: " + queryStr, e);
|
||||
log.error("Failed to execute the Select query: " + queryStr, e);
|
||||
return parser.defaultValue();
|
||||
} finally {
|
||||
if (qe != null) {
|
||||
|
@ -59,6 +66,27 @@ public class SparqlQueryRunner<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the CONSTRUCT query and return the resulting model. If an
|
||||
* exception occurs, return an empty model.
|
||||
*/
|
||||
public Model executeConstruct(String queryStr) {
|
||||
log.debug("construct query is: '" + queryStr + "'");
|
||||
QueryExecution qe = null;
|
||||
try {
|
||||
Query query = QueryFactory.create(queryStr, SYNTAX);
|
||||
qe = QueryExecutionFactory.create(query, model);
|
||||
return qe.execConstruct();
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to execute the Construct query: " + queryStr, e);
|
||||
return ModelFactory.createDefaultModel();
|
||||
} finally {
|
||||
if (qe != null) {
|
||||
qe.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This template class provides some parsing methods to help in the
|
||||
* parseResults() method.
|
||||
|
|
Loading…
Add table
Reference in a new issue