Skip to content

Commit

Permalink
Merge pull request #586 from aschweer/DS-2077-sort-collection-dropdown
Browse files Browse the repository at this point in the history
Ds 2077 sort collection dropdown
  • Loading branch information
peterdietz committed Aug 20, 2014
2 parents 88846c0 + 5cf94b6 commit 024f7dd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -71,4 +73,63 @@ public static String collectionPath(Collection col, int maxchars) throws SQLExce

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());
}
}
}
Expand Up @@ -375,11 +375,12 @@ else if (eperson != null)
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());
Expand Down
Expand Up @@ -98,9 +98,10 @@ public void addBody(Body body) throws SAXException, WingException,
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");
Expand Down

0 comments on commit 024f7dd

Please sign in to comment.