From c4244301d23fe84e1a403de90dae811f6f6d8f21 Mon Sep 17 00:00:00 2001 From: cdtank Date: Wed, 16 Mar 2011 19:16:28 +0000 Subject: [PATCH] 1. Refactored the code inside cached impl of temporal vis of pubs. Made the selects on models to be generic enough so that they can be used by other visualizations. 2. Changed the logic that was used to determine the type of a subentity. --- .../visualization/constants/VOConstants.java | 4 +- .../EntityComparisonUtilityFunctions.java | 19 ++ .../EntityPublicationRequestHandler.java | 309 +---------------- .../freemarker/valueobjects/SubEntity.java | 18 +- .../visutils/SelectOnModelUtilities.java | 312 ++++++++++++++++++ 5 files changed, 366 insertions(+), 296 deletions(-) create mode 100644 src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/SelectOnModelUtilities.java diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/constants/VOConstants.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/constants/VOConstants.java index 792f54ac..597aee33 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/constants/VOConstants.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/constants/VOConstants.java @@ -22,8 +22,8 @@ public class VOConstants { /* * Employee related constants * */ - public static enum EmployeeType { - ACADEMIC_FACULTY_EMPLOYEE, ACADEMIC_STAFF_EMPLOYEE + public static enum EntityClassType { + ORGANIZATION, PERSON, UNKNOWN } public static final int NUM_CHARS_IN_YEAR_FORMAT = 4; 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 index 1ba6c8ff..44edd3cf 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/EntityComparisonUtilityFunctions.java @@ -22,6 +22,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao; 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.Entity; 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.ModelConstructor; @@ -166,4 +167,22 @@ public class EntityComparisonUtilityFunctions { * */ return EntityComparisonUtilityFunctions.getHighestLevelOrganizationURI(log, dataset); } + + public static Entity mergeEntityIfShareSameURI(Entity entityA, Entity entityB) { + + if (StringUtils.equalsIgnoreCase(entityA.getEntityURI(), entityB.getEntityURI())) { + + Entity mergedEntity = new Entity(entityA.getEntityURI(), entityA.getEntityLabel()); + + mergedEntity.addSubEntitities(entityA.getSubEntities()); + mergedEntity.addSubEntitities(entityB.getSubEntities()); + + return mergedEntity; + + } else { + return null; + } + + } + } diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/cached/EntityPublicationRequestHandler.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/cached/EntityPublicationRequestHandler.java index 6f1ecb97..638fd2a4 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/cached/EntityPublicationRequestHandler.java +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/entitycomparison/cached/EntityPublicationRequestHandler.java @@ -3,7 +3,6 @@ package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.entitycomparison.cached; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -16,10 +15,7 @@ import org.apache.commons.logging.Log; import com.google.gson.Gson; import com.hp.hpl.jena.query.Dataset; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.RDFNode; import edu.cornell.mannlib.vitro.webapp.beans.Portal; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; @@ -27,22 +23,15 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.Res import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues; 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.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.entitycomparison.EntityComparisonUtilityFunctions; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.ModelConstructorUtilities; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.OrganizationAssociatedPeopleModelWithTypesConstructor; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.OrganizationModelWithTypesConstructor; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.OrganizationToPublicationsForSubOrganizationsModelConstructor; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.PersonToPublicationsModelConstructor; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Activity; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.ConstructedModelTracker; 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.GenericQueryRunnerOnModel; -import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.QueryRunner; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.SelectOnModelUtilities; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.UtilityFunctions; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.VisualizationRequestHandler; @@ -90,8 +79,8 @@ public class EntityPublicationRequestHandler implements throws MalformedQueryParametersException { - Entity organizationEntity = getSubjectOrganizationHierarchy(dataset, - subjectEntityURI); + Entity organizationEntity = SelectOnModelUtilities + .getSubjectOrganizationHierarchy(dataset, subjectEntityURI); if (organizationEntity.getSubEntities() == null) { @@ -106,14 +95,20 @@ public class EntityPublicationRequestHandler implements Map documentURIForAssociatedPeopleTOVO = new HashMap(); Map allDocumentURIToVOs = new HashMap(); - allDocumentURIToVOs = getPublicationsForAllSubOrganizations(dataset, organizationEntity); + allDocumentURIToVOs = SelectOnModelUtilities.getPublicationsForAllSubOrganizations(dataset, organizationEntity); - Collection associatedPeople = getSubjectOrganizationAssociatedPeople(dataset, subjectEntityURI); + Entity organizationWithAssociatedPeople = SelectOnModelUtilities + .getSubjectOrganizationAssociatedPeople(dataset, subjectEntityURI); + - if (!associatedPeople.isEmpty()) { + if (organizationWithAssociatedPeople.getSubEntities() != null) { - documentURIForAssociatedPeopleTOVO = getPublicationsForAssociatedPeople(dataset, associatedPeople); - organizationEntity.addSubEntitities(associatedPeople); + documentURIForAssociatedPeopleTOVO = SelectOnModelUtilities + .getPublicationsForAssociatedPeople(dataset, organizationWithAssociatedPeople.getSubEntities()); + + organizationEntity = EntityComparisonUtilityFunctions.mergeEntityIfShareSameURI( + organizationEntity, + organizationWithAssociatedPeople); } if (allDocumentURIToVOs.isEmpty() && documentURIForAssociatedPeopleTOVO.isEmpty()) { @@ -182,275 +177,6 @@ public class EntityPublicationRequestHandler implements return fileData; } - - private Map getPublicationsForAllSubOrganizations( - Dataset dataset, Entity organizationEntity) - throws MalformedQueryParametersException { - Map allDocumentURIToVOs = new HashMap(); - - for (SubEntity subOrganization : organizationEntity.getSubEntities()) { - - Model subOrganizationPublicationsModel = ModelConstructorUtilities - .getOrConstructModel( - subOrganization.getIndividualURI(), - OrganizationToPublicationsForSubOrganizationsModelConstructor.MODEL_TYPE, - dataset); - - System.out.println("getting publications for " + subOrganization.getIndividualLabel()); - - Map fieldLabelToOutputFieldLabel = new HashMap(); - fieldLabelToOutputFieldLabel.put("document", QueryFieldLabels.DOCUMENT_URL); - fieldLabelToOutputFieldLabel.put("documentLabel", QueryFieldLabels.DOCUMENT_LABEL); - fieldLabelToOutputFieldLabel.put("documentPublicationDate", QueryFieldLabels.DOCUMENT_PUBLICATION_DATE); - - String whereClause = "" - + " <" + subOrganization.getIndividualURI() + "> vivosocnet:hasPersonWithPublication ?document . " - + " ?document rdfs:label ?documentLabel . " - + " OPTIONAL { " - + " ?document core:dateTimeValue ?dateTimeValue . " - + " ?dateTimeValue core:dateTime ?documentPublicationDate } . "; - - QueryRunner subOrganizationPublicationsQuery = - new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel, - "", - whereClause, - "", - subOrganizationPublicationsModel); - - subOrganization.addActivities(getPublicationForEntity( - subOrganizationPublicationsQuery.getQueryResult(), - allDocumentURIToVOs)); - - } - return allDocumentURIToVOs; - } - - - private Map getPublicationsForAssociatedPeople( - Dataset dataset, Collection people) - throws MalformedQueryParametersException { - Map allDocumentURIToVOs = new HashMap(); - - for (SubEntity person : people) { - - Model personPublicationsModel = ModelConstructorUtilities - .getOrConstructModel( - person.getIndividualURI(), - PersonToPublicationsModelConstructor.MODEL_TYPE, - dataset); - - System.out.println("getting publications for " + person.getIndividualLabel()); - - Map fieldLabelToOutputFieldLabel = new HashMap(); - fieldLabelToOutputFieldLabel.put("document", QueryFieldLabels.DOCUMENT_URL); - fieldLabelToOutputFieldLabel.put("documentLabel", QueryFieldLabels.DOCUMENT_LABEL); - fieldLabelToOutputFieldLabel.put("documentPublicationDate", QueryFieldLabels.DOCUMENT_PUBLICATION_DATE); - - String whereClause = "" - + " <" + person.getIndividualURI() + "> vivosocnet:hasPublication ?document . " - + " ?document rdfs:label ?documentLabel . " - + " OPTIONAL { " - + " ?document core:dateTimeValue ?dateTimeValue . " - + " ?dateTimeValue core:dateTime ?documentPublicationDate } . "; - - QueryRunner personPublicationsQuery = - new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel, - "", - whereClause, - "", - personPublicationsModel); - - person.addActivities(getPublicationForEntity( - personPublicationsQuery.getQueryResult(), - allDocumentURIToVOs)); - - } - return allDocumentURIToVOs; - } - - private Entity getSubjectOrganizationHierarchy(Dataset dataset, - String subjectEntityURI) throws MalformedQueryParametersException { - Model organizationModel = ModelConstructorUtilities - .getOrConstructModel( - null, - OrganizationModelWithTypesConstructor.MODEL_TYPE, - dataset); - - Map fieldLabelToOutputFieldLabel = new HashMap(); - fieldLabelToOutputFieldLabel.put("organizationLabel", QueryFieldLabels.ORGANIZATION_LABEL); - fieldLabelToOutputFieldLabel.put("subOrganization", QueryFieldLabels.SUBORGANIZATION_URL); - fieldLabelToOutputFieldLabel.put("subOrganizationLabel", QueryFieldLabels.SUBORGANIZATION_LABEL); - fieldLabelToOutputFieldLabel.put("subOrganizationTypeLabel", QueryFieldLabels.SUBORGANIZATION_TYPE_LABEL); - - String whereClause = "" - + " <" + subjectEntityURI + "> rdfs:label ?organizationLabel . " - + " <" + subjectEntityURI + "> core:hasSubOrganization ?subOrganization . " - + " ?subOrganization rdfs:label ?subOrganizationLabel . " - + " ?subOrganization rdf:type ?subOrgType . " - + " ?subOrgType rdfs:label ?subOrganizationTypeLabel . "; - - QueryRunner subOrganizationsWithTypesQuery = - new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel, - "", - whereClause, - "", - organizationModel); - - Entity organizationEntity = generateEntity(subjectEntityURI, - subOrganizationsWithTypesQuery.getQueryResult()); - return organizationEntity; - } - - private Collection getSubjectOrganizationAssociatedPeople(Dataset dataset, - String subjectEntityURI) throws MalformedQueryParametersException { - Model associatedPeopleModel = ModelConstructorUtilities - .getOrConstructModel( - subjectEntityURI, - OrganizationAssociatedPeopleModelWithTypesConstructor.MODEL_TYPE, - dataset); - - Map fieldLabelToOutputFieldLabel = new HashMap(); - fieldLabelToOutputFieldLabel.put("organizationLabel", QueryFieldLabels.ORGANIZATION_LABEL); - fieldLabelToOutputFieldLabel.put("person", QueryFieldLabels.PERSON_URL); - fieldLabelToOutputFieldLabel.put("personLabel", QueryFieldLabels.PERSON_LABEL); - fieldLabelToOutputFieldLabel.put("personTypeLabel", QueryFieldLabels.PERSON_TYPE_LABEL); - - String whereClause = "" - + " <" + subjectEntityURI + "> rdfs:label ?organizationLabel . " - + " <" + subjectEntityURI + "> vivosocnet:hasPersonWithActivity ?person . " - + " ?person rdfs:label ?personLabel . " - + " ?person rdf:type ?personType . " - + " ?personType rdfs:label ?personTypeLabel . "; - - QueryRunner associatedPeopleWithTypesQuery = - new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel, - "", - whereClause, - "", - associatedPeopleModel); - - return getAssociatedPeopleSubEntitities(associatedPeopleWithTypesQuery.getQueryResult()); - } - - private Collection getAssociatedPeopleSubEntitities( - ResultSet queryResult) { - - Map associatedPeopleURIToVO = new HashMap(); - - while (queryResult.hasNext()) { - - QuerySolution solution = queryResult.nextSolution(); - - RDFNode personNode = solution.get(QueryFieldLabels.PERSON_URL); - - SubEntity subEntity; - - if (associatedPeopleURIToVO.containsKey(personNode.toString())) { - - subEntity = associatedPeopleURIToVO.get(personNode.toString()); - - } else { - - subEntity = new SubEntity(personNode.toString()); - associatedPeopleURIToVO.put(personNode.toString(), subEntity); - - RDFNode personLabelNode = solution.get(QueryFieldLabels.PERSON_LABEL); - if (personLabelNode != null) { - subEntity.setIndividualLabel(personLabelNode.toString()); - } - } - - RDFNode personTypeLabelNode = solution.get(QueryFieldLabels.PERSON_TYPE_LABEL); - if (personTypeLabelNode != null) { - subEntity.addEntityTypeLabel(personTypeLabelNode.toString()); - } - } - - return associatedPeopleURIToVO.values(); - } - - private Collection getPublicationForEntity( - ResultSet queryResult, - Map allDocumentURIToVOs) { - - Set currentEntityPublications = new HashSet(); - - while (queryResult.hasNext()) { - - QuerySolution solution = queryResult.nextSolution(); - - RDFNode documentNode = solution.get(QueryFieldLabels.DOCUMENT_URL); - Activity biboDocument; - - if (allDocumentURIToVOs.containsKey(documentNode.toString())) { - biboDocument = allDocumentURIToVOs.get(documentNode.toString()); - - } else { - - biboDocument = new Activity(documentNode.toString()); - allDocumentURIToVOs.put(documentNode.toString(), biboDocument); - - RDFNode publicationDateNode = solution.get(QueryFieldLabels - .DOCUMENT_PUBLICATION_DATE); - if (publicationDateNode != null) { - biboDocument.setActivityDate(publicationDateNode.toString()); - } - } - - currentEntityPublications.add(biboDocument); - - } - - return currentEntityPublications; - } - - private Entity generateEntity(String subjectEntityURI, ResultSet queryResult) { - - Entity entity = new Entity(subjectEntityURI); - Map subOrganizationURIToVO = new HashMap(); - - while (queryResult.hasNext()) { - - QuerySolution solution = queryResult.nextSolution(); - - if (StringUtils.isEmpty(entity.getEntityLabel())) { - - RDFNode organizationLabelNode = solution.get(QueryFieldLabels.ORGANIZATION_LABEL); - if (organizationLabelNode != null) { - entity.setIndividualLabel(organizationLabelNode.toString()); - } - } - - RDFNode subOrganizationNode = solution.get(QueryFieldLabels.SUBORGANIZATION_URL); - - SubEntity subEntity; - - if (subOrganizationURIToVO.containsKey(subOrganizationNode.toString())) { - - subEntity = subOrganizationURIToVO.get(subOrganizationNode.toString()); - - } else { - - subEntity = new SubEntity(subOrganizationNode.toString()); - subOrganizationURIToVO.put(subOrganizationNode.toString(), subEntity); - - RDFNode subOrganizationLabelNode = solution.get(QueryFieldLabels.SUBORGANIZATION_LABEL); - if (subOrganizationLabelNode != null) { - subEntity.setIndividualLabel(subOrganizationLabelNode.toString()); - } - } - - RDFNode subOrganizationTypeLabelNode = solution.get(QueryFieldLabels.SUBORGANIZATION_TYPE_LABEL); - if (subOrganizationTypeLabelNode != null) { - subEntity.addEntityTypeLabel(subOrganizationTypeLabelNode.toString()); - } - } - - entity.addSubEntitities(subOrganizationURIToVO.values()); - - return entity; - } - private Map prepareStandaloneDataErrorResponse() { Map fileData = new HashMap(); @@ -598,13 +324,14 @@ public class EntityPublicationRequestHandler implements entityJson.setEntityURI(subentity.getIndividualURI()); - boolean isPerson = UtilityFunctions.isEntityAPerson(vreq, subentity); +// boolean isPerson = UtilityFunctions.isEntityAPerson(vreq, subentity); - if (isPerson) { + if (subentity.getEntityClass().equals(VOConstants.EntityClassType.PERSON)) { entityJson.setVisMode("PERSON"); - } else { + } else if (subentity.getEntityClass().equals(VOConstants.EntityClassType.ORGANIZATION)) { entityJson.setVisMode("ORGANIZATION"); } + subEntitiesJson.add(entityJson); } return json.toJson(subEntitiesJson); 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 0fd34a0c..c96aa85a 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 @@ -5,14 +5,18 @@ import java.util.Collection; import java.util.HashSet; import java.util.Set; +import edu.cornell.mannlib.vitro.webapp.visualization.constants.VOConstants; + /** - * @author bkoniden - * Deepak Konidena + * @author bkoniden (Deepak Konidena) + * modified by @author cdtank (Chintan Tank) + * last modified at Mar 16, 2011 2:57:20 PM */ public class SubEntity extends Individual { private Set activities = new HashSet(); private Set entityTypes = new HashSet(); + private VOConstants.EntityClassType entityClass; public SubEntity(String individualURI) { super(individualURI); @@ -42,9 +46,17 @@ public class SubEntity extends Individual { public void addEntityTypeLabel(String typeLabel) { this.entityTypes.add(typeLabel); } - + public Set getEntityTypeLabels() { return entityTypes; } + + public void setEntityClass(VOConstants.EntityClassType entityClass) { + this.entityClass = entityClass; + } + + public VOConstants.EntityClassType getEntityClass() { + return entityClass; + } } diff --git a/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/SelectOnModelUtilities.java b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/SelectOnModelUtilities.java new file mode 100644 index 00000000..f1e44fb7 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/visualization/freemarker/visutils/SelectOnModelUtilities.java @@ -0,0 +1,312 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ +package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; + +import com.hp.hpl.jena.query.Dataset; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.RDFNode; + +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.modelconstructor.ModelConstructorUtilities; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.OrganizationAssociatedPeopleModelWithTypesConstructor; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.OrganizationModelWithTypesConstructor; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.OrganizationToPublicationsForSubOrganizationsModelConstructor; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.modelconstructor.PersonToPublicationsModelConstructor; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Activity; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.Entity; +import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.SubEntity; + +public class SelectOnModelUtilities { + + public static Entity getSubjectOrganizationHierarchy(Dataset dataset, + String subjectEntityURI) throws MalformedQueryParametersException { + Model organizationModel = ModelConstructorUtilities + .getOrConstructModel( + null, + OrganizationModelWithTypesConstructor.MODEL_TYPE, + dataset); + + Map fieldLabelToOutputFieldLabel = new HashMap(); + fieldLabelToOutputFieldLabel.put("organizationLabel", QueryFieldLabels.ORGANIZATION_LABEL); + fieldLabelToOutputFieldLabel.put("subOrganization", QueryFieldLabels.SUBORGANIZATION_URL); + fieldLabelToOutputFieldLabel.put("subOrganizationLabel", QueryFieldLabels.SUBORGANIZATION_LABEL); + fieldLabelToOutputFieldLabel.put("subOrganizationTypeLabel", QueryFieldLabels.SUBORGANIZATION_TYPE_LABEL); + + String whereClause = "" + + " <" + subjectEntityURI + "> rdfs:label ?organizationLabel . " + + " <" + subjectEntityURI + "> core:hasSubOrganization ?subOrganization . " + + " ?subOrganization rdfs:label ?subOrganizationLabel . " + + " ?subOrganization rdf:type ?subOrgType . " + + " ?subOrgType rdfs:label ?subOrganizationTypeLabel . "; + + QueryRunner subOrganizationsWithTypesQuery = + new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel, + "", + whereClause, + "", + organizationModel); + + return getEntityWithSubOrganizations(subjectEntityURI, + subOrganizationsWithTypesQuery.getQueryResult()); + } + + private static Entity getEntityWithSubOrganizations(String subjectEntityURI, ResultSet queryResult) { + + Entity entity = new Entity(subjectEntityURI); + Map subOrganizationURIToVO = new HashMap(); + + while (queryResult.hasNext()) { + + QuerySolution solution = queryResult.nextSolution(); + + if (StringUtils.isEmpty(entity.getEntityLabel())) { + + RDFNode organizationLabelNode = solution.get(QueryFieldLabels.ORGANIZATION_LABEL); + if (organizationLabelNode != null) { + entity.setIndividualLabel(organizationLabelNode.toString()); + } + } + + RDFNode subOrganizationNode = solution.get(QueryFieldLabels.SUBORGANIZATION_URL); + + SubEntity subEntity; + + if (subOrganizationURIToVO.containsKey(subOrganizationNode.toString())) { + + subEntity = subOrganizationURIToVO.get(subOrganizationNode.toString()); + + } else { + + subEntity = new SubEntity(subOrganizationNode.toString()); + + subEntity.setEntityClass(VOConstants.EntityClassType.ORGANIZATION); + + subOrganizationURIToVO.put(subOrganizationNode.toString(), subEntity); + + RDFNode subOrganizationLabelNode = solution.get(QueryFieldLabels.SUBORGANIZATION_LABEL); + if (subOrganizationLabelNode != null) { + subEntity.setIndividualLabel(subOrganizationLabelNode.toString()); + } + } + + RDFNode subOrganizationTypeLabelNode = solution.get(QueryFieldLabels.SUBORGANIZATION_TYPE_LABEL); + if (subOrganizationTypeLabelNode != null) { + subEntity.addEntityTypeLabel(subOrganizationTypeLabelNode.toString()); + } + } + + entity.addSubEntitities(subOrganizationURIToVO.values()); + + return entity; + } + + public static Entity getSubjectOrganizationAssociatedPeople(Dataset dataset, + String subjectEntityURI) throws MalformedQueryParametersException { + Model associatedPeopleModel = ModelConstructorUtilities + .getOrConstructModel( + subjectEntityURI, + OrganizationAssociatedPeopleModelWithTypesConstructor.MODEL_TYPE, + dataset); + + Map fieldLabelToOutputFieldLabel = new HashMap(); + fieldLabelToOutputFieldLabel.put("organizationLabel", QueryFieldLabels.ORGANIZATION_LABEL); + fieldLabelToOutputFieldLabel.put("person", QueryFieldLabels.PERSON_URL); + fieldLabelToOutputFieldLabel.put("personLabel", QueryFieldLabels.PERSON_LABEL); + fieldLabelToOutputFieldLabel.put("personTypeLabel", QueryFieldLabels.PERSON_TYPE_LABEL); + + String whereClause = "" + + " <" + subjectEntityURI + "> rdfs:label ?organizationLabel . " + + " <" + subjectEntityURI + "> vivosocnet:hasPersonWithActivity ?person . " + + " ?person rdfs:label ?personLabel . " + + " ?person rdf:type ?personType . " + + " ?personType rdfs:label ?personTypeLabel . "; + + QueryRunner associatedPeopleWithTypesQuery = + new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel, + "", + whereClause, + "", + associatedPeopleModel); + + return getEntityWithAssociatedPeopleSubEntitities(subjectEntityURI, associatedPeopleWithTypesQuery.getQueryResult()); + } + + private static Entity getEntityWithAssociatedPeopleSubEntitities( + String subjectEntityURI, ResultSet queryResult) { + + Entity entity = new Entity(subjectEntityURI); + Map associatedPeopleURIToVO = new HashMap(); + + while (queryResult.hasNext()) { + + QuerySolution solution = queryResult.nextSolution(); + + if (StringUtils.isEmpty(entity.getEntityLabel())) { + + RDFNode organizationLabelNode = solution.get(QueryFieldLabels.ORGANIZATION_LABEL); + if (organizationLabelNode != null) { + entity.setIndividualLabel(organizationLabelNode.toString()); + } + } + + RDFNode personNode = solution.get(QueryFieldLabels.PERSON_URL); + + SubEntity subEntity; + + if (associatedPeopleURIToVO.containsKey(personNode.toString())) { + + subEntity = associatedPeopleURIToVO.get(personNode.toString()); + + } else { + + subEntity = new SubEntity(personNode.toString()); + subEntity.setEntityClass(VOConstants.EntityClassType.PERSON); + associatedPeopleURIToVO.put(personNode.toString(), subEntity); + + RDFNode personLabelNode = solution.get(QueryFieldLabels.PERSON_LABEL); + if (personLabelNode != null) { + subEntity.setIndividualLabel(personLabelNode.toString()); + } + } + + RDFNode personTypeLabelNode = solution.get(QueryFieldLabels.PERSON_TYPE_LABEL); + if (personTypeLabelNode != null) { + subEntity.addEntityTypeLabel(personTypeLabelNode.toString()); + } + } + + entity.addSubEntitities(associatedPeopleURIToVO.values()); + return entity; + } + + public static Map getPublicationsForAllSubOrganizations( + Dataset dataset, Entity organizationEntity) + throws MalformedQueryParametersException { + Map allDocumentURIToVOs = new HashMap(); + + for (SubEntity subOrganization : organizationEntity.getSubEntities()) { + + Model subOrganizationPublicationsModel = ModelConstructorUtilities + .getOrConstructModel( + subOrganization.getIndividualURI(), + OrganizationToPublicationsForSubOrganizationsModelConstructor.MODEL_TYPE, + dataset); + + System.out.println("getting publications for " + subOrganization.getIndividualLabel()); + + Map fieldLabelToOutputFieldLabel = new HashMap(); + fieldLabelToOutputFieldLabel.put("document", QueryFieldLabels.DOCUMENT_URL); + fieldLabelToOutputFieldLabel.put("documentLabel", QueryFieldLabels.DOCUMENT_LABEL); + fieldLabelToOutputFieldLabel.put("documentPublicationDate", QueryFieldLabels.DOCUMENT_PUBLICATION_DATE); + + String whereClause = "" + + " <" + subOrganization.getIndividualURI() + "> vivosocnet:hasPersonWithPublication ?document . " + + " ?document rdfs:label ?documentLabel . " + + " OPTIONAL { " + + " ?document core:dateTimeValue ?dateTimeValue . " + + " ?dateTimeValue core:dateTime ?documentPublicationDate } . "; + + QueryRunner subOrganizationPublicationsQuery = + new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel, + "", + whereClause, + "", + subOrganizationPublicationsModel); + + subOrganization.addActivities(getPublicationForEntity( + subOrganizationPublicationsQuery.getQueryResult(), + allDocumentURIToVOs)); + + } + return allDocumentURIToVOs; + } + + private static Collection getPublicationForEntity( + ResultSet queryResult, + Map allDocumentURIToVOs) { + + Set currentEntityPublications = new HashSet(); + + while (queryResult.hasNext()) { + + QuerySolution solution = queryResult.nextSolution(); + + RDFNode documentNode = solution.get(QueryFieldLabels.DOCUMENT_URL); + Activity biboDocument; + + if (allDocumentURIToVOs.containsKey(documentNode.toString())) { + biboDocument = allDocumentURIToVOs.get(documentNode.toString()); + + } else { + + biboDocument = new Activity(documentNode.toString()); + allDocumentURIToVOs.put(documentNode.toString(), biboDocument); + + RDFNode publicationDateNode = solution.get(QueryFieldLabels + .DOCUMENT_PUBLICATION_DATE); + if (publicationDateNode != null) { + biboDocument.setActivityDate(publicationDateNode.toString()); + } + } + + currentEntityPublications.add(biboDocument); + + } + + return currentEntityPublications; + } + + public static Map getPublicationsForAssociatedPeople( + Dataset dataset, Collection people) + throws MalformedQueryParametersException { + Map allDocumentURIToVOs = new HashMap(); + + for (SubEntity person : people) { + + Model personPublicationsModel = ModelConstructorUtilities + .getOrConstructModel( + person.getIndividualURI(), + PersonToPublicationsModelConstructor.MODEL_TYPE, + dataset); + + System.out.println("getting publications for " + person.getIndividualLabel()); + + Map fieldLabelToOutputFieldLabel = new HashMap(); + fieldLabelToOutputFieldLabel.put("document", QueryFieldLabels.DOCUMENT_URL); + fieldLabelToOutputFieldLabel.put("documentLabel", QueryFieldLabels.DOCUMENT_LABEL); + fieldLabelToOutputFieldLabel.put("documentPublicationDate", QueryFieldLabels.DOCUMENT_PUBLICATION_DATE); + + String whereClause = "" + + " <" + person.getIndividualURI() + "> vivosocnet:hasPublication ?document . " + + " ?document rdfs:label ?documentLabel . " + + " OPTIONAL { " + + " ?document core:dateTimeValue ?dateTimeValue . " + + " ?dateTimeValue core:dateTime ?documentPublicationDate } . "; + + QueryRunner personPublicationsQuery = + new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel, + "", + whereClause, + "", + personPublicationsModel); + + person.addActivities(getPublicationForEntity( + personPublicationsQuery.getQueryResult(), + allDocumentURIToVOs)); + + } + return allDocumentURIToVOs; + } + +}