KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > WorkspaceItem


1 /*
2  * WorkspaceItem.java
3  *
4  * Version: $Revision: 1.32 $
5  *
6  * Date: $Date: 2006/11/06 04:49:51 $
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.content;
41
42 import java.io.IOException JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46
47 import org.apache.log4j.Logger;
48 import org.dspace.authorize.AuthorizeException;
49 import org.dspace.authorize.AuthorizeManager;
50 import org.dspace.core.Constants;
51 import org.dspace.core.Context;
52 import org.dspace.core.LogManager;
53 import org.dspace.eperson.EPerson;
54 import org.dspace.eperson.Group;
55 import org.dspace.history.HistoryManager;
56 import org.dspace.storage.rdbms.DatabaseManager;
57 import org.dspace.storage.rdbms.TableRow;
58 import org.dspace.storage.rdbms.TableRowIterator;
59
60 /**
61  * Class representing an item in the process of being submitted by a user
62  *
63  * @author Robert Tansley
64  * @version $Revision: 1.32 $
65  */

66 public class WorkspaceItem implements InProgressSubmission
67 {
68     /** log4j logger */
69     private static Logger log = Logger.getLogger(WorkspaceItem.class);
70
71     /** The item this workspace object pertains to */
72     private Item item;
73
74     /** Our context */
75     private Context ourContext;
76
77     /** The table row corresponding to this workspace item */
78     private TableRow wiRow;
79
80     /** The collection the item is being submitted to */
81     private Collection collection;
82
83     /**
84      * Construct a workspace item corresponding to the given database row
85      *
86      * @param context
87      * the context this object exists in
88      * @param row
89      * the database row
90      */

91     WorkspaceItem(Context context, TableRow row) throws SQLException JavaDoc
92     {
93         ourContext = context;
94         wiRow = row;
95
96         item = Item.find(context, wiRow.getIntColumn("item_id"));
97         collection = Collection.find(context, wiRow
98                 .getIntColumn("collection_id"));
99
100         // Cache ourselves
101
context.cache(this, row.getIntColumn("workspace_item_id"));
102     }
103
104     /**
105      * Get a workspace item from the database. The item, collection and
106      * submitter are loaded into memory.
107      *
108      * @param context
109      * DSpace context object
110      * @param id
111      * ID of the workspace item
112      *
113      * @return the workspace item, or null if the ID is invalid.
114      */

115     public static WorkspaceItem find(Context context, int id)
116             throws SQLException JavaDoc
117     {
118         // First check the cache
119
WorkspaceItem fromCache = (WorkspaceItem) context.fromCache(
120                 WorkspaceItem.class, id);
121
122         if (fromCache != null)
123         {
124             return fromCache;
125         }
126
127         TableRow row = DatabaseManager.find(context, "workspaceitem", id);
128
129         if (row == null)
130         {
131             if (log.isDebugEnabled())
132             {
133                 log.debug(LogManager.getHeader(context, "find_workspace_item",
134                         "not_found,workspace_item_id=" + id));
135             }
136
137             return null;
138         }
139         else
140         {
141             if (log.isDebugEnabled())
142             {
143                 log.debug(LogManager.getHeader(context, "find_workspace_item",
144                         "workspace_item_id=" + id));
145             }
146
147             return new WorkspaceItem(context, row);
148         }
149     }
150
151     /**
152      * Create a new workspace item, with a new ID. An Item is also created. The
153      * submitter is the current user in the context.
154      *
155      * @param c
156      * DSpace context object
157      * @param coll
158      * Collection being submitted to
159      * @param template
160      * if <code>true</code>, the workspace item starts as a copy
161      * of the collection's template item
162      *
163      * @return the newly created workspace item
164      */

165     public static WorkspaceItem create(Context c, Collection coll,
166             boolean template) throws AuthorizeException, SQLException JavaDoc,
167             IOException JavaDoc
168     {
169         // Check the user has permission to ADD to the collection
170
AuthorizeManager.authorizeAction(c, coll, Constants.ADD);
171
172         // Create an item
173
Item i = Item.create(c);
174         i.setSubmitter(c.getCurrentUser());
175
176         // Now create the policies for the submitter and workflow
177
// users to modify item and contents
178
// contents = bitstreams, bundles
179
// FIXME: icky hardcoded workflow steps
180
Group step1group = coll.getWorkflowGroup(1);
181         Group step2group = coll.getWorkflowGroup(2);
182         Group step3group = coll.getWorkflowGroup(3);
183
184         EPerson e = c.getCurrentUser();
185
186         // read permission
187
AuthorizeManager.addPolicy(c, i, Constants.READ, e);
188
189         if (step1group != null)
190         {
191             AuthorizeManager.addPolicy(c, i, Constants.READ, step1group);
192         }
193
194         if (step2group != null)
195         {
196             AuthorizeManager.addPolicy(c, i, Constants.READ, step2group);
197         }
198
199         if (step3group != null)
200         {
201             AuthorizeManager.addPolicy(c, i, Constants.READ, step3group);
202         }
203
204         // write permission
205
AuthorizeManager.addPolicy(c, i, Constants.WRITE, e);
206
207         if (step1group != null)
208         {
209             AuthorizeManager.addPolicy(c, i, Constants.WRITE, step1group);
210         }
211
212         if (step2group != null)
213         {
214             AuthorizeManager.addPolicy(c, i, Constants.WRITE, step2group);
215         }
216
217         if (step3group != null)
218         {
219             AuthorizeManager.addPolicy(c, i, Constants.WRITE, step3group);
220         }
221
222         // add permission
223
AuthorizeManager.addPolicy(c, i, Constants.ADD, e);
224
225         if (step1group != null)
226         {
227             AuthorizeManager.addPolicy(c, i, Constants.ADD, step1group);
228         }
229
230         if (step2group != null)
231         {
232             AuthorizeManager.addPolicy(c, i, Constants.ADD, step2group);
233         }
234
235         if (step3group != null)
236         {
237             AuthorizeManager.addPolicy(c, i, Constants.ADD, step3group);
238         }
239
240         // remove contents permission
241
AuthorizeManager.addPolicy(c, i, Constants.REMOVE, e);
242
243         if (step1group != null)
244         {
245             AuthorizeManager.addPolicy(c, i, Constants.REMOVE, step1group);
246         }
247
248         if (step2group != null)
249         {
250             AuthorizeManager.addPolicy(c, i, Constants.REMOVE, step2group);
251         }
252
253         if (step3group != null)
254         {
255             AuthorizeManager.addPolicy(c, i, Constants.REMOVE, step3group);
256         }
257
258         // Copy template if appropriate
259
Item templateItem = coll.getTemplateItem();
260
261         if (template && (templateItem != null))
262         {
263             DCValue[] md = templateItem.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY);
264
265             for (int n = 0; n < md.length; n++)
266             {
267                 i.addMetadata(md[n].schema, md[n].element, md[n].qualifier, md[n].language,
268                         md[n].value);
269             }
270         }
271
272         i.update();
273
274         // Create the workspace item row
275
TableRow row = DatabaseManager.create(c, "workspaceitem");
276
277         row.setColumn("item_id", i.getID());
278         row.setColumn("collection_id", coll.getID());
279
280         log.info(LogManager.getHeader(c, "create_workspace_item",
281                 "workspace_item_id=" + row.getIntColumn("workspace_item_id")
282                         + "item_id=" + i.getID() + "collection_id="
283                         + coll.getID()));
284
285         DatabaseManager.update(c, row);
286
287         WorkspaceItem wi = new WorkspaceItem(c, row);
288
289         HistoryManager.saveHistory(c, wi, HistoryManager.CREATE, c
290                 .getCurrentUser(), c.getExtraLogInfo());
291
292         return wi;
293     }
294
295     /**
296      * Get all workspace items for a particular e-person. These are ordered by
297      * workspace item ID, since this should likely keep them in the order in
298      * which they were created.
299      *
300      * @param context
301      * the context object
302      * @param ep
303      * the eperson
304      *
305      * @return the corresponding workspace items
306      */

307     public static WorkspaceItem[] findByEPerson(Context context, EPerson ep)
308             throws SQLException JavaDoc
309     {
310         List JavaDoc wsItems = new ArrayList JavaDoc();
311
312         TableRowIterator tri = DatabaseManager.queryTable(context, "workspaceitem",
313                 "SELECT workspaceitem.* FROM workspaceitem, item WHERE " +
314                 "workspaceitem.item_id=item.item_id AND " +
315                 "item.submitter_id= ? " +
316                 "ORDER BY workspaceitem.workspace_item_id",
317                 ep.getID());
318
319         while (tri.hasNext())
320         {
321             TableRow row = tri.next();
322
323             // Check the cache
324
WorkspaceItem wi = (WorkspaceItem) context.fromCache(
325                     WorkspaceItem.class, row.getIntColumn("workspace_item_id"));
326
327             if (wi == null)
328             {
329                 wi = new WorkspaceItem(context, row);
330             }
331
332             wsItems.add(wi);
333         }
334         // close the TableRowIterator to free up resources
335
tri.close();
336
337         WorkspaceItem[] wsArray = new WorkspaceItem[wsItems.size()];
338         wsArray = (WorkspaceItem[]) wsItems.toArray(wsArray);
339
340         return wsArray;
341     }
342
343     /**
344      * Get all workspace items for a particular collection.
345      *
346      * @param context
347      * the context object
348      * @param c
349      * the collection
350      *
351      * @return the corresponding workspace items
352      */

353     public static WorkspaceItem[] findByCollection(Context context, Collection c)
354             throws SQLException JavaDoc
355     {
356         List JavaDoc wsItems = new ArrayList JavaDoc();
357
358         TableRowIterator tri = DatabaseManager.queryTable(context, "workspaceitem",
359                 "SELECT workspaceitem.* FROM workspaceitem WHERE " +
360                 "workspaceitem.collection_id= ? ",
361                 c.getID());
362
363         while (tri.hasNext())
364         {
365             TableRow row = tri.next();
366
367             // Check the cache
368
WorkspaceItem wi = (WorkspaceItem) context.fromCache(
369                     WorkspaceItem.class, row.getIntColumn("workspace_item_id"));
370
371             // not in cache? turn row into workspaceitem
372
if (wi == null)
373             {
374                 wi = new WorkspaceItem(context, row);
375             }
376
377             wsItems.add(wi);
378         }
379         // close the TableRowIterator to free up resources
380
tri.close();
381
382         WorkspaceItem[] wsArray = new WorkspaceItem[wsItems.size()];
383         wsArray = (WorkspaceItem[]) wsItems.toArray(wsArray);
384
385         return wsArray;
386     }
387
388     /**
389      * Get all workspace items in the whole system
390      *
391      * @param context the context object
392      *
393      * @return all workspace items
394      */

395     public static WorkspaceItem[] findAll(Context context)
396         throws SQLException JavaDoc
397     {
398         List JavaDoc wsItems = new ArrayList JavaDoc();
399         String JavaDoc query = "SELECT * FROM workspaceitem ORDER BY item_id";
400         TableRowIterator tri = DatabaseManager.queryTable(context,
401                                     "workspaceitem",
402                                     query);
403
404         while (tri.hasNext())
405         {
406             TableRow row = tri.next();
407             
408             // Check the cache
409
WorkspaceItem wi = (WorkspaceItem) context.fromCache(
410                     WorkspaceItem.class, row.getIntColumn("workspace_item_id"));
411
412             // not in cache? turn row into workspaceitem
413
if (wi == null)
414             {
415                 wi = new WorkspaceItem(context, row);
416             }
417             
418             wsItems.add(wi);
419         }
420         
421         tri.close();
422         
423         WorkspaceItem[] wsArray = new WorkspaceItem[wsItems.size()];
424         wsArray = (WorkspaceItem[]) wsItems.toArray(wsArray);
425
426         return wsArray;
427     }
428     
429     /**
430      * Get the internal ID of this workspace item
431      *
432      * @return the internal identifier
433      */

434     public int getID()
435     {
436         return wiRow.getIntColumn("workspace_item_id");
437     }
438
439     /**
440      * Get the value of the stage reached column
441      *
442      * @return the value of the stage reached column
443      */

444     public int getStageReached()
445     {
446         return wiRow.getIntColumn("stage_reached");
447     }
448
449     /**
450      * Set the value of the stage reached column
451      *
452      * @param v
453      * the value of the stage reached column
454      */

455     public void setStageReached(int v)
456     {
457         wiRow.setColumn("stage_reached", v);
458     }
459
460     /**
461      * Update the workspace item, including the unarchived item.
462      */

463     public void update() throws SQLException JavaDoc, AuthorizeException, IOException JavaDoc
464     {
465         // Authorisation is checked by the item.update() method below
466
HistoryManager.saveHistory(ourContext, this, HistoryManager.MODIFY,
467                 ourContext.getCurrentUser(), ourContext.getExtraLogInfo());
468
469         log.info(LogManager.getHeader(ourContext, "update_workspace_item",
470                 "workspace_item_id=" + getID()));
471
472         // Update the item
473
item.update();
474
475         // Update ourselves
476
DatabaseManager.update(ourContext, wiRow);
477     }
478
479     /**
480      * Delete the workspace item. The entry in workspaceitem, the unarchived
481      * item and its contents are all removed (multiple inclusion
482      * notwithstanding.)
483      */

484     public void deleteAll() throws SQLException JavaDoc, AuthorizeException,
485             IOException JavaDoc
486     {
487         /*
488          * Authorisation is a special case. The submitter won't have REMOVE
489          * permission on the collection, so our policy is this: Only the
490          * original submitter or an administrator can delete a workspace item.
491          */

492         if (!AuthorizeManager.isAdmin(ourContext)
493                 && ((ourContext.getCurrentUser() == null) || (ourContext
494                         .getCurrentUser().getID() != item.getSubmitter()
495                         .getID())))
496         {
497             // Not an admit, not the submitter
498
throw new AuthorizeException("Must be an administrator or the "
499                     + "original submitter to delete a workspace item");
500         }
501
502         HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE,
503                 ourContext.getCurrentUser(), ourContext.getExtraLogInfo());
504
505         log.info(LogManager.getHeader(ourContext, "delete_workspace_item",
506                 "workspace_item_id=" + getID() + "item_id=" + item.getID()
507                         + "collection_id=" + collection.getID()));
508
509         //deleteSubmitPermissions();
510
// Remove from cache
511
ourContext.removeCached(this, getID());
512
513         // Need to delete the epersongroup2workspaceitem row first since it refers
514
// to workspaceitem ID
515
deleteEpersonGroup2WorkspaceItem();
516
517         // Need to delete the workspaceitem row first since it refers
518
// to item ID
519
DatabaseManager.delete(ourContext, wiRow);
520
521         // Delete item
522
item.delete();
523     }
524
525     private void deleteEpersonGroup2WorkspaceItem() throws SQLException JavaDoc
526     {
527         
528         String JavaDoc removeSQL="DELETE FROM epersongroup2workspaceitem WHERE workspace_item_id = ?";
529         DatabaseManager.updateQuery(ourContext, removeSQL,getID());
530         
531     }
532
533     public void deleteWrapper() throws SQLException JavaDoc, AuthorizeException,
534             IOException JavaDoc
535     {
536         // Check authorisation. We check permissions on the enclosed item.
537
AuthorizeManager.authorizeAction(ourContext, item, Constants.WRITE);
538
539         HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE,
540                 ourContext.getCurrentUser(), ourContext.getExtraLogInfo());
541
542         log.info(LogManager.getHeader(ourContext, "delete_workspace_item",
543                 "workspace_item_id=" + getID() + "item_id=" + item.getID()
544                         + "collection_id=" + collection.getID()));
545
546         // deleteSubmitPermissions();
547
// Remove from cache
548
ourContext.removeCached(this, getID());
549
550         // Need to delete the workspaceitem row first since it refers
551
// to item ID
552
DatabaseManager.delete(ourContext, wiRow);
553     }
554
555     // InProgressSubmission methods
556
public Item getItem()
557     {
558         return item;
559     }
560
561     public Collection getCollection()
562     {
563         return collection;
564     }
565
566     public EPerson getSubmitter() throws SQLException JavaDoc
567     {
568         return item.getSubmitter();
569     }
570
571     public boolean hasMultipleFiles()
572     {
573         return wiRow.getBooleanColumn("multiple_files");
574     }
575
576     public void setMultipleFiles(boolean b)
577     {
578         wiRow.setColumn("multiple_files", b);
579     }
580
581     public boolean hasMultipleTitles()
582     {
583         return wiRow.getBooleanColumn("multiple_titles");
584     }
585
586     public void setMultipleTitles(boolean b)
587     {
588         wiRow.setColumn("multiple_titles", b);
589     }
590
591     public boolean isPublishedBefore()
592     {
593         return wiRow.getBooleanColumn("published_before");
594     }
595
596     public void setPublishedBefore(boolean b)
597     {
598         wiRow.setColumn("published_before", b);
599     }
600 }
601
Popular Tags