Merge r5942-5952 from nihvivo-rel-1.1.-maint: custom sorting and collation

This commit is contained in:
rjy7 2010-09-28 15:02:04 +00:00
parent 30653fdb62
commit 22cec61b5e
3 changed files with 93 additions and 33 deletions

View file

@ -61,6 +61,7 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
private static final Log log = LogFactory.getLog(EntityMergedPropertyListController.class.getName()); private static final Log log = LogFactory.getLog(EntityMergedPropertyListController.class.getName());
private static final int MAX_GROUP_DISPLAY_RANK = 99; private static final int MAX_GROUP_DISPLAY_RANK = 99;
private static final String VIVO_CORE_NAMESPACE = "http://vivoweb.org/ontology/core#";
/** Don't include these properties in the list. */ /** Don't include these properties in the list. */
private static final Collection<String> SUPPRESSED_OBJECT_PROPERTIES = Collections private static final Collection<String> SUPPRESSED_OBJECT_PROPERTIES = Collections
@ -451,7 +452,6 @@ 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 ) { if( prop instanceof ObjectProperty ) {
@ -464,7 +464,6 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
} }
} }
} }
return mergedPropertyList; return mergedPropertyList;
} }
@ -476,9 +475,13 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
* asserted classes are used. * asserted classes are used.
*/ */
private void collateBySubclass(ObjectProperty prop) { private void collateBySubclass(ObjectProperty prop) {
List<ObjectPropertyStatement> orgStmtList = prop.getObjectPropertyStatements();
if( orgStmtList == null ) List<ObjectPropertyStatement> orgStmtList = getStatementsToCollate(prop);
if( orgStmtList == null ) {
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 ) {
@ -545,17 +548,47 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
} }
private List<ObjectPropertyStatement> getStatementsToCollate(ObjectProperty prop) {
List<ObjectPropertyStatement> statements = prop.getObjectPropertyStatements();
if (statements != null) {
String propertyUri = prop.getURI();
if (propertyUri.equals(VIVO_CORE_NAMESPACE + "authorInAuthorship")) {
return getStatementsForRelatedIndividuals(statements, VIVO_CORE_NAMESPACE + "linkedInformationResource");
}
}
return statements;
}
private List<ObjectPropertyStatement> getStatementsForRelatedIndividuals(List<ObjectPropertyStatement> statements, String op) {
List<ObjectPropertyStatement> relatedStatements = new ArrayList<ObjectPropertyStatement>(statements.size());
for (ObjectPropertyStatement statement : statements) {
Individual object = statement.getObject();
relatedStatements.add(object.getObjectPropertyStatements(op).get(0));
}
return relatedStatements;
}
private void sortStatements(ObjectProperty prop, List<ObjectPropertyStatement> statements) { private void sortStatements(ObjectProperty prop, List<ObjectPropertyStatement> statements) {
if (statements.size() < 2) {
return;
}
if (!applyCustomSort(prop, statements)) { if (!applyCustomSort(prop, statements)) {
Collections.sort(statements, Collections.sort(statements,
new Comparator<ObjectPropertyStatement>() { new Comparator<ObjectPropertyStatement>() {
public int compare(ObjectPropertyStatement o1, public int compare(ObjectPropertyStatement o1,
ObjectPropertyStatement o2) { ObjectPropertyStatement o2) {
return o1.getObject().getName().compareTo( return o1.getObject().getName().compareTo(
o2.getObject().getName()); o2.getObject().getName());
} }
}); }
);
} }
} }
@ -566,24 +599,25 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
// one that does not involve hard-coded references to the VIVO ontology in the Vitro core. // one that does not involve hard-coded references to the VIVO ontology in the Vitro core.
private boolean applyCustomSort(ObjectProperty prop, List<ObjectPropertyStatement> statements) { private boolean applyCustomSort(ObjectProperty prop, List<ObjectPropertyStatement> statements) {
String vivoCoreOntology = "http://vivoweb.org/ontology/core#";
String propertyUri = prop.getURI(); String propertyUri = prop.getURI();
// Positions in an organization // Positions in an organization
if (propertyUri.equals(vivoCoreOntology + "organizationForPosition")) { if (propertyUri.equals(VIVO_CORE_NAMESPACE + "organizationForPosition")) {
sortByRelatedIndividualNames(statements, vivoCoreOntology + "positionForPerson"); sortByRelatedIndividualName(statements, VIVO_CORE_NAMESPACE + "positionForPerson");
return true; return true;
} }
// Person's positions // Person's positions
if (propertyUri.equals(vivoCoreOntology + "personInPosition")) { if (propertyUri.equals(VIVO_CORE_NAMESPACE + "personInPosition")) {
sortReverseChron(statements, vivoCoreOntology + "endYear", vivoCoreOntology + "startYear"); sortReverseChron(statements, VIVO_CORE_NAMESPACE + "endYear", VIVO_CORE_NAMESPACE + "startYear");
return true; return true;
} }
// Person's publications // Person's publications
if (propertyUri.equals(vivoCoreOntology + "authorInAuthorship")) { // Note that, due to the custom collation, the object property statements actually have predicate
sortByReverseChronAndRelatedIndividualName(statements, vivoCoreOntology + "year", vivoCoreOntology + "linkedInformationResource"); // linkedInformationResource. The property being sorted is still authorInAuthorship, however.
if (propertyUri.equals(VIVO_CORE_NAMESPACE + "authorInAuthorship")) {
sortByYearAndName(statements, VIVO_CORE_NAMESPACE + "year");
return true; return true;
} }
@ -611,16 +645,21 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
final String startYearProperty = startYearPredicate; final String startYearProperty = startYearPredicate;
Collections.sort(statements, new Comparator<ObjectPropertyStatement>() { Collections.sort(statements, new Comparator<ObjectPropertyStatement>() {
public int compare(ObjectPropertyStatement left, ObjectPropertyStatement right) { public int compare(ObjectPropertyStatement left, ObjectPropertyStatement right) {
String endLeftValue = left.getObject().getDataValue(endYearProperty);
Individual objLeft = left.getObject();
String endLeftValue = objLeft.getDataValue(endYearProperty);
Integer endLeft = endLeftValue == null ? null : Integer.valueOf(endLeftValue); Integer endLeft = endLeftValue == null ? null : Integer.valueOf(endLeftValue);
String startLeftValue = left.getObject().getDataValue(startYearProperty); String startLeftValue = objLeft.getDataValue(startYearProperty);
Integer startLeft = startLeftValue == null ? null : Integer.valueOf(startLeftValue); Integer startLeft = startLeftValue == null ? null : Integer.valueOf(startLeftValue);
String endRightValue = right.getObject().getDataValue(endYearProperty); Individual objRight = right.getObject();
String endRightValue = objRight.getDataValue(endYearProperty);
Integer endRight = endRightValue == null ? null : Integer.valueOf(endRightValue); Integer endRight = endRightValue == null ? null : Integer.valueOf(endRightValue);
String startRightValue = right.getObject().getDataValue(startYearProperty); String startRightValue = objRight.getDataValue(startYearProperty);
Integer startRight = startRightValue == null ? null : Integer.valueOf(startRightValue); Integer startRight = startRightValue == null ? null : Integer.valueOf(startRightValue);
// No sorting for entries with no start or end year - just put at the bottom in random order // No sorting for entries with no start or end year - just put at the bottom in random order
@ -662,26 +701,23 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
}); });
} }
private void sortByReverseChronAndRelatedIndividualName(List<ObjectPropertyStatement> statements, private void sortByYearAndName(List<ObjectPropertyStatement> statements, String yearPredicate) {
String yearPredicate, String relatedIndividualPredicate) {
// 1. Sort by year descending, nulls at end // 1. Sort by year descending, nulls at end
// 2. If years are the same, sort by related individual (in the case of authorships, publication) name // 2. If years are the same, sort by name
final String yearProperty = yearPredicate; final String yearProperty = yearPredicate;
final String relatedIndividualProperty = relatedIndividualPredicate;
Collections.sort(statements, new Comparator<ObjectPropertyStatement>() { Collections.sort(statements, new Comparator<ObjectPropertyStatement>() {
public int compare(ObjectPropertyStatement left, ObjectPropertyStatement right) { public int compare(ObjectPropertyStatement left, ObjectPropertyStatement right) {
Individual indLeft = left.getObject().getRelatedIndividual(relatedIndividualProperty); Individual indLeft = left.getObject();
Individual indRight = right.getObject().getRelatedIndividual(relatedIndividualProperty);
String leftYearValue = indLeft.getDataValue(yearProperty); String leftYearValue = indLeft.getDataValue(yearProperty);
Integer leftYear = leftYearValue == null ? null : Integer.valueOf(leftYearValue); Integer leftYear = leftYearValue == null ? null : Integer.valueOf(leftYearValue);
Individual indRight = right.getObject();
String rightYearValue = indRight.getDataValue(yearProperty); String rightYearValue = indRight.getDataValue(yearProperty);
Integer rightYear = rightYearValue == null ? null : Integer.valueOf(rightYearValue); Integer rightYear = rightYearValue == null ? null : Integer.valueOf(rightYearValue);
// First sort by year, nulls at end // First sort by year, nulls at end
// But if both null, sort by publication name // But if both null, sort by name
if ( ! (leftYear == null && rightYear == null) ) { if ( ! (leftYear == null && rightYear == null) ) {
if (leftYear == null) { if (leftYear == null) {
return 1; return 1;
@ -703,13 +739,22 @@ public class EntityMergedPropertyListController extends VitroHttpServlet {
} }
// Sort statements by the name of the individual on the other side of the context node. // Sort statements by the name of the individual on the other side of the context node.
private void sortByRelatedIndividualNames(List<ObjectPropertyStatement> statements, String predicateUri) { private void sortByRelatedIndividualName(List<ObjectPropertyStatement> statements, String predicateUri) {
final String propertyUri = predicateUri; final String propertyUri = predicateUri;
Collections.sort(statements, new Comparator<ObjectPropertyStatement>() { Collections.sort(statements, new Comparator<ObjectPropertyStatement>() {
public int compare(ObjectPropertyStatement left, ObjectPropertyStatement right) { public int compare(ObjectPropertyStatement left, ObjectPropertyStatement right) {
Individual indLeft = left.getObject().getRelatedIndividual(propertyUri); Individual indLeft = left.getObject().getRelatedIndividual(propertyUri);
Individual indRight = right.getObject().getRelatedIndividual(propertyUri); Individual indRight = right.getObject().getRelatedIndividual(propertyUri);
if (indLeft == null) {
return indRight == null ? 0 : 1;
}
if (indRight == null) {
return -1;
}
return indLeft.getName().compareTo(indRight.getName()); return indLeft.getName().compareTo(indRight.getName());
} }
}); });

View file

@ -121,6 +121,20 @@ public class PropertyEditLinks extends TagSupport{
if( item instanceof ObjectPropertyStatement ){ if( item instanceof ObjectPropertyStatement ){
ObjectPropertyStatement prop = (ObjectPropertyStatement)item; ObjectPropertyStatement prop = (ObjectPropertyStatement)item;
// rjy7 Another ugly hack to support collation of authorships by publication subclasses
// (NIHVIVO-1158). To display authorships this way, we've replaced the person-to-authorship
// statements with authorship-to-publication statements. Now we have to hack the edit links
// to edit the authorInAuthorship property statement rather than the linkedInformationResource
// statement.
String propertyUri = prop.getPropertyURI();
if (propertyUri.equals("http://vivoweb.org/ontology/core#linkedInformationResource")) {
String objectUri = prop.getSubjectURI();
String predicateUri = "http://vivoweb.org/ontology/core#authorInAuthorship";
String subjectUri = ((Individual)pageContext.getRequest().getAttribute("entity")).getURI();
prop = new ObjectPropertyStatementImpl(subjectUri, predicateUri, objectUri);
}
links = doObjPropStmt( prop, policyToAccess(ids, policy, prop), contextPath ); links = doObjPropStmt( prop, policyToAccess(ids, policy, prop), contextPath );
} else if( item instanceof DataPropertyStatement ){ } else if( item instanceof DataPropertyStatement ){

View file

@ -148,6 +148,7 @@ public static Log log = LogFactory.getLog("edu.cornell.mannlib.vitro.webapp.jsp.
//up an editing form. //up an editing form.
//Note that we do not want this behavior for the delete link (handled above). //Note that we do not want this behavior for the delete link (handled above).
if ( (predicateUri != null) && (objectUri != null) && (wdf.getObjectPropertyDao().skipEditForm(predicateUri)) ) { if ( (predicateUri != null) && (objectUri != null) && (wdf.getObjectPropertyDao().skipEditForm(predicateUri)) ) {
System.out.println("redirecting for predicate " + predicateUri);
%><c:redirect url="/individual"> %><c:redirect url="/individual">
<c:param name="uri" value="${param.objectUri}"/> <c:param name="uri" value="${param.objectUri}"/>
<c:param name="relatedSubjectUri" value="${param.subjectUri}"/> <c:param name="relatedSubjectUri" value="${param.subjectUri}"/>