001: /*
002: * $Id: WfApplicationServices.java,v 1.1 2003/08/17 09:29:33 ajzeneski 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;
026:
027: import java.io.IOException;
028: import java.sql.Timestamp;
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.Date;
032: import java.util.HashMap;
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Map;
036:
037: import javax.xml.parsers.ParserConfigurationException;
038:
039: import org.ofbiz.base.util.Debug;
040: import org.ofbiz.base.util.GeneralException;
041: import org.ofbiz.base.util.ObjectType;
042: import org.ofbiz.base.util.UtilMisc;
043: import org.ofbiz.entity.GenericDelegator;
044: import org.ofbiz.entity.GenericEntityException;
045: import org.ofbiz.entity.GenericValue;
046: import org.ofbiz.entity.serialize.SerializeException;
047: import org.ofbiz.entity.serialize.XmlSerializer;
048: import org.ofbiz.service.DispatchContext;
049: import org.ofbiz.service.GenericServiceException;
050: import org.ofbiz.service.ModelService;
051: import org.xml.sax.SAXException;
052:
053: /**
054: * Workflow Application Services - 'Services' and 'Workers' for interaction with Workflow Application API
055: *
056: * @author Manuel Soto & Oswin Ondarza
057: * @version $Revision: 1.1 $
058: * @since 2.0
059: */
060: public class WfApplicationServices {
061:
062: // -------------------------------------------------------------------
063: // Appication 'Service' Methods
064: // -------------------------------------------------------------------
065:
066: public static final String module = WfApplicationServices.class
067: .getName();
068:
069: /**
070: * Activate an application by inserting expected arguments in
071: * the ApplicationSandbox table.
072: *
073: * Note: Assume that the activity (workEffort) has only one performer as
074: * defined in XPDL specification, this means that there is only one
075: * partyAssigment w/ CAL_ACEPTED state.
076: * @param ctx Service dispatch Context
077: * @param context Actual parameters
078: * @throws GenericServiceException
079: * @return Empty result
080: */
081: public static Map activateApplication(DispatchContext ctx,
082: Map context) {
083: final String workEffortId = (String) context
084: .get("workEffortId");
085: final Map result = new HashMap();
086:
087: try {
088: final GenericValue weAssigment = getWorkEffortPartyAssigment(
089: ctx.getDelegator(), workEffortId);
090:
091: final String partyId = (String) weAssigment.get("partyId");
092: final String roleTypeId = (String) weAssigment
093: .get("roleTypeId");
094: final Timestamp fromDate = (Timestamp) weAssigment
095: .get("fromDate");
096: result.put("applicationId", insertAppSandbox(ctx
097: .getDelegator(), workEffortId, partyId, roleTypeId,
098: fromDate, context));
099: result.put(ModelService.RESPONSE_MESSAGE,
100: ModelService.RESPOND_SUCCESS);
101: } catch (GenericServiceException we) {
102: we.printStackTrace();
103: result.put(ModelService.RESPONSE_MESSAGE,
104: ModelService.RESPOND_ERROR);
105: result.put(ModelService.ERROR_MESSAGE, we.getMessage());
106: }
107:
108: return result;
109: }
110:
111: public static Map getApplicationContext(DispatchContext ctx,
112: Map context) throws GenericServiceException {
113: final GenericDelegator delegator = ctx.getDelegator();
114: final Map result = new HashMap();
115: try {
116: result.put("applicationContext", getRunTimeContext(
117: delegator, getRuntimeData(delegator,
118: (String) context.get("applicationId"))));
119: result.put(ModelService.RESPONSE_MESSAGE,
120: ModelService.RESPOND_SUCCESS);
121: } catch (GenericServiceException we) {
122: we.printStackTrace();
123: result.put(ModelService.RESPONSE_MESSAGE,
124: ModelService.RESPOND_ERROR);
125: result.put(ModelService.ERROR_MESSAGE, we.getMessage());
126: }
127: return result;
128: }
129:
130: public static Map completeApplication(DispatchContext ctx,
131: Map context) throws GenericServiceException {
132: final GenericDelegator delegator = ctx.getDelegator();
133: final String applicationId = (String) context
134: .get("applicationId");
135: final Map result = new HashMap();
136:
137: GenericValue application = getApplicationSandbox(delegator,
138: applicationId);
139: GenericValue runTimeData = getRuntimeData(delegator,
140: applicationId);
141: Map runTimeContext = getRunTimeContext(delegator, runTimeData);
142: Map contextSignature = new HashMap();
143: Map resultSignature = new HashMap();
144: Map resultContext = new HashMap();
145: Map runContext = new HashMap(context);
146:
147: try {
148: // copy all OUT & INOUT formal parameters
149: getApplicationSignatures(delegator, application,
150: contextSignature, resultSignature);
151: for (Iterator names = resultSignature.keySet().iterator(); names
152: .hasNext();) {
153: String name = (String) names.next();
154: Object value = null;
155: if (runTimeContext.containsKey(name)
156: && contextSignature.containsKey(name)
157: && resultSignature.containsKey(name))
158: value = runTimeContext.get(name);
159: if (((Map) context.get("result")).containsKey(name))
160: value = ((Map) context.get("result")).get(name);
161: if (value != null)
162: resultContext.put(name, ObjectType
163: .simpleTypeConvert(value,
164: (String) resultSignature.get(name),
165: null, null));
166: }
167: runTimeContext.putAll(resultContext);
168: // fin de agregar
169: if (Debug.verboseOn()) {
170: Debug.logVerbose("Completing Application: "
171: + applicationId, module);
172: Debug.logVerbose(" Result Signature: "
173: + resultSignature.toString(), module);
174: Debug.logVerbose(" Result Values: "
175: + resultContext.toString(), module);
176:
177: }
178:
179: setRunTimeContext(runTimeData, runTimeContext);
180: // agregado por Oswin Ondarza
181:
182: runContext.remove("applicationId");
183:
184: final String workEffortId = (String) runTimeContext
185: .get("workEffortId");
186: final Timestamp fromDate = (Timestamp) application
187: .get("fromDate");
188: final String partyId = (String) application.get("partyId");
189: final String roleTypeId = (String) application
190: .get("roleTypeId");
191:
192: runContext.put("workEffortId", workEffortId);
193: runContext.put("fromDate", fromDate);
194: runContext.put("partyId", partyId);
195: runContext.put("roleTypeId", roleTypeId);
196: runContext.put("result", resultContext);
197:
198: result.putAll(ctx.getDispatcher().runSync(
199: "wfCompleteAssignment", runContext));
200: } catch (GenericEntityException we) {
201: we.printStackTrace();
202: result.put(ModelService.RESPONSE_MESSAGE,
203: ModelService.RESPOND_ERROR);
204: result.put(ModelService.ERROR_MESSAGE, we.getMessage());
205: } catch (GenericServiceException we) {
206: we.printStackTrace();
207: result.put(ModelService.RESPONSE_MESSAGE,
208: ModelService.RESPOND_ERROR);
209: result.put(ModelService.ERROR_MESSAGE, we.getMessage());
210: } catch (GeneralException ge) {
211: ge.printStackTrace();
212: result.put(ModelService.RESPONSE_MESSAGE,
213: ModelService.RESPOND_ERROR);
214: result.put(ModelService.ERROR_MESSAGE, ge.getMessage());
215: }
216: return result;
217: }
218:
219: private static String insertAppSandbox(GenericDelegator delegator,
220: String workEffortId, String partyId, String roleTypeId,
221: Timestamp fromDate, Map context)
222: throws GenericServiceException {
223: String dataId = null;
224: String infoId = null;
225: String applicationId = new String(new Long((new Date()
226: .getTime())).toString());
227:
228: try {
229: dataId = delegator.getNextSeqId("RuntimeData").toString();
230: GenericValue runtimeData = delegator.makeValue(
231: "RuntimeData", UtilMisc.toMap("runtimeDataId",
232: dataId));
233: runtimeData.set("runtimeInfo", XmlSerializer
234: .serialize(context));
235: delegator.create(runtimeData);
236: } catch (GenericEntityException ee) {
237: throw new GenericServiceException(ee.getMessage(), ee);
238: } catch (SerializeException se) {
239: throw new GenericServiceException(se.getMessage(), se);
240: } catch (IOException ioe) {
241: throw new GenericServiceException(ioe.getMessage(), ioe);
242: }
243: Map aFields = UtilMisc.toMap("applicationId", applicationId,
244: "workEffortId", workEffortId, "partyId", partyId,
245: "roleTypeId", roleTypeId, "fromDate", fromDate,
246: "runtimeDataId", dataId);
247:
248: GenericValue appV = null;
249: try {
250: appV = delegator.makeValue("ApplicationSandbox", aFields);
251: delegator.create(appV);
252: } catch (GenericEntityException e) {
253: throw new GenericServiceException(e.getMessage(), e);
254: }
255: return applicationId;
256: }
257:
258: private static GenericValue getApplicationSandbox(
259: GenericDelegator delegator, String applicationId)
260: throws GenericServiceException {
261: try {
262: GenericValue application = delegator.findByPrimaryKey(
263: "ApplicationSandbox", UtilMisc.toMap(
264: "applicationId", applicationId));
265: return application;
266: } catch (GenericEntityException ee) {
267: throw new GenericServiceException(ee.getMessage(), ee);
268: }
269: }
270:
271: private static Map getRunTimeContext(GenericDelegator delegator,
272: GenericValue runTimeData) throws GenericServiceException {
273: try {
274: return (Map) XmlSerializer.deserialize((String) runTimeData
275: .get("runtimeInfo"), delegator);
276: } catch (SerializeException se) {
277: throw new GenericServiceException(se.getMessage(), se);
278: } catch (ParserConfigurationException pe) {
279: throw new GenericServiceException(pe.getMessage(), pe);
280: } catch (SAXException se) {
281: throw new GenericServiceException(se.getMessage(), se);
282: } catch (IOException ioe) {
283: throw new GenericServiceException(ioe.getMessage(), ioe);
284: }
285: }
286:
287: private static void setRunTimeContext(GenericValue runTimeData,
288: Map context) throws GenericServiceException {
289: try {
290: runTimeData.set("runtimeInfo", XmlSerializer
291: .serialize(context));
292: runTimeData.store();
293: } catch (GenericEntityException ee) {
294: throw new GenericServiceException(ee.getMessage(), ee);
295: } catch (SerializeException se) {
296: throw new GenericServiceException(se.getMessage(), se);
297: } catch (IOException ioe) {
298: throw new GenericServiceException(ioe.getMessage(), ioe);
299: }
300: }
301:
302: private static GenericValue getRuntimeData(
303: GenericDelegator delegator, String applicationId)
304: throws GenericServiceException {
305: try {
306: GenericValue application = delegator.findByPrimaryKey(
307: "ApplicationSandbox", UtilMisc.toMap(
308: "applicationId", applicationId));
309: return application.getRelatedOne("RuntimeData");
310: } catch (GenericEntityException ee) {
311: throw new GenericServiceException(ee.getMessage(), ee);
312: }
313: }
314:
315: private static void getApplicationSignatures(
316: GenericDelegator delegator, GenericValue application,
317: Map contextSignature, Map resultSignature)
318: throws GenericEntityException {
319: Map expresions = null;
320: // look for the 1st application.
321: final GenericValue workEffort = delegator.findByPrimaryKey(
322: "WorkEffort", UtilMisc.toMap("workEffortId",
323: application.get("workEffortId")));
324: String packageId = (String) workEffort.get("workflowPackageId");
325: String packageVersion = (String) workEffort
326: .get("workflowPackageVersion");
327: String processId = (String) workEffort.get("workflowProcessId");
328: String processVersion = (String) workEffort
329: .get("workflowProcessVersion");
330: String activityId = (String) workEffort
331: .get("workflowActivityId");
332:
333: expresions = new HashMap();
334: expresions.putAll(UtilMisc.toMap("packageId", packageId));
335: expresions.putAll(UtilMisc.toMap("packageVersion",
336: packageVersion));
337: expresions.putAll(UtilMisc.toMap("processId", processId));
338: expresions.putAll(UtilMisc.toMap("processVersion",
339: processVersion));
340: expresions.putAll(UtilMisc.toMap("activityId", activityId));
341:
342: final Collection wfActivityTools = delegator.findByAnd(
343: "WorkflowActivityTool", expresions);
344: final GenericValue wfActivityTool = (GenericValue) wfActivityTools
345: .toArray()[0];
346:
347: packageId = (String) wfActivityTool.get("packageId");
348: packageVersion = (String) wfActivityTool.get("packageVersion");
349: processId = (String) wfActivityTool.get("processId");
350: processVersion = (String) wfActivityTool.get("processVersion");
351: final String applicationId = (String) wfActivityTool
352: .get("toolId");
353:
354: expresions = new HashMap();
355: expresions.putAll(UtilMisc.toMap("packageId", packageId));
356: expresions.putAll(UtilMisc.toMap("packageVersion",
357: packageVersion));
358: expresions.putAll(UtilMisc.toMap("processId", processId));
359: expresions.putAll(UtilMisc.toMap("processVersion",
360: processVersion));
361: expresions.putAll(UtilMisc
362: .toMap("applicationId", applicationId));
363:
364: final Collection params = delegator.findByAnd(
365: "WorkflowFormalParam", expresions);
366:
367: Iterator i = params.iterator();
368: while (i.hasNext()) {
369: GenericValue param = (GenericValue) i.next();
370: String name = param.getString("formalParamId");
371: String mode = param.getString("modeEnumId");
372: String type = param.getString("dataTypeEnumId");
373: if (mode.equals("WPM_IN") || mode.equals("WPM_INOUT"))
374: contextSignature.put(name, WfUtil.getJavaType(type));
375: else if (mode.equals("WPM_OUT") || mode.equals("WPM_INOUT"))
376: resultSignature.put(name, WfUtil.getJavaType(type));
377: }
378: }
379:
380: private static GenericValue getWorkEffortPartyAssigment(
381: GenericDelegator delegator, String workEffortId)
382: throws GenericServiceException {
383: Map expresions = new HashMap();
384: expresions.putAll(UtilMisc.toMap("workEffortId", workEffortId));
385: expresions.putAll(UtilMisc.toMap("statusId", "CAL_ACCEPTED"));
386: List orderBy = new ArrayList();
387: orderBy.add("-fromDate");
388:
389: try {
390: final Collection assigments = delegator.findByAnd(
391: "WorkEffortPartyAssignment", expresions, orderBy);
392: GenericValue assigment;
393: if (assigments.isEmpty()) {
394: Debug.logError(
395: "No accepted activities found for the workEffortId="
396: + workEffortId, module);
397: throw new GenericServiceException(
398: "Can not find WorkEffortPartyAssignment for the Workflow service. WorkEffortId="
399: + workEffortId);
400: }
401: if (assigments.size() != 1)
402: Debug.logWarning(
403: "More than one accepted activities found for the workEffortId="
404: + workEffortId, module);
405: return (GenericValue) assigments.iterator().next();
406: } catch (GenericEntityException ee) {
407: throw new GenericServiceException(ee.getMessage(), ee);
408: }
409: }
410: }
|