KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: WfActivityToolImplementation.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.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.StringUtil;
36 import org.ofbiz.entity.GenericEntityException;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.service.GenericResultWaiter;
39 import org.ofbiz.service.ModelService;
40 import org.ofbiz.workflow.WfException;
41
42 /**
43  * WfActivityToolImplementation.java
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @author Oswin Ondarza and Manuel Soto
47  * @version $Rev: 5462 $
48  * @since 2.0
49  */

50 public class WfActivityToolImplementation extends WfActivityAbstractImplementation {
51
52     public static final String JavaDoc module = WfActivityToolImplementation.class.getName();
53
54     public WfActivityToolImplementation(WfActivityImpl wfActivity) {
55         super(wfActivity);
56     }
57
58     /**
59      * @see org.ofbiz.workflow.impl.WfActivityAbstractImplementation#run()
60      */

61     public void run() throws WfException {
62         Collection JavaDoc tools = null;
63         //Linea agregada por Oswin Ondarza
64
String JavaDoc allParams = "";
65         try {
66             tools = getActivity().getDefinitionObject().getRelated("WorkflowActivityTool");
67         } catch (GenericEntityException e) {
68             throw new WfException(e.getMessage(), e);
69         }
70         if (tools == null) {
71             setComplete(true);
72             return; // Null tools mean nothing to do (same as route?)
73
}
74
75         if (Debug.verboseOn())
76             Debug.logVerbose("[WfActivity.runTool] : Running tools (" + tools.size() + ").", module);
77         List JavaDoc waiters = new ArrayList JavaDoc();
78         Iterator JavaDoc i = tools.iterator();
79         while (i.hasNext()) {
80             GenericValue thisTool = (GenericValue) i.next();
81             String JavaDoc serviceName = null;
82             String JavaDoc toolId = thisTool.getString("toolId");
83             String JavaDoc params = thisTool.getString("actualParameters");
84             String JavaDoc toolTypeEnumId = thisTool.getString("toolTypeEnumId");
85             
86             //Linea agregada por Oswin Ondarza
87
allParams = allParams + "," + params;
88             String JavaDoc extend = thisTool.getString("extendedAttributes");
89             
90             Map JavaDoc extendedAttr = StringUtil.strToMap(extend);
91             if (extendedAttr != null && extendedAttr.containsKey("serviceName"))
92                 serviceName = (String JavaDoc) extendedAttr.get("serviceName");
93                 
94             serviceName = serviceName != null ? serviceName : (toolTypeEnumId.equals("WTT_APPLICATION") ?
95                     "wfActivateApplication" : toolId);
96             waiters.add(runService(serviceName, params, extend));
97         }
98
99         while (waiters.size() > 0) {
100             Iterator JavaDoc wi = waiters.iterator();
101             Collection JavaDoc remove = new ArrayList JavaDoc();
102             while (wi.hasNext()) {
103                 GenericResultWaiter thw = (GenericResultWaiter) wi.next();
104                 
105                 if (thw.isCompleted()) {
106                     Map JavaDoc thwResult = null;
107                     if (thw.status() == GenericResultWaiter.SERVICE_FINISHED) {
108                         thwResult = thw.getResult();
109                         Debug.logVerbose("Service finished.", module);
110                     } else if (thw.status() == GenericResultWaiter.SERVICE_FAILED) {
111                         Debug.logError(thw.getThrowable(), "Service failed", module);
112                     }
113                     if (thwResult != null && thwResult.containsKey(ModelService.RESPONSE_MESSAGE)) {
114                         if (thwResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) {
115                             String JavaDoc errorMsg = (String JavaDoc) thwResult.remove(ModelService.ERROR_MESSAGE);
116                             Debug.logError("Service Error: " + errorMsg, module);
117                         }
118                         thwResult.remove(ModelService.RESPONSE_MESSAGE);
119                     }
120                     
121                     try {
122                         if (thwResult != null)
123                             this.setResult(thwResult, allParams);
124                     } catch (IllegalStateException JavaDoc e) {
125                         throw new WfException("Unknown error", e);
126                     }
127                     remove.add(thw);
128                 }
129             }
130             waiters.removeAll(remove);
131         }
132         
133         setComplete(true);
134     }
135
136     protected void setResult(Map JavaDoc result, String JavaDoc allParams) {
137         Map JavaDoc newResult = new HashMap JavaDoc(result);
138         List JavaDoc params = StringUtil.split(allParams, ",");
139         Iterator JavaDoc i = params.iterator();
140         while (i.hasNext()) {
141             Object JavaDoc keyExpr = i.next();
142             String JavaDoc keyExprStr = (String JavaDoc) keyExpr;
143
144             if (keyExprStr != null && keyExprStr.trim().toLowerCase().startsWith("name:")) {
145                 List JavaDoc couple = StringUtil.split(keyExprStr.trim().substring(5).trim(), "=");
146                 Object JavaDoc keyParam = ((String JavaDoc) couple.get(0)).trim();
147                 Object JavaDoc keyNewParam = ((String JavaDoc) couple.get(1)).trim();
148
149                 if (result.containsKey(keyParam)) {
150                     newResult.put(keyNewParam, result.get(keyParam));
151                     newResult.remove(keyParam);
152
153                 }
154             }
155         }
156         if (Debug.verboseOn()) Debug.logVerbose("Setting result in context: " + newResult, module);
157         this.setResult(newResult);
158     }
159 }
160
Popular Tags