NIHVIVO-3797 routing context node fields queries through RDFService

This commit is contained in:
stellamit 2012-06-25 21:34:58 +00:00
parent ac3b58874f
commit 4602b4364d
2 changed files with 58 additions and 45 deletions

View file

@ -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;
@ -81,4 +89,19 @@ public class RDFServiceUtils {
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;
}
}

View file

@ -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<String> queries = new ArrayList<String>();
protected boolean shutdown = false;
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<String> queries){
this.model = model;
protected ContextNodeFields(List<String> 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<String> 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,32 +88,27 @@ 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;
}