001: /*
002: * $Id: WfProcessMgrImpl.java,v 1.2 2003/08/19 17:45:18 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.workflow.impl;
026:
027: import java.util.ArrayList;
028: import java.util.Arrays;
029: import java.util.Collection;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034:
035: import org.ofbiz.base.util.Debug;
036: import org.ofbiz.base.util.GeneralException;
037: import org.ofbiz.base.util.ObjectType;
038: import org.ofbiz.base.util.UtilMisc;
039: import org.ofbiz.entity.GenericDelegator;
040: import org.ofbiz.entity.GenericEntityException;
041: import org.ofbiz.entity.GenericValue;
042: import org.ofbiz.entity.util.EntityUtil;
043: import org.ofbiz.workflow.CannotChangeRequester;
044: import org.ofbiz.workflow.InvalidRequester;
045: import org.ofbiz.workflow.NotEnabled;
046: import org.ofbiz.workflow.RequesterRequired;
047: import org.ofbiz.workflow.TransitionNotAllowed;
048: import org.ofbiz.workflow.WfException;
049: import org.ofbiz.workflow.WfFactory;
050: import org.ofbiz.workflow.WfProcess;
051: import org.ofbiz.workflow.WfProcessMgr;
052: import org.ofbiz.workflow.WfRequester;
053: import org.ofbiz.workflow.WfUtil;
054:
055: /**
056: * WfProcessMgrImpl - Workflow Process Manager implementation
057: *
058: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
059: * @author David Ostrovsky (d.ostrovsky@gmx.de)
060: * @version $Revision: 1.2 $
061: * @since 2.0
062: */
063: public class WfProcessMgrImpl implements WfProcessMgr {
064:
065: public static final String module = WfProcessMgrImpl.class
066: .getName();
067:
068: protected GenericValue processDef;
069:
070: protected String state; // will probably move to a runtime entity for the manager
071: protected List processList; // will probably be a related entity to the runtime entity
072:
073: protected Map contextSignature = null;
074: protected Map resultSignature = null;
075: protected Map initialContext = null;
076:
077: /**
078: * Method WfProcessMgrImpl.
079: * @param delegator
080: * @param packageId
081: * @param packageVersion
082: * @param processId
083: * @param processVersion
084: * @throws WfException
085: */
086: public WfProcessMgrImpl(GenericDelegator delegator,
087: String packageId, String packageVersion, String processId,
088: String processVersion) throws WfException {
089: Map finder = UtilMisc.toMap("packageId", packageId,
090: "processId", processId);
091: List order = UtilMisc.toList("-packageVersion",
092: "-processVersion");
093:
094: if (packageVersion != null)
095: finder.put("packageVersion", packageVersion);
096: if (processVersion != null)
097: finder.put("processVersion", processVersion);
098: try {
099: List processes = delegator.findByAnd("WorkflowProcess",
100: finder, order);
101: if (processes.size() == 0)
102: throw new WfException(
103: "No process definition found for the specified processId");
104: else
105: processDef = EntityUtil.getFirst(processes);
106: } catch (GenericEntityException e) {
107: throw new WfException(
108: "Problems getting the process definition from the WorkflowProcess entity");
109: }
110:
111: buildSignatures();
112: buildInitialContext();
113: processList = new ArrayList();
114: state = "enabled";
115: if (Debug.infoOn())
116: Debug.logInfo(
117: "[WfProcessMgr.init] : Create process manager ("
118: + packageId + "[" + packageVersion + "]"
119: + " / " + processId + "[" + processVersion
120: + "]" + ")", module);
121: }
122:
123: /**
124: * @see org.ofbiz.workflow.WfProcessMgr#setProcessMgrState(java.lang.String)
125: */
126: public void setProcessMgrState(String newState) throws WfException,
127: TransitionNotAllowed {
128: if (!newState.equals("enabled") || !newState.equals("disabled"))
129: throw new TransitionNotAllowed();
130: this .state = newState;
131: }
132:
133: /**
134: * @see org.ofbiz.workflow.WfProcessMgr#getSequenceProcess(int)
135: */
136: public List getSequenceProcess(int maxNumber) throws WfException {
137: if (maxNumber > 0)
138: return new ArrayList(processList.subList(0, maxNumber - 1));
139: return processList;
140: }
141:
142: /**
143: * @see org.ofbiz.workflow.WfProcessMgr#createProcess(org.ofbiz.workflow.WfRequester)
144: */
145: public WfProcess createProcess(WfRequester requester)
146: throws WfException, NotEnabled, InvalidRequester,
147: RequesterRequired {
148: if (state.equals("disabled"))
149: throw new NotEnabled();
150:
151: if (requester == null)
152: throw new RequesterRequired();
153:
154: // test if the requestor is OK: how?
155: WfProcess process = WfFactory.getWfProcess(processDef, this );
156:
157: try {
158: process.setRequester(requester);
159: } catch (CannotChangeRequester ccr) {
160: throw new WfException(ccr.getMessage(), ccr);
161: }
162: processList.add(process);
163: Debug.logVerbose(
164: "[WfProcessMgr.createProcess] : Process created.",
165: module);
166: return process;
167: }
168:
169: /**
170: * @see org.ofbiz.workflow.WfProcessMgr#contextSignature()
171: */
172: public Map contextSignature() throws WfException {
173: return this .contextSignature;
174: }
175:
176: /**
177: * @see org.ofbiz.workflow.WfProcessMgr#howManyProcess()
178: */
179: public int howManyProcess() throws WfException {
180: return processList.size();
181: }
182:
183: /**
184: * @see org.ofbiz.workflow.WfProcessMgr#processMgrStateType()
185: */
186: public List processMgrStateType() throws WfException {
187: String[] list = { "enabled", "disabled" };
188: return Arrays.asList(list);
189: }
190:
191: /**
192: * @see org.ofbiz.workflow.WfProcessMgr#category()
193: */
194: public String category() throws WfException {
195: return processDef.getString("category");
196: }
197:
198: /**
199: * @see org.ofbiz.workflow.WfProcessMgr#version()
200: */
201: public String version() throws WfException {
202: return processDef.getString("version");
203: }
204:
205: /**
206: * @see org.ofbiz.workflow.WfProcessMgr#description()
207: */
208: public String description() throws WfException {
209: return processDef.getString("description");
210: }
211:
212: /**
213: * @see org.ofbiz.workflow.WfProcessMgr#name()
214: */
215: public String name() throws WfException {
216: return processDef.getString("name");
217: }
218:
219: /**
220: * @see org.ofbiz.workflow.WfProcessMgr#resultSignature()
221: */
222: public Map resultSignature() throws WfException {
223: return this .resultSignature;
224: }
225:
226: /**
227: * Method getInitialContext.
228: * @return Map
229: */
230: public Map getInitialContext() {
231: return initialContext;
232: }
233:
234: /**
235: * @see org.ofbiz.workflow.WfProcessMgr#isMemberOfProcess(org.ofbiz.workflow.WfProcess)
236: */
237: public boolean isMemberOfProcess(WfProcess member)
238: throws WfException {
239: return processList.contains(member);
240: }
241:
242: /**
243: * @see org.ofbiz.workflow.WfProcessMgr#getIteratorProcess()
244: */
245: public Iterator getIteratorProcess() throws WfException {
246: return processList.iterator();
247: }
248:
249: // Constructs the context/result signatures from the formalParameters
250: private void buildSignatures() throws WfException {
251: contextSignature = new HashMap();
252: resultSignature = new HashMap();
253: Collection params = null;
254:
255: try {
256: Map fields = new HashMap();
257:
258: fields.put("packageId", processDef.getString("packageId"));
259: fields.put("packageVersion", processDef
260: .getString("packageVersion"));
261: fields.put("processId", processDef.getString("processId"));
262: fields.put("processVersion", processDef
263: .getString("processVersion"));
264: fields.put("applicationId", "_NA_");
265: params = processDef.getDelegator().findByAnd(
266: "WorkflowFormalParam", fields);
267:
268: } catch (GenericEntityException e) {
269: throw new WfException(e.getMessage(), e);
270: }
271: if (params == null)
272: return;
273:
274: Iterator i = params.iterator();
275: while (i.hasNext()) {
276: GenericValue param = (GenericValue) i.next();
277: String name = param.getString("formalParamId");
278: String mode = param.getString("modeEnumId");
279: String type = param.getString("dataTypeEnumId");
280:
281: if (mode.equals("WPM_IN") || mode.equals("WPM_INOUT"))
282: contextSignature.put(name, WfUtil.getJavaType(type));
283: else if (mode.equals("WPM_OUT") || mode.equals("WPM_INOUT"))
284: resultSignature.put(name, WfUtil.getJavaType(type));
285: }
286: }
287:
288: private void buildInitialContext() throws WfException {
289: GenericDelegator delegator = processDef.getDelegator();
290: this .initialContext = new HashMap();
291: List dataFields = new ArrayList();
292: try {
293: // make fields
294: Map fields = new HashMap();
295: fields.put("packageId", processDef.get("packageId"));
296: fields.put("packageVersion", processDef
297: .get("packageVersion"));
298:
299: // first get all package fields
300: fields.put("processId", "_NA_");
301: fields.put("processVersion", "_NA_");
302: List data1 = delegator.findByAnd("WorkflowDataField",
303: fields);
304: dataFields.addAll(data1);
305:
306: // now get all process fields
307: fields.put("processId", processDef.get("processId"));
308: fields.put("processVersion", processDef
309: .get("processVersion"));
310: List data2 = delegator.findByAnd("WorkflowDataField",
311: fields);
312: dataFields.addAll(data2);
313: } catch (GenericEntityException e) {
314: throw new WfException(e.getMessage(), e);
315: }
316: if (dataFields == null)
317: return;
318:
319: Iterator i = dataFields.iterator();
320:
321: while (i.hasNext()) {
322: GenericValue dataField = (GenericValue) i.next();
323: String name = dataField.getString("dataFieldName");
324: String type = dataField.getString("dataTypeEnumId");
325: String value = dataField.getString("initialValue");
326:
327: try {
328: initialContext.put(name, ObjectType.simpleTypeConvert(
329: value, WfUtil.getJavaType(type), null, null));
330: } catch (GeneralException e) {
331: throw new WfException(e.getMessage(), e);
332: }
333: }
334: }
335: }
|