001: /*
002: * $Id: WfActivityToolImplementation.java,v 1.3 2003/11/26 07:24:17 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.impl;
026:
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033:
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.base.util.StringUtil;
036: import org.ofbiz.entity.GenericEntityException;
037: import org.ofbiz.entity.GenericValue;
038: import org.ofbiz.service.GenericResultWaiter;
039: import org.ofbiz.service.ModelService;
040: import org.ofbiz.workflow.WfException;
041:
042: /**
043: * WfActivityToolImplementation.java
044: *
045: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
046: * @author Oswin Ondarza and Manuel Soto
047: * @version $Revision: 1.3 $
048: * @since 2.0
049: */
050: public class WfActivityToolImplementation extends
051: WfActivityAbstractImplementation {
052:
053: public static final String module = WfActivityToolImplementation.class
054: .getName();
055:
056: public WfActivityToolImplementation(WfActivityImpl wfActivity) {
057: super (wfActivity);
058: }
059:
060: /**
061: * @see org.ofbiz.workflow.impl.WfActivityAbstractImplementation#run()
062: */
063: public void run() throws WfException {
064: Collection tools = null;
065: //Linea agregada por Oswin Ondarza
066: String allParams = "";
067: try {
068: tools = getActivity().getDefinitionObject().getRelated(
069: "WorkflowActivityTool");
070: } catch (GenericEntityException e) {
071: throw new WfException(e.getMessage(), e);
072: }
073: if (tools == null) {
074: setComplete(true);
075: return; // Null tools mean nothing to do (same as route?)
076: }
077:
078: if (Debug.verboseOn())
079: Debug.logVerbose("[WfActivity.runTool] : Running tools ("
080: + tools.size() + ").", module);
081: List waiters = new ArrayList();
082: Iterator i = tools.iterator();
083: while (i.hasNext()) {
084: GenericValue this Tool = (GenericValue) i.next();
085: String serviceName = null;
086: String toolId = this Tool.getString("toolId");
087: String params = this Tool.getString("actualParameters");
088: String toolTypeEnumId = this Tool
089: .getString("toolTypeEnumId");
090:
091: //Linea agregada por Oswin Ondarza
092: allParams = allParams + "," + params;
093: String extend = this Tool.getString("extendedAttributes");
094:
095: Map extendedAttr = StringUtil.strToMap(extend);
096: if (extendedAttr != null
097: && extendedAttr.containsKey("serviceName"))
098: serviceName = (String) extendedAttr.get("serviceName");
099:
100: serviceName = serviceName != null ? serviceName
101: : (toolTypeEnumId.equals("WTT_APPLICATION") ? "wfActivateApplication"
102: : toolId);
103: waiters.add(runService(serviceName, params, extend));
104: }
105:
106: while (waiters.size() > 0) {
107: Iterator wi = waiters.iterator();
108: Collection remove = new ArrayList();
109: while (wi.hasNext()) {
110: GenericResultWaiter thw = (GenericResultWaiter) wi
111: .next();
112:
113: if (thw.isCompleted()) {
114: Map thwResult = null;
115: if (thw.status() == GenericResultWaiter.SERVICE_FINISHED) {
116: thwResult = thw.getResult();
117: Debug.logVerbose("Service finished.", module);
118: } else if (thw.status() == GenericResultWaiter.SERVICE_FAILED) {
119: Debug.logError(thw.getThrowable(),
120: "Service failed", module);
121: }
122: if (thwResult != null
123: && thwResult
124: .containsKey(ModelService.RESPONSE_MESSAGE)) {
125: if (thwResult
126: .get(ModelService.RESPONSE_MESSAGE)
127: .equals(ModelService.RESPOND_ERROR)) {
128: String errorMsg = (String) thwResult
129: .remove(ModelService.ERROR_MESSAGE);
130: Debug.logError(
131: "Service Error: " + errorMsg,
132: module);
133: }
134: thwResult.remove(ModelService.RESPONSE_MESSAGE);
135: }
136:
137: try {
138: if (thwResult != null)
139: this .setResult(thwResult, allParams);
140: } catch (IllegalStateException e) {
141: throw new WfException("Unknown error", e);
142: }
143: remove.add(thw);
144: }
145: }
146: waiters.removeAll(remove);
147: }
148:
149: setComplete(true);
150: }
151:
152: protected void setResult(Map result, String allParams) {
153: Map newResult = new HashMap(result);
154: List params = StringUtil.split(allParams, ",");
155: Iterator i = params.iterator();
156: while (i.hasNext()) {
157: Object keyExpr = i.next();
158: String keyExprStr = (String) keyExpr;
159:
160: if (keyExprStr != null
161: && keyExprStr.trim().toLowerCase().startsWith(
162: "name:")) {
163: List couple = StringUtil.split(keyExprStr.trim()
164: .substring(5).trim(), "=");
165: Object keyParam = ((String) couple.get(0)).trim();
166: Object keyNewParam = ((String) couple.get(1)).trim();
167:
168: if (result.containsKey(keyParam)) {
169: newResult.put(keyNewParam, result.get(keyParam));
170: newResult.remove(keyParam);
171:
172: }
173: }
174: }
175: if (Debug.verboseOn())
176: Debug.logVerbose("Setting result in context: " + newResult,
177: module);
178: this.setResult(newResult);
179: }
180: }
|