Merge pull request #324 from helix84/DS-1475-collection-dropdown

DS-1475 Improvement of Collection Dropdown
This commit is contained in:
Andrea Bollini
2013-10-21 17:43:44 -07:00
7 changed files with 185 additions and 29 deletions

View File

@@ -0,0 +1,74 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.util;
import java.sql.SQLException;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.core.ConfigurationManager;
/**
* Utility class for lists of collections.
*/
public class CollectionDropDown {
/**
* Get full path starting from a top-level community via subcommunities down to a collection.
* The full path will not be truncated.
*
* @param col
* Get full path for this collection
* @return Full path to the collection
*/
public static String collectionPath(Collection col) throws SQLException
{
return CollectionDropDown.collectionPath(col, 0);
}
/**
* Get full path starting from a top-level community via subcommunities down to a collection.
* The full cat will be truncated to the specified number of characters and prepended with an ellipsis.
*
* @param col
* Get full path for this collection
* @param maxchars
* Truncate the full path to maxchar characters. 0 means do not truncate.
* @return Full path to the collection (truncated)
*/
public static String collectionPath(Collection col, int maxchars) throws SQLException
{
String separator = ConfigurationManager.getProperty("subcommunity.separator");
if (separator == null)
{
separator = " > ";
}
Community[] getCom = null;
StringBuffer name = new StringBuffer("");
getCom = col.getCommunities(); // all communities containing given collection
for (Community com : getCom)
{
name.insert(0, com.getMetadata("name") + separator);
}
name.append(col.getMetadata("name"));
if (maxchars != 0)
{
int len = name.length();
if (len > maxchars)
{
name = new StringBuffer(name.substring(len - (maxchars - 1), len));
name.insert(0, "\u2026"); // prepend with an ellipsis (cut from left)
}
}
return name.toString();
}
}

View File

@@ -34,7 +34,9 @@ import org.dspace.content.Collection;
import org.dspace.content.DCDate;
import org.dspace.content.DCValue;
import org.dspace.content.Item;
import org.dspace.content.Site;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.Email;
import org.dspace.core.I18nUtil;
@@ -201,6 +203,47 @@ public class Subscribe
return (Collection[]) collections.toArray(collArray);
}
/**
* Find out which collections the currently logged in e-person can subscribe to
*
* @param context
* DSpace context
* @param eperson
* EPerson
* @return array of collections the currently logged in e-person can subscribe to
*/
public static Collection[] getAvailableSubscriptions(Context context)
throws SQLException
{
return getAvailableSubscriptions(context, null);
}
/**
* Find out which collections an e-person can subscribe to
*
* @param context
* DSpace context
* @param eperson
* EPerson
* @return array of collections e-person can subscribe to
*/
public static Collection[] getAvailableSubscriptions(Context context, EPerson eperson)
throws SQLException
{
Collection[] collections;
if (eperson != null)
{
context.setCurrentUser(eperson);
}
Site site = (Site) Site.find(context, 0);
collections = Collection.findAuthorized(context, null, Constants.ADD);
return collections;
}
/**
* Is that e-person subscribed to that collection?
*

View File

@@ -731,6 +731,7 @@ jsp.mydspace.request.export.migratecollection = Export (migrat
jsp.mydspace.request.export.migrateitem = Export (migrate) Item
jsp.mydspace.subscriptions.info1 = Your subscriptions have been updated.
jsp.mydspace.subscriptions.info2 = To subscribe to a collection, visit the collection's home page, and click on the "Subscribe" button.
jsp.mydspace.subscriptions.select_collection = ( Select Collection )
jsp.mydspace.subscriptions.info3 = Below are the collections you are subscribed to. You will be sent an e-mail each day detailing new items that have become available in these collections. On days that no new items have appeared, no e-mail will be sent.
jsp.mydspace.subscriptions.info4 = You are not currently subscribed to any collections.
jsp.mydspace.subscriptions.remove.button = Remove All Subscriptions

View File

@@ -14,6 +14,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dspace.app.util.CollectionDropDown;
import org.dspace.app.webui.util.JSPManager;
import org.dspace.app.webui.util.UIUtil;
import org.dspace.authorize.AuthorizeException;
@@ -60,6 +61,22 @@ public class SubscribeServlet extends DSpaceServlet
context.complete();
}
else if (submit.equals("submit_subscribe"))
{
int collID = UIUtil.getIntParameter(request, "collection");
Collection c = Collection.find(context, collID);
// Sanity check - ignore duff values
if (c != null)
{
Subscribe.subscribe(context, e, c);
}
// Show the list of subscriptions
showSubscriptions(context, request, response, true);
context.complete();
}
else if (submit.equals("submit_unsubscribe"))
{
int collID = UIUtil.getIntParameter(request, "collection");
@@ -102,10 +119,14 @@ public class SubscribeServlet extends DSpaceServlet
HttpServletResponse response, boolean updated)
throws ServletException, IOException, SQLException
{
// collections the currently logged in user can subscribe to
Collection[] avail = Subscribe.getAvailableSubscriptions(context);
// Subscribed collections
Collection[] subs = Subscribe.getSubscriptions(context, context
.getCurrentUser());
request.setAttribute("availableSubscriptions", avail);
request.setAttribute("subscriptions", subs);
request.setAttribute("updated", Boolean.valueOf(updated));

View File

@@ -27,8 +27,11 @@
<%@ page import="org.dspace.content.Community" %>
<%@ page import="org.dspace.content.Collection" %>
<%@ page import="org.dspace.app.util.CollectionDropDown" %>
<%
Collection[] availableSubscriptions =
(Collection[]) request.getAttribute("availableSubscriptions");
Collection[] subscriptions =
(Collection[]) request.getAttribute("subscriptions");
boolean updated =
@@ -49,10 +52,28 @@
{
%>
<p><strong><fmt:message key="jsp.mydspace.subscriptions.info1"/></strong></p>
<p><fmt:message key="jsp.mydspace.subscriptions.info2"/></p>
<%
}
%>
<p><fmt:message key="jsp.mydspace.subscriptions.info2"/></p>
<form class="form-group" action="<%= request.getContextPath() %>/subscribe" method="post">
<div class="col-md-6">
<select id="available-subscriptions" class="form-control" name="collection">
<option value="-1"><fmt:message key="jsp.mydspace.subscriptions.select_collection" /></option>
<%
for (int i = 0; i < availableSubscriptions.length; i++)
{
%>
<option value="<%= availableSubscriptions[i].getID() %>"><%= CollectionDropDown.collectionPath(availableSubscriptions[i], 0) %></option>
<%
}
%>
</select>
</div>
<input class="btn btn-success" type="submit" name="submit_subscribe" value="<fmt:message key="jsp.collection-home.subscribe"/>" />
<input class="btn btn-danger" type="submit" name="submit_clear" value="<fmt:message key="jsp.mydspace.subscriptions.remove.button"/>" />
</form>
<%
if (subscriptions.length > 0)
{
@@ -73,7 +94,7 @@
--%>
<td class="<%= row %>RowOddCol">
<a href="<%= request.getContextPath() %>/handle/<%= subscriptions[i].getHandle() %>"><%= subscriptions[i].getMetadata("name") %></a>
<a href="<%= request.getContextPath() %>/handle/<%= subscriptions[i].getHandle() %>"><%= CollectionDropDown.collectionPath(subscriptions[i],0) %></a>
</td>
<td class="<%= row %>RowEvenCol">
<form method="post" action="">
@@ -90,9 +111,6 @@
<br/>
<form method="post" action="">
<input class="btn btn-danger" type="submit" name="submit_clear" value="<fmt:message key="jsp.mydspace.subscriptions.remove.button"/>" />
</form>
<%
}
else

View File

@@ -21,6 +21,7 @@ import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.log4j.Logger;
import org.dspace.app.util.CollectionDropDown;
import org.dspace.app.xmlui.cocoon.AbstractDSpaceTransformer;
import org.dspace.app.xmlui.wing.Message;
import org.dspace.app.xmlui.wing.WingException;
@@ -33,6 +34,7 @@ import org.dspace.app.xmlui.wing.element.PageMeta;
import org.dspace.app.xmlui.wing.element.Select;
import org.dspace.app.xmlui.wing.element.Text;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.I18nUtil;
import org.dspace.core.LogManager;
@@ -40,6 +42,7 @@ import org.dspace.eperson.Group;
import org.dspace.eperson.Subscribe;
import org.xml.sax.SAXException;
/**
* Display a form that allows the user to edit their profile.
* There are two cases in which this can be used: 1) when an
@@ -374,12 +377,7 @@ public class EditProfile extends AbstractDSpaceTransformer
subscriptions.addOption(-1,T_select_collection);
for (Collection possible : possibleList)
{
String name = possible.getMetadata("name");
if (name.length() > 50)
{
name = name.substring(0, 47) + "...";
}
subscriptions.addOption(possible.getID(), name);
subscriptions.addOption(possible.getID(), CollectionDropDown.collectionPath(possible));
}
for (Collection collection: currentList)

View File

@@ -28,6 +28,8 @@ import org.dspace.core.Constants;
import org.dspace.handle.HandleManager;
import org.xml.sax.SAXException;
import org.dspace.app.util.CollectionDropDown;
/**
* Allow the user to select a collection they wish to submit an item to,
* this step is sort-of but not officialy part of the item submission
@@ -98,8 +100,7 @@ public class SelectCollectionStep extends AbstractSubmissionStep
select.addOption("",T_collection_default);
for (Collection collection : collections)
{
String name = collection.getMetadata("name");
select.addOption(collection.getHandle(),name);
select.addOption(collection.getHandle(), CollectionDropDown.collectionPath(collection));
}
Button submit = list.addItem().addButton("submit");