KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > admin > ItemMapServlet


1 /*
2  * ItemMapServlet.java
3  *
4  * Version: $Revision: 1.11 $
5  *
6  * Date: $Date: 2006/06/30 12:28:21 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.servlet.admin;
41
42 import java.util.HashMap JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.LinkedList JavaDoc;
45
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47 import javax.servlet.http.HttpServletResponse JavaDoc;
48
49 import org.dspace.app.webui.servlet.DSpaceServlet;
50 import org.dspace.app.webui.util.JSPManager;
51 import org.dspace.app.webui.util.UIUtil;
52 import org.dspace.authorize.AuthorizeException;
53 import org.dspace.authorize.AuthorizeManager;
54 import org.dspace.browse.Browse;
55 import org.dspace.content.Collection;
56 import org.dspace.content.Item;
57 import org.dspace.content.ItemIterator;
58 import org.dspace.core.Constants;
59 import org.dspace.core.Context;
60 import org.dspace.storage.rdbms.DatabaseManager;
61 import org.dspace.storage.rdbms.TableRow;
62 import org.dspace.storage.rdbms.TableRowIterator;
63
64 /**
65  * Servlet for editing and deleting (expunging) items
66  *
67  * @version $Revision: 1.11 $
68  */

69 public class ItemMapServlet extends DSpaceServlet
70 {
71     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
72             HttpServletResponse JavaDoc response) throws java.sql.SQLException JavaDoc,
73             javax.servlet.ServletException JavaDoc, java.io.IOException JavaDoc,
74             AuthorizeException
75     {
76         doDSPost(context, request, response);
77     }
78
79     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
80             HttpServletResponse JavaDoc response) throws java.sql.SQLException JavaDoc,
81             javax.servlet.ServletException JavaDoc, java.io.IOException JavaDoc,
82             AuthorizeException
83     {
84         String JavaDoc jspPage = null;
85
86         // get with a collection ID means put up browse window
87
int myID = UIUtil.getIntParameter(request, "cid");
88
89         // get collection
90
Collection myCollection = Collection.find(context, myID);
91
92         // authorize check
93
AuthorizeManager.authorizeAction(context, myCollection,
94                 Constants.COLLECTION_ADMIN);
95
96         String JavaDoc action = request.getParameter("action");
97
98         if (action == null)
99         {
100             action = "";
101         }
102     
103     // Defined non-empty value shows that 'Cancel' has been pressed
104
String JavaDoc cancel = request.getParameter("cancel");
105
106     if (cancel == null)
107     {
108         cancel = "";
109     }
110
111         if (action.equals("") || !cancel.equals(""))
112         {
113             // get with no action parameter set means to put up the main page
114
// which is statistics and some command buttons to add/remove items
115
//
116
// also holds for interruption by pressing 'Cancel'
117
int count_native = 0; // # of items owned by this collection
118
int count_import = 0; // # of virtual items
119
Map JavaDoc myItems = new HashMap JavaDoc(); // # for the browser
120
Map JavaDoc myCollections = new HashMap JavaDoc(); // collections for list
121
Map JavaDoc myCounts = new HashMap JavaDoc(); // counts for each collection
122

123             // get all items from that collection, add them to a hash
124
ItemIterator i = myCollection.getItems();
125
126             // iterate through the items in this collection, and count how many
127
// are native, and how many are imports, and which collections they
128
// came from
129
while (i.hasNext())
130             {
131                 Item myItem = i.next();
132
133                 // get key for hash
134
Integer JavaDoc myKey = new Integer JavaDoc(myItem.getID());
135
136                 if (myItem.isOwningCollection(myCollection))
137                 {
138                     count_native++;
139                 }
140                 else
141                 {
142                     count_import++;
143                 }
144
145                 // is the collection in the hash?
146
Collection owningCollection = myItem.getOwningCollection();
147                 Integer JavaDoc cKey = new Integer JavaDoc(owningCollection.getID());
148
149                 if (myCollections.containsKey(cKey))
150                 {
151                     Integer JavaDoc x = (Integer JavaDoc) myCounts.get(cKey);
152                     int myCount = x.intValue() + 1;
153
154                     // increment count for that collection
155
myCounts.put(cKey, new Integer JavaDoc(myCount));
156                 }
157                 else
158                 {
159                     // store and initialize count
160
myCollections.put(cKey, owningCollection);
161                     myCounts.put(cKey, new Integer JavaDoc(1));
162                 }
163
164                 // store the item
165
myItems.put(myKey, myItem);
166             }
167
168             // remove this collection's entry because we already have a native
169
// count
170
myCollections.remove(new Integer JavaDoc(myCollection.getID()));
171
172             // sort items - later
173
// show page
174
request.setAttribute("collection", myCollection);
175             request.setAttribute("count_native", new Integer JavaDoc(count_native));
176             request.setAttribute("count_import", new Integer JavaDoc(count_import));
177             request.setAttribute("items", myItems);
178             request.setAttribute("collections", myCollections);
179             request.setAttribute("collection_counts", myCounts);
180             request
181                     .setAttribute("all_collections", Collection
182                             .findAll(context));
183
184             // show this page when we're done
185
jspPage = "itemmap-main.jsp";
186
187             // show the page
188
JSPManager.showJSP(request, response, jspPage);
189         }
190         /*
191          * else if( action.equals("add") ) { int itemID =
192          * UIUtil.getIntParameter(request, "item_id"); String handle =
193          * (String)request.getParameter("handle"); boolean error = true; Item
194          * itemToAdd = null;
195          *
196          * if( itemID > 0 ) { itemToAdd = Item.find(context, itemID);
197          *
198          * if( itemToAdd != null ) error = false; } else if(handle != null &&
199          * !handle.equals("")) { DSpaceObject
200          * dso=HandleManager.resolveToObject(context, handle);
201          *
202          * if(dso != null && dso.getType() == Constants.ITEM) { itemToAdd =
203          * (Item)dso; error = false; } }
204          *
205          * //FIXME: error handling! if( !error ) { String myTitle =
206          * itemToAdd.getDC("title",null,Item.ANY)[0].value; String ownerName =
207          * itemToAdd.getOwningCollection().getMetadata("name");
208          * // hook up item, but first, does it belong already? TableRowIterator
209          * tri = DatabaseManager.query(context, "collection2item", "SELECT
210          * collection2item.* FROM collection2item WHERE " + "collection_id=" +
211          * myCollection.getID() + " AND item_id=" + itemToAdd.getID());
212          *
213          * if(tri.hasNext()) { request.setAttribute("message", "Item is already
214          * part of that collection!"); } else { // Create mapping
215          * myCollection.addItem( itemToAdd );
216          * // set up a nice 'done' message request.setAttribute("message",
217          * "Item added successfully: <br> " + myTitle + " <br> From Collection:
218          * <br> " + ownerName);
219          * }
220          *
221          * request.setAttribute("collection", myCollection);
222          * // show this page when we're done jspPage = "itemmap-info.jsp";
223          * // show the page JSPManager.showJSP(request, response, jspPage); }
224          * else { // Display an error } } else if( action.equals("Add Entire
225          * Collection") ) { int targetID = UIUtil.getIntParameter(request,
226          * "collection2import");
227          *
228          * Collection targetCollection = Collection.find(context, targetID);
229          * // get all items from that collection and add them if not // already
230          * added
231          * // get all items to be added ItemIterator i =
232          * targetCollection.getItems(); Map toAdd = new HashMap(); String
233          * message = "";
234          *
235          * while( i.hasNext() ) { Item myItem = i.next();
236          *
237          * toAdd.put(new Integer(myItem.getID()), myItem); }
238          * // now see what we already have, removing dups from the 'toAdd' list
239          * i = myCollection.getItems();
240          *
241          * while( i.hasNext() ) { Item myItem = i.next(); Integer myKey = new
242          * Integer(myItem.getID());
243          * // remove works even if key isn't present toAdd.remove(myKey); }
244          * // what's left in toAdd should be added Iterator addKeys =
245          * toAdd.keySet().iterator();
246          *
247          * while( addKeys.hasNext() ) { Item myItem =
248          * (Item)toAdd.get(addKeys.next()); myCollection.addItem(myItem);
249          * message += " <br> Added item ID: " + myItem.getID(); }
250          *
251          * request.setAttribute("message", message);
252          * request.setAttribute("collection", myCollection);
253          * // show this page when we're done jspPage = "itemmap-info.jsp";
254          * // show the page JSPManager.showJSP(request, response, jspPage); }
255          */

256         else if (action.equals("Remove"))
257         {
258             // get item IDs to remove
259
String JavaDoc[] itemIDs = request.getParameterValues("item_ids");
260             String JavaDoc message = "remove";
261         LinkedList JavaDoc removedItems = new LinkedList JavaDoc();
262
263             for (int j = 0; j < itemIDs.length; j++)
264             {
265                 int i = Integer.parseInt(itemIDs[j]);
266         removedItems.add(itemIDs[j]);
267
268                 Item myItem = Item.find(context, i);
269
270                 // make sure item doesn't belong to this collection
271
if (!myItem.isOwningCollection(myCollection))
272                 {
273                     myCollection.removeItem(myItem);
274                     Browse.itemChanged(context,myItem);
275                 }
276             }
277
278             request.setAttribute("message", message);
279             request.setAttribute("collection", myCollection);
280         request.setAttribute("processedItems", removedItems);
281
282             // show this page when we're done
283
jspPage = "itemmap-info.jsp";
284
285             // show the page
286
JSPManager.showJSP(request, response, jspPage);
287         }
288         else if (action.equals("Add"))
289         {
290             // get item IDs to add
291
String JavaDoc[] itemIDs = request.getParameterValues("item_ids");
292             String JavaDoc message = "added";
293         LinkedList JavaDoc addedItems = new LinkedList JavaDoc();
294         
295
296             if (itemIDs == null)
297             {
298                 message = "none-selected";
299             }
300             else
301             {
302                 for (int j = 0; j < itemIDs.length; j++)
303                 {
304                     int i = Integer.parseInt(itemIDs[j]);
305
306                     Item myItem = Item.find(context, i);
307
308                     if (AuthorizeManager.authorizeActionBoolean(context,
309                             myItem, Constants.READ))
310                     {
311                         // make sure item doesn't belong to this collection
312
if (!myItem.isOwningCollection(myCollection))
313                         {
314                             myCollection.addItem(myItem);
315                             Browse.itemChanged(context,myItem);
316                             addedItems.add(itemIDs[j]);
317                         }
318                     }
319                 }
320             }
321
322             request.setAttribute("message", message);
323             request.setAttribute("collection", myCollection);
324         request.setAttribute("processedItems", addedItems);
325
326             // show this page when we're done
327
jspPage = "itemmap-info.jsp";
328
329             // show the page
330
JSPManager.showJSP(request, response, jspPage);
331         }
332         else if (action.equals("Search Authors"))
333         {
334             // find all items with a matching author string and not currently in
335
// this collection
336
// sorting by date would be ideal...
337
String JavaDoc myQuery = (String JavaDoc) request.getParameter("namepart");
338             
339             TableRowIterator tri = DatabaseManager.query(context,
340                     "SELECT * from ItemsByAuthor WHERE sort_author like ? AND " +
341                     "item_id NOT IN (SELECT item_id FROM collection2item " +
342                     "WHERE collection_id= ? )",
343                     '%'+myQuery.toLowerCase()+'%',myCollection.getID());
344
345             Map JavaDoc items = new HashMap JavaDoc();
346
347             while (tri.hasNext())
348             {
349                 TableRow tr = tri.next();
350
351                 // now instantiate and pass items to 'Add' page
352
int itemID = tr.getIntColumn("item_id");
353
354                 Item myItem = Item.find(context, itemID);
355
356                 // only put on list if you can read item
357
if (AuthorizeManager.authorizeActionBoolean(context, myItem,
358                         Constants.READ))
359                 {
360                     items.put(new Integer JavaDoc(itemID), myItem);
361                 }
362             }
363             tri.close();
364
365             request.setAttribute("collection", myCollection);
366             request.setAttribute("browsetext", myQuery);
367             request.setAttribute("items", items);
368             request.setAttribute("browsetype", new String JavaDoc("Add"));
369
370             jspPage = "itemmap-browse.jsp";
371             JSPManager.showJSP(request, response, jspPage);
372         }
373         else if (action.equals("browse"))
374         {
375             // target collection to browse
376
int t = UIUtil.getIntParameter(request, "t");
377
378             Collection targetCollection = Collection.find(context, t);
379
380             // now find all imported items from that collection
381
// seemingly inefficient, but database should have this query cached
382
ItemIterator i = myCollection.getItems();
383             Map JavaDoc items = new HashMap JavaDoc();
384
385             while (i.hasNext())
386             {
387                 Item myItem = i.next();
388
389                 if (myItem.isOwningCollection(targetCollection))
390                 {
391                     Integer JavaDoc myKey = new Integer JavaDoc(myItem.getID());
392                     items.put(myKey, myItem);
393                 }
394             }
395
396             request.setAttribute("collection", myCollection);
397             request.setAttribute("browsetext", targetCollection
398                     .getMetadata("name"));
399             request.setAttribute("items", items);
400             request.setAttribute("browsetype", new String JavaDoc("Remove"));
401
402             // show this page when we're done
403
jspPage = "itemmap-browse.jsp";
404
405             // show the page
406
JSPManager.showJSP(request, response, jspPage);
407         }
408
409         context.complete();
410     }
411 }
412
Popular Tags