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());
|
.replace("%offset%", offset());
|
||||||
log.debug("main query: " + qString);
|
log.debug("main query: " + qString);
|
||||||
|
|
||||||
List<UserAccount> accounts = new SparqlQueryRunner<List<UserAccount>>(
|
List<UserAccount> accounts = new SparqlQueryRunner(model)
|
||||||
model, new MainQueryParser()).executeQuery(qString);
|
.executeSelect(new MainQueryParser(), qString);
|
||||||
log.debug("query returns: " + accounts);
|
log.debug("query returns: " + accounts);
|
||||||
return accounts;
|
return accounts;
|
||||||
}
|
}
|
||||||
|
@ -126,8 +126,8 @@ public class UserAccountsSelector {
|
||||||
.replace("%filterClauses%", filterClauses());
|
.replace("%filterClauses%", filterClauses());
|
||||||
log.debug("count query: " + qString);
|
log.debug("count query: " + qString);
|
||||||
|
|
||||||
int count = new SparqlQueryRunner<Integer>(model,
|
int count = new SparqlQueryRunner(model).executeSelect(
|
||||||
new CountQueryParser()).executeQuery(qString);
|
new CountQueryParser(), qString);
|
||||||
log.debug("result count: " + count);
|
log.debug("result count: " + count);
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
@ -139,8 +139,8 @@ public class UserAccountsSelector {
|
||||||
PREFIX_LINES).replace("%uri%", uri);
|
PREFIX_LINES).replace("%uri%", uri);
|
||||||
log.debug("permissions query: " + qString);
|
log.debug("permissions query: " + qString);
|
||||||
|
|
||||||
Set<String> permissions = new SparqlQueryRunner<Set<String>>(model,
|
Set<String> permissions = new SparqlQueryRunner(model)
|
||||||
new PermissionsQueryParser()).executeQuery(qString);
|
.executeSelect(new PermissionsQueryParser(), qString);
|
||||||
log.debug("permissions for '" + uri + "': " + permissions);
|
log.debug("permissions for '" + uri + "': " + permissions);
|
||||||
account.setPermissionSetUris(permissions);
|
account.setPermissionSetUris(permissions);
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,8 +94,8 @@ public class ProxyRelationshipSelector {
|
||||||
PREFIX_LINES);
|
PREFIX_LINES);
|
||||||
qString = replaceFilterClauses(qString);
|
qString = replaceFilterClauses(qString);
|
||||||
|
|
||||||
int count = new SparqlQueryRunner<Integer>(context.userAccountsModel,
|
int count = new SparqlQueryRunner(context.userAccountsModel)
|
||||||
new CountQueryParser()).executeQuery(qString);
|
.executeSelect(new CountQueryParser(), qString);
|
||||||
|
|
||||||
log.debug("result count: " + count);
|
log.debug("result count: " + count);
|
||||||
builder.count = count;
|
builder.count = count;
|
||||||
|
@ -107,8 +107,8 @@ public class ProxyRelationshipSelector {
|
||||||
return q.replace("%filterClause%", "");
|
return q.replace("%filterClause%", "");
|
||||||
} else {
|
} else {
|
||||||
String clean = SparqlQueryUtils.escapeForRegex(searchTerm);
|
String clean = SparqlQueryUtils.escapeForRegex(searchTerm);
|
||||||
return q.replace("%filterClause%",
|
return q.replace("%filterClause%", "FILTER (REGEX(str(?label), '^"
|
||||||
"FILTER (REGEX(str(?label), '^" + clean + "', 'i'))");
|
+ clean + "', 'i'))");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,9 +136,9 @@ public class ProxyRelationshipSelector {
|
||||||
.replace("%offset%", offset());
|
.replace("%offset%", offset());
|
||||||
qString = replaceFilterClauses(qString);
|
qString = replaceFilterClauses(qString);
|
||||||
|
|
||||||
List<Relationship> relationships = new SparqlQueryRunner<List<Relationship>>(
|
List<Relationship> relationships = new SparqlQueryRunner(
|
||||||
context.userAccountsModel, new ProxyBasicsParser())
|
context.userAccountsModel).executeSelect(
|
||||||
.executeQuery(qString);
|
new ProxyBasicsParser(), qString);
|
||||||
log.debug("getProxyBasics returns: " + relationships);
|
log.debug("getProxyBasics returns: " + relationships);
|
||||||
builder.relationships.addAll(relationships);
|
builder.relationships.addAll(relationships);
|
||||||
}
|
}
|
||||||
|
@ -177,9 +177,8 @@ public class ProxyRelationshipSelector {
|
||||||
.replace("%matchingProperty%", context.matchingProperty)
|
.replace("%matchingProperty%", context.matchingProperty)
|
||||||
.replace("%externalAuthId%", proxy.externalAuthId);
|
.replace("%externalAuthId%", proxy.externalAuthId);
|
||||||
|
|
||||||
ItemInfo expansion = new SparqlQueryRunner<ItemInfo>(
|
ItemInfo expansion = new SparqlQueryRunner(context.unionModel)
|
||||||
context.unionModel, new ExpandProxyParser())
|
.executeSelect(new ExpandProxyParser(), qString);
|
||||||
.executeQuery(qString);
|
|
||||||
proxy.classLabel = expansion.classLabel;
|
proxy.classLabel = expansion.classLabel;
|
||||||
proxy.imageUrl = expansion.imageUrl;
|
proxy.imageUrl = expansion.imageUrl;
|
||||||
}
|
}
|
||||||
|
@ -200,9 +199,9 @@ public class ProxyRelationshipSelector {
|
||||||
String qString = QUERY_RELATIONSHIPS.replace("%prefixes%",
|
String qString = QUERY_RELATIONSHIPS.replace("%prefixes%",
|
||||||
PREFIX_LINES).replace("%proxyUri%", proxy.uri);
|
PREFIX_LINES).replace("%proxyUri%", proxy.uri);
|
||||||
|
|
||||||
List<String> profileUris = new SparqlQueryRunner<List<String>>(
|
List<String> profileUris = new SparqlQueryRunner(
|
||||||
context.userAccountsModel, new RelationshipsParser())
|
context.userAccountsModel).executeSelect(
|
||||||
.executeQuery(qString);
|
new RelationshipsParser(), qString);
|
||||||
|
|
||||||
for (String profileUri : profileUris) {
|
for (String profileUri : profileUris) {
|
||||||
r.profileInfos
|
r.profileInfos
|
||||||
|
@ -236,9 +235,8 @@ public class ProxyRelationshipSelector {
|
||||||
String qString = QUERY_EXPAND_PROFILE.replace("%prefixes%",
|
String qString = QUERY_EXPAND_PROFILE.replace("%prefixes%",
|
||||||
PREFIX_LINES).replace("%profileUri%", profile.uri);
|
PREFIX_LINES).replace("%profileUri%", profile.uri);
|
||||||
|
|
||||||
ItemInfo expansion = new SparqlQueryRunner<ItemInfo>(
|
ItemInfo expansion = new SparqlQueryRunner(context.unionModel)
|
||||||
context.unionModel, new ExpandProfileParser())
|
.executeSelect(new ExpandProfileParser(), qString);
|
||||||
.executeQuery(qString);
|
|
||||||
profile.label = expansion.label;
|
profile.label = expansion.label;
|
||||||
profile.classLabel = expansion.classLabel;
|
profile.classLabel = expansion.classLabel;
|
||||||
profile.imageUrl = expansion.imageUrl;
|
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.config.ConfigurationProperties;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.ajax.AbstractAjaxResponder;
|
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.SparqlQueryRunner;
|
||||||
import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryUtils;
|
import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryUtils;
|
||||||
|
|
||||||
|
@ -50,7 +47,6 @@ public class BasicProfilesGetter extends AbstractAjaxResponder {
|
||||||
|
|
||||||
private final String term;
|
private final String term;
|
||||||
private final OntModel fullModel;
|
private final OntModel fullModel;
|
||||||
private final String placeholderImageUrl;
|
|
||||||
|
|
||||||
public BasicProfilesGetter(HttpServlet servlet, VitroRequest vreq,
|
public BasicProfilesGetter(HttpServlet servlet, VitroRequest vreq,
|
||||||
HttpServletResponse resp) {
|
HttpServletResponse resp) {
|
||||||
|
@ -58,9 +54,6 @@ public class BasicProfilesGetter extends AbstractAjaxResponder {
|
||||||
fullModel = vreq.getJenaOntModel();
|
fullModel = vreq.getJenaOntModel();
|
||||||
|
|
||||||
term = getStringParameter(PARAMETER_SEARCH_TERM, "");
|
term = getStringParameter(PARAMETER_SEARCH_TERM, "");
|
||||||
|
|
||||||
placeholderImageUrl = UrlBuilder.getUrl(ImageUtil
|
|
||||||
.getPlaceholderImagePathForType(VitroVocabulary.USERACCOUNT));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -73,8 +66,8 @@ public class BasicProfilesGetter extends AbstractAjaxResponder {
|
||||||
String queryStr = QUERY_BASIC_PROFILES.replace("%typesUnion%",
|
String queryStr = QUERY_BASIC_PROFILES.replace("%typesUnion%",
|
||||||
buildTypeClause()).replace("%term%", cleanTerm);
|
buildTypeClause()).replace("%term%", cleanTerm);
|
||||||
|
|
||||||
JSONArray jsonArray = new SparqlQueryRunner<JSONArray>(fullModel,
|
JSONArray jsonArray = new SparqlQueryRunner(fullModel)
|
||||||
new BasicProfileInfoParser()).executeQuery(queryStr);
|
.executeSelect(new BasicProfileInfoParser(), queryStr);
|
||||||
|
|
||||||
String response = jsonArray.toString();
|
String response = jsonArray.toString();
|
||||||
log.debug(response);
|
log.debug(response);
|
||||||
|
@ -95,7 +88,6 @@ public class BasicProfilesGetter extends AbstractAjaxResponder {
|
||||||
return typeClause;
|
return typeClause;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Parse a query row into a map of keys and values. */
|
/** Parse a query row into a map of keys and values. */
|
||||||
private static class BasicProfileInfoParser extends JsonArrayParser {
|
private static class BasicProfileInfoParser extends JsonArrayParser {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -77,9 +77,10 @@ public class BasicProxiesGetter extends AbstractAjaxResponder {
|
||||||
String cleanTerm = SparqlQueryUtils.escapeForRegex(term);
|
String cleanTerm = SparqlQueryUtils.escapeForRegex(term);
|
||||||
String queryStr = QUERY_BASIC_PROXIES.replace("%term%", cleanTerm);
|
String queryStr = QUERY_BASIC_PROXIES.replace("%term%", cleanTerm);
|
||||||
|
|
||||||
JSONArray jsonArray = new SparqlQueryRunner<JSONArray>(
|
JSONArray jsonArray = new SparqlQueryRunner(userAccountsModel)
|
||||||
userAccountsModel, new BasicProxyInfoParser(
|
.executeSelect(
|
||||||
placeholderImageUrl)).executeQuery(queryStr);
|
new BasicProxyInfoParser(placeholderImageUrl),
|
||||||
|
queryStr);
|
||||||
|
|
||||||
String response = jsonArray.toString();
|
String response = jsonArray.toString();
|
||||||
log.debug(response);
|
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.ResultSet;
|
||||||
import com.hp.hpl.jena.query.Syntax;
|
import com.hp.hpl.jena.query.Syntax;
|
||||||
import com.hp.hpl.jena.rdf.model.Literal;
|
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
|
* Take the model in the constructor. Then execute as many queries as desired,
|
||||||
* as desired, with the query contained in a String.
|
* with the query contained in a String. Exceptions are handled in a tidy
|
||||||
*
|
* manner, and the query environment is closed properly in any case.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
public class SparqlQueryRunner<T> {
|
public class SparqlQueryRunner {
|
||||||
private static final Log log = LogFactory.getLog(SparqlQueryRunner.class);
|
private static final Log log = LogFactory.getLog(SparqlQueryRunner.class);
|
||||||
|
|
||||||
private static final Syntax SYNTAX = Syntax.syntaxARQ;
|
private static final Syntax SYNTAX = Syntax.syntaxARQ;
|
||||||
|
|
||||||
private final OntModel model;
|
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.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.
|
* afterward. If an exception occurs, return the parser's default value.
|
||||||
*/
|
*/
|
||||||
public T executeQuery(String queryStr) {
|
public <T> T executeSelect(QueryParser<T> parser, String queryStr) {
|
||||||
log.debug("query is: '" + 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;
|
QueryExecution qe = null;
|
||||||
try {
|
try {
|
||||||
Query query = QueryFactory.create(queryStr, SYNTAX);
|
Query query = QueryFactory.create(queryStr, SYNTAX);
|
||||||
qe = QueryExecutionFactory.create(query, model);
|
qe = QueryExecutionFactory.create(query, model);
|
||||||
return parser.parseResults(queryStr, qe.execSelect());
|
return parser.parseResults(queryStr, qe.execSelect());
|
||||||
} catch (Exception e) {
|
} 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();
|
return parser.defaultValue();
|
||||||
} finally {
|
} finally {
|
||||||
if (qe != null) {
|
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
|
* This template class provides some parsing methods to help in the
|
||||||
* parseResults() method.
|
* parseResults() method.
|
||||||
|
|
Loading…
Add table
Reference in a new issue