001: /*
002: * WorkflowItem.java
003: *
004: * Version: $Revision: 2074 $
005: *
006: * Date: $Date: 2007-07-19 14:40:11 -0500 (Thu, 19 Jul 2007) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.workflow;
041:
042: import java.io.IOException;
043: import java.sql.SQLException;
044: import java.util.ArrayList;
045: import java.util.List;
046:
047: import org.apache.log4j.Logger;
048: import org.dspace.authorize.AuthorizeException;
049: import org.dspace.content.Collection;
050: import org.dspace.content.InProgressSubmission;
051: import org.dspace.content.Item;
052: import org.dspace.core.Context;
053: import org.dspace.core.LogManager;
054: import org.dspace.eperson.EPerson;
055: import org.dspace.storage.rdbms.DatabaseManager;
056: import org.dspace.storage.rdbms.TableRow;
057: import org.dspace.storage.rdbms.TableRowIterator;
058:
059: /**
060: * Class representing an item going through the workflow process in DSpace
061: *
062: * @author Robert Tansley
063: * @version $Revision: 2074 $
064: */
065: public class WorkflowItem implements InProgressSubmission {
066: /** log4j category */
067: private static Logger log = Logger.getLogger(WorkflowItem.class);
068:
069: /** The item this workflow object pertains to */
070: private Item item;
071:
072: /** Our context */
073: private Context ourContext;
074:
075: /** The table row corresponding to this workflow item */
076: private TableRow wfRow;
077:
078: /** The collection the item is being submitted to */
079: private Collection collection;
080:
081: /** EPerson owning the current state */
082: private EPerson owner;
083:
084: /**
085: * Construct a workspace item corresponding to the given database row
086: *
087: * @param context
088: * the context this object exists in
089: * @param row
090: * the database row
091: */
092: WorkflowItem(Context context, TableRow row) throws SQLException {
093: ourContext = context;
094: wfRow = row;
095:
096: item = Item.find(context, wfRow.getIntColumn("item_id"));
097: collection = Collection.find(context, wfRow
098: .getIntColumn("collection_id"));
099:
100: if (wfRow.isColumnNull("owner")) {
101: owner = null;
102: } else {
103: owner = EPerson.find(context, wfRow.getIntColumn("owner"));
104: }
105:
106: // Cache ourselves
107: context.cache(this , row.getIntColumn("workflow_id"));
108: }
109:
110: /**
111: * Get a workflow item from the database. The item, collection and submitter
112: * are loaded into memory.
113: *
114: * @param context
115: * DSpace context object
116: * @param id
117: * ID of the workspace item
118: *
119: * @return the workflow item, or null if the ID is invalid.
120: */
121: public static WorkflowItem find(Context context, int id)
122: throws SQLException {
123: // First check the cache
124: WorkflowItem fromCache = (WorkflowItem) context.fromCache(
125: WorkflowItem.class, id);
126:
127: if (fromCache != null) {
128: return fromCache;
129: }
130:
131: TableRow row = DatabaseManager
132: .find(context, "workflowitem", id);
133:
134: if (row == null) {
135: if (log.isDebugEnabled()) {
136: log.debug(LogManager.getHeader(context,
137: "find_workflow_item", "not_found,workflow_id="
138: + id));
139: }
140:
141: return null;
142: } else {
143: if (log.isDebugEnabled()) {
144: log.debug(LogManager.getHeader(context,
145: "find_workflow_item", "workflow_id=" + id));
146: }
147:
148: return new WorkflowItem(context, row);
149: }
150: }
151:
152: /**
153: * return all workflowitems
154: *
155: * @param c active context
156: * @return WorkflowItem [] of all workflows in system
157: */
158: public static WorkflowItem[] findAll(Context c) throws SQLException {
159: List wfItems = new ArrayList();
160: TableRowIterator tri = DatabaseManager.queryTable(c,
161: "workflowitem", "SELECT * FROM workflowitem");
162:
163: // make a list of workflow items
164: while (tri.hasNext()) {
165: TableRow row = tri.next();
166: WorkflowItem wi = new WorkflowItem(c, row);
167: wfItems.add(wi);
168: }
169:
170: tri.close();
171:
172: WorkflowItem[] wfArray = new WorkflowItem[wfItems.size()];
173: wfArray = (WorkflowItem[]) wfItems.toArray(wfArray);
174:
175: return wfArray;
176: }
177:
178: /**
179: * Get all workflow items that were original submissions by a particular
180: * e-person. These are ordered by workflow ID, since this should likely keep
181: * them in the order in which they were created.
182: *
183: * @param context
184: * the context object
185: * @param ep
186: * the eperson
187: *
188: * @return the corresponding workflow items
189: */
190: public static WorkflowItem[] findByEPerson(Context context,
191: EPerson ep) throws SQLException {
192: List wfItems = new ArrayList();
193:
194: TableRowIterator tri = DatabaseManager.queryTable(context,
195: "workflowitem",
196: "SELECT workflowitem.* FROM workflowitem, item WHERE "
197: + "workflowitem.item_id=item.item_id AND "
198: + "item.submitter_id= ? "
199: + "ORDER BY workflowitem.workflow_id", ep
200: .getID());
201:
202: while (tri.hasNext()) {
203: TableRow row = tri.next();
204:
205: // Check the cache
206: WorkflowItem wi = (WorkflowItem) context
207: .fromCache(WorkflowItem.class, row
208: .getIntColumn("workflow_id"));
209:
210: if (wi == null) {
211: wi = new WorkflowItem(context, row);
212: }
213:
214: wfItems.add(wi);
215: }
216:
217: tri.close();
218:
219: WorkflowItem[] wfArray = new WorkflowItem[wfItems.size()];
220: wfArray = (WorkflowItem[]) wfItems.toArray(wfArray);
221:
222: return wfArray;
223: }
224:
225: /**
226: * Get all workflow items for a particular collection.
227: *
228: * @param context
229: * the context object
230: * @param c
231: * the collection
232: *
233: * @return array of the corresponding workflow items
234: */
235: public static WorkflowItem[] findByCollection(Context context,
236: Collection c) throws SQLException {
237: List wsItems = new ArrayList();
238:
239: TableRowIterator tri = DatabaseManager.queryTable(context,
240: "workflowitem",
241: "SELECT workflowitem.* FROM workflowitem WHERE "
242: + "workflowitem.collection_id= ? ", c.getID());
243:
244: while (tri.hasNext()) {
245: TableRow row = tri.next();
246:
247: // Check the cache
248: WorkflowItem wi = (WorkflowItem) context
249: .fromCache(WorkflowItem.class, row
250: .getIntColumn("workflow_id"));
251:
252: // not in cache? turn row into workflowitem
253: if (wi == null) {
254: wi = new WorkflowItem(context, row);
255: }
256:
257: wsItems.add(wi);
258: }
259:
260: tri.close();
261:
262: WorkflowItem[] wsArray = new WorkflowItem[wsItems.size()];
263: wsArray = (WorkflowItem[]) wsItems.toArray(wsArray);
264:
265: return wsArray;
266: }
267:
268: /**
269: * Get the internal ID of this workflow item
270: *
271: * @return the internal identifier
272: */
273: public int getID() {
274: return wfRow.getIntColumn("workflow_id");
275: }
276:
277: /**
278: * get owner of WorkflowItem
279: *
280: * @return EPerson owner
281: */
282: public EPerson getOwner() {
283: return owner;
284: }
285:
286: /**
287: * set owner of WorkflowItem
288: *
289: * @param ep
290: * owner
291: */
292: public void setOwner(EPerson ep) {
293: owner = ep;
294:
295: if (ep == null) {
296: wfRow.setColumnNull("owner");
297: } else {
298: wfRow.setColumn("owner", ep.getID());
299: }
300: }
301:
302: /**
303: * Get state of WorkflowItem
304: *
305: * @return state
306: */
307: public int getState() {
308: return wfRow.getIntColumn("state");
309: }
310:
311: /**
312: * Set state of WorkflowItem
313: *
314: * @param newstate
315: * new state (from <code>WorkflowManager</code>)
316: */
317: public void setState(int newstate) {
318: wfRow.setColumn("state", newstate);
319: }
320:
321: /**
322: * Update the workflow item, including the unarchived item.
323: */
324: public void update() throws SQLException, IOException,
325: AuthorizeException {
326: // FIXME check auth
327: log.info(LogManager.getHeader(ourContext,
328: "update_workflow_item", "workflow_item_id=" + getID()));
329:
330: // Update the item
331: item.update();
332:
333: // Update ourselves
334: DatabaseManager.update(ourContext, wfRow);
335: }
336:
337: /**
338: * delete the WorkflowItem, retaining the Item
339: */
340: public void deleteWrapper() throws SQLException, IOException,
341: AuthorizeException {
342: // Remove from cache
343: ourContext.removeCached(this , getID());
344:
345: // delete any pending tasks
346: WorkflowManager.deleteTasks(ourContext, this );
347:
348: // FIXME - auth?
349: DatabaseManager.delete(ourContext, wfRow);
350: }
351:
352: // InProgressSubmission methods
353: public Item getItem() {
354: return item;
355: }
356:
357: public Collection getCollection() {
358: return collection;
359: }
360:
361: public EPerson getSubmitter() throws SQLException {
362: return item.getSubmitter();
363: }
364:
365: public boolean hasMultipleFiles() {
366: return wfRow.getBooleanColumn("multiple_files");
367: }
368:
369: public void setMultipleFiles(boolean b) {
370: wfRow.setColumn("multiple_files", b);
371: }
372:
373: public boolean hasMultipleTitles() {
374: return wfRow.getBooleanColumn("multiple_titles");
375: }
376:
377: public void setMultipleTitles(boolean b) {
378: wfRow.setColumn("multiple_titles", b);
379: }
380:
381: public boolean isPublishedBefore() {
382: return wfRow.getBooleanColumn("published_before");
383: }
384:
385: public void setPublishedBefore(boolean b) {
386: wfRow.setColumn("published_before", b);
387: }
388: }
|