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);
@@ -156,7 +160,16 @@ 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,10 +96,25 @@ 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
*
* @param number
@@ -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()