DS-2077 Sort collections by full path

This commit fixes the order of collection drop-down entries when starting a
submission and when editing e-mail subscriptions such that the community
hierarchy is taken into account as well as the collection name. The change in
CollectionDropDown makes the same mechanism re-usable in other places where
this might be required.
This commit is contained in:
Andrea Schweer
2014-08-15 17:06:47 +12:00
parent 5e7cb5e10a
commit 5cf94b6768
3 changed files with 68 additions and 5 deletions

View File

@@ -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<CollectionPathEntry>
{
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());
}
}
}

View File

@@ -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());

View File

@@ -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");