diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/UserAccountsSelector.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/UserAccountsSelector.java index 987d1564c..840bc51e0 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/UserAccountsSelector.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/UserAccountsSelector.java @@ -111,8 +111,8 @@ public class UserAccountsSelector { .replace("%offset%", offset()); log.debug("main query: " + qString); - List accounts = new SparqlQueryRunner>( - model, new MainQueryParser()).executeQuery(qString); + List 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(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 permissions = new SparqlQueryRunner>(model, - new PermissionsQueryParser()).executeQuery(qString); + Set permissions = new SparqlQueryRunner(model) + .executeSelect(new PermissionsQueryParser(), qString); log.debug("permissions for '" + uri + "': " + permissions); account.setPermissionSetUris(permissions); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java index 40781e278..c3d4ecfa3 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java @@ -94,8 +94,8 @@ public class ProxyRelationshipSelector { PREFIX_LINES); qString = replaceFilterClauses(qString); - int count = new SparqlQueryRunner(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 relationships = new SparqlQueryRunner>( - context.userAccountsModel, new ProxyBasicsParser()) - .executeQuery(qString); + List 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( - 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 profileUris = new SparqlQueryRunner>( - context.userAccountsModel, new RelationshipsParser()) - .executeQuery(qString); + List 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( - 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; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProfilesGetter.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProfilesGetter.java index 3b945d6f5..6ad632d71 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProfilesGetter.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProfilesGetter.java @@ -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(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 diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProxiesGetter.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProxiesGetter.java index 10f78e272..23c239e82 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProxiesGetter.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProxiesGetter.java @@ -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( - userAccountsModel, new BasicProxyInfoParser( - placeholderImageUrl)).executeQuery(queryStr); + JSONArray jsonArray = new SparqlQueryRunner(userAccountsModel) + .executeSelect( + new BasicProxyInfoParser(placeholderImageUrl), + queryStr); String response = jsonArray.toString(); log.debug(response); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryRunner.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryRunner.java index d8afb97ea..d71640aa5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryRunner.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryRunner.java @@ -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 { +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 parser; - public SparqlQueryRunner(OntModel model, QueryParser 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 executeSelect(QueryParser 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 { } } + /** + * 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.