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: import org.apache.log4j.Logger;
070:
071: public class InboxP {
072:
073: private static Logger log = Logger.getLogger(InboxP.class);
074:
075: public static void addWorkItem(int gid, String workflowName,
076: String procName, WorkItem workitem) throws XflowException {
077:
078: WorkflowId wfId = workitem.getWorkflowId();
079: WorkItemId wid = workitem.getId();
080: int workflowId = wfId.getValue();
081: int workitemId = wid.getValue();
082:
083: Connection con = null;
084: Statement s = null;
085: try {
086: con = Persistence.getConnection();
087: s = con.createStatement();
088: String timeStarted = DateUtil.getTimestamp();
089: String sql = "insert into inbox values (" + workflowId
090: + "," + gid + ",'" + workflowName + "','"
091: + procName + "'," + workitemId + ",'" + timeStarted
092: + "', false)";
093: log.info(sql);
094: s.execute(sql);
095:
096: WorkItemP.updateDB(workitem);
097: WorkItemP.savePropertiesToDB(workitem, workflowName,
098: procName, con);
099:
100: } catch (Exception e) {
101: e.printStackTrace();
102: throw new XflowException(
103: "Failed to save row in inbox in database");
104: } finally {
105: if (s != null) {
106: try {
107: s.close();
108: } catch (Exception e) {
109: e.printStackTrace();
110: }
111: }
112: if (con != null) {
113: try {
114: con.close();
115: } catch (Exception e) {
116: }
117: }
118: }
119: }
120:
121: public static void removeWorkItem(int gid, String procName,
122: WorkItem workitem) throws XflowException {
123:
124: WorkflowId wfId = workitem.getWorkflowId();
125: WorkItemId wid = workitem.getId();
126: int workflowId = wfId.getValue();
127: int workitemId = wid.getValue();
128:
129: Connection con = null;
130: Statement s = null;
131:
132: try {
133: con = Persistence.getConnection();
134: s = con.createStatement();
135:
136: String sql = "delete from inbox where workflowId = "
137: + workflowId + " and gid = " + gid
138: + " and procname = '" + procName + "'"
139: + " and workitemid = " + workitemId;
140: log.info(sql);
141: s.execute(sql);
142: } catch (Exception e) {
143: e.printStackTrace();
144: throw new XflowException(
145: "Failed to delete row in inbox from database");
146: } finally {
147: if (s != null) {
148: try {
149: s.close();
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: }
154: if (con != null) {
155: try {
156: con.close();
157: } catch (Exception e) {
158: }
159: }
160: }
161: }
162:
163: public static void removeWorkItems(int workflowId)
164: throws XflowException {
165:
166: Connection con = null;
167: Statement s = null;
168:
169: try {
170: con = Persistence.getConnection();
171: s = con.createStatement();
172:
173: String sql = "delete from inbox where workflowId = "
174: + workflowId;
175: log.info(sql);
176: s.execute(sql);
177: } catch (Exception e) {
178: e.printStackTrace();
179: throw new XflowException(
180: "Failed to delete rows from inbox from database");
181: } finally {
182: if (s != null) {
183: try {
184: s.close();
185: } catch (Exception e) {
186: e.printStackTrace();
187: }
188: }
189: if (con != null) {
190: try {
191: con.close();
192: } catch (Exception e) {
193: }
194: }
195: }
196: }
197:
198: public static boolean isWorkItemValid(int gid, String procName,
199: WorkItem workitem) throws XflowException {
200:
201: WorkflowId wfId = workitem.getWorkflowId();
202: WorkItemId wid = workitem.getId();
203: int workflowId = wfId.getValue();
204: int workitemId = wid.getValue();
205:
206: Connection con = null;
207: Statement s = null;
208: boolean result = false;
209:
210: try {
211: con = Persistence.getConnection();
212: s = con.createStatement();
213:
214: String sql = "select * from inbox where workflowId = "
215: + workflowId + " and gid = " + gid
216: + " and procname = '" + procName + "'"
217: + " and workitemid = " + workitemId;
218: log.info(sql);
219: ResultSet rs = s.executeQuery(sql);
220: if (rs.next()) {
221: result = true;
222: }
223: rs.close();
224: } catch (Exception e) {
225: e.printStackTrace();
226: throw new XflowException(
227: "Failed to select row from inbox from database");
228: } finally {
229: if (s != null) {
230: try {
231: s.close();
232: } catch (Exception e) {
233: e.printStackTrace();
234: }
235: }
236: if (con != null) {
237: try {
238: con.close();
239: } catch (Exception e) {
240: }
241: }
242: }
243: return result;
244: }
245:
246: public static java.sql.Date getTimeStarted(int workflowId,
247: String procName) throws XflowException {
248:
249: Connection con = null;
250: Statement s = null;
251: java.sql.Date timeStarted = null;
252:
253: try {
254: con = Persistence.getConnection();
255: s = con.createStatement();
256:
257: String sql = "select timeStarted from inbox where workflowId = "
258: + workflowId
259: + " and procname = '"
260: + procName
261: + "'"
262: + " and (timeout = false or timeout is null)";
263: log.info(sql);
264: ResultSet rs = s.executeQuery(sql);
265: if (rs.next()) {
266: timeStarted = rs.getDate("timeStarted");
267: }
268: rs.close();
269: } catch (Exception e) {
270: e.printStackTrace();
271: throw new XflowException(
272: "Failed to get timeStarted from inbox from database");
273: } finally {
274: if (s != null) {
275: try {
276: s.close();
277: } catch (Exception e) {
278: e.printStackTrace();
279: }
280: }
281: if (con != null) {
282: try {
283: con.close();
284: } catch (Exception e) {
285: }
286: }
287: }
288: return timeStarted;
289: }
290:
291: public static void setTimeoutFlag(int workflowId, String procName)
292: throws XflowException {
293:
294: Connection con = null;
295: Statement s = null;
296:
297: try {
298: con = Persistence.getConnection();
299: s = con.createStatement();
300:
301: String sql = "update inbox set timeout = true where workflowId = "
302: + workflowId + " and procname = '" + procName + "'";
303: log.info(sql);
304: s.execute(sql);
305: } catch (Exception e) {
306: e.printStackTrace();
307: throw new XflowException(
308: "Failed to update timeoutFlag from inbox from database");
309: } finally {
310: if (s != null) {
311: try {
312: s.close();
313: } catch (Exception e) {
314: e.printStackTrace();
315: }
316: }
317: if (con != null) {
318: try {
319: con.close();
320: } catch (Exception e) {
321: }
322: }
323: }
324: }
325:
326: }
|