diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/RDFServiceUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/RDFServiceUtils.java index c3f1f85a2..673de350f 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/RDFServiceUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/RDFServiceUtils.java @@ -8,6 +8,11 @@ import java.io.UnsupportedEncodingException; import javax.servlet.ServletContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.query.ResultSetFactory; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.sparql.resultset.ResultSetFormat; @@ -16,7 +21,9 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService.ModelSerializationFormat; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService.ResultFormat; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory; +import edu.cornell.mannlib.vitro.webapp.search.solr.ContextNodeFields; public class RDFServiceUtils { @@ -25,6 +32,7 @@ public class RDFServiceUtils { private static final String RDFSERVICEFACTORY_FILTERING_ATTR = RDFServiceUtils.class.getName() + ".RDFServiceFactory.Filtering"; + public static RDFServiceFactory getRDFServiceFactory(ServletContext context) { Object o = context.getAttribute(RDFSERVICEFACTORY_ATTR); return (o instanceof RDFServiceFactory) ? (RDFServiceFactory) o : null; @@ -80,5 +88,20 @@ public class RDFServiceUtils { return getRDFServiceFactory( vreq.getSession().getServletContext()).getRDFService(); } - + + public static ResultSet sparqlSelectQuery(String query, RDFService rdfService) { + + ResultSet resultSet = null; + + try { + InputStream resultStream = rdfService.sparqlSelectQuery(query, RDFService.ResultFormat.JSON); + resultSet = ResultSetFactory.fromJSON(resultStream); + return resultSet; + } catch (RDFServiceException e) { + Log log = LogFactory.getLog(ContextNodeFields.class); + log.error("error executing sparql select query: " + e.getMessage()); + } + + return resultSet; + } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/ContextNodeFields.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/ContextNodeFields.java index dddb0b6d6..b4e3967c9 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/ContextNodeFields.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/ContextNodeFields.java @@ -11,20 +11,16 @@ import org.apache.commons.logging.LogFactory; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.SolrInputField; -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.QuerySolutionMap; import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.query.Syntax; -import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.ResourceFactory; -import com.hp.hpl.jena.shared.Lock; import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory; +import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils; import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames; /** @@ -36,29 +32,21 @@ import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames; * */ public class ContextNodeFields implements DocumentModifier{ - protected Model model; protected List queries = new ArrayList(); - protected boolean shutdown = false; - protected Log log = LogFactory.getLog(ContextNodeFields.class); - - + protected Log log = LogFactory.getLog(ContextNodeFields.class); + protected RDFServiceFactory rdfServiceFactory; + /** * Construct this with a model to query when building Solr Documents and * a list of the SPARQL queries to run. */ - protected ContextNodeFields(Model model, List queries){ - this.model = model; + protected ContextNodeFields(List queries, RDFServiceFactory rdfServiceFactory){ this.queries = queries; + this.rdfServiceFactory = rdfServiceFactory; } - /** - * Implement this method to get values that will be added to ALLTEXT - * field of solr Document for each individual. - * - * @param individual - * @return StringBuffer with text values to add to ALLTEXT field of solr Document. - */ + protected StringBuffer getValues( Individual individual ){ return executeQueryForValues( individual, queries ); } @@ -68,10 +56,10 @@ public class ContextNodeFields implements DocumentModifier{ if( individual == null ) return; - log.debug( "doing context nodes for: " + individual.getURI()); + log.debug( "processing context nodes for: " + individual.getURI()); /* get text from the context nodes and add the to ALLTEXT */ - StringBuffer values = getValues( individual ); + StringBuffer values = executeQueryForValues(individual, queries); SolrInputField field = doc.getField(VitroSearchTermNames.ALLTEXT); if( field == null ){ @@ -81,10 +69,17 @@ public class ContextNodeFields implements DocumentModifier{ } } - + /** + * this method gets values that will be added to ALLTEXT + * field of solr Document for each individual. + * + * @param individual + * @return StringBuffer with text values to add to ALLTEXT field of solr Document. + */ protected StringBuffer executeQueryForValues( Individual individual, Collection queries){ /* execute all the queries on the list and concat the values to add to all text */ + RDFService rdfService = rdfServiceFactory.getRDFService(); StringBuffer allValues = new StringBuffer(""); QuerySolutionMap initialBinding = new QuerySolutionMap(); @@ -93,35 +88,30 @@ public class ContextNodeFields implements DocumentModifier{ for(String query : queries ){ StringBuffer valuesForQuery = new StringBuffer(); - Query sparqlQuery = QueryFactory.create( query, Syntax.syntaxARQ); - model.getLock().enterCriticalSection(Lock.READ); try{ - QueryExecution qExec = - QueryExecutionFactory.create(sparqlQuery, model, initialBinding); - try{ - ResultSet results = qExec.execSelect(); - while(results.hasNext()){ - valuesForQuery.append( - getTextForRow( results.nextSolution() ) ) ; - } - }catch(Throwable t){ - if( ! shutdown ) - log.error(t,t); - } finally{ - qExec.close(); - } - }finally{ - model.getLock().leaveCriticalSection(); - } + ResultSet results = RDFServiceUtils.sparqlSelectQuery(query, rdfService); + + while(results.hasNext()){ + valuesForQuery.append( + getTextForRow( results.nextSolution() ) ) ; + } + + }catch(Throwable t){ + if( ! shutdown ) + log.error(t,t); + } + if(log.isDebugEnabled()){ log.debug("query: '" + query + "'"); log.debug("text for query: '" + valuesForQuery.toString() + "'"); } allValues.append(valuesForQuery); } + + rdfService.close(); return allValues; } - + protected String getTextForRow( QuerySolution row){ if( row == null ) return "";