updates to vitro that enable field options to have custom comparators - this change is being implemented for Datastar but the code will be in Vitro to enable Vitro/VIVO to take advantage of these classes. New generator and field options classes have been added.

This commit is contained in:
hjkhjk54 2012-10-24 21:19:40 +00:00
parent 945243377b
commit 5973e7ec4d
13 changed files with 220 additions and 6 deletions

View file

@ -1,6 +1,7 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */ /* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields; package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -80,4 +81,8 @@ public class ChildVClassesOptions implements FieldOptions {
public String getClassUri(){ public String getClassUri(){
return vclassUri; return vclassUri;
} }
public Comparator<String[]> getCustomComparator() {
return null;
}
} }

View file

@ -1,6 +1,7 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */ /* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields; package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -62,5 +63,9 @@ public class ChildVClassesWithParent implements FieldOptions {
} }
return optionsMap; return optionsMap;
} }
public Comparator<String[]> getCustomComparator() {
return null;
}
} }

View file

@ -2,6 +2,7 @@
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields; package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -72,5 +73,9 @@ public class ConstantFieldOptions implements FieldOptions {
return optionsMap; return optionsMap;
} }
public Comparator<String[]> getCustomComparator() {
return null;
}
} }

View file

@ -1,6 +1,7 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */ /* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields; package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.util.Comparator;
import java.util.Map; import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
@ -28,6 +29,13 @@ public interface FieldOptions {
EditConfigurationVTwo editConfig, EditConfigurationVTwo editConfig,
String fieldName, String fieldName,
WebappDaoFactory wDaoFact) throws Exception; WebappDaoFactory wDaoFact) throws Exception;
/*
* Certain field options may have custom sorting requirements. If no sorting requirements exist,
* then the method will return null.
*/
public Comparator<String[]> getCustomComparator();
} }
/* /*

View file

@ -1,6 +1,7 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */ /* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields; package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.util.Comparator;
import java.util.Map; import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
@ -35,4 +36,8 @@ public class IndividualsViaClassGroupOptions implements FieldOptions {
return classGroupUri; return classGroupUri;
} }
public Comparator<String[]> getCustomComparator() {
return null;
}
} }

View file

@ -0,0 +1,58 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.text.Collator;
import java.util.Comparator;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
public class IndividualsViaObjectPropertyByRankOptions extends IndividualsViaObjectPropetyOptions {
private WebappDaoFactory wdf = null;
public IndividualsViaObjectPropertyByRankOptions(String subjectUri,
String predicateUri, String objectUri, WebappDaoFactory wdf) throws Exception {
super(subjectUri, predicateUri, objectUri);
this.wdf = wdf;
}
public Comparator<String[]> getCustomComparator() {
return new DisplayRankComparator(wdf);
}
private static class DisplayRankComparator implements Comparator<String[]> {
private WebappDaoFactory wdf = null;
public DisplayRankComparator(WebappDaoFactory wdf) {
this.wdf = wdf;
}
public int compare (String[] s1, String[] s2) {
Collator collator = Collator.getInstance();
if (s2 == null) {
return 1;
} else if (s1 == null) {
return -1;
} else {
if ("".equals(s1[0])) {
return -1;
} else if ("".equals(s2[0])) {
return 1;
}
if (s2[1]==null) {
return 1;
} else if (s1[1] == null){
return -1;
} else {
return compareRanks(collator, s1, s2);
}
}
}
private int compareRanks(Collator collator, String[] s1, String[] s2) {
String uri1 = s1[0];
String uri2 = s2[0];
Individual ind1 = this.wdf.getIndividualDao().getIndividualByURI(uri1);
Individual ind2 = this.wdf.getIndividualDao().getIndividualByURI(uri2);
//Get display ranks
return collator.compare(ind1.getLocalName(), ind2.getLocalName());
}
}
}

View file

@ -1,7 +1,9 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */ /* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields; package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -122,4 +124,10 @@ public class IndividualsViaObjectPropetyOptions implements FieldOptions {
return individuals; return individuals;
} }
public Comparator<String[]> getCustomComparator() {
return null;
}
} }

View file

@ -3,6 +3,7 @@ package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -126,6 +127,10 @@ public class IndividualsViaVClassOptions implements FieldOptions {
} }
return individualMap; return individualMap;
} }
public Comparator<String[]> getCustomComparator() {
return null;
}
} }

View file

@ -1,6 +1,7 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */ /* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields; package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -56,5 +57,9 @@ public class RdfTypeOptions implements FieldOptions {
return uriToLabel; return uriToLabel;
} }
public Comparator<String[]> getCustomComparator() {
return null;
}
} }

View file

@ -57,7 +57,8 @@ public class SelectListGeneratorVTwo {
//Methods to sort the options map //Methods to sort the options map
// from http://forum.java.sun.com/thread.jspa?threadID=639077&messageID=4250708 // from http://forum.java.sun.com/thread.jspa?threadID=639077&messageID=4250708
public static Map<String,String> getSortedMap(Map<String,String> hmap){ //Modified to allow for a custom comparator to be sent in, defaults to mapPairsComparator
public static Map<String,String> getSortedMap(Map<String,String> hmap, Comparator<String[]> comparator){
// first make temporary list of String arrays holding both the key and its corresponding value, so that the list can be sorted with a decent comparator // first make temporary list of String arrays holding both the key and its corresponding value, so that the list can be sorted with a decent comparator
List<String[]> objectsToSort = new ArrayList<String[]>(hmap.size()); List<String[]> objectsToSort = new ArrayList<String[]>(hmap.size());
for (String key:hmap.keySet()) { for (String key:hmap.keySet()) {
@ -66,7 +67,13 @@ public class SelectListGeneratorVTwo {
x[1] = hmap.get(key); x[1] = hmap.get(key);
objectsToSort.add(x); objectsToSort.add(x);
} }
Collections.sort(objectsToSort, new MapPairsComparator());
//if no comparator is passed in, utilize MapPairsComparator
if(comparator == null) {
comparator = new MapPairsComparator();
}
Collections.sort(objectsToSort, comparator);
HashMap<String,String> map = new LinkedHashMap<String,String>(objectsToSort.size()); HashMap<String,String> map = new LinkedHashMap<String,String>(objectsToSort.size());
for (String[] pair:objectsToSort) { for (String[] pair:objectsToSort) {
@ -75,6 +82,7 @@ public class SelectListGeneratorVTwo {
return map; return map;
} }
//Sorts by the value of the 2nd element in each of the arrays
private static class MapPairsComparator implements Comparator<String[]> { private static class MapPairsComparator implements Comparator<String[]> {
public int compare (String[] s1, String[] s2) { public int compare (String[] s1, String[] s2) {
Collator collator = Collator.getInstance(); Collator collator = Collator.getInstance();

View file

@ -343,7 +343,7 @@ public class DefaultObjectPropertyFormGenerator implements EditConfigurationGene
return map; return map;
} }
private void setFields(EditConfigurationVTwo editConfiguration, VitroRequest vreq, String predicateUri) throws Exception { protected void setFields(EditConfigurationVTwo editConfiguration, VitroRequest vreq, String predicateUri) throws Exception {
FieldVTwo field = new FieldVTwo(); FieldVTwo field = new FieldVTwo();
field.setName("objectVar"); field.setName("objectVar");
@ -474,7 +474,18 @@ public class DefaultObjectPropertyFormGenerator implements EditConfigurationGene
return rangeIndividualsFound; return rangeIndividualsFound;
} }
public String getSubjectUri() {
return subjectUri;
}
public String getPredicateUri() {
return predicateUri;
}
public String getObjectUri() {
return objectUri;
}
/** get the auto complete edit mode */ /** get the auto complete edit mode */
public EditMode getEditMode(VitroRequest vreq) { public EditMode getEditMode(VitroRequest vreq) {
@ -497,5 +508,7 @@ public class DefaultObjectPropertyFormGenerator implements EditConfigurationGene
"<" + subject + "> <" + predicate + "> ?objectVar .} "; "<" + subject + "> <" + predicate + "> ?objectVar .} ";
return query; return query;
} }
} }

View file

@ -0,0 +1,89 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators;
import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
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.EditConfigurationUtils;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.FieldVTwo;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.IndividualsViaObjectPropertyByRankOptions;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.IndividualsViaObjectPropetyOptions;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.validators.AntiXssValidation;
import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames;
import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch;
import edu.cornell.mannlib.vitro.webapp.search.solr.SolrSetup;
import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils;
import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils.EditMode;
/**
* Generates the edit configuration for a default property form.
* This handles the default object property auto complete.
*
* If a default property form is request and the number of indivdiuals
* found in the range is too large, the the auto complete setup and
* template will be used instead.
*/
public class IndividualsByRankFormGenerator extends DefaultObjectPropertyFormGenerator implements EditConfigurationGenerator {
private Log log = LogFactory.getLog(IndividualsByRankFormGenerator.class);
/*
* (non-Javadoc)
* @see edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators.DefaultObjectPropertyFormGenerator#setFields(edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo, edu.cornell.mannlib.vitro.webapp.controller.VitroRequest, java.lang.String)
*
* Updates here include using different field options that enable sorting of individuals for the property by display rank
*/
@Override
protected void setFields(EditConfigurationVTwo editConfiguration, VitroRequest vreq, String predicateUri) throws Exception {
FieldVTwo field = new FieldVTwo();
field.setName("objectVar");
List<String> validators = new ArrayList<String>();
validators.add("nonempty");
field.setValidators(validators);
if( ! doAutoComplete ){
field.setOptions( new IndividualsViaObjectPropertyByRankOptions(
super.getSubjectUri(),
super.getPredicateUri(),
super.getObjectUri(),
vreq.getWebappDaoFactory()));
}else{
field.setOptions(null);
}
Map<String, FieldVTwo> fields = new HashMap<String, FieldVTwo>();
fields.put(field.getName(), field);
editConfiguration.setFields(fields);
}
}

View file

@ -101,8 +101,8 @@ public class EditConfigurationTemplateModel extends BaseTemplateModel {
//empty options //empty options
field.setOptions(new ConstantFieldOptions()); field.setOptions(new ConstantFieldOptions());
} }
Map<String, String> optionsMap = SelectListGeneratorVTwo.getOptions(editConfig, fieldName, wdf); Map<String, String> optionsMap = SelectListGeneratorVTwo.getOptions(editConfig, fieldName, wdf);
optionsMap = SelectListGeneratorVTwo.getSortedMap(optionsMap); optionsMap = SelectListGeneratorVTwo.getSortedMap(optionsMap, field.getFieldOptions().getCustomComparator());
if(pageData.containsKey(fieldName)) { if(pageData.containsKey(fieldName)) {
log.error("Check the edit configuration setup as pageData already contains " + fieldName + " and this will be overwritten now with empty collection"); log.error("Check the edit configuration setup as pageData already contains " + fieldName + " and this will be overwritten now with empty collection");
} }