Add the capability to define in which order the index should be listed by default (ascending or descending)

Also, removed two uses of 'magic' strings - replaced with static values from SortOption

git-svn-id: http://scm.dspace.org/svn/repo/trunk@2259 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2007-10-24 11:47:31 +00:00
parent 024ba53adf
commit 8c2692ea7d
7 changed files with 152 additions and 49 deletions

View File

@@ -83,6 +83,9 @@ public class BrowseIndex
/** a three part array of the metadata bits (e.g. dc.contributor.author) */
private String[] mdBits;
/** default order (asc / desc) for this index */
private String defaultOrder;
/** additional 'internal' tables that are always defined */
private static BrowseIndex itemIndex = new BrowseIndex("bi_item");
private static BrowseIndex withdrawnIndex = new BrowseIndex("bi_withdrawn");
@@ -135,9 +138,10 @@ public class BrowseIndex
throws BrowseException
{
boolean valid = true;
this.defaultOrder = SortOption.ASCENDING;
this.number = number;
String rx = "(\\w+):(\\w+):([\\w\\.\\*]+):?(\\w*)";
String rx = "(\\w+):(\\w+):([\\w\\.\\*]+):?(\\w*):?(\\w*)";
Pattern pattern = Pattern.compile(rx);
Matcher matcher = pattern.matcher(definition);
@@ -157,6 +161,15 @@ public class BrowseIndex
if (datatype == null || datatype.equals(""))
valid = false;
// If an optional ordering configuration is supplied,
// set the defaultOrder appropriately (asc or desc)
if (matcher.groupCount() > 4)
{
String order = matcher.group(5);
if (SortOption.DESCENDING.equalsIgnoreCase(order))
this.defaultOrder = SortOption.DESCENDING;
}
tableBaseName = makeTableBaseName(number);
}
else if (isItemIndex())
@@ -191,6 +204,14 @@ public class BrowseIndex
}
}
/**
* @return Default order for this index, null if not specified
*/
public String getDefaultOrder()
{
return defaultOrder;
}
/**
* @return Returns the datatype.
*/

View File

@@ -39,6 +39,7 @@ import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.core.Context;
import org.apache.log4j.Logger;
/**
* A class which represents the initial request to the browse system.
@@ -50,6 +51,9 @@ import org.dspace.core.Context;
*/
public class BrowserScope
{
/** the logger for this class */
private static Logger log = Logger.getLogger(BrowserScope.class);
/** the DSpace context */
private Context context;
@@ -57,7 +61,7 @@ public class BrowserScope
private BrowseIndex browseIndex;
/** the order in which to display results */
private String order = "ASC";
private String order;
/** the field upon which to sort */
private int sortBy;
@@ -325,17 +329,43 @@ public class BrowserScope
*/
public String getOrder()
{
return order;
}
if (order != null)
return order;
try
{
SortOption so = getSortOption();
if (so != null)
return so.getDefaultOrder();
}
catch (BrowseException be)
{
// recoverable problem, just log the error and continue
log.debug("Unable to retrieve a sort option for this browse", be);
}
return SortOption.ASCENDING;
}
/**
* @param order The order to set.
*/
public void setOrder(String order)
{
if (order != null && !"".equals(order))
this.order = order;
}
if (order == null)
{
this.order = null;
}
else if (SortOption.ASCENDING.equalsIgnoreCase(order))
{
this.order = SortOption.ASCENDING;
}
else if (SortOption.DESCENDING.equalsIgnoreCase(order))
{
this.order = SortOption.DESCENDING;
}
}
/**
* @return Returns the resultsPerPage.
@@ -392,7 +422,7 @@ public class BrowserScope
// Create a dummy sortOption for the metadata sort
String dataType = browseIndex.getDataType();
String type = ("date".equals(dataType) ? "date" : "text");
sortOption = new SortOption(0, browseIndex.getName(), browseIndex.getMetadata(), type);
sortOption = new SortOption(0, browseIndex.getName(), browseIndex.getMetadata(), type, browseIndex.getDefaultOrder());
}
else
{
@@ -505,15 +535,34 @@ public class BrowserScope
}
/**
* @return true if ascending, false if not
* @return true if ascending, false if not - or not set
*/
public boolean isAscending()
{
if ("ASC".equals(order))
if (SortOption.ASCENDING.equalsIgnoreCase(order))
{
return true;
}
return false;
if (SortOption.DESCENDING.equalsIgnoreCase(order))
{
return false;
}
try
{
SortOption so = getSortOption();
if (so != null && SortOption.DESCENDING.equalsIgnoreCase(so.getDefaultOrder()))
return false;
}
catch (BrowseException be)
{
// recoverable problem, just log the error and continue
log.debug("Unable to retrieve a sort option for this browse", be);
}
return true;
}
/**

View File

@@ -55,7 +55,10 @@ import org.dspace.core.ConfigurationManager;
*/
public class SortOption
{
/** the sort configuration number */
public static final String ASCENDING = "ASC";
public static final String DESCENDING = "DESC";
/** the sort configuration number */
private int number;
/** the name of the sort */
@@ -70,6 +73,9 @@ public class SortOption
/** the metadata broken down into bits for convenience */
private String[] mdBits;
/** default ordering for this sort, when not supplied by the user */
private String defaultOrder;
/** the sort options available for this index */
private static Set<SortOption> sortOptionsSet = null;
private static Map<Integer, SortOption> sortOptionsMap = null;
@@ -90,9 +96,24 @@ public class SortOption
this.type = type;
this.metadata = md;
this.number = number;
generateMdBits();
this.defaultOrder = SortOption.ASCENDING;
generateMdBits();
}
public SortOption(int number, String name, String md, String type, String defaultOrder)
throws BrowseException
{
this.name = name;
this.type = type;
this.metadata = md;
this.number = number;
if (SortOption.DESCENDING.equalsIgnoreCase(defaultOrder))
this.defaultOrder = SortOption.DESCENDING;
else
this.defaultOrder = SortOption.ASCENDING;
generateMdBits();
}
/**
* Construct a new SortOption object using the definition from the configuration
*
@@ -105,7 +126,7 @@ public class SortOption
{
this.number = number;
String rx = "(\\w+):([\\w\\.\\*]+):(\\w+)";
String rx = "(\\w+):([\\w\\.\\*]+):(\\w+):?(\\w*)";
Pattern pattern = Pattern.compile(rx);
Matcher matcher = pattern.matcher(definition);
@@ -118,10 +139,28 @@ public class SortOption
name = matcher.group(1);
metadata = matcher.group(2);
type = matcher.group(3);
if (matcher.groupCount() > 3)
defaultOrder = matcher.group(4);
// If the default order is specified as descending, keep it
// Otherwise, set the default order to ascending
if (SortOption.DESCENDING.equalsIgnoreCase(defaultOrder))
defaultOrder = SortOption.DESCENDING;
else
defaultOrder = SortOption.ASCENDING;
generateMdBits();
}
/**
/**
* @return Returns the default ordering for this sort option
*/
public String getDefaultOrder()
{
return defaultOrder;
}
/**
* @return Returns the metadata.
*/
public String getMetadata()

View File

@@ -103,7 +103,7 @@ public class RecentSubmissionsManager
// fill in the scope with the relevant gubbins
bs.setBrowseIndex(bi);
bs.setOrder("DESC"); // FIXME: eek - plain text flag! Should be BrowserScope.ASC or something
bs.setOrder(SortOption.DESCENDING);
bs.setResultsPerPage(Integer.parseInt(count));
bs.setBrowseContainer(dso);
for (SortOption so : SortOption.getSortOptions())

View File

@@ -145,12 +145,6 @@ public abstract class AbstractBrowserServlet extends DSpaceServlet
resultsperpage = 20;
}
// if no order parameter, default to ascending
if (order == null || "".equals(order))
{
order = "ASC";
}
// if year and perhaps month have been selected, we translate these into "startsWith"
// if startsWith has already been defined then it is overwritten
if (year != null && !"".equals(year) && !"-1".equals(year))

View File

@@ -39,27 +39,13 @@
*/
package org.dspace.app.webui.servlet.admin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.dspace.app.webui.servlet.DSpaceServlet;
import org.dspace.app.webui.util.JSPManager;
import org.dspace.app.webui.util.UIUtil;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.AuthorizeManager;
import org.dspace.browse.BrowseEngine;
import org.dspace.browse.BrowseException;
import org.dspace.browse.BrowseIndex;
import org.dspace.browse.BrowseInfo;
import org.dspace.browse.BrowserScope;
import org.dspace.browse.IndexBrowse;
import org.dspace.browse.*;
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.content.ItemIterator;
@@ -67,6 +53,14 @@ import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**
* Servlet for editing and deleting (expunging) items
*
@@ -372,7 +366,7 @@ public class ItemMapServlet extends DSpaceServlet
// set up the browse scope
bs.setBrowseIndex(bi);
bs.setOrder("ASC");
bs.setOrder(SortOption.ASCENDING);
bs.setFilterValue(name);
bs.setJumpToValue(null);
bs.setResultsPerPage(10000); // an arbitrary number (large) for the time being

View File

@@ -376,7 +376,7 @@ webui.licence_bundle.show = false
#
# webui.browse.index.<n> = <index name> : metadata : \
# <schema prefix>.<element>[.<qualifier>|.*] : \
# (date | title | text) :
# (date | title | text) : (asc | desc)
#
# This form represent a unique index of metadata values from the item.
#
@@ -389,6 +389,10 @@ webui.licence_bundle.show = false
# <other>: any other datatype will be treated the same as 'text', although
# it will apply any custom ordering normalisation configured below
#
# The final part of the configuration is optional, and specifies the default ordering
# for the index - whether it is ASCending (the default, and best for text indexes), or
# DESCending (useful for dates - ie. most recent submissions)
#
# NOTE: the text to render the index will use the <index name> parameter to select
# the message key from Messages.properties using a key of the form:
#
@@ -429,13 +433,15 @@ webui.browse.index.4 = subject:metadata:dc.subject.*:text
#
# webui.browse.sort-option.<n> = <option name> : \
# <schema prefix>.<element>[.<qualifier>|.*] : \
# (date, text)
# (date | text | ...) : (asc | desc)
#
# This is defined much the same as above. The only difference is that the final
# parameter just lets the sorter know whether to expect to sort by plain text (using
# the "text" option), or by a proper date (using the "date" option). If no options
# are specified, you will not be able to sort any results by anthing other than the
# key values.
# This is defined much the same as above. The parameter after the metadata
# just lets the sorter know which normalisation to use - standard normalisations are title,
# text or date - however additional normalisations can be defined using the PluginManager.
#
# The final part of the configuration is optional, and specifies the default ordering
# for the index - whether it is ASCending (the default, and best for text indexes), or
# DESCending (useful for dates - ie. most recent submissions)
#
webui.browse.sort-option.1 = title:dc.title:title
webui.browse.sort-option.2 = dateissued:dc.date.issued:date