[DS-544] Removal of mapped items can lead to NPE

git-svn-id: http://scm.dspace.org/svn/repo/dspace/trunk@4941 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Kim Shepherd
2010-05-14 01:54:20 +00:00
parent ae725ce66f
commit 43315884ea
3 changed files with 29 additions and 85 deletions

View File

@@ -1235,6 +1235,7 @@ jsp.tools.itemmap-info.button.continue = Continue
jsp.tools.itemmap-info.heading = Item Map Info
jsp.tools.itemmap-info.msg.added = Add item {0}
jsp.tools.itemmap-info.msg.none-selected = No items selected, none added.
jsp.tools.itemmap-info.msg.none-removed = No items selected, none removed.
# In the following 2 messages the only parameter is an item ID
jsp.tools.itemmap-info.msg.remove = Remove item {0}
jsp.tools.itemmap-info.title = Item Map Info

View File

@@ -198,72 +198,6 @@ public class ItemMapServlet extends DSpaceServlet
// show the page
JSPManager.showJSP(request, response, jspPage);
}
/*
* else if( action.equals("add") ) { int itemID =
* UIUtil.getIntParameter(request, "item_id"); String handle =
* (String)request.getParameter("handle"); boolean error = true; Item
* itemToAdd = null;
*
* if( itemID > 0 ) { itemToAdd = Item.find(context, itemID);
*
* if( itemToAdd != null ) error = false; } else if(handle != null &&
* !handle.equals("")) { DSpaceObject
* dso=HandleManager.resolveToObject(context, handle);
*
* if(dso != null && dso.getType() == Constants.ITEM) { itemToAdd =
* (Item)dso; error = false; } }
*
* //FIXME: error handling! if( !error ) { String myTitle =
* itemToAdd.getDC("title",null,Item.ANY)[0].value; String ownerName =
* itemToAdd.getOwningCollection().getMetadata("name");
* // hook up item, but first, does it belong already? TableRowIterator
* tri = DatabaseManager.query(context, "collection2item", "SELECT
* collection2item.* FROM collection2item WHERE " + "collection_id=" +
* myCollection.getID() + " AND item_id=" + itemToAdd.getID());
*
* if(tri.hasNext()) { request.setAttribute("message", "Item is already
* part of that collection!"); } else { // Create mapping
* myCollection.addItem( itemToAdd );
* // set up a nice 'done' message request.setAttribute("message",
* "Item added successfully: <br> " + myTitle + " <br> From Collection:
* <br> " + ownerName);
* }
*
* request.setAttribute("collection", myCollection);
* // show this page when we're done jspPage = "itemmap-info.jsp";
* // show the page JSPManager.showJSP(request, response, jspPage); }
* else { // Display an error } } else if( action.equals("Add Entire
* Collection") ) { int targetID = UIUtil.getIntParameter(request,
* "collection2import");
*
* Collection targetCollection = Collection.find(context, targetID);
* // get all items from that collection and add them if not // already
* added
* // get all items to be added ItemIterator i =
* targetCollection.getItems(); Map toAdd = new HashMap(); String
* message = "";
*
* while( i.hasNext() ) { Item myItem = i.next();
*
* toAdd.put(new Integer(myItem.getID()), myItem); }
* // now see what we already have, removing dups from the 'toAdd' list
* i = myCollection.getItems();
*
* while( i.hasNext() ) { Item myItem = i.next(); Integer myKey = new
* Integer(myItem.getID());
* // remove works even if key isn't present toAdd.remove(myKey); }
* // what's left in toAdd should be added Iterator addKeys =
* toAdd.keySet().iterator();
*
* while( addKeys.hasNext() ) { Item myItem =
* (Item)toAdd.get(addKeys.next()); myCollection.addItem(myItem);
* message += " <br> Added item ID: " + myItem.getID(); }
*
* request.setAttribute("message", message);
* request.setAttribute("collection", myCollection);
* // show this page when we're done jspPage = "itemmap-info.jsp";
* // show the page JSPManager.showJSP(request, response, jspPage); }
*/
else if (action.equals("Remove"))
{
// get item IDs to remove
@@ -271,29 +205,36 @@ public class ItemMapServlet extends DSpaceServlet
String message = "remove";
LinkedList removedItems = new LinkedList();
for (int j = 0; j < itemIDs.length; j++)
{
int i = Integer.parseInt(itemIDs[j]);
removedItems.add(itemIDs[j]);
if (itemIDs == null)
{
message = "none-removed";
}
else
{
for (int j = 0; j < itemIDs.length; j++)
{
int i = Integer.parseInt(itemIDs[j]);
removedItems.add(itemIDs[j]);
Item myItem = Item.find(context, i);
Item myItem = Item.find(context, i);
// make sure item doesn't belong to this collection
if (!myItem.isOwningCollection(myCollection))
{
myCollection.removeItem(myItem);
try
// make sure item doesn't belong to this collection
if (!myItem.isOwningCollection(myCollection))
{
IndexBrowse ib = new IndexBrowse(context);
ib.itemChanged(myItem);
}
catch (BrowseException e)
{
log.error("caught exception: ", e);
throw new ServletException(e);
myCollection.removeItem(myItem);
try
{
IndexBrowse ib = new IndexBrowse(context);
ib.itemChanged(myItem);
}
catch (BrowseException e)
{
log.error("caught exception: ", e);
throw new ServletException(e);
}
}
}
}
}
request.setAttribute("message", message);
request.setAttribute("collection", myCollection);

View File

@@ -63,7 +63,7 @@
<%
Collection collection = (Collection)request.getAttribute("collection");
// supported values: "none-selected", "added", "remove"
// supported values: "none-selected", "none-removed", "added", "remove"
String message = (String)request.getAttribute("message");
LinkedList processedItems = (LinkedList)request.getAttribute("processedItems");
@@ -76,6 +76,8 @@
<p>
<% if (message.equals("none-selected")) { %>
<fmt:message key="jsp.tools.itemmap-info.msg.none-selected" />
<% } else if (message.equals("none-removed")) { %>
<fmt:message key="jsp.tools.itemmap-info.msg.none-removed" />
<% } else if (message.equals("added")) { %>
<%-- Iterate through processed items --%>
<% for (int i=0; i<processedItems.size(); i++) { %>