KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CollectionWizardServlet.java
3  *
4  * Version: $Revision: 1.21 $
5  *
6  * Date: $Date: 2005/11/16 21:40:50 $
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 import java.util.List JavaDoc;
49
50 import javax.servlet.ServletException JavaDoc;
51 import javax.servlet.http.HttpServletRequest JavaDoc;
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53
54 import org.apache.log4j.Logger;
55 import org.dspace.app.webui.servlet.DSpaceServlet;
56 import org.dspace.app.webui.util.FileUploadRequest;
57 import org.dspace.app.webui.util.JSPManager;
58 import org.dspace.app.webui.util.UIUtil;
59 import org.dspace.authorize.AuthorizeException;
60 import org.dspace.authorize.AuthorizeManager;
61 import org.dspace.content.Bitstream;
62 import org.dspace.content.BitstreamFormat;
63 import org.dspace.content.Collection;
64 import org.dspace.content.Community;
65 import org.dspace.content.FormatIdentifier;
66 import org.dspace.content.Item;
67 import org.dspace.content.MetadataField;
68 import org.dspace.content.MetadataSchema;
69 import org.dspace.core.Constants;
70 import org.dspace.core.Context;
71 import org.dspace.core.LogManager;
72 import org.dspace.eperson.EPerson;
73 import org.dspace.eperson.Group;
74
75 /**
76  * Collection creation wizard UI
77  *
78  * @author Robert Tansley
79  * @version $Revision: 1.21 $
80  */

81 public class CollectionWizardServlet extends DSpaceServlet
82 {
83     /** Initial questions page */
84     public final static int INITIAL_QUESTIONS = 1;
85
86     /** Basic information page */
87     public final static int BASIC_INFO = 2;
88
89     /** Permissions pages */
90     public final static int PERMISSIONS = 3;
91
92     /** Default item page */
93     public final static int DEFAULT_ITEM = 4;
94
95     /** Summary page */
96     public final static int SUMMARY = 5;
97
98     /** Permissions page for who gets read permissions on new items */
99     public final static int PERM_READ = 10;
100
101     /** Permissions page for submitters */
102     public final static int PERM_SUBMIT = 11;
103
104     /** Permissions page for workflow step 1 */
105     public final static int PERM_WF1 = 12;
106
107     /** Permissions page for workflow step 2 */
108     public final static int PERM_WF2 = 13;
109
110     /** Permissions page for workflow step 3 */
111     public final static int PERM_WF3 = 14;
112
113     /** Permissions page for collection administrators */
114     public final static int PERM_ADMIN = 15;
115
116     /** Logger */
117     private static Logger log = Logger.getLogger(CollectionWizardServlet.class);
118
119     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
120             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
121             SQLException JavaDoc, AuthorizeException
122     {
123         /*
124          * For GET, all we should really get is a community_id parameter (DB ID
125          * of community to add collection to). doDSPost handles this
126          */

127         doDSPost(context, request, response);
128     }
129
130     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
131             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
132             SQLException JavaDoc, AuthorizeException
133     {
134         /*
135          * For POST, we expect from the form:
136          *
137          * community_id DB ID if it was a 'create a new collection' button press
138          *
139          * OR
140          *
141          * collection_id DB ID of collection we're dealing with stage Stage
142          * we're at (from constants above)
143          */

144
145         // First, see if we have a multipart request
146
// (the 'basic info' page which might include uploading a logo)
147
String JavaDoc contentType = request.getContentType();
148
149         if ((contentType != null)
150                 && (contentType.indexOf("multipart/form-data") != -1))
151         {
152             // This is a multipart request, so it's a file upload
153
processBasicInfo(context, request, response);
154
155             return;
156         }
157
158         int communityID = UIUtil.getIntParameter(request, "community_id");
159
160         if (communityID > -1)
161         {
162             // We have a community ID, "create new collection" button pressed
163
Community c = Community.find(context, communityID);
164
165             if (c == null)
166             {
167                 log.warn(LogManager.getHeader(context, "integrity_error",
168                         UIUtil.getRequestLogInfo(request)));
169                 JSPManager.showIntegrityError(request, response);
170
171                 return;
172             }
173
174             // Create the collection
175
Collection newCollection = c.createCollection();
176             request.setAttribute("collection", newCollection);
177             if (AuthorizeManager.isAdmin(context))
178             {
179                 // set a variable to show all buttons
180
request.setAttribute("admin_button", new Boolean JavaDoc(true));
181             }
182             JSPManager.showJSP(request, response,
183                     "/dspace-admin/wizard-questions.jsp");
184             context.complete();
185         }
186         else
187         {
188             // Collection already created, dealing with one of the wizard pages
189
int collectionID = UIUtil.getIntParameter(request, "collection_id");
190             int stage = UIUtil.getIntParameter(request, "stage");
191
192             // Get the collection
193
Collection collection = Collection.find(context, collectionID);
194
195             // Put it in request attributes, as most JSPs will need it
196
request.setAttribute("collection", collection);
197
198             if (collection == null)
199             {
200                 log.warn(LogManager.getHeader(context, "integrity_error",
201                         UIUtil.getRequestLogInfo(request)));
202                 JSPManager.showIntegrityError(request, response);
203
204                 return;
205             }
206
207             // All pages will need this attribute
208
request.setAttribute("collection.id", String.valueOf(collection
209                     .getID()));
210
211             switch (stage)
212             {
213             case INITIAL_QUESTIONS:
214                 processInitialQuestions(context, request, response, collection);
215
216                 break;
217
218             case PERMISSIONS:
219                 processPermissions(context, request, response, collection);
220
221                 break;
222
223             case DEFAULT_ITEM:
224                 processDefaultItem(context, request, response, collection);
225
226                 break;
227
228             default:
229                 log.warn(LogManager.getHeader(context, "integrity_error",
230                         UIUtil.getRequestLogInfo(request)));
231                 JSPManager.showIntegrityError(request, response);
232             }
233         }
234     }
235
236     /**
237      * Process input from initial questions page
238      *
239      * @param context
240      * DSpace context
241      * @param request
242      * HTTP request
243      * @param response
244      * HTTP response
245      * @param collection
246      * Collection we're editing
247      */

248     private void processInitialQuestions(Context context,
249             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
250             Collection collection) throws SQLException JavaDoc, ServletException JavaDoc,
251             IOException JavaDoc, AuthorizeException
252     {
253         Group anonymousGroup = Group.find(context, 0);
254
255         // "Public read" checkbox. Only need to do anything
256
// if it's not checked.
257
if (!UIUtil.getBoolParameter(request, "public_read"))
258         {
259             // Remove anonymous default policies for new items
260
AuthorizeManager.removePoliciesActionFilter(context, collection,
261                     Constants.DEFAULT_ITEM_READ);
262             AuthorizeManager.removePoliciesActionFilter(context, collection,
263                     Constants.DEFAULT_BITSTREAM_READ);
264         }
265
266         // Some people authorised to submit
267
if (UIUtil.getBoolParameter(request, "submitters"))
268         {
269             // Create submitters group
270
Group g = collection.createSubmitters();
271         }
272
273         // Check for the workflow steps
274
for (int i = 1; i <= 3; i++)
275         {
276             if (UIUtil.getBoolParameter(request, "workflow" + i))
277             {
278                 // should have workflow step i
279
Group g = collection.createWorkflowGroup(i);
280             }
281         }
282
283         // Check for collection administrators
284
if (UIUtil.getBoolParameter(request, "admins"))
285         {
286             // Create administrators group
287
Group g = collection.createAdministrators();
288         }
289
290         // Default item stuff?
291
if (UIUtil.getBoolParameter(request, "default.item"))
292         {
293             collection.createTemplateItem();
294         }
295
296         // Need to set a name so that the indexer won't throw an exception
297
collection.setMetadata("name", "");
298         collection.update();
299
300         // Now display "basic info" screen
301
JSPManager.showJSP(request, response,
302                 "/dspace-admin/wizard-basicinfo.jsp");
303         context.complete();
304     }
305
306     /**
307      * Process input from one of the permissions pages
308      *
309      * @param context
310      * DSpace context
311      * @param request
312      * HTTP request
313      * @param response
314      * HTTP response
315      * @param collection
316      * Collection we're editing
317      */

318     private void processPermissions(Context context,
319             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
320             Collection collection) throws SQLException JavaDoc, ServletException JavaDoc,
321             IOException JavaDoc, AuthorizeException
322     {
323         // Which permission are we dealing with?
324
int permission = UIUtil.getIntParameter(request, "permission");
325
326         // First, we deal with the special case of the MIT group...
327
if (UIUtil.getBoolParameter(request, "mitgroup"))
328         {
329             Group mitGroup = Group.findByName(context, "MIT Users");
330             int action;
331
332             if (permission == PERM_READ)
333             {
334                 // assign default item and bitstream read to mitGroup
335
AuthorizeManager.addPolicy(context, collection,
336                         Constants.DEFAULT_ITEM_READ, mitGroup);
337                 AuthorizeManager.addPolicy(context, collection,
338                         Constants.DEFAULT_BITSTREAM_READ, mitGroup);
339             }
340             else
341             {
342                 // Must be submit
343
AuthorizeManager.addPolicy(context, collection, Constants.ADD,
344                         mitGroup);
345             }
346         }
347
348         //We need to add the selected people to the group.
349
// First, get the relevant group
350
Group g = null;
351
352         switch (permission)
353         {
354         case PERM_READ:
355
356             // Actually need to create a group for this.
357
g = Group.create(context);
358
359             // Name it according to our conventions
360
g
361                     .setName("COLLECTION_" + collection.getID()
362                             + "_DEFAULT_ITEM_READ");
363
364             // Give it the needed permission
365
AuthorizeManager.addPolicy(context, collection,
366                     Constants.DEFAULT_ITEM_READ, g);
367             AuthorizeManager.addPolicy(context, collection,
368                     Constants.DEFAULT_BITSTREAM_READ, g);
369
370             break;
371
372         case PERM_SUBMIT:
373             g = collection.getSubmitters();
374
375             break;
376
377         case PERM_WF1:
378             g = collection.getWorkflowGroup(1);
379
380             break;
381
382         case PERM_WF2:
383             g = collection.getWorkflowGroup(2);
384
385             break;
386
387         case PERM_WF3:
388             g = collection.getWorkflowGroup(3);
389
390             break;
391
392         case PERM_ADMIN:
393             g = collection.getAdministrators();
394
395             break;
396         }
397
398         // Add people and groups from the form to the group
399
int[] eperson_ids = UIUtil.getIntParameters(request, "eperson_id");
400         int[] group_ids = UIUtil.getIntParameters(request, "group_ids");
401         
402         if (eperson_ids != null)
403         {
404             for (int i = 0; i < eperson_ids.length; i++)
405             {
406                 EPerson eperson = EPerson.find(context, eperson_ids[i]);
407
408                 if (eperson != null)
409                 {
410                     g.addMember(eperson);
411                 }
412             }
413         }
414         
415         if (group_ids != null)
416         {
417             for (int i = 0; i < group_ids.length; i++)
418             {
419                 Group group = Group.find(context, group_ids[i]);
420             
421                 if (group != null)
422                 {
423                     g.addMember(group);
424                 }
425             }
426         }
427         
428
429         // Update group
430
g.update();
431
432         showNextPage(context, request, response, collection, permission);
433
434         context.complete();
435     }
436
437     /**
438      * process input from basic info page
439      *
440      * @param context
441      * @param request
442      * @param response
443      * @param collection
444      * @throws SQLException
445      * @throws ServletException
446      * @throws IOException
447      * @throws AuthorizeException
448      */

449     private void processBasicInfo(Context context, HttpServletRequest JavaDoc request,
450             HttpServletResponse JavaDoc response) throws SQLException JavaDoc,
451             ServletException JavaDoc, IOException JavaDoc, AuthorizeException
452     {
453         // Wrap multipart request to get the submission info
454
FileUploadRequest wrapper = new FileUploadRequest(request);
455
456         Collection collection = Collection.find(context, UIUtil
457                 .getIntParameter(wrapper, "collection_id"));
458
459         if (collection == null)
460         {
461             log.warn(LogManager.getHeader(context, "integrity_error", UIUtil
462                     .getRequestLogInfo(wrapper)));
463             JSPManager.showIntegrityError(request, response);
464
465             return;
466         }
467
468         // Get metadata
469
collection.setMetadata("name", wrapper.getParameter("name"));
470         collection.setMetadata("short_description", wrapper
471                 .getParameter("short_description"));
472         collection.setMetadata("introductory_text", wrapper
473                 .getParameter("introductory_text"));
474         collection.setMetadata("copyright_text", wrapper
475                 .getParameter("copyright_text"));
476         collection.setMetadata("side_bar_text", wrapper
477                 .getParameter("side_bar_text"));
478         collection.setMetadata("provenance_description", wrapper
479                 .getParameter("provenance_description"));
480
481         // Need to be more careful about license -- make sure it's null if
482
// nothing was entered
483
String JavaDoc license = wrapper.getParameter("license");
484
485         if ((license != null) || "".equals(license))
486         {
487             collection.setLicense(license);
488         }
489
490         File JavaDoc temp = wrapper.getFile("file");
491
492         if (temp != null)
493         {
494             // Read the temp file as logo
495
InputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(temp));
496             Bitstream logoBS = collection.setLogo(is);
497
498             // Strip all but the last filename. It would be nice
499
// to know which OS the file came from.
500
String JavaDoc noPath = wrapper.getFilesystemName("file");
501
502             while (noPath.indexOf('/') > -1)
503             {
504                 noPath = noPath.substring(noPath.indexOf('/') + 1);
505             }
506
507             while (noPath.indexOf('\\') > -1)
508             {
509                 noPath = noPath.substring(noPath.indexOf('\\') + 1);
510             }
511
512             logoBS.setName(noPath);
513             logoBS.setSource(wrapper.getFilesystemName("file"));
514
515             // Identify the format
516
BitstreamFormat bf = FormatIdentifier.guessFormat(context, logoBS);
517             logoBS.setFormat(bf);
518             logoBS.update();
519
520             // Remove temp file
521
temp.delete();
522         }
523
524         collection.update();
525
526         // Now work out what next page is
527
showNextPage(context, request, response, collection, BASIC_INFO);
528
529         context.complete();
530     }
531
532     /**
533      * Process input from default item page
534      *
535      * @param context
536      * DSpace context
537      * @param request
538      * HTTP request
539      * @param response
540      * HTTP response
541      * @param collection
542      * Collection we're editing
543      */

544     private void processDefaultItem(Context context,
545             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
546             Collection collection) throws SQLException JavaDoc, ServletException JavaDoc,
547             IOException JavaDoc, AuthorizeException
548     {
549         Item item = collection.getTemplateItem();
550
551         for (int i = 0; i < 10; i++)
552         {
553             int dcTypeID = UIUtil.getIntParameter(request, "dctype_" + i);
554             String JavaDoc value = request.getParameter("value_" + i);
555             String JavaDoc lang = request.getParameter("lang_" + i);
556
557             if ((dcTypeID != -1) && (value != null) && !value.equals(""))
558             {
559                 MetadataField field = MetadataField.find(context,dcTypeID);
560                 MetadataSchema schema = MetadataSchema.find(context,field.getSchemaID());
561                 item.addMetadata(schema.getName(),field.getElement(), field.getQualifier(), lang, value);
562             }
563         }
564
565         item.update();
566
567         // Now work out what next page is
568
showNextPage(context, request, response, collection, DEFAULT_ITEM);
569
570         context.complete();
571     }
572
573     /**
574      * Work out which page to show next, and show it
575      *
576      * @param context
577      * @param request
578      * @param response
579      * @param collection
580      * @param stage
581      * the stage the user just finished, or if PERMISSIONS, the
582      * particular permissions page
583      * @throws SQLException
584      * @throws ServletException
585      * @throws IOException
586      * @throws AuthorizeException
587      */

588     private void showNextPage(Context context, HttpServletRequest JavaDoc request,
589             HttpServletResponse JavaDoc response, Collection collection, int stage)
590             throws SQLException JavaDoc, ServletException JavaDoc, IOException JavaDoc,
591             AuthorizeException
592     {
593         // Put collection in request attributes, as most JSPs will need it
594
request.setAttribute("collection", collection);
595
596         // FIXME: Not a nice hack -- do we show the MIT users checkbox?
597
if (Group.findByName(context, "MIT Users") != null)
598         {
599             request.setAttribute("mitgroup", new Boolean JavaDoc(true));
600         }
601
602         log.debug(LogManager.getHeader(context, "nextpage", "stage=" + stage));
603
604         switch (stage)
605         {
606         case BASIC_INFO:
607
608             // Next page is 'permission to read' page iff ITEM_DEFAULT_READ
609
// for anonymous group is NOT there
610
List JavaDoc anonReadPols = AuthorizeManager.getPoliciesActionFilter(
611                     context, collection, Constants.DEFAULT_ITEM_READ);
612
613             // At this stage, if there's any ITEM_DEFAULT_READ, it can only
614
// be an anonymous one.
615
if (anonReadPols.size() == 0)
616             {
617                 request.setAttribute("permission", new Integer JavaDoc(PERM_READ));
618                 JSPManager.showJSP(request, response,
619                         "/dspace-admin/wizard-permissions.jsp");
620
621                 break;
622             }
623
624         case PERM_READ:
625
626             // Next page is 'permission to submit' iff there's a submit group
627
// defined
628
if (collection.getSubmitters() != null)
629             {
630                 request.setAttribute("permission", new Integer JavaDoc(PERM_SUBMIT));
631                 JSPManager.showJSP(request, response,
632                         "/dspace-admin/wizard-permissions.jsp");
633
634                 break;
635             }
636
637         case PERM_SUBMIT:
638
639             // Next page is 'workflow step 1' iff there's a wf step 1 group
640
// defined
641
if (collection.getWorkflowGroup(1) != null)
642             {
643                 request.setAttribute("permission", new Integer JavaDoc(PERM_WF1));
644                 JSPManager.showJSP(request, response,
645                         "/dspace-admin/wizard-permissions.jsp");
646
647                 break;
648             }
649
650         case PERM_WF1:
651
652             // Next page is 'workflow step 2' iff there's a wf step 2 group
653
// defined
654
if (collection.getWorkflowGroup(2) != null)
655             {
656                 request.setAttribute("permission", new Integer JavaDoc(PERM_WF2));
657                 JSPManager.showJSP(request, response,
658                         "/dspace-admin/wizard-permissions.jsp");
659
660                 break;
661             }
662
663         case PERM_WF2:
664
665             // Next page is 'workflow step 3' iff there's a wf step 2 group
666
// defined
667
if (collection.getWorkflowGroup(3) != null)
668             {
669                 request.setAttribute("permission", new Integer JavaDoc(PERM_WF3));
670                 JSPManager.showJSP(request, response,
671                         "/dspace-admin/wizard-permissions.jsp");
672
673                 break;
674             }
675
676         case PERM_WF3:
677
678             // Next page is 'collection administrator' iff there's a collection
679
// administrator group
680
if (collection.getAdministrators() != null)
681             {
682                 request.setAttribute("permission", new Integer JavaDoc(PERM_ADMIN));
683                 JSPManager.showJSP(request, response,
684                         "/dspace-admin/wizard-permissions.jsp");
685
686                 break;
687             }
688
689         case PERM_ADMIN:
690
691             // Next page is 'default item' iff there's a default item
692
if (collection.getTemplateItem() != null)
693             {
694                 MetadataField[] types = MetadataField.findAll(context);
695                 request.setAttribute("dctypes", types);
696                 JSPManager.showJSP(request, response,
697                         "/dspace-admin/wizard-default-item.jsp");
698
699                 break;
700             }
701
702         case DEFAULT_ITEM:
703
704             // Next page is 'summary page (the last page)
705
// sort of a hack to pass the community ID to the edit collection
706
// page,
707
// which needs it in other contexts
708
if (collection != null)
709             {
710                 Community[] communities = collection.getCommunities();
711                 request.setAttribute("community", communities[0]);
712
713                 if (AuthorizeManager.isAdmin(context))
714                 {
715                     // set a variable to show all buttons
716
request.setAttribute("admin_button", new Boolean JavaDoc(true));
717                 }
718             }
719
720             JSPManager.showJSP(request, response, "/tools/edit-collection.jsp");
721
722             break;
723         }
724     }
725 }
726
Popular Tags