KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > workflow > WorkflowItem


1 /*
2  * WorkflowItem.java
3  *
4  * Version: $Revision: 1.19 $
5  *
6  * Date: $Date: 2006/05/26 14:23:04 $
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.workflow;
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.content.Collection;
50 import org.dspace.content.InProgressSubmission;
51 import org.dspace.content.Item;
52 import org.dspace.core.Context;
53 import org.dspace.core.LogManager;
54 import org.dspace.eperson.EPerson;
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 going through the workflow process in DSpace
62  *
63  * @author Robert Tansley
64  * @version $Revision: 1.19 $
65  */

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

94     WorkflowItem(Context context, TableRow row) throws SQLException JavaDoc
95     {
96         ourContext = context;
97         wfRow = row;
98
99         item = Item.find(context, wfRow.getIntColumn("item_id"));
100         collection = Collection.find(context, wfRow
101                 .getIntColumn("collection_id"));
102
103         if (wfRow.isColumnNull("owner"))
104         {
105             owner = null;
106         }
107         else
108         {
109             owner = EPerson.find(context, wfRow.getIntColumn("owner"));
110         }
111
112         // Cache ourselves
113
context.cache(this, row.getIntColumn("workflow_id"));
114     }
115
116     /**
117      * Get a workflow item from the database. The item, collection and submitter
118      * are loaded into memory.
119      *
120      * @param context
121      * DSpace context object
122      * @param id
123      * ID of the workspace item
124      *
125      * @return the workflow item, or null if the ID is invalid.
126      */

127     public static WorkflowItem find(Context context, int id)
128             throws SQLException JavaDoc
129     {
130         // First check the cache
131
WorkflowItem fromCache = (WorkflowItem) context.fromCache(
132                 WorkflowItem.class, id);
133
134         if (fromCache != null)
135         {
136             return fromCache;
137         }
138
139         TableRow row = DatabaseManager.find(context, "workflowitem", id);
140
141         if (row == null)
142         {
143             if (log.isDebugEnabled())
144             {
145                 log.debug(LogManager.getHeader(context, "find_workflow_item",
146                         "not_found,workflow_id=" + id));
147             }
148
149             return null;
150         }
151         else
152         {
153             if (log.isDebugEnabled())
154             {
155                 log.debug(LogManager.getHeader(context, "find_workflow_item",
156                         "workflow_id=" + id));
157             }
158
159             return new WorkflowItem(context, row);
160         }
161     }
162
163     /**
164      * return all workflowitems
165      *
166      * @param c active context
167      * @return WorkflowItem [] of all workflows in system
168      */

169     public static WorkflowItem[] findAll(Context c) throws SQLException JavaDoc
170     {
171         List JavaDoc wfItems = new ArrayList JavaDoc();
172         TableRowIterator tri = DatabaseManager.queryTable(c, "workflowitem",
173                 "SELECT * FROM workflowitem");
174
175         // make a list of workflow items
176
while (tri.hasNext())
177         {
178             TableRow row = tri.next();
179             WorkflowItem wi = new WorkflowItem(c, row);
180             wfItems.add(wi);
181         }
182         
183         tri.close();
184
185         WorkflowItem[] wfArray = new WorkflowItem[wfItems.size()];
186         wfArray = (WorkflowItem[]) wfItems.toArray(wfArray);
187
188         return wfArray;
189     }
190
191     /**
192      * Get all workflow items that were original submissions by a particular
193      * e-person. These are ordered by workflow ID, since this should likely keep
194      * them in the order in which they were created.
195      *
196      * @param context
197      * the context object
198      * @param ep
199      * the eperson
200      *
201      * @return the corresponding workflow items
202      */

203     public static WorkflowItem[] findByEPerson(Context context, EPerson ep)
204             throws SQLException JavaDoc
205     {
206         List JavaDoc wfItems = new ArrayList JavaDoc();
207
208         TableRowIterator tri = DatabaseManager.queryTable(context, "workflowitem",
209                 "SELECT workflowitem.* FROM workflowitem, item WHERE " +
210                 "workflowitem.item_id=item.item_id AND " +
211                 "item.submitter_id= ? " +
212                 "ORDER BY workflowitem.workflow_id",
213                 ep.getID());
214
215         while (tri.hasNext())
216         {
217             TableRow row = tri.next();
218
219             // Check the cache
220
WorkflowItem wi = (WorkflowItem) context.fromCache(
221                     WorkflowItem.class, row.getIntColumn("workflow_id"));
222
223             if (wi == null)
224             {
225                 wi = new WorkflowItem(context, row);
226             }
227
228             wfItems.add(wi);
229         }
230         
231         tri.close();
232
233         WorkflowItem[] wfArray = new WorkflowItem[wfItems.size()];
234         wfArray = (WorkflowItem[]) wfItems.toArray(wfArray);
235
236         return wfArray;
237     }
238
239     /**
240      * Get all workflow items for a particular collection.
241      *
242      * @param context
243      * the context object
244      * @param c
245      * the collection
246      *
247      * @return array of the corresponding workflow items
248      */

249     public static WorkflowItem[] findByCollection(Context context, Collection c)
250             throws SQLException JavaDoc
251     {
252         List JavaDoc wsItems = new ArrayList JavaDoc();
253
254         TableRowIterator tri = DatabaseManager.queryTable(context, "workflowitem",
255                 "SELECT workflowitem.* FROM workflowitem WHERE " +
256                 "workflowitem.collection_id= ? ",
257                 c.getID());
258
259         while (tri.hasNext())
260         {
261             TableRow row = tri.next();
262
263             // Check the cache
264
WorkflowItem wi = (WorkflowItem) context.fromCache(
265                     WorkflowItem.class, row.getIntColumn("workflow_id"));
266
267             // not in cache? turn row into workflowitem
268
if (wi == null)
269             {
270                 wi = new WorkflowItem(context, row);
271             }
272
273             wsItems.add(wi);
274         }
275         
276         tri.close();
277
278         WorkflowItem[] wsArray = new WorkflowItem[wsItems.size()];
279         wsArray = (WorkflowItem[]) wsItems.toArray(wsArray);
280
281         return wsArray;
282     }
283
284     /**
285      * Get the internal ID of this workflow item
286      *
287      * @return the internal identifier
288      */

289     public int getID()
290     {
291         return wfRow.getIntColumn("workflow_id");
292     }
293
294     /**
295      * get owner of WorkflowItem
296      *
297      * @return EPerson owner
298      */

299     public EPerson getOwner()
300     {
301         return owner;
302     }
303
304     /**
305      * set owner of WorkflowItem
306      *
307      * @param ep
308      * owner
309      */

310     public void setOwner(EPerson ep)
311     {
312         owner = ep;
313
314         if (ep == null)
315         {
316             wfRow.setColumnNull("owner");
317         }
318         else
319         {
320             wfRow.setColumn("owner", ep.getID());
321         }
322     }
323
324     /**
325      * Get state of WorkflowItem
326      *
327      * @return state
328      */

329     public int getState()
330     {
331         return wfRow.getIntColumn("state");
332     }
333
334     /**
335      * Set state of WorkflowItem
336      *
337      * @param newstate
338      * new state (from <code>WorkflowManager</code>)
339      */

340     public void setState(int newstate)
341     {
342         wfRow.setColumn("state", newstate);
343     }
344
345     /**
346      * Update the workflow item, including the unarchived item.
347      */

348     public void update() throws SQLException JavaDoc, IOException JavaDoc, AuthorizeException
349     {
350         // FIXME check auth
351
log.info(LogManager.getHeader(ourContext, "update_workflow_item",
352                 "workflow_item_id=" + getID()));
353
354         // Update the item
355
item.update();
356
357         // Update ourselves
358
DatabaseManager.update(ourContext, wfRow);
359
360         HistoryManager.saveHistory(ourContext, this, HistoryManager.MODIFY,
361                 ourContext.getCurrentUser(), ourContext.getExtraLogInfo());
362     }
363
364     /**
365      * delete the WorkflowItem, retaining the Item
366      */

367     public void deleteWrapper() throws SQLException JavaDoc, IOException JavaDoc,
368             AuthorizeException
369     {
370         // Remove from cache
371
ourContext.removeCached(this, getID());
372
373         HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE,
374                 ourContext.getCurrentUser(), ourContext.getExtraLogInfo());
375
376         // delete any pending tasks
377
WorkflowManager.deleteTasks(ourContext, this);
378
379         // FIXME - auth?
380
DatabaseManager.delete(ourContext, wfRow);
381     }
382
383     // InProgressSubmission methods
384
public Item getItem()
385     {
386         return item;
387     }
388
389     public Collection getCollection()
390     {
391         return collection;
392     }
393
394     public EPerson getSubmitter() throws SQLException JavaDoc
395     {
396         return item.getSubmitter();
397     }
398
399     public boolean hasMultipleFiles()
400     {
401         return wfRow.getBooleanColumn("multiple_files");
402     }
403
404     public void setMultipleFiles(boolean b)
405     {
406         wfRow.setColumn("multiple_files", b);
407     }
408
409     public boolean hasMultipleTitles()
410     {
411         return wfRow.getBooleanColumn("multiple_titles");
412     }
413
414     public void setMultipleTitles(boolean b)
415     {
416         wfRow.setColumn("multiple_titles", b);
417     }
418
419     public boolean isPublishedBefore()
420     {
421         return wfRow.getBooleanColumn("published_before");
422     }
423
424     public void setPublishedBefore(boolean b)
425     {
426         wfRow.setColumn("published_before", b);
427     }
428 }
429
Popular Tags