1) Added the freemarker version of EntityPublicationCount backend (RequestHandler, 2 QueryRunners)

2) Added the ftl version of entity_comparison.jsp (entityComparisonStandaloneActivator.ftl)
This commit is contained in:
bkoniden 2011-01-05 16:23:53 +00:00
parent c1303951ef
commit d55a3380d3
4 changed files with 779 additions and 0 deletions

View file

@ -189,6 +189,7 @@ public class EntityPublicationCountRequestHandler implements
jsonContent = writePublicationsOverTimeJSON(entity.getSubEntities(), subOrganizationTypesResult, log);
request.setAttribute("OrganizationURI", entityURI);
request.setAttribute("OrganizationLabel", entity.getEntityLabel());
request.setAttribute("JsonContent", jsonContent);
request.setAttribute("bodyJsp",

View file

@ -0,0 +1,310 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.entitycomparison;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import com.hp.hpl.jena.iri.IRI;
import com.hp.hpl.jena.iri.IRIFactory;
import com.hp.hpl.jena.iri.Violation;
import com.hp.hpl.jena.query.DataSource;
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.RDFNode;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryFieldLabels;
import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.BiboDocument;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Entity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.SubEntity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.QueryRunner;
/**
* This query runner is used to execute a sparql query that will fetch all the
* publications defined by bibo:Document property for a particular
* department/school/university.
*
* @author bkoniden
*/
public class EntityPublicationCountQueryRunner implements QueryRunner<Entity> {
protected static final Syntax SYNTAX = Syntax.syntaxARQ;
private String entityURI;
private DataSource dataSource;
private Log log;
private String visMode;
private static final String SPARQL_QUERY_COMMON_SELECT_CLAUSE = ""
+ " (str(?Person) as ?personLit) "
+ " (str(?PersonLabel) as ?personLabelLit) "
+ " (str(?SecondaryPositionLabel) as ?SecondaryPositionLabelLit)"
+ " (str(?Document) as ?documentLit) "
+ " (str(?DocumentLabel) as ?documentLabelLit) "
+ " (str(?publicationYear) as ?publicationYearLit) "
+ " (str(?publicationYearMonth) as ?publicationYearMonthLit) "
+ " (str(?publicationDate) as ?publicationDateLit) "
+ " (str(?StartYear) as ?StartYearLit)";
private static final String SPARQL_QUERY_COMMON_WHERE_CLAUSE = ""
+ "?Document rdf:type bibo:Document ;"
+ " rdfs:label ?DocumentLabel ."
+ "OPTIONAL { ?Document core:year ?publicationYear } ."
+ "OPTIONAL { ?Document core:yearMonth ?publicationYearMonth } ."
+ "OPTIONAL { ?Document core:date ?publicationDate } ."
+ "OPTIONAL { ?SecondaryPosition core:startYear ?StartYear } .";
private static String ENTITY_LABEL;
private static String ENTITY_URL;
private static String SUBENTITY_LABEL;
private static String SUBENTITY_URL;
public EntityPublicationCountQueryRunner(String entityURI,
DataSource dataSource, Log log, String visMode) {
this.entityURI = entityURI;
this.dataSource = dataSource;
this.log = log;
this.visMode = visMode;
}
private Entity createJavaValueObjects(ResultSet resultSet) {
Entity entity = null;
Map<String, BiboDocument> biboDocumentURLToVO = new HashMap<String, BiboDocument>();
Map<String, SubEntity> subentityURLToVO = new HashMap<String, SubEntity>();
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.nextSolution();
if (entity == null) {
entity = new Entity(solution.get(ENTITY_URL).toString(),
solution.get(ENTITY_LABEL).toString());
}
RDFNode documentNode = solution.get(QueryFieldLabels.DOCUMENT_URL);
BiboDocument biboDocument;
if (biboDocumentURLToVO.containsKey(documentNode.toString())) {
biboDocument = biboDocumentURLToVO.get(documentNode.toString());
} else {
biboDocument = new BiboDocument(documentNode.toString());
biboDocumentURLToVO.put(documentNode.toString(), biboDocument);
RDFNode documentLabelNode = solution
.get(QueryFieldLabels.DOCUMENT_LABEL);
if (documentLabelNode != null) {
biboDocument.setDocumentLabel(documentLabelNode.toString());
}
RDFNode publicationYearNode = solution
.get(QueryFieldLabels.DOCUMENT_PUBLICATION_YEAR);
if (publicationYearNode != null) {
biboDocument.setPublicationYear(publicationYearNode
.toString());
}
RDFNode publicationYearMonthNode = solution
.get(QueryFieldLabels.DOCUMENT_PUBLICATION_YEAR_MONTH);
if (publicationYearMonthNode != null) {
biboDocument
.setPublicationYearMonth(publicationYearMonthNode
.toString());
}
RDFNode publicationDateNode = solution
.get(QueryFieldLabels.DOCUMENT_PUBLICATION_DATE);
if (publicationDateNode != null) {
biboDocument.setPublicationDate(publicationDateNode
.toString());
}
}
RDFNode subEntityURLNode = solution.get(SUBENTITY_URL);
if (subEntityURLNode != null) {
SubEntity subEntity;
if (subentityURLToVO.containsKey(subEntityURLNode.toString())) {
subEntity = subentityURLToVO.get(subEntityURLNode
.toString());
} else {
subEntity = new SubEntity(subEntityURLNode.toString());
subentityURLToVO
.put(subEntityURLNode.toString(), subEntity);
}
RDFNode subEntityLabelNode = solution.get(SUBENTITY_LABEL);
if (subEntityLabelNode != null) {
subEntity.setIndividualLabel(subEntityLabelNode.toString());
}
entity.addSubEntity(subEntity);
subEntity.addPublications(biboDocument);
}
entity.addPublications(biboDocument);
}
return entity;
}
private ResultSet executeQuery(String queryURI, DataSource dataSource) {
QueryExecution queryExecution = null;
Query query = QueryFactory.create(
getSparqlQuery(queryURI, this.visMode), SYNTAX);
queryExecution = QueryExecutionFactory.create(query, dataSource);
return queryExecution.execSelect();
}
private String getSparqlQuery(String queryURI, String visMode) {
String result = "";
if (visMode.equals("DEPARTMENT")) {
// result = getSparqlQueryForDepartment(queryURI);
ENTITY_URL = QueryFieldLabels.DEPARTMENT_URL;
ENTITY_LABEL = QueryFieldLabels.DEPARTMENT_LABEL;
SUBENTITY_URL = QueryFieldLabels.PERSON_URL;
SUBENTITY_LABEL = QueryFieldLabels.PERSON_LABEL;
} else {
// result = getSparqlQueryForOrganization(queryURI);
ENTITY_URL = QueryFieldLabels.ORGANIZATION_URL;
ENTITY_LABEL = QueryFieldLabels.ORGANIZATION_LABEL;
SUBENTITY_URL = QueryFieldLabels.SUBORGANIZATION_URL;
SUBENTITY_LABEL = QueryFieldLabels.SUBORGANIZATION_LABEL;
}
result = getSparqlQueryForOrganization(queryURI);
return result;
}
// private String getSparqlQueryForDepartment(String queryURI) {
//
// String sparqlQuery = QueryConstants.getSparqlPrefixQuery()
// + "SELECT (str(?DepartmentLabel) as ?departmentLabelLit) "
// + SPARQL_QUERY_COMMON_SELECT_CLAUSE + " (str(<" + queryURI
// + ">) as ?" + QueryFieldLabels.DEPARTMENT_URL + ") "
// + "WHERE { " + "<" + queryURI + "> rdf:type core:Department ;"
// + " rdfs:label ?DepartmentLabel ;"
// + " core:organizationForPosition ?Position . "
// + " ?Position rdf:type core:Position ;"
// + " core:positionForPerson ?Person . "
// + " ?Person core:authorInAuthorship ?Resource ; "
// + " rdfs:label ?PersonLabel ; core:personInPosition ?SecondaryPosition . "
// + " ?Resource core:linkedInformationResource ?Document ."
// + " ?SecondaryPosition rdfs:label ?SecondaryPositionLabel ."
// + SPARQL_QUERY_COMMON_WHERE_CLAUSE + "}"
// + " ORDER BY ?DocumentLabel";
// System.out.println("\nThe sparql query is :\n" + sparqlQuery);
// return sparqlQuery;
//
// }
// private String getSparqlQueryForOrganization(String queryURI) {
//
// String sparqlQuery = QueryConstants.getSparqlPrefixQuery()
// + "SELECT (str(?organizationLabel) as ?organizationLabelLit) "
// + " (str(?subOrganization) as ?subOrganizationLit) "
// + " (str(?subOrganizationLabel) as ?subOrganizationLabelLit) "
// + SPARQL_QUERY_COMMON_SELECT_CLAUSE + " (str(<" + queryURI
// + ">) as ?" + QueryFieldLabels.ORGANIZATION_URL + ") "
// + "WHERE { " + "<" + queryURI + "> rdf:type foaf:Organization ;"
// + " rdfs:label ?organizationLabel ;"
// + " core:hasSubOrganization ?subOrganization ."
// + " ?subOrganization rdfs:label ?subOrganizationLabel ;"
// + " core:organizationForPosition ?Position . "
// + " ?Position rdf:type core:Position ;"
// + " core:positionForPerson ?Person . "
// + " ?Person core:authorInAuthorship ?Resource ; "
// + " rdfs:label ?PersonLabel ; core:personInPosition ?SecondaryPosition . "
// + " ?Resource core:linkedInformationResource ?Document ."
// + " ?SecondaryPosition rdfs:label ?SecondaryPositionLabel ."
// + SPARQL_QUERY_COMMON_WHERE_CLAUSE + "}"
// + " ORDER BY ?DocumentLabel";
// System.out.println("\nThe sparql query is :\n" + sparqlQuery);
// return sparqlQuery;
//
// }
private String getSparqlQueryForOrganization(String queryURI){
String sparqlQuery = QueryConstants.getSparqlPrefixQuery()
+ "SELECT (str(?organizationLabel) as ?organizationLabelLit) "
+ " (str(?subOrganization) as ?subOrganizationLit) "
+ " (str(?subOrganizationLabel) as ?subOrganizationLabelLit) "
+ " (str(?DepartmentLabel) as ?departmentLabelLit) "
+ SPARQL_QUERY_COMMON_SELECT_CLAUSE + " (str(<" + queryURI
+ ">) as ?" + ENTITY_URL + ") "
+ "WHERE { " + "<" + queryURI + "> rdf:type foaf:Organization ;"
+ " rdfs:label ?organizationLabel ."
+ "{ "
+ "<" + queryURI + "> core:hasSubOrganization ?subOrganization ."
+ "?subOrganization rdfs:label ?subOrganizationLabel ; core:organizationForPosition ?Position . "
+ " ?Position rdf:type core:Position ; core:positionForPerson ?Person ."
+ " ?Person core:authorInAuthorship ?Resource ; rdfs:label ?PersonLabel ; core:personInPosition ?SecondaryPosition . "
+ " ?Resource core:linkedInformationResource ?Document . "
+ " ?SecondaryPosition rdfs:label ?SecondaryPositionLabel ."
+ SPARQL_QUERY_COMMON_WHERE_CLAUSE + "}"
+ "UNION "
+ "{ "
+ "<" + queryURI + "> rdf:type core:Department ; rdfs:label ?DepartmentLabel ; core:organizationForPosition ?Position ."
+ " ?Position rdf:type core:Position ; core:positionForPerson ?Person ."
+ " ?Person core:authorInAuthorship ?Resource ; rdfs:label ?PersonLabel ; core:personInPosition ?SecondaryPosition . "
+ " ?Resource core:linkedInformationResource ?Document ."
+ " ?SecondaryPosition rdfs:label ?SecondaryPositionLabel ."
+ SPARQL_QUERY_COMMON_WHERE_CLAUSE + "}"
+ "}";
log.debug("\nThe sparql query is :\n" + sparqlQuery);
return sparqlQuery;
}
public Entity getQueryResult() throws MalformedQueryParametersException {
if (StringUtils.isNotBlank(this.entityURI)) {
/*
* To test for the validity of the URI submitted.
*/
IRIFactory iRIFactory = IRIFactory.jenaImplementation();
IRI iri = iRIFactory.create(this.entityURI);
if (iri.hasViolation(false)) {
String errorMsg = ((Violation) iri.violations(false).next())
.getShortMessage();
log.error("Entity Comparison vis Query " + errorMsg);
throw new MalformedQueryParametersException(
"URI provided for an entity is malformed.");
}
} else {
throw new MalformedQueryParametersException(
"URL parameter is either null or empty.");
}
ResultSet resultSet = executeQuery(this.entityURI, this.dataSource);
return createJavaValueObjects(resultSet);
}
}

View file

@ -0,0 +1,254 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.entitycomparison;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import com.google.gson.Gson;
import com.hp.hpl.jena.query.DataSource;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.visualization.freemarker.VisualizationFrameworkConstants;
import edu.cornell.mannlib.vitro.webapp.controller.visualization.freemarker.DataVisualizationController;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.VOConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Entity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.JsonObject;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.SubEntity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.QueryRunner;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.UtilityFunctions;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.VisualizationRequestHandler;
public class EntityPublicationCountRequestHandler implements
VisualizationRequestHandler {
/*
* Vis container holds the "id" of the div on the final response html page
* that the visualization actually appears on.
*/
public static String ENTITY_VIS_MODE;
public static String SUB_ENTITY_VIS_MODE;
@Override
public ResponseValues generateStandardVisualization(
VitroRequest vitroRequest, Log log, DataSource dataSource)
throws MalformedQueryParametersException {
String entityURI = vitroRequest
.getParameter(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY);
ENTITY_VIS_MODE = vitroRequest
.getParameter(VisualizationFrameworkConstants.VIS_MODE_KEY);
String visContainer = vitroRequest
.getParameter(VisualizationFrameworkConstants.VIS_CONTAINER_KEY);
QueryRunner<Entity> queryManager = new EntityPublicationCountQueryRunner(
entityURI, dataSource, log, ENTITY_VIS_MODE);
Entity entity = queryManager.getQueryResult();
setVisModes();
QueryRunner<Map<String, Set<String>>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner(
entityURI, dataSource, log, ENTITY_VIS_MODE);
Map<String, Set<String>> subOrganizationTypesResult = queryManagerForsubOrganisationTypes.getQueryResult();
return prepareStandaloneResponse(vitroRequest,
entity,entityURI, subOrganizationTypesResult);
}
@Override
public Map<String, String> generateDataVisualization(
VitroRequest vitroRequest, Log log, DataSource dataSource)
throws MalformedQueryParametersException {
String entityURI = vitroRequest
.getParameter(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY);
QueryRunner<Entity> queryManager = new EntityPublicationCountQueryRunner(
entityURI, dataSource, log, ENTITY_VIS_MODE);
Entity entity = queryManager.getQueryResult();
setVisModes();
QueryRunner<Map<String, Set<String>>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner(
entityURI, dataSource, log, ENTITY_VIS_MODE);
Map<String, Set<String>> subOrganizationTypesResult = queryManagerForsubOrganisationTypes.getQueryResult();
return prepareDataResponse(entity, entity.getSubEntities(),subOrganizationTypesResult);
}
@Override
public Object generateAjaxVisualization(VitroRequest vitroRequest, Log log,
DataSource dataSource) throws MalformedQueryParametersException {
throw new UnsupportedOperationException("Temporal Graph does not provide Ajax Response.");
}
/**
* Provides response when json file containing the publication count over the
* years is requested.
*
* @param entity
* @param subentities
* @param subOrganizationTypesResult
*/
private Map<String, String> prepareDataResponse(Entity entity, Set<SubEntity> subentities,
Map<String, Set<String>> subOrganizationTypesResult) {
String entityLabel = entity.getEntityLabel();
/*
* To make sure that null/empty records for entity names do not cause any mischief.
* */
if (StringUtils.isBlank(entityLabel)) {
entityLabel = "no-organization";
}
String outputFileName = UtilityFunctions.slugify(entityLabel)
+ "_publications-per-year" + ".json";
Map<String, String> fileData = new HashMap<String, String>();
fileData.put(DataVisualizationController.FILE_NAME_KEY,
outputFileName);
fileData.put(DataVisualizationController.FILE_CONTENT_TYPE_KEY,
"application/octet-stream");
fileData.put(DataVisualizationController.FILE_CONTENT_KEY,
writePublicationsOverTimeJSON(subentities, subOrganizationTypesResult));
return fileData;
}
/**
*
* @param vreq
* @param valueObjectContainer
* @return
*/
private TemplateResponseValues prepareStandaloneResponse(VitroRequest vreq,
Entity entity, String entityURI, Map<String, Set<String>> subOrganizationTypesResult) {
Portal portal = vreq.getPortal();
String standaloneTemplate = "entityComparisonStandaloneActivator.ftl";
String jsonContent = "";
jsonContent = writePublicationsOverTimeJSON(entity.getSubEntities(), subOrganizationTypesResult);
Map<String, Object> body = new HashMap<String, Object>();
body.put("portalBean", portal);
body.put("title", "Temporal Graph Visualization");
body.put("organizationURI", entityURI);
body.put("organizationLabel", entity.getEntityLabel());
body.put("jsonContent", jsonContent);
return new TemplateResponseValues(standaloneTemplate, body);
}
private void setVisModes() {
if (ENTITY_VIS_MODE.equalsIgnoreCase("DEPARTMENT")) {
SUB_ENTITY_VIS_MODE = "PERSON";
}else if (ENTITY_VIS_MODE.equalsIgnoreCase("SCHOOL")) {
SUB_ENTITY_VIS_MODE = "DEPARTMENT";
}else {
SUB_ENTITY_VIS_MODE = "SCHOOL";
}
}
/**
* function to generate a json file for year <-> publication count mapping
* @param subOrganizationTypesResult
* @param log
*
* @param yearToPublicationCount
* @param responseWriter
* @param visMode
*/
private String writePublicationsOverTimeJSON(Set<SubEntity> subentities, Map<String, Set<String>> subOrganizationTypesResult) {
// System.out.println("\nsub entity vis mode ------>"
// + SUB_ENTITY_VIS_MODE + "\n");
Gson json = new Gson();
Set<JsonObject> subEntitiesJson = new HashSet<JsonObject>();
for (SubEntity subentity : subentities) {
JsonObject entityJson = new JsonObject(
subentity.getIndividualLabel());
List<List<Integer>> yearPubCount = new ArrayList<List<Integer>>();
for (Map.Entry<String, Integer> pubEntry : UtilityFunctions
.getYearToPublicationCount(subentity.getDocuments())
.entrySet()) {
List<Integer> currentPubYear = new ArrayList<Integer>();
if (pubEntry.getKey().equals(
VOConstants.DEFAULT_PUBLICATION_YEAR))
currentPubYear.add(-1);
else
currentPubYear.add(Integer.parseInt(pubEntry.getKey()));
currentPubYear.add(pubEntry.getValue());
yearPubCount.add(currentPubYear);
}
entityJson.setYearToPublicationCount(yearPubCount);
entityJson.getOrganizationType().addAll(subOrganizationTypesResult.get(entityJson.getLabel()));
entityJson.setEntityURI(subentity.getIndividualURI());
setEntityVisMode(entityJson);
//entityJson.setVisMode(SUB_ENTITY_VIS_MODE);
subEntitiesJson.add(entityJson);
}
// System.out.println("\nStopWords are "+ EntitySubOrganizationTypesQueryRunner.stopWords.toString() + "\n");
return json.toJson(subEntitiesJson);
}
private void setEntityVisMode(JsonObject entityJson) {
if(entityJson.getOrganizationType().contains("Department")){
entityJson.setVisMode("DEPARTMENT");
}else if(entityJson.getOrganizationType().contains("School")){
entityJson.setVisMode("SCHOOL");
}else{
entityJson.setVisMode(SUB_ENTITY_VIS_MODE);
}
}
}

View file

@ -0,0 +1,214 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.entitycomparison;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import com.hp.hpl.jena.iri.IRI;
import com.hp.hpl.jena.iri.IRIFactory;
import com.hp.hpl.jena.iri.Violation;
import com.hp.hpl.jena.query.DataSource;
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.RDFNode;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryFieldLabels;
import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.QueryRunner;
import java.util.Set;
import java.util.HashSet;
/**
* @author bkoniden
* Deepak Konidena
*/
public class EntitySubOrganizationTypesQueryRunner implements QueryRunner<Map<String, Set<String>>> {
protected static final Syntax SYNTAX = Syntax.syntaxARQ;
private String entityURI;
private DataSource dataSource;
private Log log;
private String visMode;
static String SUBORGANISATION_LABEL;
static String SUBORGANISATION_TYPE_LABEL;
// public static Map<String, Integer> subOrganizationTypesToCount = new HashMap<String, Integer>();
// public static Set<String> stopWords = new HashSet<String>();
// public static Set<String> subOrganizations = new HashSet<String>();
// public static Set<String> STOP_WORDS = new HashSet<String>() {
// {
// add("Person");
// add("Organization");
// }
// };
private static final String SPARQL_QUERY_SELECT_CLAUSE = ""
+ " (str(?organizationLabel) as ?"+QueryFieldLabels.ORGANIZATION_LABEL+") "
+ " (str(?subOrganizationLabel) as ?"+QueryFieldLabels.SUBORGANIZATION_LABEL+") "
+ " (str(?subOrganizationType) as ?"+QueryFieldLabels.SUBORGANIZATION_TYPE +")"
+ " (str(?subOrganizationTypeLabel) as ?"+QueryFieldLabels.SUBORGANIZATION_TYPE_LABEL+") ";
public EntitySubOrganizationTypesQueryRunner(String entityURI,
DataSource dataSource, Log log, String visMode){
this.entityURI = entityURI;
this.dataSource = dataSource;
this.log = log;
this.visMode = visMode;
// stopWords.clear();
// subOrganizations.clear();
// subOrganizationTypesToCount.clear();
}
private ResultSet executeQuery(String queryURI, DataSource dataSource) {
QueryExecution queryExecution = null;
Query query = QueryFactory.create(
getSparqlQuery(queryURI), SYNTAX);
queryExecution = QueryExecutionFactory.create(query, dataSource);
return queryExecution.execSelect();
}
private String getSparqlQuery(String queryURI) {
String sparqlQuery = "";
if (!this.visMode.equals("DEPARTMENT")) {
SUBORGANISATION_LABEL = QueryFieldLabels.SUBORGANIZATION_LABEL;
SUBORGANISATION_TYPE_LABEL = QueryFieldLabels.SUBORGANIZATION_TYPE_LABEL;
sparqlQuery = QueryConstants.getSparqlPrefixQuery()
+ "SELECT "
+ SPARQL_QUERY_SELECT_CLAUSE
+ " WHERE { "
+ "<"
+ queryURI
+ "> rdf:type foaf:Organization ;"
+ " rdfs:label ?organizationLabel ;"
+ " core:hasSubOrganization ?subOrganization . "
+ " ?subOrganization rdfs:label ?subOrganizationLabel ;"
+ " rdf:type ?subOrganizationType . "
+ " ?subOrganizationType rdfs:label ?subOrganizationTypeLabel ."
+ "}";
} else{
SUBORGANISATION_LABEL = QueryFieldLabels.PERSON_LABEL;
SUBORGANISATION_TYPE_LABEL = QueryFieldLabels.PERSON_TYPE_LABEL;
sparqlQuery = QueryConstants.getSparqlPrefixQuery()
+ "SELECT "
+ " (str(?departmentLabel) as ?"+QueryFieldLabels.DEPARTMENT_LABEL+") "
+ " (str(?personLabel) as ?"+QueryFieldLabels.PERSON_LABEL+") "
+ " (str(?personType) as ?"+QueryFieldLabels.PERSON_TYPE +")"
+ " (str(?personTypeLabel) as ?"+QueryFieldLabels.PERSON_TYPE_LABEL+") "
+ " WHERE { "
+ "<"
+ queryURI
+ "> rdf:type core:Department ;"
+ " rdfs:label ?departmentLabel ;"
+ " core:organizationForPosition ?position . "
+ " ?position rdf:type core:Position ; core:positionForPerson ?person . "
+ " ?person rdfs:label ?personLabel ;"
+ " rdf:type ?personType . "
+ " ?personType rdfs:label ?personTypeLabel ."
+ "}";;
}
log.debug("\nThe sparql query is :\n" + sparqlQuery);
return sparqlQuery;
}
private Map<String, Set<String>> createJavaValueObjects(ResultSet resultSet) {
Map<String, Set<String>> subOrganizationLabelToTypes = new HashMap<String, Set<String>>();
while(resultSet.hasNext()){
QuerySolution solution = resultSet.nextSolution();
RDFNode subOrganizationLabel = solution.get(SUBORGANISATION_LABEL);
if(subOrganizationLabelToTypes.containsKey(subOrganizationLabel.toString())){
RDFNode subOrganizationType = solution.get(SUBORGANISATION_TYPE_LABEL);
if(subOrganizationType != null){
subOrganizationLabelToTypes.get(subOrganizationLabel.toString()).add(subOrganizationType.toString());
// updateSubOrganizationTypesToCount(subOrganizationType.toString());
// subOrganizations.add(subOrganizationLabel.toString());
}
}else{
RDFNode subOrganizationType = solution.get(SUBORGANISATION_TYPE_LABEL);
if(subOrganizationType != null){
subOrganizationLabelToTypes.put(subOrganizationLabel.toString(), new HashSet<String>());
subOrganizationLabelToTypes.get(subOrganizationLabel.toString()).add(subOrganizationType.toString());
// updateSubOrganizationTypesToCount(subOrganizationType.toString());
// subOrganizations.add(subOrganizationLabel.toString());
}
}
}
// collectStopWords();
return subOrganizationLabelToTypes;
}
// private void collectStopWords() {
// System.out.println("Inside collectStopWords \n-----------------------------\n");
// for(Map.Entry<String, Integer> typesCount : subOrganizationTypesToCount.entrySet()){
// System.out.println(typesCount.getKey() + ": "+ typesCount.getValue());
// if(typesCount.getValue() >= subOrganizations.size()){
// stopWords.add(typesCount.getKey());
// }
// }
// }
//
// private void updateSubOrganizationTypesToCount(String typeLabel) {
// int count = 0;
// if(subOrganizationTypesToCount.containsKey(typeLabel)){
// count = subOrganizationTypesToCount.get(typeLabel);
// subOrganizationTypesToCount.put(typeLabel, ++count);
// }else{
// subOrganizationTypesToCount.put(typeLabel, 1);
// }
// }
public Map<String, Set<String>> getQueryResult() throws MalformedQueryParametersException {
if (StringUtils.isNotBlank(this.entityURI)) {
/*
* To test for the validity of the URI submitted.
*/
IRIFactory iRIFactory = IRIFactory.jenaImplementation();
IRI iri = iRIFactory.create(this.entityURI);
if (iri.hasViolation(false)) {
String errorMsg = ((Violation) iri.violations(false).next())
.getShortMessage();
log.error("Entity Comparison sub organization types query " + errorMsg);
throw new MalformedQueryParametersException(
"URI provided for an entity is malformed.");
}
} else {
throw new MalformedQueryParametersException(
"URL parameter is either null or empty.");
}
ResultSet resultSet = executeQuery(this.entityURI, this.dataSource);
return createJavaValueObjects(resultSet);
}
}