diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/coprincipalinvestigator/CoPIGrantCountQueryRunner.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/coprincipalinvestigator/CoPIGrantCountQueryRunner.java index 0a659498..34f29f60 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/coprincipalinvestigator/CoPIGrantCountQueryRunner.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/coprincipalinvestigator/CoPIGrantCountQueryRunner.java @@ -114,8 +114,6 @@ public class CoPIGrantCountQueryRunner implements QueryRunner { + "UNION " -// + "{ " -// + "<" + queryURI + "> rdfs:label ?PILabel . " + "{ " + "<" + queryURI + "> core:hasCo-PrincipalInvestigatorRole ?Role . " 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 7446e662..5d0ce513 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 @@ -256,7 +256,7 @@ public class EntityPublicationCountQueryRunner implements QueryRunner { if (iri.hasViolation(false)) { String errorMsg = ((Violation) iri.violations(false).next()) .getShortMessage(); - log.error("Entity Pub Count Query Query " + errorMsg); + log.error("Entity Pub Count Query " + errorMsg); throw new MalformedQueryParametersException( "URI provided for an entity is malformed."); } 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 d8862b20..748f87cc 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 @@ -14,19 +14,29 @@ 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 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.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; @@ -42,25 +52,109 @@ public class EntityPublicationCountRequestHandler implements String entityURI = vitroRequest .getParameter(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY); - QueryRunner queryManager = new EntityPublicationCountQueryRunner( - entityURI, dataSource, log); + if(StringUtils.isNotBlank(entityURI)){ - Entity entity = queryManager.getQueryResult(); - - if(entity.getEntityLabel().equals("no-label")){ - - return prepareStandaloneErrorResponse(vitroRequest,entityURI); - - } else { - - QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( + QueryRunner queryManager = new EntityPublicationCountQueryRunner( entityURI, dataSource, log); + + Entity entity = queryManager.getQueryResult(); + + if(entity.getEntityLabel().equals("no-label")){ - Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes + return prepareStandaloneErrorResponse(vitroRequest,entityURI); + + } else { + + QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( + entityURI, dataSource, log); + + Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes + .getQueryResult(); + + return prepareStandaloneResponse(vitroRequest, entity, entityURI, + subOrganizationTypesResult); + } + } 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 { + + QueryRunner queryManager = new EntityPublicationCountQueryRunner( + staffProvidedHighestLevelOrganization, dataSource, log); + + Entity entity = queryManager.getQueryResult(); + + QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( + staffProvidedHighestLevelOrganization, dataSource, log); + + Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes .getQueryResult(); - - return prepareStandaloneResponse(vitroRequest, entity, entityURI, - subOrganizationTypesResult); + + return prepareStandaloneResponse(vitroRequest, entity, staffProvidedHighestLevelOrganization, + subOrganizationTypesResult); + + } + } + + 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 = getHighestLevelOrganizationURI( + highestLevelOrganizationQueryHandler.getQueryResult(), + fieldLabelToOutputFieldLabel); + + QueryRunner queryManager = new EntityPublicationCountQueryRunner( + highestLevelOrgURI, dataSource, log); + + Entity entity = queryManager.getQueryResult(); + + QueryRunner>> queryManagerForsubOrganisationTypes = new EntitySubOrganizationTypesQueryRunner( + highestLevelOrgURI, dataSource, log); + + Map> subOrganizationTypesResult = queryManagerForsubOrganisationTypes + .getQueryResult(); + + return prepareStandaloneResponse(vitroRequest, entity, highestLevelOrgURI, + subOrganizationTypesResult); } } @@ -257,5 +351,45 @@ public class EntityPublicationCountRequestHandler implements return csvFileContent.toString(); } + + 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