diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountConstructQueryRunner.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountConstructQueryRunner.java index 42727c24..f50c103c 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountConstructQueryRunner.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountConstructQueryRunner.java @@ -1,9 +1,13 @@ /* $This file is distributed under the terms of the license in /doc/license.txt$ */ package edu.cornell.mannlib.vitro.webapp.visualization.coprincipalinvestigator; +import java.io.InputStream; import java.util.HashSet; import java.util.Set; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException; +import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -30,7 +34,7 @@ public class CoPIGrantCountConstructQueryRunner implements ModelConstructor { private String egoURI; - private Dataset dataset; + private RDFService rdfService; private long before, after; @@ -42,10 +46,9 @@ public class CoPIGrantCountConstructQueryRunner implements ModelConstructor { + "?Grant rdf:type core:Grant ." + "?Grant core:relates ?RelatedRole . "; - public CoPIGrantCountConstructQueryRunner(String egoURI, Dataset dataset, - Log log) { + public CoPIGrantCountConstructQueryRunner(String egoURI, RDFService rdfService, Log log) { this.egoURI = egoURI; - this.dataset = dataset; + this.rdfService = rdfService; // this.log = log; } @@ -220,38 +223,29 @@ public class CoPIGrantCountConstructQueryRunner implements ModelConstructor { return sparqlQuery; } - private Model executeQuery(Set constructQueries, Dataset dataset) { + private Model executeQuery(Set constructQueries, RDFService rdfService) { Model constructedModel = ModelFactory.createDefaultModel(); for (String queryString : constructQueries) { - before = System.currentTimeMillis(); - log.debug("CONSTRUCT query string : " + queryString); - Query query = null; - + InputStream is = null; try { - query = QueryFactory.create( - QueryConstants.getSparqlPrefixQuery() + queryString, - SYNTAX); - } catch (Throwable th) { - log.error("Could not create CONSTRUCT SPARQL query for query " - + "string. " + th.getMessage()); - log.error(queryString); - } - - QueryExecution qe = QueryExecutionFactory.create(query, dataset); - try { - qe.execConstruct(constructedModel); + is = rdfService.sparqlConstructQuery(QueryConstants.getSparqlPrefixQuery() + queryString, RDFService.ModelSerializationFormat.N3); + constructedModel.read(is, null, RDFServiceUtils.getSerializationFormatString(RDFService.ModelSerializationFormat.N3)); + } catch (RDFServiceException e) { + log.error("Unable to execute query", e); + throw new RuntimeException(e); } finally { - qe.close(); + if (is != null) { + try { is.close(); } catch (Throwable t) { } + } } after = System.currentTimeMillis(); - log.debug("Time taken to execute the CONSTRUCT query is in milliseconds: " - + (after - before)); + log.debug("Time taken to execute the CONSTRUCT query is in milliseconds: " + (after - before)); } @@ -282,7 +276,7 @@ public class CoPIGrantCountConstructQueryRunner implements ModelConstructor { populateConstructQueries(constructQueries); - Model model = executeQuery(constructQueries, this.dataset); + Model model = executeQuery(constructQueries, this.rdfService); return model; diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountQueryRunner.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountQueryRunner.java index 09ebd208..4f4262d6 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountQueryRunner.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountQueryRunner.java @@ -2,6 +2,7 @@ package edu.cornell.mannlib.vitro.webapp.visualization.coprincipalinvestigator; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -12,6 +13,9 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import com.hp.hpl.jena.query.ResultSetFactory; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -52,7 +56,7 @@ public class CoPIGrantCountQueryRunner implements QueryRunner private String egoURI; - private Model dataSource; + private RDFService rdfService; private Log log = LogFactory.getLog(CoPIGrantCountQueryRunner.class.getName()); @@ -86,10 +90,10 @@ public class CoPIGrantCountQueryRunner implements QueryRunner public CoPIGrantCountQueryRunner(String egoURI, - Model dataSource, Log log) { + RDFService rdfService, Log log) { this.egoURI = egoURI; - this.dataSource = dataSource; + this.rdfService = rdfService; // this.log = log; this.nodeIDGenerator = new UniqueIDGenerator(); @@ -114,276 +118,134 @@ public class CoPIGrantCountQueryRunner implements QueryRunner + "{ " + "<" + queryURI + "> rdfs:label ?PILabel . " + "{ " - + "<" + queryURI + "> ?Role . " - + "?Role rdf:type core:CoPrincipalInvestigatorRole . " - + "?Role core:relatedBy ?Grant . " - + "?Grant rdf:type core:Grant . " - + "?Grant core:relates ?RelatedRole . " - - + "?RelatedRole rdf:type core:PrincipalInvestigatorRole . " - - + "?RelatedRole ?CoPI . " - + + "?RelatedRole rdf:type core:PrincipalInvestigatorRole . " + + "?RelatedRole ?CoPI . " + "?CoPI rdf:type foaf:Person . " - + "?CoPI rdfs:label ?CoPILabel . " - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_ROLE_DATE_TIME - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_GRANT_DATE_TIME - + "} " - + "UNION " - + "{ " - + "<" + queryURI + "> ?Role . " - + "?Role rdf:type core:CoPrincipalInvestigatorRole . " - + "?Role core:relatedBy ?Grant . " - + "?Grant rdf:type core:Grant . " - + "?Grant core:relates ?RelatedRole . " - - + "?RelatedRole rdf:type core:InvestigatorRole . " - - + "?RelatedRole ?CoPI . " - + + "?RelatedRole rdf:type core:InvestigatorRole . " + + "?RelatedRole ?CoPI . " + "?CoPI rdf:type foaf:Person . " - + "?CoPI rdfs:label ?CoPILabel . " - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_ROLE_DATE_TIME - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_GRANT_DATE_TIME - - + "} " - + "UNION " - + "{ " - + "<" + queryURI + "> ?Role . " - + "?Role rdf:type core:CoPrincipalInvestigatorRole . " - + "?Role core:relatedBy ?Grant . " - + "?Grant rdf:type core:Grant . " - + "?Grant core:relates ?RelatedRole . " - - + "?RelatedRole rdf:type core:CoPrincipalInvestigatorRole . " - - + "?RelatedRole ?CoPI . " - + + "?RelatedRole rdf:type core:CoPrincipalInvestigatorRole . " + + "?RelatedRole ?CoPI . " + "?CoPI rdf:type foaf:Person . " - + "?CoPI rdfs:label ?CoPILabel . " - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_ROLE_DATE_TIME - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_GRANT_DATE_TIME - - + "} " - + "UNION " - - + "{ " - + "<" + queryURI + "> ?Role . " - + "?Role rdf:type core:PrincipalInvestigatorRole . " - + "?Role core:relatedBy ?Grant . " - + "?Grant rdf:type core:Grant . " - + "?Grant core:relates ?RelatedRole . " - - + "?RelatedRole rdf:type core:PrincipalInvestigatorRole . " - - + "?RelatedRole ?CoPI . " - - + "?CoPI rdf:type foaf:Person . " - + + "?RelatedRole rdf:type core:PrincipalInvestigatorRole . " + + "?RelatedRole ?CoPI . " + + "?CoPI rdf:type foaf:Person . " + "?CoPI rdfs:label ?CoPILabel . " - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_ROLE_DATE_TIME - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_GRANT_DATE_TIME - - + "} " - + "UNION " - + "{ " - + "<" + queryURI + "> ?Role . " - + "?Role rdf:type core:PrincipalInvestigatorRole . " - + "?Role core:relatedBy ?Grant . " - + "?Grant rdf:type core:Grant . " - + "?Grant core:relates ?RelatedRole . " - - + "?RelatedRole rdf:type core:InvestigatorRole . " - - + "?RelatedRole ?CoPI . " - + + "?RelatedRole rdf:type core:InvestigatorRole . " + + "?RelatedRole ?CoPI . " + "?CoPI rdf:type foaf:Person . " - + "?CoPI rdfs:label ?CoPILabel . " - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_ROLE_DATE_TIME - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_GRANT_DATE_TIME - - + "} " - + "UNION " - + "{ " - + "<" + queryURI + "> ?Role . " - + "?Role rdf:type core:PrincipalInvestigatorRole . " - + "?Role core:relatedBy ?Grant . " - + "?Grant rdf:type core:Grant . " - + "?Grant core:relates ?RelatedRole . " - - + "?RelatedRole rdf:type core:CoPrincipalInvestigatorRole . " - - + "?RelatedRole ?CoPI . " - + + "?RelatedRole rdf:type core:CoPrincipalInvestigatorRole . " + + "?RelatedRole ?CoPI . " + "?CoPI rdf:type foaf:Person . " - + "?CoPI rdfs:label ?CoPILabel . " - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_ROLE_DATE_TIME - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_GRANT_DATE_TIME - - + "} " - + "UNION " - + "{ " - + "<" + queryURI + "> ?Role . " - + "?Role rdf:type core:InvestigatorRole . " - + "?Role core:relatedBy ?Grant . " - + "?Grant rdf:type core:Grant . " - + "?Grant core:relates ?RelatedRole . " - - + "?RelatedRole rdf:type core:InvestigatorRole . " - - + "?RelatedRole ?CoPI . " - + + "?RelatedRole rdf:type core:InvestigatorRole . " + + "?RelatedRole ?CoPI . " + "?CoPI rdf:type foaf:Person . " - + "?CoPI rdfs:label ?CoPILabel . " - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_ROLE_DATE_TIME - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_GRANT_DATE_TIME - - + "} " - + "UNION " - + "{ " - + "<" + queryURI + "> ?Role . " - + "?Role rdf:type core:InvestigatorRole . " - + "?Role core:relatedBy ?Grant . " - + "?Grant rdf:type core:Grant . " - + "?Grant core:relates ?RelatedRole . " - - + "?RelatedRole rdf:type core:CoPrincipalInvestigatorRole . " - - + "?RelatedRole ?CoPI . " - + + "?RelatedRole rdf:type core:CoPrincipalInvestigatorRole . " + + "?RelatedRole ?CoPI . " + "?CoPI rdf:type foaf:Person . " - + "?CoPI rdfs:label ?CoPILabel . " - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_ROLE_DATE_TIME - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_GRANT_DATE_TIME - - + "} " - + "UNION " - + "{ " - + "<" + queryURI + "> ?Role . " - + "?Role rdf:type core:InvestigatorRole . " - + "?Role core:relatedBy ?Grant . " - + "?Grant rdf:type core:Grant . " - + "?Grant core:relates ?RelatedRole . " - - + "?RelatedRole rdf:type core:PrincipalInvestigatorRole . " - - + "?RelatedRole ?CoPI . " - + + "?RelatedRole rdf:type core:PrincipalInvestigatorRole . " + + "?RelatedRole ?CoPI . " + "?CoPI rdf:type foaf:Person . " - + "?CoPI rdfs:label ?CoPILabel . " - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_ROLE_DATE_TIME - + SPARQL_QUERY_COMMON_OPTIONAL_BLOCK_FOR_GRANT_DATE_TIME - - - + "} " + + "} " + "} "; return sparqlQuery; } - private ResultSet executeQuery(String queryText, Model dataSource) { - - QueryExecution queryExecution = null; - Query query = QueryFactory.create(queryText, SYNTAX); - - queryExecution = QueryExecutionFactory.create(query, dataSource); - return queryExecution.execSelect(); - } - public CollaborationData getQueryResult() throws MalformedQueryParametersException { @@ -431,13 +293,25 @@ public class CoPIGrantCountQueryRunner implements QueryRunner } before = System.currentTimeMillis(); - ResultSet resultSet = executeQuery(generateEgoCoPIquery(this.egoURI), this.dataSource); + + InputStream is = null; + ResultSet rs = null; + try { + is = rdfService.sparqlSelectQuery(generateEgoCoPIquery(this.egoURI), RDFService.ResultFormat.JSON); + rs = ResultSetFactory.fromJSON(is); + data = createQueryResult(rs); + } catch (RDFServiceException e) { + log.error("Unable to execute query", e); + throw new RuntimeException(e); + } finally { + if (is != null) { + try { is.close(); } catch (Throwable t) { } + } + } after = System.currentTimeMillis(); log.debug("Time taken to execute the SELECT queries is in milliseconds: " + (after - before)); - data = createQueryResult(resultSet); - CollaborationDataCacheEntry newEntry = new CollaborationDataCacheEntry(); newEntry.uri = this.egoURI; diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountRequestHandler.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountRequestHandler.java index af314ef1..4804a009 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountRequestHandler.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/coprincipalinvestigator/CoPIGrantCountRequestHandler.java @@ -54,11 +54,11 @@ public class CoPIGrantCountRequestHandler implements VisualizationRequestHandler String visMode = vitroRequest.getParameter(VisualizationFrameworkConstants.VIS_MODE_KEY); ModelConstructor constructQueryRunner = - new CoPIGrantCountConstructQueryRunner(egoURI, dataset, log); + new CoPIGrantCountConstructQueryRunner(egoURI, vitroRequest.getRDFService(), log); Model constructedModel = constructQueryRunner.getConstructedModel(); QueryRunner queryManager = - new CoPIGrantCountQueryRunner(egoURI, constructedModel, log); + new CoPIGrantCountQueryRunner(egoURI, vitroRequest.getRDFService(), log); CollaborationData investigatorNodesAndEdges = queryManager.getQueryResult(); diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/personlevel/PersonLevelRequestHandler.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/personlevel/PersonLevelRequestHandler.java index 9412d752..3484e7bc 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/personlevel/PersonLevelRequestHandler.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/personlevel/PersonLevelRequestHandler.java @@ -104,11 +104,11 @@ public class PersonLevelRequestHandler implements VisualizationRequestHandler { ModelConstructor constructQueryRunner = - new CoPIGrantCountConstructQueryRunner(egoURI, dataset, log); + new CoPIGrantCountConstructQueryRunner(egoURI, vitroRequest.getRDFService(), log); Model constructedModel = constructQueryRunner.getConstructedModel(); QueryRunner coPIQueryManager = - new CoPIGrantCountQueryRunner(egoURI, constructedModel, log); + new CoPIGrantCountQueryRunner(egoURI, vitroRequest.getRDFService(), log); CollaborationData coPIData = coPIQueryManager.getQueryResult();