KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > workflow > WfApplicationServices


1 /*
2  * $Id: WfApplicationServices.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;
26
27 import java.io.IOException JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.GeneralException;
41 import org.ofbiz.base.util.ObjectType;
42 import org.ofbiz.base.util.UtilMisc;
43 import org.ofbiz.entity.GenericDelegator;
44 import org.ofbiz.entity.GenericEntityException;
45 import org.ofbiz.entity.GenericValue;
46 import org.ofbiz.entity.serialize.SerializeException;
47 import org.ofbiz.entity.serialize.XmlSerializer;
48 import org.ofbiz.service.DispatchContext;
49 import org.ofbiz.service.GenericServiceException;
50 import org.ofbiz.service.ModelService;
51 import org.xml.sax.SAXException JavaDoc;
52
53 /**
54  * Workflow Application Services - 'Services' and 'Workers' for interaction with Workflow Application API
55  *
56  * @author Manuel Soto & Oswin Ondarza
57  * @version $Rev: 5462 $
58  * @since 2.0
59  */

60 public class WfApplicationServices {
61
62     // -------------------------------------------------------------------
63
// Appication 'Service' Methods
64
// -------------------------------------------------------------------
65

66     public static final String JavaDoc module = WfApplicationServices.class.getName();
67
68     /**
69      * Activate an application by inserting expected arguments in
70      * the ApplicationSandbox table.
71      *
72      * Note: Assume that the activity (workEffort) has only one performer as
73      * defined in XPDL specification, this means that there is only one
74      * partyAssigment w/ CAL_ACEPTED state.
75      * @param ctx Service dispatch Context
76      * @param context Actual parameters
77      * @throws GenericServiceException
78      * @return Empty result
79      */

80     public static Map JavaDoc activateApplication(DispatchContext ctx, Map JavaDoc context) {
81         final String JavaDoc workEffortId = (String JavaDoc) context.get("workEffortId");
82         final Map JavaDoc result = new HashMap JavaDoc();
83
84         try {
85             final GenericValue weAssigment = getWorkEffortPartyAssigment(ctx.getDelegator(), workEffortId);
86
87             final String JavaDoc partyId = (String JavaDoc) weAssigment.get("partyId");
88             final String JavaDoc roleTypeId = (String JavaDoc) weAssigment.get("roleTypeId");
89             final Timestamp JavaDoc fromDate = (Timestamp JavaDoc) weAssigment.get("fromDate");
90             result.put("applicationId", insertAppSandbox(ctx.getDelegator(), workEffortId, partyId,
91                     roleTypeId, fromDate, context));
92             result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
93         } catch (GenericServiceException we) {
94             we.printStackTrace();
95             result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
96             result.put(ModelService.ERROR_MESSAGE, we.getMessage());
97         }
98
99         return result;
100     }
101
102     public static Map JavaDoc getApplicationContext(DispatchContext ctx, Map JavaDoc context) throws GenericServiceException {
103         final GenericDelegator delegator = ctx.getDelegator();
104         final Map JavaDoc result = new HashMap JavaDoc();
105         try {
106             result.put("applicationContext",
107                     getRunTimeContext(delegator, getRuntimeData(delegator, (String JavaDoc) context.get("applicationId"))));
108             result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
109         } catch (GenericServiceException we) {
110             we.printStackTrace();
111             result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
112             result.put(ModelService.ERROR_MESSAGE, we.getMessage());
113         }
114         return result;
115     }
116
117     public static Map JavaDoc completeApplication(DispatchContext ctx, Map JavaDoc context) throws GenericServiceException {
118         final GenericDelegator delegator = ctx.getDelegator();
119         final String JavaDoc applicationId = (String JavaDoc) context.get("applicationId");
120         final Map JavaDoc result = new HashMap JavaDoc();
121
122         GenericValue application = getApplicationSandbox(delegator, applicationId);
123         GenericValue runTimeData = getRuntimeData(delegator, applicationId);
124         Map JavaDoc runTimeContext = getRunTimeContext(delegator, runTimeData);
125         Map JavaDoc contextSignature = new HashMap JavaDoc();
126         Map JavaDoc resultSignature = new HashMap JavaDoc();
127         Map JavaDoc resultContext = new HashMap JavaDoc();
128         Map JavaDoc runContext = new HashMap JavaDoc(context);
129
130         try {
131             // copy all OUT & INOUT formal parameters
132
getApplicationSignatures(delegator, application, contextSignature, resultSignature);
133             for (Iterator JavaDoc names = resultSignature.keySet().iterator(); names.hasNext();) {
134                 String JavaDoc name = (String JavaDoc) names.next();
135                 Object JavaDoc value = null;
136                 if (runTimeContext.containsKey(name)
137                     && contextSignature.containsKey(name)
138                     && resultSignature.containsKey(name))
139                     value = runTimeContext.get(name);
140                 if (((Map JavaDoc) context.get("result")).containsKey(name))
141                     value = ((Map JavaDoc) context.get("result")).get(name);
142                 if (value != null)
143                     resultContext.put(name,
144                             ObjectType.simpleTypeConvert(value, (String JavaDoc) resultSignature.get(name), null, null));
145             }
146             runTimeContext.putAll(resultContext);
147             // fin de agregar
148
if (Debug.verboseOn()) {
149                 Debug.logVerbose("Completing Application: " + applicationId, module);
150                 Debug.logVerbose(" Result Signature: " + resultSignature.toString(), module);
151                 Debug.logVerbose(" Result Values: " + resultContext.toString(), module);
152
153             }
154
155             setRunTimeContext(runTimeData, runTimeContext);
156             // agregado por Oswin Ondarza
157

158             runContext.remove("applicationId");
159
160             final String JavaDoc workEffortId = (String JavaDoc) runTimeContext.get("workEffortId");
161             final Timestamp JavaDoc fromDate = (Timestamp JavaDoc) application.get("fromDate");
162             final String JavaDoc partyId = (String JavaDoc) application.get("partyId");
163             final String JavaDoc roleTypeId = (String JavaDoc) application.get("roleTypeId");
164
165             runContext.put("workEffortId", workEffortId);
166             runContext.put("fromDate", fromDate);
167             runContext.put("partyId", partyId);
168             runContext.put("roleTypeId", roleTypeId);
169             runContext.put("result", resultContext);
170
171             result.putAll(ctx.getDispatcher().runSync("wfCompleteAssignment", runContext));
172         } catch (GenericEntityException we) {
173             we.printStackTrace();
174             result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
175             result.put(ModelService.ERROR_MESSAGE, we.getMessage());
176         } catch (GenericServiceException we) {
177             we.printStackTrace();
178             result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
179             result.put(ModelService.ERROR_MESSAGE, we.getMessage());
180         } catch (GeneralException ge) {
181             ge.printStackTrace();
182             result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
183             result.put(ModelService.ERROR_MESSAGE, ge.getMessage());
184         }
185         return result;
186     }
187
188     private static String JavaDoc insertAppSandbox(GenericDelegator delegator, String JavaDoc workEffortId, String JavaDoc partyId,
189             String JavaDoc roleTypeId, Timestamp JavaDoc fromDate, Map JavaDoc context) throws GenericServiceException {
190         String JavaDoc dataId = null;
191         String JavaDoc infoId = null;
192         String JavaDoc applicationId = new String JavaDoc(new Long JavaDoc((new Date JavaDoc().getTime())).toString());
193         
194         try {
195             dataId = delegator.getNextSeqId("RuntimeData").toString();
196             GenericValue runtimeData = delegator.makeValue("RuntimeData", UtilMisc.toMap("runtimeDataId", dataId));
197             runtimeData.set("runtimeInfo", XmlSerializer.serialize(context));
198             delegator.create(runtimeData);
199         } catch (GenericEntityException ee) {
200             throw new GenericServiceException(ee.getMessage(), ee);
201         } catch (SerializeException se) {
202             throw new GenericServiceException(se.getMessage(), se);
203         } catch (IOException JavaDoc ioe) {
204             throw new GenericServiceException(ioe.getMessage(), ioe);
205         }
206         Map JavaDoc aFields = UtilMisc.toMap("applicationId", applicationId, "workEffortId", workEffortId,
207                 "partyId", partyId, "roleTypeId", roleTypeId, "fromDate", fromDate, "runtimeDataId", dataId);
208
209         GenericValue appV = null;
210         try {
211             appV = delegator.makeValue("ApplicationSandbox", aFields);
212             delegator.create(appV);
213         } catch (GenericEntityException e) {
214             throw new GenericServiceException(e.getMessage(), e);
215         }
216         return applicationId;
217     }
218
219     private static GenericValue getApplicationSandbox(GenericDelegator delegator, String JavaDoc applicationId)
220             throws GenericServiceException {
221         try {
222             GenericValue application =
223                 delegator.findByPrimaryKey("ApplicationSandbox", UtilMisc.toMap("applicationId", applicationId));
224             return application;
225         } catch (GenericEntityException ee) {
226             throw new GenericServiceException(ee.getMessage(), ee);
227         }
228     }
229
230     private static Map JavaDoc getRunTimeContext(GenericDelegator delegator, GenericValue runTimeData)
231             throws GenericServiceException {
232         try {
233             return (Map JavaDoc) XmlSerializer.deserialize((String JavaDoc) runTimeData.get("runtimeInfo"), delegator);
234         } catch (SerializeException se) {
235             throw new GenericServiceException(se.getMessage(), se);
236         } catch (ParserConfigurationException JavaDoc pe) {
237             throw new GenericServiceException(pe.getMessage(), pe);
238         } catch (SAXException JavaDoc se) {
239             throw new GenericServiceException(se.getMessage(), se);
240         } catch (IOException JavaDoc ioe) {
241             throw new GenericServiceException(ioe.getMessage(), ioe);
242         }
243     }
244
245     private static void setRunTimeContext(GenericValue runTimeData, Map JavaDoc context) throws GenericServiceException {
246         try {
247             runTimeData.set("runtimeInfo", XmlSerializer.serialize(context));
248             runTimeData.store();
249         } catch (GenericEntityException ee) {
250             throw new GenericServiceException(ee.getMessage(), ee);
251         } catch (SerializeException se) {
252             throw new GenericServiceException(se.getMessage(), se);
253         } catch (IOException JavaDoc ioe) {
254             throw new GenericServiceException(ioe.getMessage(), ioe);
255         }
256     }
257
258     private static GenericValue getRuntimeData(GenericDelegator delegator, String JavaDoc applicationId)
259             throws GenericServiceException {
260         try {
261             GenericValue application =
262                 delegator.findByPrimaryKey("ApplicationSandbox", UtilMisc.toMap("applicationId", applicationId));
263             return application.getRelatedOne("RuntimeData");
264         } catch (GenericEntityException ee) {
265             throw new GenericServiceException(ee.getMessage(), ee);
266         }
267     }
268
269     private static void getApplicationSignatures(GenericDelegator delegator, GenericValue application,
270             Map JavaDoc contextSignature, Map JavaDoc resultSignature) throws GenericEntityException {
271         Map JavaDoc expresions = null;
272         // look for the 1st application.
273
final GenericValue workEffort =
274             delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", application.get("workEffortId")));
275         String JavaDoc packageId = (String JavaDoc) workEffort.get("workflowPackageId");
276         String JavaDoc packageVersion = (String JavaDoc) workEffort.get("workflowPackageVersion");
277         String JavaDoc processId = (String JavaDoc) workEffort.get("workflowProcessId");
278         String JavaDoc processVersion = (String JavaDoc) workEffort.get("workflowProcessVersion");
279         String JavaDoc activityId = (String JavaDoc) workEffort.get("workflowActivityId");
280
281         expresions = new HashMap JavaDoc();
282         expresions.putAll(UtilMisc.toMap("packageId", packageId));
283         expresions.putAll(UtilMisc.toMap("packageVersion", packageVersion));
284         expresions.putAll(UtilMisc.toMap("processId", processId));
285         expresions.putAll(UtilMisc.toMap("processVersion", processVersion));
286         expresions.putAll(UtilMisc.toMap("activityId", activityId));
287
288         final Collection JavaDoc wfActivityTools = delegator.findByAnd("WorkflowActivityTool", expresions);
289         final GenericValue wfActivityTool = (GenericValue) wfActivityTools.toArray()[0];
290
291         packageId = (String JavaDoc) wfActivityTool.get("packageId");
292         packageVersion = (String JavaDoc) wfActivityTool.get("packageVersion");
293         processId = (String JavaDoc) wfActivityTool.get("processId");
294         processVersion = (String JavaDoc) wfActivityTool.get("processVersion");
295         final String JavaDoc applicationId = (String JavaDoc) wfActivityTool.get("toolId");
296
297         expresions = new HashMap JavaDoc();
298         expresions.putAll(UtilMisc.toMap("packageId", packageId));
299         expresions.putAll(UtilMisc.toMap("packageVersion", packageVersion));
300         expresions.putAll(UtilMisc.toMap("processId", processId));
301         expresions.putAll(UtilMisc.toMap("processVersion", processVersion));
302         expresions.putAll(UtilMisc.toMap("applicationId", applicationId));
303
304         final Collection JavaDoc params = delegator.findByAnd("WorkflowFormalParam", expresions);
305
306         Iterator JavaDoc i = params.iterator();
307         while (i.hasNext()) {
308             GenericValue param = (GenericValue) i.next();
309             String JavaDoc name = param.getString("formalParamId");
310             String JavaDoc mode = param.getString("modeEnumId");
311             String JavaDoc type = param.getString("dataTypeEnumId");
312             if (mode.equals("WPM_IN") || mode.equals("WPM_INOUT"))
313                 contextSignature.put(name, WfUtil.getJavaType(type));
314             else if (mode.equals("WPM_OUT") || mode.equals("WPM_INOUT"))
315                 resultSignature.put(name, WfUtil.getJavaType(type));
316         }
317     }
318
319     private static GenericValue getWorkEffortPartyAssigment(GenericDelegator delegator, String JavaDoc workEffortId)
320             throws GenericServiceException {
321         Map JavaDoc expresions = new HashMap JavaDoc();
322         expresions.putAll(UtilMisc.toMap("workEffortId", workEffortId));
323         expresions.putAll(UtilMisc.toMap("statusId", "CAL_ACCEPTED"));
324         List JavaDoc orderBy = new ArrayList JavaDoc();
325         orderBy.add("-fromDate");
326
327         try {
328             final Collection JavaDoc assigments = delegator.findByAnd("WorkEffortPartyAssignment", expresions, orderBy);
329             GenericValue assigment;
330             if (assigments.isEmpty()) {
331                 Debug.logError("No accepted activities found for the workEffortId=" + workEffortId, module);
332                 throw new GenericServiceException("Can not find WorkEffortPartyAssignment for the Workflow service. WorkEffortId=" + workEffortId);
333             }
334             if (assigments.size() != 1)
335                 Debug.logWarning("More than one accepted activities found for the workEffortId=" + workEffortId, module);
336             return (GenericValue) assigments.iterator().next();
337         } catch (GenericEntityException ee) {
338             throw new GenericServiceException(ee.getMessage(), ee);
339         }
340     }
341 }
342
Popular Tags