KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > MyDSpaceServlet


1 /*
2  * MyDSpaceServlet.java
3  *
4  * Version: $Revision: 1.14 $
5  *
6  * Date: $Date: 2005/10/13 05:48:02 $
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;
41
42 import java.io.IOException JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.util.LinkedList JavaDoc;
45 import java.util.List JavaDoc;
46
47 import javax.servlet.ServletException JavaDoc;
48 import javax.servlet.http.HttpServletRequest JavaDoc;
49 import javax.servlet.http.HttpServletResponse JavaDoc;
50
51 import org.apache.log4j.Logger;
52 import org.dspace.app.webui.util.JSPManager;
53 import org.dspace.app.webui.util.UIUtil;
54 import org.dspace.authorize.AuthorizeException;
55 import org.dspace.content.Item;
56 import org.dspace.content.ItemIterator;
57 import org.dspace.content.SupervisedItem;
58 import org.dspace.content.WorkspaceItem;
59 import org.dspace.core.Context;
60 import org.dspace.core.LogManager;
61 import org.dspace.eperson.Group;
62 import org.dspace.handle.HandleManager;
63 import org.dspace.workflow.WorkflowItem;
64 import org.dspace.workflow.WorkflowManager;
65
66 /**
67  * Servlet for constructing the components of the "My DSpace" page
68  *
69  * @author Robert Tansley
70  * @version $Id: MyDSpaceServlet.java,v 1.14 2005/10/13 05:48:02 syeadon Exp $
71  */

72 public class MyDSpaceServlet extends DSpaceServlet
73 {
74     /** Logger */
75     private static Logger log = Logger.getLogger(MyDSpaceServlet.class);
76
77     /** The main screen */
78     public static final int MAIN_PAGE = 0;
79
80     /** The remove item page */
81     public static final int REMOVE_ITEM_PAGE = 1;
82
83     /** The preview task page */
84     public static final int PREVIEW_TASK_PAGE = 2;
85
86     /** The perform task page */
87     public static final int PERFORM_TASK_PAGE = 3;
88
89     /** The "reason for rejection" page */
90     public static final int REJECT_REASON_PAGE = 4;
91
92     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
93             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
94             SQLException JavaDoc, AuthorizeException
95     {
96         // GET displays the main page - everthing else is a POST
97
showMainPage(context, request, response);
98     }
99
100     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
101             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
102             SQLException JavaDoc, AuthorizeException
103     {
104         // First get the step
105
int step = UIUtil.getIntParameter(request, "step");
106
107         switch (step)
108         {
109         case MAIN_PAGE:
110             processMainPage(context, request, response);
111
112             break;
113
114         case REMOVE_ITEM_PAGE:
115             processRemoveItem(context, request, response);
116
117             break;
118
119         case PREVIEW_TASK_PAGE:
120             processPreviewTask(context, request, response);
121
122             break;
123
124         case PERFORM_TASK_PAGE:
125             processPerformTask(context, request, response);
126
127             break;
128
129         case REJECT_REASON_PAGE:
130             processRejectReason(context, request, response);
131
132             break;
133
134         default:
135             log.warn(LogManager.getHeader(context, "integrity_error", UIUtil
136                     .getRequestLogInfo(request)));
137             JSPManager.showIntegrityError(request, response);
138         }
139     }
140
141     // ****************************************************************
142
// ****************************************************************
143
// METHODS FOR PROCESSING POSTED FORMS
144
// ****************************************************************
145
// ****************************************************************
146
protected void processMainPage(Context context, HttpServletRequest JavaDoc request,
147             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
148             SQLException JavaDoc, AuthorizeException
149     {
150         String JavaDoc buttonPressed = UIUtil.getSubmitButton(request, "submit_own");
151
152         // Get workspace item, if any
153
WorkspaceItem workspaceItem;
154
155         try
156         {
157             int wsID = Integer.parseInt(request.getParameter("workspace_id"));
158             workspaceItem = WorkspaceItem.find(context, wsID);
159         }
160         catch (NumberFormatException JavaDoc nfe)
161         {
162             workspaceItem = null;
163         }
164
165         // Get workflow item specified, if any
166
WorkflowItem workflowItem;
167
168         try
169         {
170             int wfID = Integer.parseInt(request.getParameter("workflow_id"));
171             workflowItem = WorkflowItem.find(context, wfID);
172         }
173         catch (NumberFormatException JavaDoc nfe)
174         {
175             workflowItem = null;
176         }
177
178         // Respond to button press
179
boolean ok = false;
180
181         if (buttonPressed.equals("submit_new"))
182         {
183             // New submission: Redirect to submit
184
response.sendRedirect(response.encodeRedirectURL(request
185                     .getContextPath()
186                     + "/submit"));
187             ok = true;
188         }
189         else if (buttonPressed.equals("submit_own"))
190         {
191             // Review own submissions
192
showPreviousSubmissions(context, request, response);
193             ok = true;
194         }
195         else if (buttonPressed.equals("submit_resume"))
196         {
197             // User clicked on a "Resume" button for a workspace item.
198
String JavaDoc wsID = request.getParameter("workspace_id");
199             response.sendRedirect(response.encodeRedirectURL(request
200                     .getContextPath()
201                     + "/submit?resume=" + wsID));
202             ok = true;
203         }
204         else if (buttonPressed.equals("submit_delete"))
205         {
206             // User clicked on a "Remove" button for a workspace item
207
if (workspaceItem != null)
208             {
209                 log.info(LogManager.getHeader(context, "confirm_removal",
210                         "workspace_item_id=" + workspaceItem.getID()));
211
212                 request.setAttribute("workspace.item", workspaceItem);
213                 JSPManager.showJSP(request, response,
214                         "/mydspace/remove-item.jsp");
215             }
216             else
217             {
218                 log.warn(LogManager.getHeader(context, "integrity_error",
219                         UIUtil.getRequestLogInfo(request)));
220                 JSPManager.showIntegrityError(request, response);
221             }
222
223             ok = true;
224         }
225         else if (buttonPressed.equals("submit_claim"))
226         {
227             // User clicked "take task" button on workflow task
228
if (workflowItem != null)
229             {
230                 log.info(LogManager.getHeader(context, "view_workflow",
231                         "workflow_id=" + workflowItem.getID()));
232
233                 request.setAttribute("workflow.item", workflowItem);
234                 JSPManager.showJSP(request, response,
235                         "/mydspace/preview-task.jsp");
236                 ok = true;
237             }
238         }
239         else if (buttonPressed.equals("submit_perform"))
240         {
241             // User clicked "Do This Task" button on workflow task
242
if (workflowItem != null)
243             {
244                 log.info(LogManager.getHeader(context, "view_workflow",
245                         "workflow_id=" + workflowItem.getID()));
246
247                 request.setAttribute("workflow.item", workflowItem);
248                 JSPManager.showJSP(request, response,
249                         "/mydspace/perform-task.jsp");
250                 ok = true;
251             }
252         }
253         else if (buttonPressed.equals("submit_return"))
254         {
255             // User clicked "Return to pool" button on workflow task
256
if (workflowItem != null)
257             {
258                 log.info(LogManager.getHeader(context, "unclaim_workflow",
259                         "workflow_id=" + workflowItem.getID()));
260
261                 WorkflowManager.unclaim(context, workflowItem, context
262                         .getCurrentUser());
263
264                 showMainPage(context, request, response);
265                 context.complete();
266                 ok = true;
267             }
268         }
269
270         if (ok == false)
271         {
272             log.warn(LogManager.getHeader(context, "integrity_error", UIUtil
273                     .getRequestLogInfo(request)));
274             JSPManager.showIntegrityError(request, response);
275         }
276     }
277
278     /**
279      * Process input from remove item page
280      *
281      * @param context
282      * current context
283      * @param request
284      * current servlet request object
285      * @param response
286      * current servlet response object
287      */

288     private void processRemoveItem(Context context, HttpServletRequest JavaDoc request,
289             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
290             SQLException JavaDoc, AuthorizeException
291     {
292         String JavaDoc buttonPressed = UIUtil.getSubmitButton(request, "submit_cancel");
293
294         // Get workspace item
295
WorkspaceItem workspaceItem;
296
297         try
298         {
299             int wsID = Integer.parseInt(request.getParameter("workspace_id"));
300             workspaceItem = WorkspaceItem.find(context, wsID);
301         }
302         catch (NumberFormatException JavaDoc nfe)
303         {
304             workspaceItem = null;
305         }
306
307         if (workspaceItem == null)
308         {
309             log.warn(LogManager.getHeader(context, "integrity_error", UIUtil
310                     .getRequestLogInfo(request)));
311             JSPManager.showIntegrityError(request, response);
312
313             return;
314         }
315
316         // We have a workspace item
317
if (buttonPressed.equals("submit_delete"))
318         {
319             // User has clicked on "delete"
320
log.info(LogManager.getHeader(context, "remove_submission",
321                     "workspace_item_id=" + workspaceItem.getID() + ",item_id="
322                             + workspaceItem.getItem().getID()));
323             workspaceItem.deleteAll();
324             showMainPage(context, request, response);
325             context.complete();
326         }
327         else
328         {
329             // User has cancelled. Back to main page.
330
showMainPage(context, request, response);
331         }
332     }
333
334     /**
335      * Process input from preview task page
336      *
337      * @param context
338      * current context
339      * @param request
340      * current servlet request object
341      * @param response
342      * current servlet response object
343      */

344     private void processPreviewTask(Context context,
345             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
346             throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc,
347             AuthorizeException
348     {
349         String JavaDoc buttonPressed = UIUtil.getSubmitButton(request, "submit_cancel");
350
351         // Get workflow item
352
WorkflowItem workflowItem;
353
354         try
355         {
356             int wfID = Integer.parseInt(request.getParameter("workflow_id"));
357             workflowItem = WorkflowItem.find(context, wfID);
358         }
359         catch (NumberFormatException JavaDoc nfe)
360         {
361             workflowItem = null;
362         }
363
364         if (workflowItem == null)
365         {
366             log.warn(LogManager.getHeader(context, "integrity_error", UIUtil
367                     .getRequestLogInfo(request)));
368             JSPManager.showIntegrityError(request, response);
369
370             return;
371         }
372
373         if (buttonPressed.equals("submit_start"))
374         {
375             // User clicked "start" button to claim the task
376
WorkflowManager.claim(context, workflowItem, context
377                     .getCurrentUser());
378
379             // Display "perform task" page
380
request.setAttribute("workflow.item", workflowItem);
381             JSPManager.showJSP(request, response, "/mydspace/perform-task.jsp");
382             context.complete();
383         }
384         else
385         {
386             // Return them to main page
387
showMainPage(context, request, response);
388         }
389     }
390
391     /**
392      * Process input from perform task page
393      *
394      * @param context
395      * current context
396      * @param request
397      * current servlet request object
398      * @param response
399      * current servlet response object
400      */

401     private void processPerformTask(Context context,
402             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
403             throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc,
404             AuthorizeException
405     {
406         String JavaDoc buttonPressed = UIUtil.getSubmitButton(request, "submit_cancel");
407
408         // Get workflow item
409
WorkflowItem workflowItem;
410
411         try
412         {
413             int wfID = Integer.parseInt(request.getParameter("workflow_id"));
414             workflowItem = WorkflowItem.find(context, wfID);
415         }
416         catch (NumberFormatException JavaDoc nfe)
417         {
418             workflowItem = null;
419         }
420
421         if (workflowItem == null)
422         {
423             log.warn(LogManager.getHeader(context, "integrity_error", UIUtil
424                     .getRequestLogInfo(request)));
425             JSPManager.showIntegrityError(request, response);
426
427             return;
428         }
429
430         if (buttonPressed.equals("submit_approve"))
431         {
432             Item item = workflowItem.getItem();
433
434             // Advance the item along the workflow
435
WorkflowManager.advance(context, workflowItem, context
436                     .getCurrentUser());
437
438             // FIXME: This should be a return value from advance()
439
// See if that gave the item a Handle. If it did,
440
// the item made it into the archive, so we
441
// should display a suitable page.
442
String JavaDoc handle = HandleManager.findHandle(context, item);
443
444             if (handle != null)
445             {
446                 String JavaDoc displayHandle = HandleManager.getCanonicalForm(handle);
447
448                 request.setAttribute("handle", displayHandle);
449                 JSPManager.showJSP(request, response,
450                         "/mydspace/in-archive.jsp");
451             }
452             else
453             {
454                 JSPManager.showJSP(request, response,
455                         "/mydspace/task-complete.jsp");
456             }
457
458             context.complete();
459         }
460         else if (buttonPressed.equals("submit_reject"))
461         {
462             // Submission rejected. Ask the user for a reason
463
log.info(LogManager.getHeader(context, "get_reject_reason",
464                     "workflow_id=" + workflowItem.getID() + ",item_id="
465                             + workflowItem.getItem().getID()));
466
467             request.setAttribute("workflow.item", workflowItem);
468             JSPManager
469                     .showJSP(request, response, "/mydspace/reject-reason.jsp");
470         }
471         else if (buttonPressed.equals("submit_edit"))
472         {
473             // FIXME: Check auth
474
log.info(LogManager.getHeader(context, "edit_workflow_item",
475                     "workflow_id=" + workflowItem.getID() + ",item_id="
476                             + workflowItem.getItem().getID()));
477
478             // Forward control to the submission interface
479
// with the relevant item
480
response.sendRedirect(response.encodeRedirectURL(request
481                     .getContextPath()
482                     + "/submit?workflow=" + workflowItem.getID()));
483         }
484         else if (buttonPressed.equals("submit_pool"))
485         {
486             // Return task to pool
487
WorkflowManager.unclaim(context, workflowItem, context
488                     .getCurrentUser());
489             showMainPage(context, request, response);
490             context.complete();
491         }
492         else
493         {
494             // Cancelled. The user hasn't taken the task.
495
// Just return to the main My DSpace page.
496
showMainPage(context, request, response);
497         }
498     }
499
500     /**
501      * Process input from "reason for rejection" page
502      *
503      * @param context
504      * current context
505      * @param request
506      * current servlet request object
507      * @param response
508      * current servlet response object
509      */

510     private void processRejectReason(Context context,
511             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
512             throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc,
513             AuthorizeException
514     {
515         String JavaDoc buttonPressed = UIUtil.getSubmitButton(request, "submit_cancel");
516
517         // Get workflow item
518
WorkflowItem workflowItem;
519
520         try
521         {
522             int wfID = Integer.parseInt(request.getParameter("workflow_id"));
523             workflowItem = WorkflowItem.find(context, wfID);
524         }
525         catch (NumberFormatException JavaDoc nfe)
526         {
527             workflowItem = null;
528         }
529
530         if (workflowItem == null)
531         {
532             log.warn(LogManager.getHeader(context, "integrity_error", UIUtil
533                     .getRequestLogInfo(request)));
534             JSPManager.showIntegrityError(request, response);
535
536             return;
537         }
538
539         if (buttonPressed.equals("submit_send"))
540         {
541             String JavaDoc reason = request.getParameter("reason");
542
543             WorkspaceItem wsi = WorkflowManager.reject(context, workflowItem,
544                     context.getCurrentUser(), reason);
545
546             // Set the "stage_reached" column on the returned workspace item
547
// to the "verify" stage
548
wsi.setStageReached(SubmitServlet.REVIEW_SUBMISSION);
549             wsi.update();
550
551             JSPManager
552                     .showJSP(request, response, "/mydspace/task-complete.jsp");
553             context.complete();
554         }
555         else
556         {
557             request.setAttribute("workflow.item", workflowItem);
558             JSPManager.showJSP(request, response, "/mydspace/perform-task.jsp");
559         }
560     }
561
562     // ****************************************************************
563
// ****************************************************************
564
// METHODS FOR SHOWING FORMS
565
// ****************************************************************
566
// ****************************************************************
567

568     /**
569      * Show the main My DSpace page
570      *
571      * @param context
572      * current context
573      * @param request
574      * current servlet request object
575      * @param response
576      * current servlet response object
577      */

578     private void showMainPage(Context context, HttpServletRequest JavaDoc request,
579             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
580             SQLException JavaDoc, AuthorizeException
581     {
582         log.info(LogManager.getHeader(context, "view_mydspace", ""));
583
584         // FIXME: WorkflowManager should return arrays
585
List JavaDoc ownedList = WorkflowManager.getOwnedTasks(context, context
586                 .getCurrentUser());
587         WorkflowItem[] owned = new WorkflowItem[ownedList.size()];
588         owned = (WorkflowItem[]) ownedList.toArray(owned);
589
590         // Pooled workflow items
591
List JavaDoc pooledList = WorkflowManager.getPooledTasks(context, context
592                 .getCurrentUser());
593         WorkflowItem[] pooled = new WorkflowItem[pooledList.size()];
594         pooled = (WorkflowItem[]) pooledList.toArray(pooled);
595
596         // User's WorkflowItems
597
WorkflowItem[] workflowItems = WorkflowItem.findByEPerson(context,
598                 context.getCurrentUser());
599
600         // User's PersonalWorkspace
601
WorkspaceItem[] workspaceItems = WorkspaceItem.findByEPerson(context,
602                 context.getCurrentUser());
603
604         // User's authorization groups
605
Group[] memberships = context.getCurrentUser().getGroupMemberships();
606
607         SupervisedItem[] supervisedItems = SupervisedItem.findbyEPerson(
608                 context, context.getCurrentUser());
609
610         // Set attributes
611
request.setAttribute("mydspace.user", context.getCurrentUser());
612         request.setAttribute("workspace.items", workspaceItems);
613         request.setAttribute("workflow.items", workflowItems);
614         request.setAttribute("workflow.owned", owned);
615         request.setAttribute("workflow.pooled", pooled);
616         request.setAttribute("group.memberships", memberships);
617         request.setAttribute("supervised.items", supervisedItems);
618
619         // Forward to main mydspace page
620
JSPManager.showJSP(request, response, "/mydspace/main.jsp");
621     }
622
623     /**
624      * Show the user's previous submissions.
625      *
626      * @param context
627      * current context
628      * @param request
629      * current servlet request object
630      * @param response
631      * current servlet response object
632      */

633     private void showPreviousSubmissions(Context context,
634             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
635             throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc,
636             AuthorizeException
637     {
638         ItemIterator subs = Item.findBySubmitter(context, context
639                 .getCurrentUser());
640
641         // Turn the iterator into a list
642
List JavaDoc subList = new LinkedList JavaDoc();
643
644         while (subs.hasNext())
645         {
646             subList.add(subs.next());
647         }
648
649         Item[] items = new Item[subList.size()];
650
651         for (int i = 0; i < subList.size(); i++)
652         {
653             items[i] = (Item) subList.get(i);
654         }
655
656         log.info(LogManager.getHeader(context, "view_own_submissions", "count="
657                 + items.length));
658
659         request.setAttribute("user", context.getCurrentUser());
660         request.setAttribute("items", items);
661
662         JSPManager.showJSP(request, response, "/mydspace/own-submissions.jsp");
663     }
664 }
665
Popular Tags