KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * EditCommunitiesServlet.java
3  *
4  * Version: $Revision: 1.22 $
5  *
6  * Date: $Date: 2005/04/20 14:22:30 $
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.io.BufferedInputStream JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.sql.SQLException JavaDoc;
48
49 import javax.servlet.ServletException JavaDoc;
50 import javax.servlet.http.HttpServletRequest JavaDoc;
51 import javax.servlet.http.HttpServletResponse JavaDoc;
52
53 import org.apache.log4j.Logger;
54 import org.dspace.app.webui.servlet.DSpaceServlet;
55 import org.dspace.app.webui.util.FileUploadRequest;
56 import org.dspace.app.webui.util.JSPManager;
57 import org.dspace.app.webui.util.UIUtil;
58 import org.dspace.authorize.AuthorizeException;
59 import org.dspace.authorize.AuthorizeManager;
60 import org.dspace.content.Bitstream;
61 import org.dspace.content.BitstreamFormat;
62 import org.dspace.content.Collection;
63 import org.dspace.content.Community;
64 import org.dspace.content.FormatIdentifier;
65 import org.dspace.content.Item;
66 import org.dspace.core.Constants;
67 import org.dspace.core.Context;
68 import org.dspace.core.LogManager;
69 import org.dspace.eperson.Group;
70
71 /**
72  * Servlet for editing communities and collections, including deletion,
73  * creation, and metadata editing
74  *
75  * @author Robert Tansley
76  * @version $Revision: 1.22 $
77  */

78 public class EditCommunitiesServlet extends DSpaceServlet
79 {
80     /** User wants to edit a community */
81     public static final int START_EDIT_COMMUNITY = 1;
82
83     /** User wants to delete a community */
84     public static final int START_DELETE_COMMUNITY = 2;
85
86     /** User wants to create a community */
87     public static final int START_CREATE_COMMUNITY = 3;
88
89     /** User wants to edit a collection */
90     public static final int START_EDIT_COLLECTION = 4;
91
92     /** User wants to delete a collection */
93     public static final int START_DELETE_COLLECTION = 5;
94
95     /** User wants to create a collection */
96     public static final int START_CREATE_COLLECTION = 6;
97
98     /** User commited community edit or creation */
99     public static final int CONFIRM_EDIT_COMMUNITY = 7;
100
101     /** User confirmed community deletion */
102     public static final int CONFIRM_DELETE_COMMUNITY = 8;
103
104     /** User commited collection edit or creation */
105     public static final int CONFIRM_EDIT_COLLECTION = 9;
106
107     /** User wants to delete a collection */
108     public static final int CONFIRM_DELETE_COLLECTION = 10;
109
110     /** Logger */
111     private static Logger log = Logger.getLogger(EditCommunitiesServlet.class);
112
113     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
114             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
115             SQLException JavaDoc, AuthorizeException
116     {
117         // GET just displays the list of communities and collections
118
showControls(context, request, response);
119     }
120
121     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
122             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
123             SQLException JavaDoc, AuthorizeException
124     {
125         // First, see if we have a multipart request (uploading a logo)
126
String JavaDoc contentType = request.getContentType();
127
128         if ((contentType != null)
129                 && (contentType.indexOf("multipart/form-data") != -1))
130         {
131             // This is a multipart request, so it's a file upload
132
processUploadLogo(context, request, response);
133
134             return;
135         }
136
137         /*
138          * Respond to submitted forms. Each form includes an "action" parameter
139          * indicating what needs to be done (from the constants above.)
140          */

141         int action = UIUtil.getIntParameter(request, "action");
142
143         /*
144          * Most of the forms supply one or more of these values. Since we just
145          * get null if we try and find something with ID -1, we'll just try and
146          * find both here to save hassle later on
147          */

148         Community community = Community.find(context, UIUtil.getIntParameter(
149                 request, "community_id"));
150         Community parentCommunity = Community.find(context, UIUtil
151                 .getIntParameter(request, "parent_community_id"));
152         Collection collection = Collection.find(context, UIUtil
153                 .getIntParameter(request, "collection_id"));
154
155         // Just about every JSP will need the values we received
156
request.setAttribute("community", community);
157         request.setAttribute("parent", parentCommunity);
158         request.setAttribute("collection", collection);
159
160         /*
161          * First we check for a "cancel" button - if it's been pressed, we
162          * simply return to the main control page
163          */

164         if (request.getParameter("submit_cancel") != null)
165         {
166             showControls(context, request, response);
167
168             return;
169         }
170
171         if (AuthorizeManager.isAdmin(context))
172         {
173             // set a variable to show all buttons
174
request.setAttribute("admin_button", new Boolean JavaDoc(true));
175         }
176
177         // Now proceed according to "action" parameter
178
switch (action)
179         {
180         case START_EDIT_COMMUNITY:
181
182             // Display the relevant "edit community" page
183
JSPManager.showJSP(request, response, "/tools/edit-community.jsp");
184
185             break;
186
187         case START_DELETE_COMMUNITY:
188
189             // Show "confirm delete" page
190
JSPManager.showJSP(request, response,
191                     "/tools/confirm-delete-community.jsp");
192
193             break;
194
195         case START_CREATE_COMMUNITY:
196
197             // Display edit community page with empty fields + create button
198
JSPManager.showJSP(request, response, "/tools/edit-community.jsp");
199
200             break;
201
202         case START_EDIT_COLLECTION:
203
204             // Display the relevant "edit collection" page
205
JSPManager.showJSP(request, response, "/tools/edit-collection.jsp");
206
207             break;
208
209         case START_DELETE_COLLECTION:
210
211             // Show "confirm delete" page
212
JSPManager.showJSP(request, response,
213                     "/tools/confirm-delete-collection.jsp");
214
215             break;
216
217         case START_CREATE_COLLECTION:
218
219             // Forward to collection creation wizard
220
response.sendRedirect(response.encodeRedirectURL(request
221                     .getContextPath()
222                     + "/tools/collection-wizard?community_id="
223                     + community.getID()));
224
225             break;
226
227         case CONFIRM_EDIT_COMMUNITY:
228
229             // Edit or creation of a community confirmed
230
processConfirmEditCommunity(context, request, response, community);
231
232             break;
233
234         case CONFIRM_DELETE_COMMUNITY:
235
236             // remember the parent community, if any
237
Community parent = community.getParentCommunity();
238
239             // Delete the community
240
community.delete();
241
242             // if community was top-level, redirect to community-list page
243
if (parent == null)
244             {
245                 response.sendRedirect(response.encodeRedirectURL(request
246                         .getContextPath()
247                         + "/community-list"));
248             }
249             else
250             // redirect to parent community page
251
{
252                 response.sendRedirect(response.encodeRedirectURL(request
253                         .getContextPath()
254                         + "/handle/" + parent.getHandle()));
255             }
256
257             // Show main control page
258
//showControls(context, request, response);
259
// Commit changes to DB
260
context.complete();
261
262             break;
263
264         case CONFIRM_EDIT_COLLECTION:
265
266             // Edit or creation of a collection confirmed
267
processConfirmEditCollection(context, request, response, community,
268                     collection);
269
270             break;
271
272         case CONFIRM_DELETE_COLLECTION:
273
274             // Delete the collection
275
community.removeCollection(collection);
276
277             // Show main control page
278
showControls(context, request, response);
279
280             // Commit changes to DB
281
context.complete();
282
283             break;
284
285         default:
286
287             // Erm... weird action value received.
288
log.warn(LogManager.getHeader(context, "integrity_error", UIUtil
289                     .getRequestLogInfo(request)));
290             JSPManager.showIntegrityError(request, response);
291         }
292     }
293
294     /**
295      * Show community home page with admin controls
296      *
297      * @param context
298      * Current DSpace context
299      * @param request
300      * Current HTTP request
301      * @param response
302      * Current HTTP response
303      */

304     private void showControls(Context context, HttpServletRequest JavaDoc request,
305             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
306             SQLException JavaDoc, AuthorizeException
307     {
308         // new approach - eliminate the 'list-communities' page in favor of the
309
// community home page, enhanced with admin controls. If no community,
310
// or no parent community, just fall back to the community-list page
311
Community community = (Community) request.getAttribute("community");
312
313         if (community != null)
314         {
315             response.sendRedirect(response.encodeRedirectURL(request
316                     .getContextPath()
317                     + "/handle/" + community.getHandle()));
318         }
319         else
320         {
321             // see if a parent community was specified
322
Community parent = (Community) request.getAttribute("parent");
323
324             if (parent != null)
325             {
326                 response.sendRedirect(response.encodeRedirectURL(request
327                         .getContextPath()
328                         + "/handle/" + parent.getHandle()));
329             }
330             else
331             {
332                 // fall back on community-list page
333
response.sendRedirect(response.encodeRedirectURL(request
334                         .getContextPath()
335                         + "/community-list"));
336             }
337         }
338     }
339
340     /**
341      * Create/update community metadata from a posted form
342      *
343      * @param context
344      * DSpace context
345      * @param request
346      * the HTTP request containing posted info
347      * @param response
348      * the HTTP response
349      * @param community
350      * the community to update (or null for creation)
351      */

352     private void processConfirmEditCommunity(Context context,
353             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
354             Community community) throws ServletException JavaDoc, IOException JavaDoc,
355             SQLException JavaDoc, AuthorizeException
356     {
357         if (request.getParameter("create").equals("true"))
358         {
359             // if there is a parent community id specified, create community
360
// as its child; otherwise, create it as a top-level community
361
int parentCommunityID = UIUtil.getIntParameter(request,
362                     "parent_community_id");
363
364             if (parentCommunityID != -1)
365             {
366                 Community parent = Community.find(context, parentCommunityID);
367
368                 if (parent != null)
369                 {
370                     community = parent.createSubcommunity();
371                 }
372             }
373             else
374             {
375                 community = Community.create(null, context);
376             }
377
378             // Set attribute
379
request.setAttribute("community", community);
380         }
381
382         community.setMetadata("name", request.getParameter("name"));
383         community.setMetadata("short_description", request
384                 .getParameter("short_description"));
385
386         String JavaDoc intro = request.getParameter("introductory_text");
387
388         if (intro.equals(""))
389         {
390             intro = null;
391         }
392
393         String JavaDoc copy = request.getParameter("copyright_text");
394
395         if (copy.equals(""))
396         {
397             copy = null;
398         }
399
400         String JavaDoc side = request.getParameter("side_bar_text");
401
402         if (side.equals(""))
403         {
404             side = null;
405         }
406
407         community.setMetadata("introductory_text", intro);
408         community.setMetadata("copyright_text", copy);
409         community.setMetadata("side_bar_text", side);
410         community.update();
411
412         // Which button was pressed?
413
String JavaDoc button = UIUtil.getSubmitButton(request, "submit");
414
415         if (button.equals("submit_set_logo"))
416         {
417             // Change the logo - delete any that might be there first
418
community.setLogo(null);
419             community.update();
420
421             // Display "upload logo" page. Necessary attributes already set by
422
// doDSPost()
423
JSPManager.showJSP(request, response,
424                     "/dspace-admin/upload-logo.jsp");
425         }
426         else if (button.equals("submit_delete_logo"))
427         {
428             // Simply delete logo
429
community.setLogo(null);
430             community.update();
431
432             // Show edit page again - attributes set in doDSPost()
433
JSPManager.showJSP(request, response, "/tools/edit-community.jsp");
434         }
435         else if (button.equals("submit_authorization_edit"))
436         {
437             // Forward to policy edit page
438
response.sendRedirect(response.encodeRedirectURL(request
439                     .getContextPath()
440                     + "/dspace-admin/authorize?community_id="
441                     + community.getID() + "&submit_community_select=1"));
442         }
443         else
444         {
445             // Button at bottom clicked - show main control page
446
showControls(context, request, response);
447         }
448
449         // Commit changes to DB
450
context.complete();
451     }
452
453     /**
454      * Create/update collection metadata from a posted form
455      *
456      * @param context
457      * DSpace context
458      * @param request
459      * the HTTP request containing posted info
460      * @param response
461      * the HTTP response
462      * @param community
463      * the community the collection is in
464      * @param collection
465      * the collection to update (or null for creation)
466      */

467     private void processConfirmEditCollection(Context context,
468             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
469             Community community, Collection collection)
470             throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc,
471             AuthorizeException
472     {
473         if (request.getParameter("create").equals("true"))
474         {
475             // We need to create a new community
476
collection = community.createCollection();
477             request.setAttribute("collection", collection);
478         }
479
480         // Update the basic metadata
481
collection.setMetadata("name", request.getParameter("name"));
482         collection.setMetadata("short_description", request
483                 .getParameter("short_description"));
484
485         String JavaDoc intro = request.getParameter("introductory_text");
486
487         if (intro.equals(""))
488         {
489             intro = null;
490         }
491
492         String JavaDoc copy = request.getParameter("copyright_text");
493
494         if (copy.equals(""))
495         {
496             copy = null;
497         }
498
499         String JavaDoc side = request.getParameter("side_bar_text");
500
501         if (side.equals(""))
502         {
503             side = null;
504         }
505
506         String JavaDoc license = request.getParameter("license");
507
508         if (license.equals(""))
509         {
510             license = null;
511         }
512
513         String JavaDoc provenance = request.getParameter("provenance_description");
514
515         if (provenance.equals(""))
516         {
517             provenance = null;
518         }
519
520         collection.setMetadata("introductory_text", intro);
521         collection.setMetadata("copyright_text", copy);
522         collection.setMetadata("side_bar_text", side);
523         collection.setMetadata("license", license);
524         collection.setMetadata("provenance_description", provenance);
525
526         // Which button was pressed?
527
String JavaDoc button = UIUtil.getSubmitButton(request, "submit");
528
529         if (button.equals("submit_set_logo"))
530         {
531             // Change the logo - delete any that might be there first
532
collection.setLogo(null);
533
534             // Display "upload logo" page. Necessary attributes already set by
535
// doDSPost()
536
JSPManager.showJSP(request, response,
537                     "/dspace-admin/upload-logo.jsp");
538         }
539         else if (button.equals("submit_delete_logo"))
540         {
541             // Simply delete logo
542
collection.setLogo(null);
543
544             // Show edit page again - attributes set in doDSPost()
545
JSPManager.showJSP(request, response, "/tools/edit-collection.jsp");
546         }
547         else if (button.startsWith("submit_wf_create_"))
548         {
549             int step = Integer.parseInt(button.substring(17));
550
551             // Create new group
552
Group newGroup = Group.create(context);
553             newGroup.setName("COLLECTION_" + collection.getID() + "_WFSTEP_"
554                     + step);
555             newGroup.update();
556             collection.setWorkflowGroup(step, newGroup);
557             collection.update();
558
559             // Forward to group edit page
560
response.sendRedirect(response.encodeRedirectURL(request
561                     .getContextPath()
562                     + "/tools/group-edit?group_id=" + newGroup.getID()));
563         }
564         else if (button.equals("submit_admins_create"))
565         {
566             // Create new group
567
Group newGroup = collection.createAdministrators();
568
569             // Forward to group edit page
570
response.sendRedirect(response.encodeRedirectURL(request
571                     .getContextPath()
572                     + "/tools/group-edit?group_id=" + newGroup.getID()));
573         }
574         else if (button.equals("submit_submitters_create"))
575         {
576             // Create new group
577
Group newGroup = collection.createSubmitters();
578
579             // Forward to group edit page
580
response.sendRedirect(response.encodeRedirectURL(request
581                     .getContextPath()
582                     + "/tools/group-edit?group_id=" + newGroup.getID()));
583         }
584         else if (button.equals("submit_authorization_edit"))
585         {
586             // Forward to policy edit page
587
response.sendRedirect(response.encodeRedirectURL(request
588                     .getContextPath()
589                     + "/dspace-admin/authorize?collection_id="
590                     + collection.getID() + "&submit_collection_select=1"));
591         }
592         else if (button.startsWith("submit_wf_edit_"))
593         {
594             int step = Integer.parseInt(button.substring(15));
595
596             // Edit workflow group
597
Group g = collection.getWorkflowGroup(step);
598             response.sendRedirect(response.encodeRedirectURL(request
599                     .getContextPath()
600                     + "/tools/group-edit?group_id=" + g.getID()));
601         }
602         else if (button.equals("submit_submitters_edit"))
603         {
604             // Edit submitters group
605
Group g = collection.getSubmitters();
606             response.sendRedirect(response.encodeRedirectURL(request
607                     .getContextPath()
608                     + "/tools/group-edit?group_id=" + g.getID()));
609         }
610         else if (button.equals("submit_admins_edit"))
611         {
612             // Edit 'collection administrators' group
613
Group g = collection.getAdministrators();
614             response.sendRedirect(response.encodeRedirectURL(request
615                     .getContextPath()
616                     + "/tools/group-edit?group_id=" + g.getID()));
617         }
618         else if (button.startsWith("submit_wf_delete_"))
619         {
620             // Delete workflow group
621
int step = Integer.parseInt(button.substring(17));
622
623             Group g = collection.getWorkflowGroup(step);
624             collection.setWorkflowGroup(step, null);
625
626             // Have to update to avoid ref. integrity error
627
collection.update();
628             g.delete();
629
630             // Show edit page again - attributes set in doDSPost()
631
JSPManager.showJSP(request, response, "/tools/edit-collection.jsp");
632         }
633         else if (button.equals("submit_create_template"))
634         {
635             // Create a template item
636
collection.createTemplateItem();
637
638             // Forward to edit page for new template item
639
Item i = collection.getTemplateItem();
640             i.setOwningCollection(collection);
641
642             // have to update to avoid ref. integrity error
643
i.update();
644             collection.update();
645             context.complete();
646             response.sendRedirect(response.encodeRedirectURL(request
647                     .getContextPath()
648                     + "/tools/edit-item?item_id=" + i.getID()));
649
650             return;
651         }
652         else if (button.equals("submit_edit_template"))
653         {
654             // Forward to edit page for template item
655
Item i = collection.getTemplateItem();
656             response.sendRedirect(response.encodeRedirectURL(request
657                     .getContextPath()
658                     + "/tools/edit-item?item_id=" + i.getID()));
659         }
660         else if (button.equals("submit_delete_template"))
661         {
662             collection.removeTemplateItem();
663
664             // Show edit page again - attributes set in doDSPost()
665
JSPManager.showJSP(request, response, "/tools/edit-collection.jsp");
666         }
667         else
668         {
669             // Plain old "create/update" button pressed - go back to main page
670
showControls(context, request, response);
671         }
672
673         // Commit changes to DB
674
collection.update();
675         context.complete();
676     }
677
678     /**
679      * Process the input from the upload logo page
680      *
681      * @param context
682      * current DSpace context
683      * @param request
684      * current servlet request object
685      * @param response
686      * current servlet response object
687      */

688     private void processUploadLogo(Context context, HttpServletRequest JavaDoc request,
689             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
690             SQLException JavaDoc, AuthorizeException
691     {
692         // Wrap multipart request to get the submission info
693
FileUploadRequest wrapper = new FileUploadRequest(request);
694
695         Community community = Community.find(context, UIUtil.getIntParameter(
696                 wrapper, "community_id"));
697         Collection collection = Collection.find(context, UIUtil
698                 .getIntParameter(wrapper, "collection_id"));
699
700         File JavaDoc temp = wrapper.getFile("file");
701
702         // Read the temp file as logo
703
InputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(temp));
704         Bitstream logoBS;
705
706         if (collection == null)
707         {
708             logoBS = community.setLogo(is);
709         }
710         else
711         {
712             logoBS = collection.setLogo(is);
713         }
714
715         // Strip all but the last filename. It would be nice
716
// to know which OS the file came from.
717
String JavaDoc noPath = wrapper.getFilesystemName("file");
718
719         while (noPath.indexOf('/') > -1)
720         {
721             noPath = noPath.substring(noPath.indexOf('/') + 1);
722         }
723
724         while (noPath.indexOf('\\') > -1)
725         {
726             noPath = noPath.substring(noPath.indexOf('\\') + 1);
727         }
728
729         logoBS.setName(noPath);
730         logoBS.setSource(wrapper.getFilesystemName("file"));
731
732         // Identify the format
733
BitstreamFormat bf = FormatIdentifier.guessFormat(context, logoBS);
734         logoBS.setFormat(bf);
735         AuthorizeManager.addPolicy(context, logoBS, Constants.WRITE, context
736                 .getCurrentUser());
737         logoBS.update();
738
739         if (AuthorizeManager.isAdmin(context))
740         {
741             // set a variable to show all buttons
742
request.setAttribute("admin_button", new Boolean JavaDoc(true));
743         }
744
745         if (collection == null)
746         {
747             community.update();
748
749             // Show community edit page
750
request.setAttribute("community", community);
751             JSPManager.showJSP(request, response, "/tools/edit-community.jsp");
752         }
753         else
754         {
755             collection.update();
756
757             // Show collection edit page
758
request.setAttribute("collection", collection);
759             request.setAttribute("community", community);
760             JSPManager.showJSP(request, response, "/tools/edit-collection.jsp");
761         }
762
763         // Remove temp file
764
temp.delete();
765
766         // Update DB
767
context.complete();
768     }
769 }
770
Popular Tags