KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > WfProcessMgrImpl


1 package org.enhydra.shark;
2
3 import java.util.Collection JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.enhydra.shark.api.SharkTransaction;
9 import org.enhydra.shark.api.TransactionException;
10 import org.enhydra.shark.api.client.wfbase.BaseException;
11 import org.enhydra.shark.api.client.wfmodel.InvalidRequester;
12 import org.enhydra.shark.api.client.wfmodel.NotEnabled;
13 import org.enhydra.shark.api.client.wfmodel.RequesterRequired;
14 import org.enhydra.shark.api.client.wfmodel.TransitionNotAllowed;
15 import org.enhydra.shark.api.client.wfmodel.process_mgr_stateType;
16 import org.enhydra.shark.api.common.SharkConstants;
17 import org.enhydra.shark.api.internal.instancepersistence.PersistenceException;
18 import org.enhydra.shark.api.internal.instancepersistence.ProcessMgrPersistenceInterface;
19 import org.enhydra.shark.api.internal.working.WfProcessInternal;
20 import org.enhydra.shark.api.internal.working.WfProcessMgrInternal;
21 import org.enhydra.shark.api.internal.working.WfRequesterInternal;
22 import org.enhydra.shark.xpdl.XMLCollectionElement;
23 import org.enhydra.shark.xpdl.XPDLConstants;
24 import org.enhydra.shark.xpdl.elements.FormalParameter;
25 import org.enhydra.shark.xpdl.elements.FormalParameters;
26 import org.enhydra.shark.xpdl.elements.WorkflowProcess;
27
28 /**
29  * WfProcessMgrImpl - Workflow Process Manager implementation
30  * @author Sasa Bojanic
31  * @author Vladimir Puskas
32  */

33 public class WfProcessMgrImpl implements WfProcessMgrInternal {
34
35    private WorkflowProcess wp;
36
37    private String JavaDoc packageId;
38    private String JavaDoc processDefinitionId;
39
40    private String JavaDoc name;
41
42    private process_mgr_stateType state=process_mgr_stateType.enabled;
43
44    Map JavaDoc contextSignature;
45    Map JavaDoc resultSignature;
46    Map JavaDoc inputSignature;
47
48    private String JavaDoc version;
49    private long created;
50
51    private boolean justCreated=false;
52
53    /**
54     * Creates new WfProcessMgrImpl
55     * @param pkgId The Id of package where process definition exists.
56     * @param pDefId The Id of process definition.
57     */

58    protected WfProcessMgrImpl(SharkTransaction t,String JavaDoc pkgId,String JavaDoc version,String JavaDoc pDefId) throws BaseException {
59       this.justCreated = true;
60       this.name=SharkUtilities.createProcessMgrKey(pkgId,version,pDefId);
61       if (this.name==null) {
62          SharkEngineManager.getInstance().getCallbackUtilities().error("ERROR - MGR NAME NULL WHILE CRE OF PROCMGR");
63          throw new BaseException ("Problems while determining process mgr name");
64       }
65
66       this.packageId=pkgId;
67       this.processDefinitionId=pDefId;
68       this.version=version;
69       this.created=System.currentTimeMillis();
70       try {
71          persist(t);
72       } catch (TransactionException tme) {
73          throw new BaseException(tme);
74       }
75    }
76
77    /**
78     * Used to create object when restoring it from database.
79     */

80    protected WfProcessMgrImpl (ProcessMgrPersistenceInterface po) {
81       restore(po);
82    }
83
84    public process_mgr_stateType process_mgr_state (SharkTransaction t) throws BaseException {
85       return state;
86    }
87
88    public void set_process_mgr_state (SharkTransaction t,process_mgr_stateType new_state) throws BaseException, TransitionNotAllowed {
89       if (!state.equals(new_state)) {
90          state=new_state;
91          try {
92             persist(t);
93          } catch (Exception JavaDoc ex) {
94             throw new BaseException(ex);
95          }
96          String JavaDoc msg="ProcessDefinition "+toString()+" - the instantiation from process definition is ";
97          if (state.equals(process_mgr_stateType.enabled)) {
98             msg+="enabled";
99          } else {
100             msg+="disabled";
101          }
102          SharkEngineManager.getInstance().getCallbackUtilities().info(msg);
103       }
104    }
105
106    public final String JavaDoc name (SharkTransaction t) throws BaseException {
107       return name;
108    }
109
110    public String JavaDoc description (SharkTransaction t) throws BaseException {
111       try {
112          String JavaDoc desc=getProcessDefinition().getProcessHeader().getDescription();
113
114          return desc;
115       } catch (Exception JavaDoc ex) {
116          throw new BaseException(ex);
117       }
118    }
119
120    public String JavaDoc category (SharkTransaction t) throws BaseException {
121       try {
122          String JavaDoc cat=getProcessDefinition().getAccessLevel();
123          return cat;
124       } catch (Exception JavaDoc ex) {
125          throw new BaseException(ex);
126       }
127    }
128
129    public final String JavaDoc version (SharkTransaction t) throws BaseException {
130       return version;
131    }
132
133    public Map JavaDoc context_signature (SharkTransaction t) throws BaseException {
134       try {
135          if (contextSignature==null) {
136             buildSignatures(t);
137          }
138          return new HashMap JavaDoc(contextSignature);
139       } catch (Exception JavaDoc ex) {
140          throw new BaseException(ex);
141       }
142    }
143
144    public Map JavaDoc result_signature (SharkTransaction t) throws BaseException {
145       try {
146          if (resultSignature==null) {
147             buildSignatures(t);
148          }
149          return new HashMap JavaDoc(resultSignature);
150       } catch (Exception JavaDoc ex) {
151          throw new BaseException(ex);
152       }
153    }
154
155    public Map JavaDoc input_signature (SharkTransaction t) throws BaseException {
156       try {
157          if (inputSignature==null) {
158             buildSignatures(t);
159          }
160          return new HashMap JavaDoc(inputSignature);
161       } catch (Exception JavaDoc ex) {
162          throw new BaseException(ex);
163       }
164    }
165
166    /**
167     * Create a WfProcess object
168     */

169    public WfProcessInternal create_process (SharkTransaction t,WfRequesterInternal requester) throws BaseException,
170       NotEnabled, InvalidRequester, RequesterRequired {
171       if (state.value()==process_mgr_stateType._disabled) {
172          throw new NotEnabled("Can't create process based on WfProcessMgr '"+this+"' - it is disabled!");
173       }
174       // This can be changed - we can allow that some processes do not have to have requester
175
if (requester == null) {
176          throw new RequesterRequired("Can't create process based on WfProcessMgr '"+this+"' - the requester is required!");
177       }
178       String JavaDoc procId=getNextProcessKey();
179       WfProcessInternal process = SharkEngineManager.getInstance().
180          getObjectFactory().createProcess(t,this,requester,procId);
181       return process;
182    }
183
184    public final String JavaDoc process_definition_id(SharkTransaction t) throws BaseException {
185       return processDefinitionId;
186    }
187
188    public final String JavaDoc package_id(SharkTransaction t) throws BaseException {
189       return packageId;
190    }
191
192    public String JavaDoc process_definition_name(SharkTransaction t) throws BaseException {
193       try {
194          return getProcessDefinition().getName();
195       } catch (Exception JavaDoc ex){
196          throw new BaseException(ex);
197       }
198    }
199
200    // Constructs the context/result signatures from the formalParameters
201
private void buildSignatures(SharkTransaction t) throws BaseException {
202       contextSignature = new HashMap JavaDoc();
203       resultSignature = new HashMap JavaDoc();
204       inputSignature = new HashMap JavaDoc();
205
206       FormalParameters fps=getProcessDefinition().getFormalParameters();
207       Iterator JavaDoc it=fps.toElements().iterator();
208       while (it.hasNext()) {
209          FormalParameter fp=(FormalParameter)it.next();
210          String JavaDoc id = fp.getId();
211          String JavaDoc mode=fp.getMode();
212          String JavaDoc javaType=SharkUtilities.getJavaClass(fp).getName();
213
214          if (mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_IN) || mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
215             inputSignature.put(id,javaType);
216          }
217          if (mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT) || mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
218             resultSignature.put(id,javaType);
219          }
220       }
221
222       Collection JavaDoc dfsAndFPs=getProcessDefinition().getAllVariables().values();
223
224       it=dfsAndFPs.iterator();
225       while (it.hasNext()) {
226          XMLCollectionElement dfOrFp=(XMLCollectionElement)it.next();
227          String JavaDoc id = dfOrFp.getId();
228
229          String JavaDoc javaType=SharkUtilities.getJavaClass(dfOrFp).getName();
230          contextSignature.put(id,javaType);
231       }
232
233    }
234
235    protected String JavaDoc getNextProcessKey() {
236       String JavaDoc pk = SharkUtilities.getNextId(SharkConstants.PROCESS__ID_NAME);
237       // calculate 20 spaces for the Long, and do it twice because of
238
// the activities Id, also calculate 3 spaces for underscore
239
if (pk==null || packageId==null || processDefinitionId==null) {
240          SharkEngineManager.getInstance().getCallbackUtilities().error("PK="+pk+", packageId="+packageId+", pDefId="+processDefinitionId);
241       }
242       pk=pk+"_"+packageId+"_"+processDefinitionId;
243       if (pk.length()>100) pk=pk.substring(0,100);
244       return pk;
245    }
246
247    private WorkflowProcess getProcessDefinition () throws BaseException{
248       if (wp==null) {
249          wp=SharkUtilities.getWorkflowProcess(
250             packageId,
251             version,
252             processDefinitionId);
253       }
254       return wp;
255    }
256
257    public String JavaDoc toString () {
258       return "[name="+name+",version="+version+",pkgId="+packageId+",pDefId="+processDefinitionId+"]";
259    }
260
261    /**
262     * It is assumed that there can't be two or more processes mgrs
263     * having the same package id and process definition id.
264     */

265    public boolean equals (Object JavaDoc obj) {
266       if (!(obj instanceof WfProcessMgrImpl)) return false;
267       return (((WfProcessMgrImpl)obj).name.equals(name)); //&& mgr.version().equals(version()));
268
}
269
270    public void persist (SharkTransaction t) throws TransactionException {
271       try {
272          SharkEngineManager
273             .getInstance()
274             .getInstancePersistenceManager()
275             .persist(createAndFillPersistentObject(), this.justCreated, t);
276          this.justCreated = false;
277       } catch (PersistenceException pe) {
278          throw new TransactionException(pe);
279       }
280    }
281
282    public void delete (SharkTransaction t) throws TransactionException {
283       try {
284          SharkEngineManager
285             .getInstance()
286             .getInstancePersistenceManager()
287             .deleteProcessMgr(name,t);
288       } catch (Exception JavaDoc ex) {
289          throw new TransactionException("Exception while deleting ProcessMgr",ex);
290       }
291    }
292
293    private ProcessMgrPersistenceInterface createAndFillPersistentObject () {
294       ProcessMgrPersistenceInterface po=
295          SharkEngineManager.getInstance().getInstancePersistenceManager().createProcessMgr();
296       fillPersistentObject(po);
297       return po;
298    }
299
300    private void fillPersistentObject (ProcessMgrPersistenceInterface po) {
301       po.setName(this.name);
302       po.setPackageId(this.packageId);
303       po.setProcessDefinitionId(this.processDefinitionId);
304       po.setState(this.state.value());
305       po.setVersion(this.version);
306       po.setCreated(this.created);
307    }
308
309    private void restore (ProcessMgrPersistenceInterface po) {
310       this.name=po.getName();
311       if (this.name==null) {
312          SharkEngineManager.getInstance().getCallbackUtilities().error("ERROR - MGR NAME NULL WHILE REST OF PROCMGR");
313       }
314
315       this.packageId=po.getPackageId();
316       this.processDefinitionId=po.getProcessDefinitionId();
317       this.state=process_mgr_stateType.from_int(po.getState());
318       this.version=po.getVersion();
319       this.created=po.getCreated();
320    }
321
322 }
323
Popular Tags