From 35487d0f3066abffc34f41ef06270da4708708e6 Mon Sep 17 00:00:00 2001 From: cdtank Date: Mon, 7 Feb 2011 23:58:18 +0000 Subject: [PATCH] 1. Refactored some back-end code for temporal graph vis. 2. Made sure that if no uri is provided to grants temporal graph vis, then the one for highest level is selected. 3. Made sure that any entity (person or organization) directly attached to an organization is considered for temporal graph vis. 4. Changed the page title for temporal graph vis to always start with the label of the entity in question. --- .../EntityComparisonUtilityFunctions.java | 91 ++++++++ .../EntityPublicationCountQueryRunner.java | 7 +- .../EntityPublicationCountRequestHandler.java | 196 ++++++------------ .../EntityGrantCountQueryRunner.java | 80 ++++--- .../EntityGrantCountRequestHandler.java | 119 ++++++++--- .../freemarker/valueobjects/Entity.java | 4 +- .../freemarker/valueobjects/SubEntity.java | 4 +- 7 files changed, 301 insertions(+), 200 deletions(-) create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java new file mode 100644 index 00000000..e2cde700 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java @@ -0,0 +1,91 @@ +package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.entitycomparison; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.logging.Log; + +import com.hp.hpl.jena.query.DataSource; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.RDFNode; + +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.GenericQueryMap; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.GenericQueryRunner; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.QueryRunner; + +public class EntityComparisonUtilityFunctions { + + public static String getHighestLevelOrganizationURI(ResultSet resultSet, + Map fieldLabelToOutputFieldLabel) { + + GenericQueryMap queryResult = new GenericQueryMap(); + + while (resultSet.hasNext()) { + QuerySolution solution = resultSet.nextSolution(); + + RDFNode organizationNode = solution + .get(fieldLabelToOutputFieldLabel.get("organization")); + + if (organizationNode != null) { + queryResult.addEntry( + fieldLabelToOutputFieldLabel.get("organization"), + organizationNode.toString()); + + return organizationNode.toString(); + + } + + RDFNode organizationLabelNode = solution + .get(fieldLabelToOutputFieldLabel.get("organizationLabel")); + + if (organizationLabelNode != null) { + queryResult.addEntry( + fieldLabelToOutputFieldLabel.get("organizationLabel"), + organizationLabelNode.toString()); + } + + RDFNode numberOfChildrenNode = solution.getLiteral("numOfChildren"); + + if (numberOfChildrenNode != null) { + queryResult.addEntry("numOfChildren", String + .valueOf(numberOfChildrenNode.asLiteral().getInt())); + } + } + + return ""; + } + + public static String getHighestLevelOrganizationURI(Log log, DataSource dataSource) + throws MalformedQueryParametersException { + + Map fieldLabelToOutputFieldLabel = new HashMap(); + fieldLabelToOutputFieldLabel.put("organization", + QueryFieldLabels.ORGANIZATION_URL); + fieldLabelToOutputFieldLabel.put("organizationLabel", + QueryFieldLabels.ORGANIZATION_LABEL); + + String aggregationRules = "(count(?organization) AS ?numOfChildren)"; + + String whereClause = "?organization rdf:type foaf:Organization ; rdfs:label ?organizationLabel . \n" + + "OPTIONAL { ?organization core:hasSubOrganization ?subOrg } . \n" + + "OPTIONAL { ?organization core:subOrganizationWithin ?parent } . \n" + + "FILTER ( !bound(?parent) ). \n"; + + String groupOrderClause = "GROUP BY ?organization ?organizationLabel \n" + + "ORDER BY DESC(?numOfChildren)\n" + "LIMIT 1\n"; + + QueryRunner highestLevelOrganizationQueryHandler = new GenericQueryRunner( + fieldLabelToOutputFieldLabel, aggregationRules, whereClause, + groupOrderClause, dataSource, log); + + String highestLevelOrgURI = EntityComparisonUtilityFunctions + .getHighestLevelOrganizationURI( + highestLevelOrganizationQueryHandler.getQueryResult(), + fieldLabelToOutputFieldLabel); + return highestLevelOrgURI; + } + +} 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 index 53359e3d..a0535307 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountQueryRunner.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountQueryRunner.java @@ -87,7 +87,6 @@ public class EntityPublicationCountQueryRunner implements QueryRunner { Map subentityURLToVO = new HashMap(); Map personURLToVO = new HashMap(); - while (resultSet.hasNext()) { QuerySolution solution = resultSet.nextSolution(); @@ -149,7 +148,7 @@ public class EntityPublicationCountQueryRunner implements QueryRunner { entity.addSubEntity(subEntity); - subEntity.addPublications(biboDocument); + subEntity.addPublication(biboDocument); } RDFNode personURLNode = solution.get(QueryFieldLabels.PERSON_URL); @@ -182,11 +181,11 @@ public class EntityPublicationCountQueryRunner implements QueryRunner { } - person.addPublications(biboDocument); + person.addPublication(biboDocument); } - entity.addPublications(biboDocument); + entity.addPublication(biboDocument); } /* 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 index d123b61d..8cacdb65 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityPublicationCountRequestHandler.java @@ -19,25 +19,19 @@ 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.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.rdf.model.RDFNode; import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.beans.Portal; 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.QueryFieldLabels; +import edu.cornell.mannlib.vitro.webapp.controller.visualization.freemarker.VisualizationFrameworkConstants; 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.GenericQueryMap; 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.GenericQueryRunner; 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; @@ -55,28 +49,10 @@ public class EntityPublicationCountRequestHandler implements String entityURI = vitroRequest .getParameter(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY); - if(StringUtils.isNotBlank(entityURI)){ + if (StringUtils.isNotBlank(entityURI)){ - QueryRunner queryManager = new EntityPublicationCountQueryRunner( - entityURI, dataSource, log); - - Entity entity = queryManager.getQueryResult(); - - if(entity.getEntityLabel().equals("no-label")){ - - return prepareStandaloneErrorResponse(vitroRequest,entityURI); - - } else { - - QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( - entityURI, dataSource, log); - - Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes - .getQueryResult(); - - return prepareStandaloneResponse(vitroRequest, entity, entityURI, - subOrganizationTypesResult); - } + return getSubjectEntityAndGenerateResponse(vitroRequest, log, + dataSource, entityURI); } else { String staffProvidedHighestLevelOrganization = ConfigurationProperties.getProperty("visualization.topLevelOrg"); @@ -100,80 +76,59 @@ public class EntityPublicationCountRequestHandler implements } else { - QueryRunner queryManager = new EntityPublicationCountQueryRunner( - staffProvidedHighestLevelOrganization, dataSource, log); - - Entity entity = queryManager.getQueryResult(); - - if(entity.getEntityLabel().equals("no-label")){ - - return prepareStandaloneErrorResponse(vitroRequest,staffProvidedHighestLevelOrganization); - - } else { - - QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( - staffProvidedHighestLevelOrganization, dataSource, log); - - Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes - .getQueryResult(); - - return prepareStandaloneResponse(vitroRequest, entity, staffProvidedHighestLevelOrganization, - subOrganizationTypesResult); - } + return getSubjectEntityAndGenerateResponse(vitroRequest, + log, dataSource, + staffProvidedHighestLevelOrganization); } } - Map fieldLabelToOutputFieldLabel = new HashMap(); - fieldLabelToOutputFieldLabel.put("organization", - QueryFieldLabels.ORGANIZATION_URL); - fieldLabelToOutputFieldLabel.put("organizationLabel", QueryFieldLabels.ORGANIZATION_LABEL); + String highestLevelOrgURI = EntityComparisonUtilityFunctions.getHighestLevelOrganizationURI(log, + dataSource); - String aggregationRules = "(count(?organization) AS ?numOfChildren)"; - - String whereClause = "?organization rdf:type foaf:Organization ; rdfs:label ?organizationLabel . \n" - + "OPTIONAL { ?organization core:hasSubOrganization ?subOrg } . \n" - + "OPTIONAL { ?organization core:subOrganizationWithin ?parent } . \n" - + "FILTER ( !bound(?parent) ). \n"; - - String groupOrderClause = "GROUP BY ?organization ?organizationLabel \n" - + "ORDER BY DESC(?numOfChildren)\n" - + "LIMIT 1\n"; - - QueryRunner highestLevelOrganizationQueryHandler = - new GenericQueryRunner(fieldLabelToOutputFieldLabel, - aggregationRules, - whereClause, - groupOrderClause, - dataSource, log); - - - String highestLevelOrgURI = getHighestLevelOrganizationURI( - highestLevelOrganizationQueryHandler.getQueryResult(), - fieldLabelToOutputFieldLabel); - - QueryRunner queryManager = new EntityPublicationCountQueryRunner( - highestLevelOrgURI, dataSource, log); - - Entity entity = queryManager.getQueryResult(); - - if(entity.getEntityLabel().equals("no-label")){ - - return prepareStandaloneErrorResponse(vitroRequest,highestLevelOrgURI); - - } else { - - QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( - highestLevelOrgURI, dataSource, log); - - Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes - .getQueryResult(); - - return prepareStandaloneResponse(vitroRequest, entity, highestLevelOrgURI, - subOrganizationTypesResult); - } + return getSubjectEntityAndGenerateResponse(vitroRequest, log, + dataSource, highestLevelOrgURI); } } + + + private ResponseValues getSubjectEntityAndGenerateResponse( + VitroRequest vitroRequest, Log log, DataSource dataSource, + String subjectEntityURI) + throws MalformedQueryParametersException { + + QueryRunner queryManager = new EntityPublicationCountQueryRunner( + subjectEntityURI, dataSource, log); + + Entity entity = queryManager.getQueryResult(); + + if (entity.getEntityLabel().equals("no-label")) { + + return prepareStandaloneErrorResponse(vitroRequest, subjectEntityURI); + + } else { + + return getSubEntityTypesAndRenderStandaloneResponse( + vitroRequest, log, dataSource, + subjectEntityURI, entity); + } + } + + + private ResponseValues getSubEntityTypesAndRenderStandaloneResponse( + VitroRequest vitroRequest, Log log, DataSource dataSource, + String subjectEntityURI, Entity entity) + throws MalformedQueryParametersException { + + QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( + subjectEntityURI, dataSource, log); + + Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes + .getQueryResult(); + + return prepareStandaloneResponse(vitroRequest, entity, subjectEntityURI, + subOrganizationTypesResult); + } @Override @@ -256,11 +211,15 @@ public class EntityPublicationCountRequestHandler implements String jsonContent = ""; jsonContent = writePublicationsOverTimeJSON(vreq, entity.getSubEntities(), subOrganizationTypesResult); + String title = ""; + if (StringUtils.isNotBlank(entity.getEntityLabel())) { + title = entity.getEntityLabel() + " - "; + } Map body = new HashMap(); body.put("portalBean", portal); - body.put("title", "Temporal Graph Visualization"); + body.put("title", title + "Temporal Graph Visualization"); body.put("organizationURI", entityURI); body.put("organizationLabel", entity.getEntityLabel()); body.put("jsonContent", jsonContent); @@ -292,7 +251,10 @@ public class EntityPublicationCountRequestHandler implements * @param subentities * @param subOrganizationTypesResult */ - private String writePublicationsOverTimeJSON(VitroRequest vreq, Set subentities, Map> subOrganizationTypesResult) { + private String writePublicationsOverTimeJSON(VitroRequest vreq, + Set subentities, + Map> + subOrganizationTypesResult) { Gson json = new Gson(); Set subEntitiesJson = new HashSet(); @@ -368,44 +330,4 @@ public class EntityPublicationCountRequestHandler implements } - private String getHighestLevelOrganizationURI(ResultSet resultSet, - Map fieldLabelToOutputFieldLabel) { - - GenericQueryMap queryResult = new GenericQueryMap(); - - - while (resultSet.hasNext()) { - QuerySolution solution = resultSet.nextSolution(); - - - RDFNode organizationNode = solution.get( - fieldLabelToOutputFieldLabel - .get("organization")); - - if (organizationNode != null) { - queryResult.addEntry(fieldLabelToOutputFieldLabel.get("organization"), organizationNode.toString()); - - return organizationNode.toString(); - - } - - RDFNode organizationLabelNode = solution.get( - fieldLabelToOutputFieldLabel - .get("organizationLabel")); - - if (organizationLabelNode != null) { - queryResult.addEntry(fieldLabelToOutputFieldLabel.get("organizationLabel"), organizationLabelNode.toString()); - } - - RDFNode numberOfChildrenNode = solution.getLiteral("numOfChildren"); - - if (numberOfChildrenNode != null) { - queryResult.addEntry("numOfChildren", String.valueOf(numberOfChildrenNode.asLiteral().getInt())); - } - } - - return ""; - } - - } \ No newline at end of file diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitygrantcount/EntityGrantCountQueryRunner.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitygrantcount/EntityGrantCountQueryRunner.java index 5ed4f380..91bc36e1 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitygrantcount/EntityGrantCountQueryRunner.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitygrantcount/EntityGrantCountQueryRunner.java @@ -7,6 +7,7 @@ import java.util.Map; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import com.hp.hpl.jena.iri.IRI; import com.hp.hpl.jena.iri.IRIFactory; @@ -24,8 +25,8 @@ 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.Grant; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Entity; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Grant; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.SubEntity; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.QueryRunner; @@ -44,7 +45,7 @@ public class EntityGrantCountQueryRunner implements QueryRunner { private String entityURI; private DataSource dataSource; - private Log log; + private Log log = LogFactory.getLog(EntityGrantCountQueryRunner.class.getName()); private static final String SPARQL_QUERY_COMMON_SELECT_CLAUSE = "SELECT " @@ -101,7 +102,6 @@ public class EntityGrantCountQueryRunner implements QueryRunner { this.entityURI = entityURI; this.dataSource = dataSource; - this.log = log; } @@ -138,24 +138,28 @@ public class EntityGrantCountQueryRunner implements QueryRunner { grant.setGrantLabel(grantLabelNode.toString()); } - RDFNode grantStartDateNode = solution.get(QueryFieldLabels.ROLE_START_DATE); - if(grantStartDateNode != null){ + RDFNode grantStartDateNode = solution + .get(QueryFieldLabels.ROLE_START_DATE); + if (grantStartDateNode != null) { grant.setGrantStartDate(grantStartDateNode.toString()); - }else { - grantStartDateNode = solution.get(QueryFieldLabels.GRANT_START_DATE); - if(grantStartDateNode != null){ + } else { + grantStartDateNode = solution + .get(QueryFieldLabels.GRANT_START_DATE); + if (grantStartDateNode != null) { grant.setGrantStartDate(grantStartDateNode.toString()); } } - - RDFNode grantEndDateNode = solution.get(QueryFieldLabels.ROLE_END_DATE); - if(grantEndDateNode != null){ + + RDFNode grantEndDateNode = solution + .get(QueryFieldLabels.ROLE_END_DATE); + if (grantEndDateNode != null) { grant.setGrantEndDate(grantEndDateNode.toString()); - }else { - grantEndDateNode = solution.get(QueryFieldLabels.GRANT_END_DATE); - if(grantEndDateNode != null){ + } else { + grantEndDateNode = solution + .get(QueryFieldLabels.GRANT_END_DATE); + if (grantEndDateNode != null) { grant.setGrantEndDate(grantEndDateNode.toString()); - } + } } } @@ -178,39 +182,53 @@ public class EntityGrantCountQueryRunner implements QueryRunner { subEntity.setIndividualLabel(subEntityLabelNode.toString()); } entity.addSubEntity(subEntity); - subEntity.addGrants(grant); + subEntity.addGrant(grant); } - - + RDFNode personURLNode = solution.get(QueryFieldLabels.PERSON_URL); - - if(personURLNode != null){ - SubEntity person ; - if(personURLToVO.containsKey(personURLNode.toString())) { + + if (personURLNode != null) { + SubEntity person; + + if (personURLToVO.containsKey(personURLNode.toString())) { person = personURLToVO.get(personURLNode.toString()); } else { person = new SubEntity(personURLNode.toString()); personURLToVO.put(personURLNode.toString(), person); } + + RDFNode personLabelNode = solution + .get(QueryFieldLabels.PERSON_LABEL); - RDFNode personLabelNode = solution.get(QueryFieldLabels.PERSON_LABEL); if (personLabelNode != null) { person.setIndividualLabel(personLabelNode.toString()); } + + /* + * This makes sure that either, + * 1. the parent organization is a department-like organization with no organizations + * beneath it, or + * 2. the parent organizations has both sub-organizations and people directly + * attached to that organizations e.g. president of a university. + * */ + if (subEntityURLNode == null) { + + entity.addSubEntity(person); + + } -// entity.addSubEntity(person); - person.addGrants(grant); + person.addGrant(grant); } - - entity.addGrants(grant); + + entity.addGrant(grant); } - - if(subentityURLToVO.size() == 0 && personURLToVO.size() != 0){ - for(SubEntity person : personURLToVO.values()){ + + /*if (subentityURLToVO.size() == 0 && personURLToVO.size() != 0) { + for (SubEntity person : personURLToVO.values()) { entity.addSubEntity(person); } - } else if (subentityURLToVO.size() == 0 && personURLToVO.size() == 0){ + } else */if (subentityURLToVO.size() == 0 && personURLToVO.size() == 0) { entity = new Entity(this.entityURI, "no-label"); } diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitygrantcount/EntityGrantCountRequestHandler.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitygrantcount/EntityGrantCountRequestHandler.java index 21619dc1..00b416eb 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitygrantcount/EntityGrantCountRequestHandler.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitygrantcount/EntityGrantCountRequestHandler.java @@ -14,21 +14,26 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import com.google.gson.Gson; +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 edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.beans.Portal; 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.controller.visualization.freemarker.VisualizationFrameworkConstants; 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.entitycomparison.EntityComparisonUtilityFunctions; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.entitycomparison.EntitySubOrganizationTypesQueryRunner; 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.entitycomparison.EntitySubOrganizationTypesQueryRunner; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.UtilityFunctions; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.VisualizationRequestHandler; @@ -44,29 +49,50 @@ public class EntityGrantCountRequestHandler implements String entityURI = vitroRequest .getParameter(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY); - QueryRunner queryManager = new EntityGrantCountQueryRunner( - entityURI, dataSource, log); - - Entity entity = queryManager.getQueryResult(); - - if(entity.getEntityLabel().equals("no-label")){ - - return prepareStandaloneErrorResponse(vitroRequest,entityURI); - - } else{ + if (StringUtils.isNotBlank(entityURI)){ - QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( - entityURI, dataSource, log); - - Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes - .getQueryResult(); - - return prepareStandaloneResponse(vitroRequest, entity, entityURI, - subOrganizationTypesResult); + return getSubjectEntityAndGenerateResponse(vitroRequest, log, + dataSource, entityURI); + + } else { + + String staffProvidedHighestLevelOrganization = ConfigurationProperties.getProperty("visualization.topLevelOrg"); + + /* + * First checking if the staff has provided highest level organization in deploy.properties + * if so use to temporal graph vis. + */ + if (StringUtils.isNotBlank(staffProvidedHighestLevelOrganization)) { + + /* + * To test for the validity of the URI submitted. + */ + IRIFactory iRIFactory = IRIFactory.jenaImplementation(); + IRI iri = iRIFactory.create(staffProvidedHighestLevelOrganization); + + if (iri.hasViolation(false)) { + + String errorMsg = ((Violation) iri.violations(false).next()).getShortMessage(); + log.error("Highest Level Organization URI provided is invalid " + errorMsg); + + } else { + + return getSubjectEntityAndGenerateResponse(vitroRequest, + log, dataSource, + staffProvidedHighestLevelOrganization); + } + } + + String highestLevelOrgURI = EntityComparisonUtilityFunctions.getHighestLevelOrganizationURI(log, + dataSource); + + return getSubjectEntityAndGenerateResponse(vitroRequest, log, + dataSource, highestLevelOrgURI); } + } - + @Override public Map generateDataVisualization( VitroRequest vitroRequest, Log log, DataSource dataSource) @@ -96,6 +122,44 @@ public class EntityGrantCountRequestHandler implements throw new UnsupportedOperationException("Entity Grant Count does not provide Ajax Response."); } + private ResponseValues getSubjectEntityAndGenerateResponse( + VitroRequest vitroRequest, Log log, DataSource dataSource, + String subjectEntityURI) + throws MalformedQueryParametersException { + + QueryRunner queryManager = new EntityGrantCountQueryRunner( + subjectEntityURI, dataSource, log); + + Entity entity = queryManager.getQueryResult(); + + if (entity.getEntityLabel().equals("no-label")) { + + return prepareStandaloneErrorResponse(vitroRequest, subjectEntityURI); + + } else { + + return getSubEntityTypesAndRenderStandaloneResponse( + vitroRequest, log, dataSource, + subjectEntityURI, entity); + } + } + + private ResponseValues getSubEntityTypesAndRenderStandaloneResponse( + VitroRequest vitroRequest, Log log, DataSource dataSource, + String subjectOrganization, Entity entity) + throws MalformedQueryParametersException { + + QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( + subjectOrganization, dataSource, log); + + Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes + .getQueryResult(); + + return prepareStandaloneResponse(vitroRequest, entity, subjectOrganization, + subOrganizationTypesResult); + } + + /** * Provides response when json file containing the grant count over the * years is requested. @@ -146,11 +210,16 @@ public class EntityGrantCountRequestHandler implements String jsonContent = ""; jsonContent = writeGrantsOverTimeJSON(vreq, entity.getSubEntities(), subOrganizationTypesResult); + String title = ""; + if (StringUtils.isNotBlank(entity.getEntityLabel())) { + title = entity.getEntityLabel() + " - "; + } + Map body = new HashMap(); body.put("portalBean", portal); - body.put("title", "Temporal Graph Visualization"); + body.put("title", title + "Temporal Graph Visualization"); body.put("organizationURI", entityURI); body.put("organizationLabel", entity.getEntityLabel()); body.put("jsonContent", jsonContent); @@ -197,10 +266,12 @@ public class EntityGrantCountRequestHandler implements List currentGrantYear = new ArrayList(); if (grantEntry.getKey().equals( - VOConstants.DEFAULT_GRANT_YEAR)) + VOConstants.DEFAULT_GRANT_YEAR)) { currentGrantYear.add(-1); - else + } else { currentGrantYear.add(Integer.parseInt(grantEntry.getKey())); + } + currentGrantYear.add(grantEntry.getValue()); yearGrantCount.add(currentGrantYear); } diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/valueobjects/Entity.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/valueobjects/Entity.java index db6729a6..3726a16d 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/valueobjects/Entity.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/valueobjects/Entity.java @@ -41,7 +41,7 @@ public class Entity extends Individual{ return children; } - public void addPublications(BiboDocument biboDocument) { + public void addPublication(BiboDocument biboDocument) { this.publications.add(biboDocument); } @@ -55,7 +55,7 @@ public class Entity extends Individual{ } - public void addGrants(Grant grant) { + public void addGrant(Grant grant) { this.grants.add(grant); } diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/valueobjects/SubEntity.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/valueobjects/SubEntity.java index 4a86259e..4dad26fa 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/valueobjects/SubEntity.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/valueobjects/SubEntity.java @@ -63,12 +63,12 @@ public class SubEntity extends Individual { return this.getIndividualLabel(); } - public void addPublications(BiboDocument biboDocument) { + public void addPublication(BiboDocument biboDocument) { this.publications.add(biboDocument); } - public void addGrants(Grant grant) { + public void addGrant(Grant grant) { this.grants.add(grant); } }