KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > workflow > impl > WfProcessMgrImpl


1 /*
2  * $Id: WfProcessMgrImpl.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.workflow.impl;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.GeneralException;
37 import org.ofbiz.base.util.ObjectType;
38 import org.ofbiz.base.util.UtilMisc;
39 import org.ofbiz.entity.GenericDelegator;
40 import org.ofbiz.entity.GenericEntityException;
41 import org.ofbiz.entity.GenericValue;
42 import org.ofbiz.entity.util.EntityUtil;
43 import org.ofbiz.workflow.CannotChangeRequester;
44 import org.ofbiz.workflow.InvalidRequester;
45 import org.ofbiz.workflow.NotEnabled;
46 import org.ofbiz.workflow.RequesterRequired;
47 import org.ofbiz.workflow.TransitionNotAllowed;
48 import org.ofbiz.workflow.WfException;
49 import org.ofbiz.workflow.WfFactory;
50 import org.ofbiz.workflow.WfProcess;
51 import org.ofbiz.workflow.WfProcessMgr;
52 import org.ofbiz.workflow.WfRequester;
53 import org.ofbiz.workflow.WfUtil;
54
55 /**
56  * WfProcessMgrImpl - Workflow Process Manager implementation
57  *
58  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
59  * @author David Ostrovsky (d.ostrovsky@gmx.de)
60  * @version $Rev: 5462 $
61  * @since 2.0
62  */

63 public class WfProcessMgrImpl implements WfProcessMgr {
64
65     public static final String JavaDoc module = WfProcessMgrImpl.class.getName();
66
67     protected GenericValue processDef;
68     
69     protected String JavaDoc state; // will probably move to a runtime entity for the manager
70
protected List JavaDoc processList; // will probably be a related entity to the runtime entity
71

72     protected Map JavaDoc contextSignature = null;
73     protected Map JavaDoc resultSignature = null;
74     protected Map JavaDoc initialContext = null;
75     
76     /**
77      * Method WfProcessMgrImpl.
78      * @param delegator
79      * @param packageId
80      * @param packageVersion
81      * @param processId
82      * @param processVersion
83      * @throws WfException
84      */

85     public WfProcessMgrImpl(GenericDelegator delegator, String JavaDoc packageId, String JavaDoc packageVersion,
86             String JavaDoc processId, String JavaDoc processVersion) throws WfException {
87         Map JavaDoc finder = UtilMisc.toMap("packageId", packageId, "processId", processId);
88         List JavaDoc order = UtilMisc.toList("-packageVersion", "-processVersion");
89
90         if (packageVersion != null) finder.put("packageVersion", packageVersion);
91         if (processVersion != null) finder.put("processVersion", processVersion);
92         try {
93             List JavaDoc processes = delegator.findByAnd("WorkflowProcess", finder, order);
94             if (processes.size() == 0)
95                 throw new WfException("No process definition found for the specified processId");
96             else
97                 processDef = EntityUtil.getFirst(processes);
98         } catch (GenericEntityException e) {
99             throw new WfException("Problems getting the process definition from the WorkflowProcess entity");
100         }
101
102         buildSignatures();
103         buildInitialContext();
104         processList = new ArrayList JavaDoc();
105         state = "enabled";
106         if (Debug.infoOn()) Debug.logInfo("[WfProcessMgr.init] : Create process manager (" +
107                 packageId + "[" + packageVersion + "]" + " / " + processId + "[" + processVersion + "]" + ")", module);
108     }
109
110     /**
111      * @see org.ofbiz.workflow.WfProcessMgr#setProcessMgrState(java.lang.String)
112      */

113     public void setProcessMgrState(String JavaDoc newState) throws WfException, TransitionNotAllowed {
114         if (!newState.equals("enabled") || !newState.equals("disabled"))
115             throw new TransitionNotAllowed();
116         this.state = newState;
117     }
118
119     /**
120      * @see org.ofbiz.workflow.WfProcessMgr#getSequenceProcess(int)
121      */

122     public List JavaDoc getSequenceProcess(int maxNumber) throws WfException {
123         if (maxNumber > 0)
124             return new ArrayList JavaDoc(processList.subList(0, maxNumber - 1));
125         return processList;
126     }
127
128     /**
129      * @see org.ofbiz.workflow.WfProcessMgr#createProcess(org.ofbiz.workflow.WfRequester)
130      */

131     public WfProcess createProcess(WfRequester requester) throws WfException, NotEnabled,
132             InvalidRequester, RequesterRequired {
133         if (state.equals("disabled"))
134             throw new NotEnabled();
135
136         if (requester == null)
137             throw new RequesterRequired();
138
139         // test if the requestor is OK: how?
140
WfProcess process = WfFactory.getWfProcess(processDef, this);
141
142         try {
143             process.setRequester(requester);
144         } catch (CannotChangeRequester ccr) {
145             throw new WfException(ccr.getMessage(), ccr);
146         }
147         processList.add(process);
148         Debug.logVerbose("[WfProcessMgr.createProcess] : Process created.", module);
149         return process;
150     }
151
152     /**
153      * @see org.ofbiz.workflow.WfProcessMgr#contextSignature()
154      */

155     public Map JavaDoc contextSignature() throws WfException {
156         return this.contextSignature;
157     }
158     
159     /**
160      * @see org.ofbiz.workflow.WfProcessMgr#howManyProcess()
161      */

162     public int howManyProcess() throws WfException {
163         return processList.size();
164     }
165
166     /**
167      * @see org.ofbiz.workflow.WfProcessMgr#processMgrStateType()
168      */

169     public List JavaDoc processMgrStateType() throws WfException {
170         String JavaDoc[] list = {"enabled", "disabled"};
171         return Arrays.asList(list);
172     }
173
174     /**
175      * @see org.ofbiz.workflow.WfProcessMgr#category()
176      */

177     public String JavaDoc category() throws WfException {
178         return processDef.getString("category");
179     }
180
181     /**
182      * @see org.ofbiz.workflow.WfProcessMgr#version()
183      */

184     public String JavaDoc version() throws WfException {
185         return processDef.getString("version");
186     }
187
188     /**
189      * @see org.ofbiz.workflow.WfProcessMgr#description()
190      */

191     public String JavaDoc description() throws WfException {
192         return processDef.getString("description");
193     }
194
195     /**
196      * @see org.ofbiz.workflow.WfProcessMgr#name()
197      */

198     public String JavaDoc name() throws WfException {
199         return processDef.getString("name");
200     }
201
202     /**
203      * @see org.ofbiz.workflow.WfProcessMgr#resultSignature()
204      */

205     public Map JavaDoc resultSignature() throws WfException {
206         return this.resultSignature;
207     }
208     
209     /**
210      * Method getInitialContext.
211      * @return Map
212      */

213     public Map JavaDoc getInitialContext() {
214         return initialContext;
215     }
216
217     /**
218      * @see org.ofbiz.workflow.WfProcessMgr#isMemberOfProcess(org.ofbiz.workflow.WfProcess)
219      */

220     public boolean isMemberOfProcess(WfProcess member) throws WfException {
221         return processList.contains(member);
222     }
223
224     /**
225      * @see org.ofbiz.workflow.WfProcessMgr#getIteratorProcess()
226      */

227     public Iterator JavaDoc getIteratorProcess() throws WfException {
228         return processList.iterator();
229     }
230
231     // Constructs the context/result signatures from the formalParameters
232
private void buildSignatures() throws WfException {
233         contextSignature = new HashMap JavaDoc();
234         resultSignature = new HashMap JavaDoc();
235         Collection JavaDoc params = null;
236
237         try {
238             Map JavaDoc fields = new HashMap JavaDoc();
239
240             fields.put("packageId", processDef.getString("packageId"));
241             fields.put("packageVersion", processDef.getString("packageVersion"));
242             fields.put("processId", processDef.getString("processId"));
243             fields.put("processVersion", processDef.getString("processVersion"));
244             fields.put("applicationId", "_NA_");
245             params = processDef.getDelegator().findByAnd("WorkflowFormalParam", fields);
246
247         } catch (GenericEntityException e) {
248             throw new WfException(e.getMessage(), e);
249         }
250         if (params == null)
251             return;
252
253         Iterator JavaDoc i = params.iterator();
254         while (i.hasNext()) {
255             GenericValue param = (GenericValue) i.next();
256             String JavaDoc name = param.getString("formalParamId");
257             String JavaDoc mode = param.getString("modeEnumId");
258             String JavaDoc type = param.getString("dataTypeEnumId");
259
260             if (mode.equals("WPM_IN") || mode.equals("WPM_INOUT"))
261                 contextSignature.put(name, WfUtil.getJavaType(type));
262             else if (mode.equals("WPM_OUT") || mode.equals("WPM_INOUT"))
263                 resultSignature.put(name, WfUtil.getJavaType(type));
264         }
265     }
266                     
267     private void buildInitialContext() throws WfException {
268         GenericDelegator delegator = processDef.getDelegator();
269         this.initialContext = new HashMap JavaDoc();
270         List JavaDoc dataFields = new ArrayList JavaDoc();
271         try {
272             // make fields
273
Map JavaDoc fields = new HashMap JavaDoc();
274             fields.put("packageId", processDef.get("packageId"));
275             fields.put("packageVersion", processDef.get("packageVersion"));
276             
277             // first get all package fields
278
fields.put("processId", "_NA_");
279             fields.put("processVersion", "_NA_");
280             List JavaDoc data1 = delegator.findByAnd("WorkflowDataField", fields);
281             dataFields.addAll(data1);
282             
283             // now get all process fields
284
fields.put("processId", processDef.get("processId"));
285             fields.put("processVersion", processDef.get("processVersion"));
286             List JavaDoc data2 = delegator.findByAnd("WorkflowDataField", fields);
287             dataFields.addAll(data2);
288         } catch (GenericEntityException e) {
289             throw new WfException(e.getMessage(), e);
290         }
291         if (dataFields == null)
292             return;
293
294         Iterator JavaDoc i = dataFields.iterator();
295         
296         while (i.hasNext()) {
297             GenericValue dataField = (GenericValue) i.next();
298             String JavaDoc name = dataField.getString("dataFieldName");
299             String JavaDoc type = dataField.getString("dataTypeEnumId");
300             String JavaDoc value = dataField.getString("initialValue");
301    
302             try {
303                 initialContext.put(name, ObjectType.simpleTypeConvert(value, WfUtil.getJavaType(type), null, null));
304             } catch (GeneralException e) {
305                 throw new WfException(e.getMessage(), e);
306             }
307         }
308     }
309 }
310
311
Popular Tags