001: /*
002: * ====================================================================
003: *
004: * XFLOW - Process Management System
005: * Copyright (C) 2003 Rob Tan
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions, and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions, and the disclaimer that follows
017: * these conditions in the documentation and/or other materials
018: * provided with the distribution.
019: *
020: * 3. The name "XFlow" must not be used to endorse or promote products
021: * derived from this software without prior written permission. For
022: * written permission, please contact rcktan@yahoo.com
023: *
024: * 4. Products derived from this software may not be called "XFlow", nor
025: * may "XFlow" appear in their name, without prior written permission
026: * from the XFlow Project Management (rcktan@yahoo.com)
027: *
028: * In addition, we request (but do not require) that you include in the
029: * end-user documentation provided with the redistribution and/or in the
030: * software itself an acknowledgement equivalent to the following:
031: * "This product includes software developed by the
032: * XFlow Project (http://xflow.sourceforge.net/)."
033: * Alternatively, the acknowledgment may be graphical using the logos
034: * available at http://xflow.sourceforge.net/
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE XFLOW AUTHORS OR THE PROJECT
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: * ====================================================================
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the XFlow Project and was originally
052: * created by Rob Tan (rcktan@yahoo.com)
053: * For more information on the XFlow Project, please see:
054: * <http://xflow.sourceforge.net/>.
055: * ====================================================================
056: */
057:
058: package xflow.server.controller;
059:
060: import java.io.*;
061: import java.util.*;
062: import java.lang.*;
063: import java.sql.*;
064: import org.apache.log4j.Logger;
065: import xflow.common.*;
066: import xflow.protocol.*;
067: import xflow.util.*;
068:
069: public class WorkItemP {
070:
071: private static Logger log = Logger.getLogger(WorkItemP.class);
072:
073: public static Vector getWorkItems(String workflowName,
074: String processName) throws XflowException {
075:
076: Vector v = new Vector();
077: Connection con = null;
078: Statement s = null;
079: try {
080: con = Persistence.getConnection();
081: s = con.createStatement();
082:
083: String sql = "select workflowid, workitemid from inbox i where i.procName = '"
084: + processName
085: + "' and i.workflowName = '"
086: + workflowName + "'";
087: log.info(sql);
088: ResultSet rs = s.executeQuery(sql);
089: while (rs.next()) {
090: int wid = rs.getInt("workitemid");
091: int wfid = rs.getInt("workflowid");
092: Statement s2 = con.createStatement();
093: ResultSet rs2 = s2
094: .executeQuery("select payload, payloadType from workitem where workitemId = "
095: + wid);
096: if (rs2.next()) {
097: String pstr = rs2.getString("payload");
098: Object payload = null;
099: WorkItem witem = new WorkItem(new WorkItemId(wid));
100: if (pstr != null && !pstr.equals("")) {
101: payload = HexUtil.hexDecodeObject(pstr);
102: }
103: witem.setPayload(payload);
104: witem.setPayloadType(rs2.getString("payloadType"));
105: witem.setWorkflowId(new WorkflowId(wfid));
106: loadPropertiesFromDB(witem, workflowName,
107: processName, con);
108: v.addElement(witem);
109: } else {
110: log.error("Can't load workitem: " + wid);
111: }
112: s2.close();
113: rs2.close();
114: }
115: } catch (Exception e) {
116: e.printStackTrace();
117: throw new XflowException(
118: "Failed obtain workitems from database");
119: } finally {
120: if (s != null) {
121: try {
122: s.close();
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: }
127: if (con != null) {
128: try {
129: con.close();
130: } catch (Exception e) {
131: }
132: }
133: }
134: return v;
135: }
136:
137: public static WorkItem getNextWorkItem(String workflowName,
138: String processName) throws XflowException {
139:
140: WorkItem witem = null;
141: Connection con = null;
142: Statement s = null;
143: try {
144: con = Persistence.getConnection();
145: s = con.createStatement();
146:
147: String sql = "select count(*) from inbox i where i.procName = '"
148: + processName
149: + "' and i.workflowName = '"
150: + workflowName + "'";
151: ResultSet rs = s.executeQuery(sql);
152: int count = -1;
153: if (rs.next()) {
154: count = rs.getInt(1);
155: }
156: if (count > 0) {
157: sql = "select timeStarted, workflowid, workitemid from inbox where procName = '"
158: + processName
159: + "' and workflowName = '"
160: + workflowName + "' order by timeStarted";
161: log.info(sql);
162: rs = s.executeQuery(sql);
163: if (rs.next()) {
164: int wid = rs.getInt("workitemid");
165: int wfid = rs.getInt("workflowid");
166: Statement s2 = con.createStatement();
167: ResultSet rs2 = s2
168: .executeQuery("select * from workitem where workitemId = "
169: + wid);
170: if (rs2.next()) {
171: String pstr = rs2.getString("payload");
172: Object payload = null;
173: witem = new WorkItem(new WorkItemId(wid));
174: witem.setWorkflowId(new WorkflowId(wfid));
175: if (pstr != null && !pstr.equals("")) {
176: payload = HexUtil.hexDecodeObject(pstr);
177: witem.setPayload(payload);
178: }
179: witem.setPayloadType(rs2
180: .getString("payloadType"));
181: loadPropertiesFromDB(witem, workflowName,
182: processName, con);
183: } else {
184: log.error("Can't load workitem: " + wid);
185: }
186: s2.close();
187: rs2.close();
188: }
189: }
190: } catch (Exception e) {
191: e.printStackTrace();
192: throw new XflowException(
193: "Failed obtain workitems from database");
194: } finally {
195: if (s != null) {
196: try {
197: s.close();
198: } catch (Exception e) {
199: e.printStackTrace();
200: }
201: }
202: if (con != null) {
203: try {
204: con.close();
205: } catch (Exception e) {
206: }
207: }
208: }
209: return witem;
210: }
211:
212: public static WorkItem getWorkItem(WorkItemId wid,
213: String processName) throws XflowException {
214:
215: WorkItem witem = null;
216: int workitemId = wid.getValue();
217: WorkflowId wfId = witem.getWorkflowId();
218: int workflowId = wfId.getId();
219: String workflowName = "";
220:
221: Connection con = null;
222: Statement s = null;
223: try {
224: con = Persistence.getConnection();
225: s = con.createStatement();
226:
227: String sql = "select g.name from graph g, workflow w where g.gid = w.gid and w.workflowId = '"
228: + workflowId;
229: log.info(sql);
230: ResultSet rs = s.executeQuery(sql);
231: if (rs.next()) {
232: workflowName = rs.getString("name");
233: }
234: rs.close();
235:
236: sql = "select workflowid from inbox where workitemid = "
237: + workitemId + " and procName = '" + processName
238: + "'";
239: log.info(sql);
240: rs = s.executeQuery(sql);
241: if (rs.next()) {
242: int wfid = rs.getInt("workflowid");
243: Statement s2 = con.createStatement();
244: ResultSet rs2 = s2
245: .executeQuery("select * from workitem where workitemId = "
246: + wid);
247: if (rs2.next()) {
248: witem = new WorkItem(wid);
249: witem.setWorkflowId(new WorkflowId(wfid));
250: String pstr = rs2.getString("payload");
251: Object payload = null;
252: if (pstr != null && !pstr.equals("")) {
253: payload = HexUtil.hexDecodeObject(pstr);
254: }
255: witem.setPayload(payload);
256: witem.setPayloadType(rs2.getString("payloadType"));
257: loadPropertiesFromDB(witem, workflowName,
258: processName, con);
259: } else {
260: log.error("Can't load workitem: " + wid);
261: }
262: s2.close();
263: rs2.close();
264: }
265: } catch (Exception e) {
266: e.printStackTrace();
267: throw new XflowException(
268: "Failed obtain workitems from database");
269: } finally {
270: if (s != null) {
271: try {
272: s.close();
273: } catch (Exception e) {
274: e.printStackTrace();
275: }
276: }
277: if (con != null) {
278: try {
279: con.close();
280: } catch (Exception e) {
281: }
282: }
283: }
284: return witem;
285: }
286:
287: public static void saveDB(WorkItem witem) throws XflowException {
288:
289: Connection con = null;
290: Statement s = null;
291:
292: try {
293: con = Persistence.getConnection();
294: s = con.createStatement();
295:
296: Object payload = witem.getPayload();
297: String payloadStr = "";
298: String payloadType = witem.getPayloadType();
299: if (payload != null) {
300: payloadStr = HexUtil.hexEncodeObject(payload);
301: }
302:
303: String sql = "insert into workitem values (null, '"
304: + payloadStr + "','" + payloadType + "')";
305: log.info(sql);
306: s.execute(sql);
307:
308: sql = "select max(workitemId), workitemId from workitem";
309: ResultSet rs = s.executeQuery(sql);
310: if (rs.next()) {
311: WorkItemId id = new WorkItemId(rs.getInt("workitemId"));
312: witem.setId(id);
313: }
314:
315: } catch (Exception e) {
316: e.printStackTrace();
317: throw new XflowException(
318: "Failed to save workitem to database");
319: } finally {
320: if (s != null) {
321: try {
322: s.close();
323: } catch (Exception e) {
324: e.printStackTrace();
325: }
326: }
327: if (con != null) {
328: try {
329: con.close();
330: } catch (Exception e) {
331: }
332: }
333: }
334: }
335:
336: public static void updateDB(WorkItem witem) throws XflowException {
337:
338: int workitemId = witem.getId().getValue();
339: Connection con = null;
340: Statement s = null;
341:
342: try {
343: con = Persistence.getConnection();
344: s = con.createStatement();
345:
346: Object payload = witem.getPayload();
347: String payloadStr = "";
348: String payloadType = witem.getPayloadType();
349: if (payload != null) {
350: payloadStr = HexUtil.hexEncodeObject(payload);
351: }
352:
353: String sql = "update workitem set payload = '" + payloadStr
354: + "' where workitemId = " + workitemId;
355: log.info(sql);
356: s.execute(sql);
357:
358: } catch (Exception e) {
359: e.printStackTrace();
360: throw new XflowException(
361: "Failed to save workitem to database");
362: } finally {
363: if (s != null) {
364: try {
365: s.close();
366: } catch (Exception e) {
367: e.printStackTrace();
368: }
369: }
370: if (con != null) {
371: try {
372: con.close();
373: } catch (Exception e) {
374: }
375: }
376: }
377: }
378:
379: public static void loadPropertiesFromDB(WorkItem witem,
380: String workflowName, String procName, Connection con)
381: throws XflowException {
382:
383: HashMap properties = witem.getProperties();
384: Statement s = null;
385:
386: int wid = witem.getId().getValue();
387: try {
388: s = con.createStatement();
389: String sql = "select name, value from workitemprops where workitemId = "
390: + wid
391: + " and procname = '"
392: + procName
393: + "' and workflowname = '" + workflowName + "'";
394: ResultSet rs = s.executeQuery(sql);
395: while (rs.next()) {
396: String name = rs.getString("name");
397: String vstr = rs.getString("value");
398: Object obj = HexUtil.hexDecodeObject(vstr);
399: properties.put(name, obj);
400: }
401: } catch (Exception e) {
402: e.printStackTrace();
403: throw new XflowException(
404: "Failed to load workitem properties from database");
405: } finally {
406: if (s != null) {
407: try {
408: s.close();
409: } catch (Exception e) {
410: e.printStackTrace();
411: }
412: }
413: }
414: }
415:
416: public static void savePropertiesToDB(WorkItem witem,
417: String workflowName, String procName, Connection con)
418: throws XflowException {
419:
420: Statement s = null;
421:
422: try {
423: s = con.createStatement();
424:
425: // Insert the work item properties
426: HashMap properties = witem.getProperties();
427: Iterator itr = properties.keySet().iterator();
428: while (itr.hasNext()) {
429: String key = (String) itr.next();
430: Object value = properties.get(key);
431: String valueStr = HexUtil.hexEncodeObject(value);
432: if (valueStr == null) {
433: continue;
434: }
435: WorkItemId workItemId = witem.getId();
436: String sql = "insert into workitemprops values ("
437: + workItemId.getValue() + ",'" + workflowName
438: + "','" + procName + "','" + key + "','"
439: + valueStr + "')";
440:
441: log.info(sql);
442: s.execute(sql);
443: }
444: } catch (Exception e) {
445: e.printStackTrace();
446: throw new XflowException(
447: "Failed to save workitem properties to database");
448: } finally {
449: if (s != null) {
450: try {
451: s.close();
452: } catch (Exception e) {
453: e.printStackTrace();
454: }
455: }
456: }
457: }
458:
459: public static void deleteDB(WorkItem wi) throws XflowException {
460:
461: Connection con = null;
462: Statement s = null;
463:
464: try {
465: con = Persistence.getConnection();
466: s = con.createStatement();
467:
468: WorkItemId workItemId = wi.getId();
469: String sql = "delete from workitemprops where workitemId = "
470: + workItemId.getValue();
471: s.execute(sql);
472:
473: sql = "delete from workitem where workitemId = "
474: + workItemId.getValue();
475: s.execute(sql);
476: } catch (Exception e) {
477: e.printStackTrace();
478: throw new XflowException(
479: "Failed to delete workitem from database");
480: } finally {
481: if (s != null) {
482: try {
483: s.close();
484: } catch (Exception e) {
485: e.printStackTrace();
486: }
487: }
488: if (con != null) {
489: try {
490: con.close();
491: } catch (Exception e) {
492: }
493: }
494: }
495: }
496: }
|