From d55a3380d30a20e7f8eb53eabe72f465e3952875 Mon Sep 17 00:00:00 2001 From: bkoniden Date: Wed, 5 Jan 2011 16:23:53 +0000 Subject: [PATCH] 1) Added the freemarker version of EntityPublicationCount backend (RequestHandler, 2 QueryRunners) 2) Added the ftl version of entity_comparison.jsp (entityComparisonStandaloneActivator.ftl) --- .../EntityPublicationCountRequestHandler.java | 1 + .../EntityPublicationCountQueryRunner.java | 310 ++++++++++++++++++ .../EntityPublicationCountRequestHandler.java | 254 ++++++++++++++ ...EntitySubOrganizationTypesQueryRunner.java | 214 ++++++++++++ 4 files changed, 779 insertions(+) create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountQueryRunner.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntitySubOrganizationTypesQueryRunner.java diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/entitycomparison/EntityPublicationCountRequestHandler.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/entitycomparison/EntityPublicationCountRequestHandler.java index 0be3b8ca..8d5fd4f4 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/entitycomparison/EntityPublicationCountRequestHandler.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/entitycomparison/EntityPublicationCountRequestHandler.java @@ -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", diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountQueryRunner.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountQueryRunner.java new file mode 100644 index 00000000..64048822 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountQueryRunner.java @@ -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 { + + 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 biboDocumentURLToVO = new HashMap(); + Map subentityURLToVO = new HashMap(); + + 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); + } + +} + + + diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java new file mode 100644 index 00000000..fa49e333 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java @@ -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 queryManager = new EntityPublicationCountQueryRunner( + entityURI, dataSource, log, ENTITY_VIS_MODE); + Entity entity = queryManager.getQueryResult(); + setVisModes(); + + QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( + entityURI, dataSource, log, ENTITY_VIS_MODE); + + Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes.getQueryResult(); + + return prepareStandaloneResponse(vitroRequest, + entity,entityURI, subOrganizationTypesResult); + + } + + + @Override + public Map generateDataVisualization( + VitroRequest vitroRequest, Log log, DataSource dataSource) + throws MalformedQueryParametersException { + + String entityURI = vitroRequest + .getParameter(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY); + + QueryRunner queryManager = new EntityPublicationCountQueryRunner( + entityURI, dataSource, log, ENTITY_VIS_MODE); + + Entity entity = queryManager.getQueryResult(); + setVisModes(); + + QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( + entityURI, dataSource, log, ENTITY_VIS_MODE); + + Map> 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 prepareDataResponse(Entity entity, Set subentities, + Map> 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 fileData = new HashMap(); + + 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> subOrganizationTypesResult) { + + Portal portal = vreq.getPortal(); + String standaloneTemplate = "entityComparisonStandaloneActivator.ftl"; + + String jsonContent = ""; + jsonContent = writePublicationsOverTimeJSON(entity.getSubEntities(), subOrganizationTypesResult); + + + + Map body = new HashMap(); + 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 subentities, Map> subOrganizationTypesResult) { +// System.out.println("\nsub entity vis mode ------>" +// + SUB_ENTITY_VIS_MODE + "\n"); + + Gson json = new Gson(); + Set subEntitiesJson = new HashSet(); + + for (SubEntity subentity : subentities) { + JsonObject entityJson = new JsonObject( + subentity.getIndividualLabel()); + + List> yearPubCount = new ArrayList>(); + + for (Map.Entry pubEntry : UtilityFunctions + .getYearToPublicationCount(subentity.getDocuments()) + .entrySet()) { + + List currentPubYear = new ArrayList(); + 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); + } + + } + +} \ No newline at end of file diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntitySubOrganizationTypesQueryRunner.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntitySubOrganizationTypesQueryRunner.java new file mode 100644 index 00000000..5a045909 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntitySubOrganizationTypesQueryRunner.java @@ -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>> { + + 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 subOrganizationTypesToCount = new HashMap(); +// public static Set stopWords = new HashSet(); +// public static Set subOrganizations = new HashSet(); +// public static Set STOP_WORDS = new HashSet() { +// { +// 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> createJavaValueObjects(ResultSet resultSet) { + + Map> subOrganizationLabelToTypes = new HashMap>(); + + 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()); + 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 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> 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); + } + +} +