From 20278bde17129e5c69ecdca6583a1551e18938f9 Mon Sep 17 00:00:00 2001 From: hudajkhan Date: Fri, 1 Nov 2013 11:23:48 -0400 Subject: [PATCH] updates to allow individuals with most specific type in dropdown to exclude subject and any objects for object property --- .../IndividualsViaObjectPropetyOptions.java | 26 ++--------- .../IndividualsViaSolrQueryOptions.java | 40 ++++++++++++++--- .../vitro/webapp/utils/fields/FieldUtils.java | 44 +++++++++++++++++++ 3 files changed, 82 insertions(+), 28 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/utils/fields/FieldUtils.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/fields/IndividualsViaObjectPropetyOptions.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/fields/IndividualsViaObjectPropetyOptions.java index fdc1f008a..fcc1453b6 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/fields/IndividualsViaObjectPropetyOptions.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/fields/IndividualsViaObjectPropetyOptions.java @@ -19,6 +19,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.VClassDao; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo; +import edu.cornell.mannlib.vitro.webapp.utils.fields.FieldUtils; public class IndividualsViaObjectPropetyOptions implements FieldOptions { @@ -100,7 +101,7 @@ public class IndividualsViaObjectPropetyOptions implements FieldOptions { List stmts = subject.getObjectPropertyStatements(); - individuals = removeIndividualsAlreadyInRange( + individuals = FieldUtils.removeIndividualsAlreadyInRange( individuals, stmts, predicateUri, objectUri); // Collections.sort(individuals,new compareIndividualsByName());a @@ -162,28 +163,7 @@ public class IndividualsViaObjectPropetyOptions implements FieldOptions { return filteredVClassesURIs; } - // copied from OptionsForPropertyTag.java in the thought that class may be deprecated - private static List removeIndividualsAlreadyInRange(List individuals, - List stmts, String predicateUri, String objectUriBeingEdited){ - HashSet range = new HashSet(); - - for(ObjectPropertyStatement ops : stmts){ - if( ops.getPropertyURI().equals(predicateUri)) - range.add( ops.getObjectURI() ); - } - - int removeCount=0; - ListIterator it = individuals.listIterator(); - while(it.hasNext()){ - Individual ind = it.next(); - if( range.contains( ind.getURI()) && !(ind.getURI().equals(objectUriBeingEdited)) ) { - it.remove(); - ++removeCount; - } - } - - return individuals; - } + public Comparator getCustomComparator() { return null; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/fields/IndividualsViaSolrQueryOptions.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/fields/IndividualsViaSolrQueryOptions.java index fc9527297..75cac343e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/fields/IndividualsViaSolrQueryOptions.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/fields/IndividualsViaSolrQueryOptions.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.ListIterator; import java.util.Map; import javax.servlet.ServletContext; @@ -18,11 +19,13 @@ import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement; import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo; import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames; import edu.cornell.mannlib.vitro.webapp.search.solr.SolrSetup; +import edu.cornell.mannlib.vitro.webapp.utils.fields.FieldUtils; /* * This runs a solr query to get individuals of a certain class instead of relying on the dao classes. @@ -32,9 +35,15 @@ public class IndividualsViaSolrQueryOptions extends IndividualsViaVClassOptions private Log log = LogFactory.getLog(IndividualsViaSolrQueryOptions.class); private ServletContext servletContext; - public IndividualsViaSolrQueryOptions(ServletContext context, String ... vclassURIs) throws Exception { + private String subjectUri; + private String predicateUri; + private String objectUri; + public IndividualsViaSolrQueryOptions(ServletContext context, String inputSubjectUri, String inputPredicateUri, String inputObjectUri, String ... vclassURIs) throws Exception { super(vclassURIs); this.servletContext = context; + this.subjectUri = inputSubjectUri; + this.predicateUri = inputPredicateUri; + this.objectUri = inputObjectUri; } @Override @@ -96,8 +105,18 @@ public class IndividualsViaSolrQueryOptions extends IndividualsViaVClassOptions //sort the individuals List individuals = new ArrayList(); individuals.addAll(individualMap.values()); - Collections.sort(individuals); + + //Here we will remove individuals already in the range + Individual subject = wDaoFact.getIndividualDao().getIndividualByURI(subjectUri); + List stmts = subject.getObjectPropertyStatements(); + individuals = FieldUtils.removeIndividualsAlreadyInRange( + individuals, stmts, predicateUri, objectUri); + //Also remove subjectUri if it + individuals = removeSubjectUri(individuals, subjectUri); + //sort the list + Collections.sort(individuals); + //set up the options map Map optionsMap = new HashMap(); if (defaultOptionLabel != null) { @@ -117,8 +136,19 @@ public class IndividualsViaSolrQueryOptions extends IndividualsViaVClassOptions return optionsMap; } - - -} + + //TODO: Check if this can be done simply by reference + private List removeSubjectUri(List individuals, + String subjectUri) { + ListIterator it = individuals.listIterator(); + while(it.hasNext()){ + Individual ind = it.next(); + if( ind.getURI().equals(subjectUri)) { + it.remove(); + } + } + return individuals; + } +} \ No newline at end of file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/fields/FieldUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/fields/FieldUtils.java new file mode 100644 index 000000000..15a042232 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/fields/FieldUtils.java @@ -0,0 +1,44 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ +package edu.cornell.mannlib.vitro.webapp.utils.fields; + +import java.util.HashSet; +import java.util.List; +import java.util.ListIterator; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement; + +public class FieldUtils { + + private static final Log log = LogFactory.getLog(FieldUtils.class); + + + // copied from OptionsForPropertyTag.java in the thought that class may be deprecated + public static List removeIndividualsAlreadyInRange(List individuals, + List stmts, String predicateUri, String objectUriBeingEdited){ + HashSet range = new HashSet(); + + for(ObjectPropertyStatement ops : stmts){ + if( ops.getPropertyURI().equals(predicateUri)) + range.add( ops.getObjectURI() ); + } + + int removeCount=0; + ListIterator it = individuals.listIterator(); + while(it.hasNext()){ + Individual ind = it.next(); + if( range.contains( ind.getURI()) && !(ind.getURI().equals(objectUriBeingEdited)) ) { + it.remove(); + ++removeCount; + } + } + + return individuals; + } + + + +}