Merge r5933:5935 and r5937 from nihvivo-rel-1.1-maint

This commit is contained in:
rjy7 2010-09-24 21:35:03 +00:00
parent 715fad38f4
commit 503652796c

View file

@ -216,9 +216,15 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
for (Property p : g.getPropertyList()) { for (Property p : g.getPropertyList()) {
if (p instanceof ObjectProperty) { if (p instanceof ObjectProperty) {
ObjectProperty op = (ObjectProperty)p; ObjectProperty op = (ObjectProperty)p;
if (op.getObjectPropertyStatements()!=null && op.getObjectPropertyStatements().size()>0) { List<ObjectPropertyStatement> opStmts = op.getObjectPropertyStatements();
statementCount += op.getObjectPropertyStatements().size(); if (op.getObjectPropertyStatements()!=null && opStmts.size()>0) {
statementCount += opStmts.size();
// If not collated, we need to apply custom sorting now.
applyCustomSortToUncollatedProperty(op, opStmts);
} }
} }
} }
} }
@ -229,6 +235,14 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
log.error("Exception on trying to prune groups list with properties: "+ex.getMessage()); log.error("Exception on trying to prune groups list with properties: "+ex.getMessage());
} }
mergedPropertyList.clear(); mergedPropertyList.clear();
} else { // ungrouped mode
for (Property p : mergedPropertyList) {
if (p instanceof ObjectProperty) {
ObjectProperty op = (ObjectProperty)p;
applyCustomSortToUncollatedProperty(op, op.getObjectPropertyStatements());
}
}
} }
} }
@ -440,10 +454,17 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
private List<Property> collateBySubclass(List<Property> mergedPropertyList) { private List<Property> collateBySubclass(List<Property> mergedPropertyList) {
for( Property prop : mergedPropertyList){ for( Property prop : mergedPropertyList){
if( prop instanceof ObjectProperty && ((ObjectProperty)prop).getCollateBySubclass() ){ if( prop instanceof ObjectProperty ) {
collateBySubclass((ObjectProperty)prop); ObjectProperty op = (ObjectProperty) prop;
if (op.getCollateBySubclass() ){
log.debug("Collating property " + prop.getURI() + " by subclass");
collateBySubclass(op);
} else {
} }
} }
}
return mergedPropertyList; return mergedPropertyList;
} }
@ -460,10 +481,11 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
return; return;
Map<String,VClass> directClasses = getDirectClasses( getObjectsFromStmts( orgStmtList ) ); Map<String,VClass> directClasses = getDirectClasses( getObjectsFromStmts( orgStmtList ) );
//don't do collateBySubclass if there is only one class //don't do collateBySubclass if there is only one class
if( directClasses.size() < 2 ) if( directClasses.size() < 2 ) {
prop.setCollateBySubclass(false); //this overrides the value from the model prop.setCollateBySubclass(false); //this overrides the value from the model
}
else{ else{
System.out.println("statements for object property: " + orgStmtList.size()); log.debug("statements for object property: " + orgStmtList.size());
//get list of direct classes and sort them //get list of direct classes and sort them
List<VClass> vclasses = new LinkedList<VClass>(directClasses.values()); List<VClass> vclasses = new LinkedList<VClass>(directClasses.values());
Collections.sort( Collections.sort(
@ -480,7 +502,7 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
// get all obj prop stmts with objects of this class // get all obj prop stmts with objects of this class
List<ObjectPropertyStatement> stmtsForClass = new ArrayList<ObjectPropertyStatement>(); List<ObjectPropertyStatement> stmtsForClass = new ArrayList<ObjectPropertyStatement>();
System.out.println("statements for object property: " + orgStmtList.size()); log.debug("statements for object property: " + orgStmtList.size());
Iterator<ObjectPropertyStatement> it = orgStmtList.iterator(); Iterator<ObjectPropertyStatement> it = orgStmtList.iterator();
while( it.hasNext()){ while( it.hasNext()){
ObjectPropertyStatement stmt = it.next(); ObjectPropertyStatement stmt = it.next();
@ -489,13 +511,13 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
Individual obj = stmt.getObject(); Individual obj = stmt.getObject();
List<VClass> vclassesForObj = obj.getVClasses(true); List<VClass> vclassesForObj = obj.getVClasses(true);
if (vclassesForObj != null && vclassesForObj.contains(clazz)) { if (vclassesForObj != null && vclassesForObj.contains(clazz)) {
System.out.println("adding " + stmt + " to class " log.debug("adding " + stmt + " to class "
+ clazz.getURI()); + clazz.getURI());
System.out.println("subjectURI " + stmt.getSubjectURI() log.debug("subjectURI " + stmt.getSubjectURI()
+ " objectURI" + stmt.getObject().getURI()); + " objectURI" + stmt.getObject().getURI());
System.out.println("stmtsForclass size: " log.debug("stmtsForclass size: "
+ stmtsForClass.size()); + stmtsForClass.size());
System.out.println("stmtsForclass size: " log.debug("stmtsForclass size: "
+ stmtsForClass.size()); + stmtsForClass.size());
stmtsForClass.add(stmt); stmtsForClass.add(stmt);
@ -506,20 +528,16 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
//types? For now we just show them in whichever type shows up first. related to NIHVIVO-876 //types? For now we just show them in whichever type shows up first. related to NIHVIVO-876
orgStmtList.removeAll(stmtsForClass); orgStmtList.removeAll(stmtsForClass);
Collections.sort(stmtsForClass, sortStatements(prop, stmtsForClass);
new Comparator<ObjectPropertyStatement>() {
public int compare(ObjectPropertyStatement o1, log.debug("stmtsForclass size after sort: "
ObjectPropertyStatement o2) {
return o1.getObject().getName().compareTo(
o2.getObject().getName());
}
});
System.out.println("stmtsForclass size after sort: "
+ stmtsForClass.size()); + stmtsForClass.size());
System.out.println("sortedStmtList size before add: " log.debug("sortedStmtList size before add: "
+ sortedStmtList.size()); + sortedStmtList.size());
sortedStmtList.addAll(stmtsForClass); sortedStmtList.addAll(stmtsForClass);
System.out.println("sortedStmtList size after add: "
log.debug("sortedStmtList size after add: "
+ sortedStmtList.size()); + sortedStmtList.size());
} }
prop.setObjectPropertyStatements(sortedStmtList); prop.setObjectPropertyStatements(sortedStmtList);
@ -527,7 +545,154 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
} }
private void sortStatements(ObjectProperty prop, List<ObjectPropertyStatement> statements) {
if (!applyCustomSort(prop, statements)) {
Collections.sort(statements,
new Comparator<ObjectPropertyStatement>() {
public int compare(ObjectPropertyStatement o1,
ObjectPropertyStatement o2) {
return o1.getObject().getName().compareTo(
o2.getObject().getName());
}
});
}
}
// rjy7 Quick and dirty fix to achieve custom sorting for specific properties in the VIVO ontology.
// See NIHVIVO-426, NIHVIVO-1158, NIHVIVO-1160. Some of these involve sorting on data properties of an
// individual two graph edges away from the individual being displayed, for which there is currently
// no provision. A better strategy will be designed and implemented in a later version, in particular
// one that does not involve hard-coded references to the VIVO ontology in the Vitro core.
private boolean applyCustomSort(ObjectProperty prop, List<ObjectPropertyStatement> statements) {
String vivoCoreOntology = "http://vivoweb.org/ontology/core#";
String propertyUri = prop.getURI();
// Positions in an organization
if (propertyUri.equals(vivoCoreOntology + "organizationForPosition")) {
sortByRelatedIndividualNames(statements, vivoCoreOntology + "positionForPerson");
return true;
}
// Person's positions
if (propertyUri.equals(vivoCoreOntology + "personInPosition")) {
sortReverseChron(statements, vivoCoreOntology + "endYear", vivoCoreOntology + "startYear");
return true;
}
// // Person's publications
// if (propertyUri.equals(vivoCoreOntology + "authorInAuthorship")) {
// sortByReverseChronAndRelatedIndividualName(statements, vivoCoreOntology + " ", vivoCoreOntology + " ");
// return true;
// }
return false;
}
// Apply custom sorting to an uncollated property. If the property is collated, the custom sorting has already
// been applied to each subclass listing individually.
private void applyCustomSortToUncollatedProperty(ObjectProperty op, List<ObjectPropertyStatement> opStmts) {
// This includes the case where the ontology doesn't specify collating, as well as the case
// where we don't collate because only one subclass is populated, since then we've set
// the collation value to false.
if (!op.getCollateBySubclass()) {
if (applyCustomSort(op, opStmts)) {
op.setObjectPropertyStatements(opStmts);
}
}
}
private void sortReverseChron(List<ObjectPropertyStatement> statements, String endYearPredicate, String startYearPredicate) {
// 1. Sort by end date descending, null dates first
// 2. Then by start date descending, null dates last
// 3. No sorting for entries with no start or end date - just put at the bottom in random order
final String endYearProperty = endYearPredicate;
final String startYearProperty = startYearPredicate;
Collections.sort(statements, new Comparator<ObjectPropertyStatement>() {
public int compare(ObjectPropertyStatement left, ObjectPropertyStatement right) {
String endLeftValue = left.getObject().getDataValue(endYearProperty);
Integer endLeft = endLeftValue == null ? null : Integer.valueOf(endLeftValue);
String startLeftValue = left.getObject().getDataValue(startYearProperty);
Integer startLeft = startLeftValue == null ? null : Integer.valueOf(startLeftValue);
String endRightValue = right.getObject().getDataValue(endYearProperty);
Integer endRight = endRightValue == null ? null : Integer.valueOf(endRightValue);
String startRightValue = right.getObject().getDataValue(startYearProperty);
Integer startRight = startRightValue == null ? null : Integer.valueOf(startRightValue);
// No sorting for entries with no start or end date - just put at the bottom in random order
if (endLeft == null && startLeft == null) {
return 1;
}
if (endRight == null && startRight == null) {
return -1;
}
// First sort by end date
// A null end date precedes
// But if both end dates are null, compare start dates
if ( ! (endLeft == null && endRight == null) ) {
if (endLeft == null) {
return -1;
}
if (endRight == null) {
return 1;
}
int endComp = endLeft.compareTo(endRight);
if (endComp != 0) {
return 0 - endComp;
}
}
// If end dates are equal, sort by start date
// A null start date follows
if (startLeft == null) {
return 1;
}
if (startRight == null) {
return -1;
}
return 0 - startLeft.compareTo(startRight);
}
});
}
// private void sortByReverseChronAndRelatedIndividualName(List<ObjectPropertyStatement> statements,
// String datePredicate, String relatedIndividualPredicate) {
//
// }
// Sort statements by the name of the individual on the other side of the context node.
private void sortByRelatedIndividualNames(List<ObjectPropertyStatement> statements, String predicateUri) {
log.debug("In sortByRelatedIndividualNames(), before sorting");
if (log.isDebugEnabled()) {
for (ObjectPropertyStatement ops : statements) {
log.debug(ops.getObject().getRelatedIndividual(predicateUri).getName());
}
}
final String propertyUri = predicateUri;
Collections.sort(statements, new Comparator<ObjectPropertyStatement>() {
public int compare(ObjectPropertyStatement left, ObjectPropertyStatement right) {
Individual indLeft = left.getObject().getRelatedIndividual(propertyUri);
Individual indRight = right.getObject().getRelatedIndividual(propertyUri);
return indLeft.getName().compareTo(indRight.getName());
}
});
log.debug("In sortByRelatedIndividualNames(), after sorting");
if (log.isDebugEnabled()) {
for (ObjectPropertyStatement ops : statements) {
log.debug(ops.getObject().getRelatedIndividual(predicateUri).getName());
}
}
}
private List<Individual> getObjectsFromStmts(List<ObjectPropertyStatement> orgStmtList) { private List<Individual> getObjectsFromStmts(List<ObjectPropertyStatement> orgStmtList) {
List<Individual> individuals = new LinkedList<Individual>(); List<Individual> individuals = new LinkedList<Individual>();