/usr/java/lib/xflow1.2.1/src/xflow/server/controller/WorkflowP.java

1    /* 
2    * ==================================================================== 
3    * 
4    * XFLOW - Process Management System 
5    * Copyright (C) 2003 Rob Tan 
6    * All rights reserved. 
7    * 
8    * Redistribution and use in source and binary forms, with or without 
9    * modification, are permitted provided that the following conditions 
10   * are met: 
11   * 
12   * 1. Redistributions of source code must retain the above copyright 
13   *    notice, this list of conditions, and the following disclaimer. 
14   * 
15   * 2. Redistributions in binary form must reproduce the above copyright 
16   *    notice, this list of conditions, and the disclaimer that follows 
17   *    these conditions in the documentation and/or other materials 
18   *    provided with the distribution. 
19   * 
20   * 3. The name "XFlow" must not be used to endorse or promote products 
21   *    derived from this software without prior written permission.  For 
22   *    written permission, please contact rcktan@yahoo.com 
23   * 
24   * 4. Products derived from this software may not be called "XFlow", nor 
25   *    may "XFlow" appear in their name, without prior written permission 
26   *    from the XFlow Project Management (rcktan@yahoo.com) 
27   * 
28   * In addition, we request (but do not require) that you include in the 
29   * end-user documentation provided with the redistribution and/or in the 
30   * software itself an acknowledgement equivalent to the following: 
31   *     "This product includes software developed by the 
32   *      XFlow Project (http://xflow.sourceforge.net/)." 
33   * Alternatively, the acknowledgment may be graphical using the logos 
34   * available at http://xflow.sourceforge.net/ 
35   * 
36   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
37   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
38   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
39   * DISCLAIMED.  IN NO EVENT SHALL THE XFLOW AUTHORS OR THE PROJECT 
40   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
41   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
42   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
43   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
44   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
45   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
46   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
47   * SUCH DAMAGE. 
48   * 
49   * ==================================================================== 
50   * This software consists of voluntary contributions made by many 
51   * individuals on behalf of the XFlow Project and was originally 
52   * created by Rob Tan (rcktan@yahoo.com) 
53   * For more information on the XFlow Project, please see: 
54   *           <http://xflow.sourceforge.net/>. 
55   * ==================================================================== 
56   */ 
57    
58   package xflow.server.controller; 
59    
60   import org.apache.log4j.Logger; 
61   import xflow.common.WorkflowModel; 
62   import xflow.common.WorkflowState; 
63   import xflow.common.XflowException; 
64   import xflow.server.util.ProcessWithTimeout; 
65    
66   import xflow.util.Persistence; 
67   import xflow.util.Util; 
68    
69   import java.sql.SQLException; 
70   import java.util.*; 
71    
72    
73   public class WorkflowP { 
74    
75     private static Logger log = Logger.getLogger(WorkflowP.class); 
76    
77     public void insertOrtab ( Integer workflowId, int nodeId) throws SQLException { 
78       Map params = new Hashtable(); 
79       params.put("workflowId",  ( workflowId )); 
80       params.put("nodeId" , new Integer( nodeId ) ); 
81       Persistence.getThreadSqlMapSession().insert( "insertOrtab",params  ); 
82     } 
83    
84     public boolean existsOrtab (Integer workflowId, int nodeId) throws SQLException { 
85       Map params = new Hashtable(); 
86       params.put("workflowId",  ( workflowId )); 
87       params.put("nodeId" , new Integer( nodeId ) ); 
88       Integer wfId = (Integer) Persistence.getThreadSqlMapSession().queryForObject( "selectOrtab",params  ); 
89       return ( wfId != null ); 
90     } 
91    
92     /** 
93      * 
94      * @param graphId 
95      * @param workflowName 
96      * @param initiator 
97      * @param parentWorkflowId 
98      * @return ID of the new Workflow 
99      * @throws XflowException 
100     */ 
101    public Integer saveNewWorkflow (final int graphId, String workflowName, final String initiator, final int parentWorkflowId) 
102        throws XflowException { 
103      Map params = new Hashtable(); 
104      if (parentWorkflowId != -1) { 
105        params.put( "pWfId", new Integer( parentWorkflowId ) ); 
106      } 
107      params.put("graphId", new Integer( graphId )); 
108      params.put("initiator" , initiator); 
109      params.put("timeStarted", new Date() ); 
110      Object result = null; 
111      try { 
112        result = Persistence.getThreadSqlMapSession().insert( "insertWorkflow",params  ); 
113      } catch (SQLException e) { 
114        throw new XflowException( e ); 
115      } 
116      return ((Integer) result ); 
117    } 
118   
119    public int getGraphId (final Integer workflowId) throws SQLException { 
120      Object  result = Persistence.getThreadSqlMapSession().queryForObject( "selectWorkflowGid", workflowId  ); 
121      return ((Integer)result).intValue(); 
122    } 
123   
124    public List getActiveWorkflows () throws  SQLException { 
125      return  Persistence.getThreadSqlMapSession().queryForList( "activeWorkflowIDs", null  ); 
126    } 
127   
128    public List getSuspendedWorkflows () throws  SQLException { 
129      return   Persistence.getThreadSqlMapSession().queryForList( "suspendedWorkflowIDs", null ); 
130    } 
131   
132    public List getAllWorkflows () throws SQLException { 
133      return Persistence.getThreadSqlMapSession().queryForList( "allWorkflowIDs", null ); 
134    } 
135   
136    public  List getWorkflowsByName (final String name) throws SQLException { 
137      return   Persistence.getThreadSqlMapSession().queryForList( "getWorkflowIDsByName", name  ); 
138    } 
139   
140    /** 
141     * 
142     * @return list of {@link WorkflowModel WorkflowModel} 
143     * @throws SQLException 
144     */ 
145    public  List getModels () throws SQLException { 
146      return  Persistence.getThreadSqlMapSession().queryForList( "getModels",null ) ; 
147    } 
148   
149    public  void abortWorkflow (final Integer workflowId) throws SQLException { 
150      Map params = new Hashtable(); 
151      params.put("workflowId",workflowId ); 
152      params.put("timeEnded", new Date() ); 
153      Persistence.getThreadSqlMapSession().update( "abortWorkflow",params  ); 
154    } 
155   
156    public  void suspendWorkflow (final Integer workflowId) throws XflowException, SQLException { 
157      Persistence.getThreadSqlMapSession().update( "suspendWorkflow",workflowId   ); 
158    } 
159   
160    public void resumeWorkflow (final Integer workflowId) throws SQLException { 
161      Persistence.getThreadSqlMapSession().update( "resumeWorkflow",workflowId   ); 
162    } 
163   
164    public  WorkflowState getWorkflowState (Integer workflowId) throws XflowException, SQLException { 
165   
166      WorkflowState state = (WorkflowState) Persistence.getThreadSqlMapSession().queryForObject( "getWorkflowState", workflowId ); 
167      if ( state== null ) return null; 
168      if (state.isActive )  state.timeEnded = null; 
169      if (state.state == null || state.state.equals("")) { 
170        if (state.isActive) { 
171          state.state = "RUNNING"; 
172        } else { 
173          state.state = "COMPLETED"; 
174        } 
175      } 
176   
177      // Load the active processes 
178      List procStateRecords = Persistence.getThreadSqlMapSession().queryForList( "selectProcessStateRecords", workflowId ); 
179      for (Iterator j = procStateRecords.iterator(); j.hasNext();) { 
180        ProcessStateRec stateRec = (ProcessStateRec) j.next(); 
181        state.activeProcesses.add ( stateRec.makeProcessState() ); 
182      } 
183   
184      // Load the workflow variables 
185      List wfVars = Persistence.getThreadSqlMapSession().queryForList( "selectWorkwlowVariables", workflowId ); 
186      for (Iterator j = wfVars.iterator(); j.hasNext();) { 
187        WorkflowVariable wfVar = (WorkflowVariable) j.next(); 
188        state.variables.put (wfVar.getName(), Util.objFromXML( wfVar.getValue() )); 
189      } 
190      return state; 
191    } 
192   
193    public  void setVariable (Integer workflowId, String name, Object value) throws  SQLException { 
194      if( log.isDebugEnabled() ){ 
195        log.debug( "Hex Encoding: " + value); 
196      } 
197      String valueStr = Util.objToXML(value); 
198      if( log.isDebugEnabled() ){ 
199        log.debug ("String to be stored: " + valueStr); 
200      } 
201      Map params = new Hashtable(); 
202      params.put( "workflowId",  workflowId ); 
203      params.put( "name", name ); 
204      params.put( "varVal", valueStr ); 
205      Persistence.getThreadSqlMapSession().delete( "deleteWorkflowVar", params ); 
206      Persistence.getThreadSqlMapSession().insert( "insertWorkflowVar", params ); 
207    } 
208   
209    public  Object getVariable (final Integer workflowId, final String name) throws SQLException { 
210      Map params = new Hashtable(); 
211      params.put("workflowId", workflowId ); 
212      params.put("name", name ); 
213      Object result = Persistence.getThreadSqlMapSession().queryForObject( "getVariable",params  ); 
214      if( result == null) return null; 
215      return Util.objFromXML((String ) result); 
216    } 
217   
218    public  void setCompleted (final Integer workflowId) throws SQLException { 
219      Map params = new Hashtable(); 
220      params.put("workflowId", (workflowId)); 
221      params.put("timeEnded", new Date() ); 
222      Persistence.getThreadSqlMapSession().update( "setCompleted",params  ); 
223    } 
224   
225    public boolean isCompleted (final int workflowId) throws SQLException { 
226      Object res = Persistence.getThreadSqlMapSession().queryForObject( "isCompleted", new Integer(workflowId )  ); 
227      return (  res != null ); 
228    } 
229   
230    public  List getProcessesWithTimeouts() throws XflowException, SQLException { 
231      List v = new ArrayList(); 
232      String pName = null; 
233      Integer    pId; 
234      int    pToutMinutes = -1; 
235      String pThdl = null; 
236   
237      List nodesWithTimeout = Persistence.getThreadSqlMapSession().queryForList( "selectNodesWithTimeout", null ); 
238      for (Iterator j = nodesWithTimeout.iterator(); j.hasNext();) { 
239        HashMap map = (HashMap) j.next(); 
240        pName = (String) map.get("name"); 
241        pId   = ((Integer)map.get("nid")); 
242        String vstr = (String) map.get("value"); 
243        Object value = Util.objFromXML(vstr); 
244        Integer iValue = (Integer)value; 
245        pToutMinutes = iValue.intValue(); 
246   
247        pThdl = null; 
248        vstr = (String) Persistence.getThreadSqlMapSession().queryForObject( "selectTimeoutHandler", pId ); 
249        if( vstr != null ){ 
250          value = Util.objFromXML(vstr); 
251          pThdl = (String)value; 
252        } 
253   
254        List workflowIDs = Persistence.getThreadSqlMapSession().queryForList( "selectWorkflowIdByNodeId", pId ); 
255        for (Iterator k = workflowIDs.iterator(); k.hasNext();) { 
256          Integer wf_id = (Integer) k.next(); 
257          int wfId = wf_id.intValue(); 
258          ProcessWithTimeout pto = new ProcessWithTimeout(); 
259          pto.workflowId = wfId; 
260          pto.processName = pName; 
261          pto.timeoutMinutes = pToutMinutes; 
262          pto.timeoutHandler = pThdl; 
263          v.add (pto); 
264        } 
265   
266      } 
267   
268      return v; 
269    } 
270  } 
271