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.
This commit is contained in:
cdtank 2011-03-16 19:16:28 +00:00
parent af0f3e5a3a
commit c4244301d2
5 changed files with 366 additions and 296 deletions

View file

@ -22,8 +22,8 @@ public class VOConstants {
/* /*
* Employee related constants * Employee related constants
* */ * */
public static enum EmployeeType { public static enum EntityClassType {
ACADEMIC_FACULTY_EMPLOYEE, ACADEMIC_STAFF_EMPLOYEE ORGANIZATION, PERSON, UNKNOWN
} }
public static final int NUM_CHARS_IN_YEAR_FORMAT = 4; public static final int NUM_CHARS_IN_YEAR_FORMAT = 4;

View file

@ -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.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.QueryFieldLabels; 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.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.GenericQueryMap;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.GenericQueryRunner; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.GenericQueryRunner;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.ModelConstructor; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.ModelConstructor;
@ -166,4 +167,22 @@ public class EntityComparisonUtilityFunctions {
* */ * */
return EntityComparisonUtilityFunctions.getHighestLevelOrganizationURI(log, dataset); 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;
}
}
} }

View file

@ -3,7 +3,6 @@
package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.entitycomparison.cached; package edu.cornell.mannlib.vitro.webapp.visualization.freemarker.entitycomparison.cached;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -16,10 +15,7 @@ import org.apache.commons.logging.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.hp.hpl.jena.query.Dataset; 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.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import edu.cornell.mannlib.vitro.webapp.beans.Portal; import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; 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.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.visualization.freemarker.DataVisualizationController; 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.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.constants.VOConstants;
import edu.cornell.mannlib.vitro.webapp.visualization.exceptions.MalformedQueryParametersException; 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.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.Activity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.ConstructedModelTracker; 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.Entity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.valueobjects.JsonObject; 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.valueobjects.SubEntity;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.GenericQueryRunnerOnModel; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.SelectOnModelUtilities;
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.UtilityFunctions;
import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.VisualizationRequestHandler; import edu.cornell.mannlib.vitro.webapp.visualization.freemarker.visutils.VisualizationRequestHandler;
@ -90,8 +79,8 @@ public class EntityPublicationRequestHandler implements
throws MalformedQueryParametersException { throws MalformedQueryParametersException {
Entity organizationEntity = getSubjectOrganizationHierarchy(dataset, Entity organizationEntity = SelectOnModelUtilities
subjectEntityURI); .getSubjectOrganizationHierarchy(dataset, subjectEntityURI);
if (organizationEntity.getSubEntities() == null) { if (organizationEntity.getSubEntities() == null) {
@ -106,14 +95,20 @@ public class EntityPublicationRequestHandler implements
Map<String, Activity> documentURIForAssociatedPeopleTOVO = new HashMap<String, Activity>(); Map<String, Activity> documentURIForAssociatedPeopleTOVO = new HashMap<String, Activity>();
Map<String, Activity> allDocumentURIToVOs = new HashMap<String, Activity>(); Map<String, Activity> allDocumentURIToVOs = new HashMap<String, Activity>();
allDocumentURIToVOs = getPublicationsForAllSubOrganizations(dataset, organizationEntity); allDocumentURIToVOs = SelectOnModelUtilities.getPublicationsForAllSubOrganizations(dataset, organizationEntity);
Collection<SubEntity> associatedPeople = getSubjectOrganizationAssociatedPeople(dataset, subjectEntityURI); Entity organizationWithAssociatedPeople = SelectOnModelUtilities
.getSubjectOrganizationAssociatedPeople(dataset, subjectEntityURI);
if (!associatedPeople.isEmpty()) { if (organizationWithAssociatedPeople.getSubEntities() != null) {
documentURIForAssociatedPeopleTOVO = getPublicationsForAssociatedPeople(dataset, associatedPeople); documentURIForAssociatedPeopleTOVO = SelectOnModelUtilities
organizationEntity.addSubEntitities(associatedPeople); .getPublicationsForAssociatedPeople(dataset, organizationWithAssociatedPeople.getSubEntities());
organizationEntity = EntityComparisonUtilityFunctions.mergeEntityIfShareSameURI(
organizationEntity,
organizationWithAssociatedPeople);
} }
if (allDocumentURIToVOs.isEmpty() && documentURIForAssociatedPeopleTOVO.isEmpty()) { if (allDocumentURIToVOs.isEmpty() && documentURIForAssociatedPeopleTOVO.isEmpty()) {
@ -182,275 +177,6 @@ public class EntityPublicationRequestHandler implements
return fileData; return fileData;
} }
private Map<String, Activity> getPublicationsForAllSubOrganizations(
Dataset dataset, Entity organizationEntity)
throws MalformedQueryParametersException {
Map<String, Activity> allDocumentURIToVOs = new HashMap<String, Activity>();
for (SubEntity subOrganization : organizationEntity.getSubEntities()) {
Model subOrganizationPublicationsModel = ModelConstructorUtilities
.getOrConstructModel(
subOrganization.getIndividualURI(),
OrganizationToPublicationsForSubOrganizationsModelConstructor.MODEL_TYPE,
dataset);
System.out.println("getting publications for " + subOrganization.getIndividualLabel());
Map<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
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<ResultSet> subOrganizationPublicationsQuery =
new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel,
"",
whereClause,
"",
subOrganizationPublicationsModel);
subOrganization.addActivities(getPublicationForEntity(
subOrganizationPublicationsQuery.getQueryResult(),
allDocumentURIToVOs));
}
return allDocumentURIToVOs;
}
private Map<String, Activity> getPublicationsForAssociatedPeople(
Dataset dataset, Collection<SubEntity> people)
throws MalformedQueryParametersException {
Map<String, Activity> allDocumentURIToVOs = new HashMap<String, Activity>();
for (SubEntity person : people) {
Model personPublicationsModel = ModelConstructorUtilities
.getOrConstructModel(
person.getIndividualURI(),
PersonToPublicationsModelConstructor.MODEL_TYPE,
dataset);
System.out.println("getting publications for " + person.getIndividualLabel());
Map<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
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<ResultSet> 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<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
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<ResultSet> subOrganizationsWithTypesQuery =
new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel,
"",
whereClause,
"",
organizationModel);
Entity organizationEntity = generateEntity(subjectEntityURI,
subOrganizationsWithTypesQuery.getQueryResult());
return organizationEntity;
}
private Collection<SubEntity> getSubjectOrganizationAssociatedPeople(Dataset dataset,
String subjectEntityURI) throws MalformedQueryParametersException {
Model associatedPeopleModel = ModelConstructorUtilities
.getOrConstructModel(
subjectEntityURI,
OrganizationAssociatedPeopleModelWithTypesConstructor.MODEL_TYPE,
dataset);
Map<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
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<ResultSet> associatedPeopleWithTypesQuery =
new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel,
"",
whereClause,
"",
associatedPeopleModel);
return getAssociatedPeopleSubEntitities(associatedPeopleWithTypesQuery.getQueryResult());
}
private Collection<SubEntity> getAssociatedPeopleSubEntitities(
ResultSet queryResult) {
Map<String, SubEntity> associatedPeopleURIToVO = new HashMap<String, SubEntity>();
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<Activity> getPublicationForEntity(
ResultSet queryResult,
Map<String, Activity> allDocumentURIToVOs) {
Set<Activity> currentEntityPublications = new HashSet<Activity>();
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<String, SubEntity> subOrganizationURIToVO = new HashMap<String, SubEntity>();
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<String, String> prepareStandaloneDataErrorResponse() { private Map<String, String> prepareStandaloneDataErrorResponse() {
Map<String, String> fileData = new HashMap<String, String>(); Map<String, String> fileData = new HashMap<String, String>();
@ -598,13 +324,14 @@ public class EntityPublicationRequestHandler implements
entityJson.setEntityURI(subentity.getIndividualURI()); 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"); entityJson.setVisMode("PERSON");
} else { } else if (subentity.getEntityClass().equals(VOConstants.EntityClassType.ORGANIZATION)) {
entityJson.setVisMode("ORGANIZATION"); entityJson.setVisMode("ORGANIZATION");
} }
subEntitiesJson.add(entityJson); subEntitiesJson.add(entityJson);
} }
return json.toJson(subEntitiesJson); return json.toJson(subEntitiesJson);

View file

@ -5,14 +5,18 @@ import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.visualization.constants.VOConstants;
/** /**
* @author bkoniden * @author bkoniden (Deepak Konidena)
* Deepak Konidena * modified by @author cdtank (Chintan Tank)
* last modified at Mar 16, 2011 2:57:20 PM
*/ */
public class SubEntity extends Individual { public class SubEntity extends Individual {
private Set<Activity> activities = new HashSet<Activity>(); private Set<Activity> activities = new HashSet<Activity>();
private Set<String> entityTypes = new HashSet<String>(); private Set<String> entityTypes = new HashSet<String>();
private VOConstants.EntityClassType entityClass;
public SubEntity(String individualURI) { public SubEntity(String individualURI) {
super(individualURI); super(individualURI);
@ -42,9 +46,17 @@ public class SubEntity extends Individual {
public void addEntityTypeLabel(String typeLabel) { public void addEntityTypeLabel(String typeLabel) {
this.entityTypes.add(typeLabel); this.entityTypes.add(typeLabel);
} }
public Set<String> getEntityTypeLabels() { public Set<String> getEntityTypeLabels() {
return entityTypes; return entityTypes;
} }
public void setEntityClass(VOConstants.EntityClassType entityClass) {
this.entityClass = entityClass;
}
public VOConstants.EntityClassType getEntityClass() {
return entityClass;
}
} }

View file

@ -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<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
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<ResultSet> 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<String, SubEntity> subOrganizationURIToVO = new HashMap<String, SubEntity>();
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<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
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<ResultSet> 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<String, SubEntity> associatedPeopleURIToVO = new HashMap<String, SubEntity>();
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<String, Activity> getPublicationsForAllSubOrganizations(
Dataset dataset, Entity organizationEntity)
throws MalformedQueryParametersException {
Map<String, Activity> allDocumentURIToVOs = new HashMap<String, Activity>();
for (SubEntity subOrganization : organizationEntity.getSubEntities()) {
Model subOrganizationPublicationsModel = ModelConstructorUtilities
.getOrConstructModel(
subOrganization.getIndividualURI(),
OrganizationToPublicationsForSubOrganizationsModelConstructor.MODEL_TYPE,
dataset);
System.out.println("getting publications for " + subOrganization.getIndividualLabel());
Map<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
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<ResultSet> subOrganizationPublicationsQuery =
new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel,
"",
whereClause,
"",
subOrganizationPublicationsModel);
subOrganization.addActivities(getPublicationForEntity(
subOrganizationPublicationsQuery.getQueryResult(),
allDocumentURIToVOs));
}
return allDocumentURIToVOs;
}
private static Collection<Activity> getPublicationForEntity(
ResultSet queryResult,
Map<String, Activity> allDocumentURIToVOs) {
Set<Activity> currentEntityPublications = new HashSet<Activity>();
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<String, Activity> getPublicationsForAssociatedPeople(
Dataset dataset, Collection<SubEntity> people)
throws MalformedQueryParametersException {
Map<String, Activity> allDocumentURIToVOs = new HashMap<String, Activity>();
for (SubEntity person : people) {
Model personPublicationsModel = ModelConstructorUtilities
.getOrConstructModel(
person.getIndividualURI(),
PersonToPublicationsModelConstructor.MODEL_TYPE,
dataset);
System.out.println("getting publications for " + person.getIndividualLabel());
Map<String, String> fieldLabelToOutputFieldLabel = new HashMap<String, String>();
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<ResultSet> personPublicationsQuery =
new GenericQueryRunnerOnModel(fieldLabelToOutputFieldLabel,
"",
whereClause,
"",
personPublicationsModel);
person.addActivities(getPublicationForEntity(
personPublicationsQuery.getQueryResult(),
allDocumentURIToVOs));
}
return allDocumentURIToVOs;
}
}