diff --git a/dspace-api/src/main/java/org/dspace/app/util/CollectionDropDown.java b/dspace-api/src/main/java/org/dspace/app/util/CollectionDropDown.java index a6bb2e57dc..39b1a982e2 100644 --- a/dspace-api/src/main/java/org/dspace/app/util/CollectionDropDown.java +++ b/dspace-api/src/main/java/org/dspace/app/util/CollectionDropDown.java @@ -8,6 +8,8 @@ package org.dspace.app.util; import java.sql.SQLException; +import java.util.*; + import org.dspace.content.Collection; import org.dspace.content.Community; import org.dspace.core.ConfigurationManager; @@ -71,4 +73,63 @@ public class CollectionDropDown { return name.toString(); } + + /** + * Annotates an array of collections with their respective full paths (@see #collectionPath() method in this class). + * @param collections An array of collections to annotate with their hierarchical paths. + * The array and all its entries must be non-null. + * @return A sorted array of collection path entries (essentially collection/path pairs). + * @throws SQLException In case there are problems annotating a collection with its path. + */ + public static CollectionPathEntry[] annotateWithPaths(Collection[] collections) throws SQLException + { + CollectionPathEntry[] result = new CollectionPathEntry[collections.length]; + for (int i = 0; i < collections.length; i++) + { + Collection collection = collections[i]; + CollectionPathEntry entry = new CollectionPathEntry(collection, collectionPath(collection)); + result[i] = entry; + } + Arrays.sort(result); + return result; + } + + /** + * A helper class to hold (collection, full path) pairs. Instances of the helper class are sortable: + * two instances will be compared first on their full path and if those are equal, + * the comparison will fall back to comparing collection IDs. + */ + public static class CollectionPathEntry implements Comparable + { + public Collection collection; + public String path; + + public CollectionPathEntry(Collection collection, String path) + { + this.collection = collection; + this.path = path; + } + + @Override + public int compareTo(CollectionPathEntry other) + { + if (!this.path.equals(other.path)) + { + return this.path.compareTo(other.path); + } + return Integer.compare(this.collection.getID(), other.collection.getID()); + } + + @Override + public boolean equals(Object o) + { + return o != null && o instanceof CollectionPathEntry && this.compareTo((CollectionPathEntry) o) == 0; + } + + @Override + public int hashCode() + { + return Objects.hash(path, collection.getID()); + } + } } diff --git a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/eperson/EditProfile.java b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/eperson/EditProfile.java index 4caf5b889d..f73a15c830 100644 --- a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/eperson/EditProfile.java +++ b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/eperson/EditProfile.java @@ -375,11 +375,12 @@ public class EditProfile extends AbstractDSpaceTransformer subscriptions.enableDeleteOperation(); subscriptions.addOption(-1,T_select_collection); - for (Collection possible : possibleList) + CollectionDropDown.CollectionPathEntry[] possibleEntries = CollectionDropDown.annotateWithPaths(possibleList); + for (CollectionDropDown.CollectionPathEntry possible : possibleEntries) { - subscriptions.addOption(possible.getID(), CollectionDropDown.collectionPath(possible)); + subscriptions.addOption(possible.collection.getID(), possible.path); } - + for (Collection collection: currentList) { subscriptions.addInstance().setOptionSelected(collection.getID()); diff --git a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/submission/submit/SelectCollectionStep.java b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/submission/submit/SelectCollectionStep.java index ec178f7754..41cf7e59fb 100644 --- a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/submission/submit/SelectCollectionStep.java +++ b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/submission/submit/SelectCollectionStep.java @@ -98,9 +98,10 @@ public class SelectCollectionStep extends AbstractSubmissionStep select.setHelp(T_collection_help); select.addOption("",T_collection_default); - for (Collection collection : collections) + CollectionDropDown.CollectionPathEntry[] collectionPaths = CollectionDropDown.annotateWithPaths(collections); + for (CollectionDropDown.CollectionPathEntry entry : collectionPaths) { - select.addOption(collection.getHandle(), CollectionDropDown.collectionPath(collection)); + select.addOption(entry.collection.getHandle(), entry.path); } Button submit = list.addItem().addButton("submit");