From b0b2fb98e1a95d9acedfc857a7398c67c9f2c1f9 Mon Sep 17 00:00:00 2001 From: j2blake Date: Tue, 25 Oct 2011 19:38:31 +0000 Subject: [PATCH] NIHVIVO-2343 Move the SPARQL query execution boilerplate into a utility class so it can be used elsewhere. --- .../accounts/UserAccountsSelector.java | 86 +++------------ .../vitro/webapp/utils/SparqlQueryRunner.java | 102 ++++++++++++++++++ 2 files changed, 117 insertions(+), 71 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryRunner.java 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 ef5395505..c9af01ee0 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 @@ -3,7 +3,6 @@ package edu.cornell.mannlib.vitro.webapp.controller.accounts; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -13,10 +12,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.hp.hpl.jena.ontology.OntModel; -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QueryExecutionFactory; -import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.query.Syntax; @@ -26,6 +21,8 @@ import com.hp.hpl.jena.rdf.model.Resource; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount.Status; import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsOrdering.Field; +import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner; +import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner.QueryParser; /** * Pull some UserAccounts from the model, based on a set of criteria. @@ -72,8 +69,6 @@ public class UserAccountsSelector { + " <%uri%> auth:hasPermissionSet ?ps \n" // + "} \n"; - private static final Syntax SYNTAX = Syntax.syntaxARQ; - /** * If the user enters any of these characters in a search term, escape it * with a backslash. @@ -123,8 +118,8 @@ public class UserAccountsSelector { .replace("%offset%", offset()); log.debug("main query: " + qString); - List accounts = executeQuery(qString, - new MainQueryParser()); + List accounts = new SparqlQueryRunner>( + model, new MainQueryParser()).executeQuery(qString); log.debug("query returns: " + accounts); return accounts; } @@ -138,7 +133,8 @@ public class UserAccountsSelector { .replace("%filterClauses%", filterClauses()); log.debug("count query: " + qString); - int count = executeQuery(qString, new CountQueryParser()); + int count = new SparqlQueryRunner(model, + new CountQueryParser()).executeQuery(qString); log.debug("result count: " + count); return count; } @@ -150,8 +146,8 @@ public class UserAccountsSelector { PREFIX_LINES).replace("%uri%", uri); log.debug("permissions query: " + qString); - Collection permissions = executeQuery(qString, - new PermissionsQueryParser()); + Set permissions = new SparqlQueryRunner>(model, + new PermissionsQueryParser()).executeQuery(qString); log.debug("permissions for '" + uri + "': " + permissions); account.setPermissionSetUris(permissions); } @@ -245,67 +241,15 @@ public class UserAccountsSelector { return String.valueOf(offset); } - private T executeQuery(String queryStr, QueryParser parser) { - 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); - return parser.defaultValue(); - } finally { - if (qe != null) { - qe.close(); - } - } - } - - private static abstract class QueryParser { - abstract T parseResults(String queryStr, ResultSet results); - - abstract T defaultValue(); - - protected String ifLiteralPresent(QuerySolution solution, - String variableName, String defaultValue) { - Literal literal = solution.getLiteral(variableName); - if (literal == null) { - return defaultValue; - } else { - return literal.getString(); - } - } - - protected long ifLongPresent(QuerySolution solution, - String variableName, long defaultValue) { - Literal literal = solution.getLiteral(variableName); - if (literal == null) { - return defaultValue; - } else { - return literal.getLong(); - } - } - - protected int ifIntPresent(QuerySolution solution, String variableName, - int defaultValue) { - Literal literal = solution.getLiteral(variableName); - if (literal == null) { - return defaultValue; - } else { - return literal.getInt(); - } - } - - } - private static class MainQueryParser extends QueryParser> { @Override - public List defaultValue() { + protected List defaultValue() { return Collections.emptyList(); } @Override - public List parseResults(String queryStr, ResultSet results) { + protected List parseResults(String queryStr, + ResultSet results) { List accounts = new ArrayList(); while (results.hasNext()) { try { @@ -359,12 +303,12 @@ public class UserAccountsSelector { private static class CountQueryParser extends QueryParser { @Override - public Integer defaultValue() { + protected Integer defaultValue() { return 0; } @Override - public Integer parseResults(String queryStr, ResultSet results) { + protected Integer parseResults(String queryStr, ResultSet results) { int count = 0; if (!results.hasNext()) { @@ -384,12 +328,12 @@ public class UserAccountsSelector { private static class PermissionsQueryParser extends QueryParser> { @Override - Set defaultValue() { + protected Set defaultValue() { return Collections.emptySet(); } @Override - Set parseResults(String queryStr, ResultSet results) { + protected Set parseResults(String queryStr, ResultSet results) { Set permissions = new HashSet(); while (results.hasNext()) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryRunner.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryRunner.java new file mode 100644 index 000000000..219a17664 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryRunner.java @@ -0,0 +1,102 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.QueryFactory; +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; + +/** + * Execute a SPARQL query. + * + * 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. + */ +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) { + this.model = model; + this.parser = parser; + } + + /** + * Execute the 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) { + 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); + return parser.defaultValue(); + } finally { + if (qe != null) { + qe.close(); + } + } + } + + /** + * This template class provides some parsing methods to help in the + * parseResults() method. + */ + public static abstract class QueryParser { + protected abstract T parseResults(String queryStr, ResultSet results); + + protected abstract T defaultValue(); + + protected String ifLiteralPresent(QuerySolution solution, + String variableName, String defaultValue) { + Literal literal = solution.getLiteral(variableName); + if (literal == null) { + return defaultValue; + } else { + return literal.getString(); + } + } + + protected long ifLongPresent(QuerySolution solution, + String variableName, long defaultValue) { + Literal literal = solution.getLiteral(variableName); + if (literal == null) { + return defaultValue; + } else { + return literal.getLong(); + } + } + + protected int ifIntPresent(QuerySolution solution, String variableName, + int defaultValue) { + Literal literal = solution.getLiteral(variableName); + if (literal == null) { + return defaultValue; + } else { + return literal.getInt(); + } + } + + } + +}