Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aschweer/97c5d69004d0badd7d5f to your computer and use it in GitHub Desktop.
Save aschweer/97c5d69004d0badd7d5f to your computer and use it in GitHub Desktop.
DSpace show browse index only where configured
From e5c7214d3f908c72c3ce2b86c608a1eb1252ac42 Mon Sep 17 00:00:00 2001
From: Andrea Schweer <schweer@waikato.ac.nz>
Date: Fri, 11 May 2012 15:10:45 +1200
Subject: [PATCH] [LCoNZ] Show browse indexes only where configured
---
.../aspect/browseArtifacts/CollectionBrowse.java | 45 ++++++++-
.../aspect/browseArtifacts/CommunityBrowse.java | 44 +++++++-
.../xmlui/aspect/browseArtifacts/Navigation.java | 111 ++++++++++++++++++++-
3 files changed, 193 insertions(+), 7 deletions(-)
diff --git a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/CollectionBrowse.java b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/CollectionBrowse.java
index bc76e56..dbd60a2 100644
--- a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/CollectionBrowse.java
+++ b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/CollectionBrowse.java
@@ -19,11 +19,15 @@ import org.dspace.authorize.AuthorizeException;
import org.dspace.browse.BrowseException;
import org.dspace.browse.BrowseIndex;
import org.dspace.content.Collection;
+import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
+import org.dspace.core.ConfigurationManager;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -74,7 +78,10 @@ public class CollectionBrowse extends AbstractDSpaceTransformer {
try {
// Get a Map of all the browse tables
BrowseIndex[] bis = BrowseIndex.getBrowseIndices();
- for (BrowseIndex bix : bis) {
+ for (BrowseIndex bix : bis)
+ {
+ if (showBrowseIndex(bix, collection))
+ {
// Create a Map of the query parameters for this link
Map<String, String> queryParams = new HashMap<String, String>();
@@ -84,10 +91,46 @@ public class CollectionBrowse extends AbstractDSpaceTransformer {
browse.addItemXref(generateURL(url + "/browse", queryParams),
message("xmlui.ArtifactBrowser.Navigation.browse_" + bix.getName()));
}
+ }
} catch (BrowseException bex) {
browse.addItemXref(url + "/browse?type=title", T_browse_titles);
browse.addItemXref(url + "/browse?type=author", T_browse_authors);
browse.addItemXref(url + "/browse?type=dateissued", T_browse_dates);
}
}
+
+ private boolean showBrowseIndex(BrowseIndex index, Collection collection) throws SQLException {
+ String name = index.getName();
+
+ String showWhere = ConfigurationManager.getProperty("webui.browse.show.index." + name);
+ java.util.List<String> showHandles = new ArrayList<String>();
+ if (showWhere != null)
+ {
+ String[] handles = showWhere.split(",\\s*");
+ if (handles != null && handles.length > 0)
+ {
+ showHandles = Arrays.asList(handles);
+ }
+ }
+ // as a default, if there is no configuration, show the browse index
+ if (showHandles.isEmpty())
+ {
+ return true;
+ }
+
+ // there is configuration -- check if current collection or one of its parent communities are included
+ boolean showHere = showHandles.contains(collection.getHandle());
+ if (!showHere)
+ {
+ for (Community comm : collection.getCommunities())
+ {
+ if (showHandles.contains(comm.getHandle()))
+ {
+ showHere = true;
+ break;
+ }
+ }
+ }
+ return showHere;
+ }
}
\ No newline at end of file
diff --git a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/CommunityBrowse.java b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/CommunityBrowse.java
index 04b3a01..a25fee3 100644
--- a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/CommunityBrowse.java
+++ b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/CommunityBrowse.java
@@ -20,10 +20,13 @@ import org.dspace.browse.BrowseException;
import org.dspace.browse.BrowseIndex;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
+import org.dspace.core.ConfigurationManager;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -74,7 +77,10 @@ public class CommunityBrowse extends AbstractDSpaceTransformer {
try {
// Get a Map of all the browse tables
BrowseIndex[] bis = BrowseIndex.getBrowseIndices();
- for (BrowseIndex bix : bis) {
+ for (BrowseIndex bix : bis)
+ {
+ if (showBrowseIndex(bix, community))
+ {
// Create a Map of the query parameters for this link
Map<String, String> queryParams = new HashMap<String, String>();
@@ -84,10 +90,46 @@ public class CommunityBrowse extends AbstractDSpaceTransformer {
browse.addItemXref(generateURL(url + "/browse", queryParams),
message("xmlui.ArtifactBrowser.Navigation.browse_" + bix.getName()));
}
+ }
} catch (BrowseException bex) {
browse.addItemXref(url + "/browse?type=title", T_browse_titles);
browse.addItemXref(url + "/browse?type=author", T_browse_authors);
browse.addItemXref(url + "/browse?type=dateissued", T_browse_dates);
}
}
+
+ private boolean showBrowseIndex(BrowseIndex index, Community community) throws SQLException {
+ String name = index.getName();
+
+ String showWhere = ConfigurationManager.getProperty("webui.browse.show.index." + name);
+ java.util.List<String> showHandles = new ArrayList<String>();
+ if (showWhere != null)
+ {
+ String[] handles = showWhere.split(",\\s*");
+ if (handles != null && handles.length > 0)
+ {
+ showHandles = Arrays.asList(handles);
+ }
+ }
+ // as a default, if there is no configuration, show the browse index
+ if (showHandles.isEmpty())
+ {
+ return true;
+ }
+
+ // there is configuration -- check if current community or one of its parents is included
+ boolean showHere = showHandles.contains(community.getHandle());
+ if (!showHere)
+ {
+ for (Community parentComm : community.getAllParents())
+ {
+ if (showHandles.contains(parentComm.getHandle()))
+ {
+ showHere = true;
+ break;
+ }
+ }
+ }
+ return showHere;
+ }
}
\ No newline at end of file
diff --git a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/Navigation.java b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/Navigation.java
index 77b4254..c0e71a5 100644
--- a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/Navigation.java
+++ b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/browseArtifacts/Navigation.java
@@ -27,11 +27,15 @@ import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
+import org.dspace.core.ConfigurationManager;
+import org.dspace.core.Constants;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -127,7 +131,7 @@ public class Navigation extends AbstractDSpaceTransformer implements CacheablePr
browseGlobal.addItemXref(contextPath + "/community-list",T_communities_and_collections);
// Add the configured browse lists for 'top level' browsing
- addBrowseOptions(browseGlobal, contextPath + "/browse");
+ addBrowseOptions(browseGlobal, contextPath + "/browse", true );
DSpaceObject dso = HandleUtil.obtainHandle(objectModel);
if (dso != null)
@@ -150,7 +154,7 @@ public class Navigation extends AbstractDSpaceTransformer implements CacheablePr
// Add the configured browse lists for scoped browsing
String handle = dso.getHandle();
- addBrowseOptions(browseContext, contextPath + "/handle/" + handle + "/browse");
+ addBrowseOptions(browseContext, contextPath + "/handle/" + handle + "/browse", false);
}
}
@@ -161,7 +165,7 @@ public class Navigation extends AbstractDSpaceTransformer implements CacheablePr
* @param browseURL
* @throws WingException
*/
- private void addBrowseOptions(List browseList, String browseURL) throws WingException
+ private void addBrowseOptions(List browseList, String browseURL, boolean global) throws WingException
{
// FIXME Exception handling
try
@@ -170,20 +174,117 @@ public class Navigation extends AbstractDSpaceTransformer implements CacheablePr
BrowseIndex[] bis = BrowseIndex.getBrowseIndices();
for (BrowseIndex bix : bis)
{
+ String name = bix.getName();
+
+ boolean showHere = showBrowseIndex(global, name);
+
+ if (showHere)
+ {
// Create a Map of the query parameters for this link
Map<String, String> queryParams = new HashMap<String, String>();
- queryParams.put("type", bix.getName());
+ queryParams.put("type", name);
// Add a link to this browse
browseList.addItemXref(super.generateURL(browseURL, queryParams),
- message("xmlui.ArtifactBrowser.Navigation.browse_" + bix.getName()));
+ message("xmlui.ArtifactBrowser.Navigation.browse_" + name));
}
}
+ }
catch (BrowseException bex)
{
throw new UIException("Unable to get browse indicies", bex);
}
}
+ private boolean showBrowseIndex(boolean global, String name) throws UIException {
+ String showWhere = ConfigurationManager.getProperty("webui.browse.show.index." + name);
+ java.util.List<String> showHandles = new ArrayList<String>();
+ if (showWhere != null)
+ {
+ String[] handles = showWhere.split(",\\s*");
+ if (handles != null && handles.length > 0)
+ {
+ showHandles = Arrays.asList(handles);
+ }
+ }
+ // as a default, if there is no configuration, show the browse index
+ boolean showHere = showHandles.isEmpty();
+
+ // for local (collection/community) browse, there's one more chance to show the browse index: if
+ // the local scope is given in the config or is a child of one in the config
+ if (!global && !showHandles.isEmpty())
+ {
+ try {
+ DSpaceObject dso = HandleUtil.obtainHandle(objectModel);
+ if (dso == null)
+ {
+ showHere = false;
+ }
+ else if (dso.getType() == Constants.ITEM)
+ {
+ Item item = (Item) dso;
+ for (Community comm : item.getCommunities())
+ {
+ if (showHandles.contains(comm.getHandle()))
+ {
+ showHere = true;
+ break;
+ }
+ }
+ if (!showHere)
+ {
+ for (Collection coll : item.getCollections())
+ {
+ if (showHandles.contains(coll.getHandle()))
+ {
+ showHere = true;
+ break;
+ }
+ }
+ }
+ }
+ else if (dso.getType() == Constants.COLLECTION)
+ {
+ Collection coll = (Collection) dso;
+ showHere = showHandles.contains(coll.getHandle());
+ if (!showHere)
+ {
+ for (Community comm : coll.getCommunities())
+ {
+ if (showHandles.contains(comm.getHandle()))
+ {
+ showHere = true;
+ break;
+ }
+ }
+ }
+ }
+ else if (dso.getType() == Constants.COMMUNITY)
+ {
+ Community comm = (Community) dso;
+ showHere = showHandles.contains(comm.getHandle());
+ if (!showHere)
+ {
+ for (Community parentComm : comm.getAllParents())
+ {
+ if (showHandles.contains(parentComm.getHandle()))
+ {
+ showHere = true;
+ break;
+ }
+ }
+ }
+ }
+ else
+ {
+ showHere = false;
+ }
+ } catch (SQLException e) {
+ throw new UIException("Cannot determine which browse indexes to show", e);
+ }
+ }
+ return showHere;
+ }
+
}
--
1.8.3.2
# LCoNZ customisation -- show some indexes only for some collections/communities
#
# 'Normal' browse indexes (shown everywhere) need no additional configuration here.
# Browse indexes that are limited to some collections/communities need to be listed here
# with their name and the handles of the communities/collections, like so:
#
# webui.browse.show.index.degreename = 123456789/2, 123456789/7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment