1 25 package org.ofbiz.workflow.impl; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 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 50 public class WfActivityToolImplementation extends WfActivityAbstractImplementation { 51 52 public static final String module = WfActivityToolImplementation.class.getName(); 53 54 public WfActivityToolImplementation(WfActivityImpl wfActivity) { 55 super(wfActivity); 56 } 57 58 61 public void run() throws WfException { 62 Collection tools = null; 63 String 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; } 74 75 if (Debug.verboseOn()) 76 Debug.logVerbose("[WfActivity.runTool] : Running tools (" + tools.size() + ").", module); 77 List waiters = new ArrayList (); 78 Iterator i = tools.iterator(); 79 while (i.hasNext()) { 80 GenericValue thisTool = (GenericValue) i.next(); 81 String serviceName = null; 82 String toolId = thisTool.getString("toolId"); 83 String params = thisTool.getString("actualParameters"); 84 String toolTypeEnumId = thisTool.getString("toolTypeEnumId"); 85 86 allParams = allParams + "," + params; 88 String extend = thisTool.getString("extendedAttributes"); 89 90 Map extendedAttr = StringUtil.strToMap(extend); 91 if (extendedAttr != null && extendedAttr.containsKey("serviceName")) 92 serviceName = (String ) 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 wi = waiters.iterator(); 101 Collection remove = new ArrayList (); 102 while (wi.hasNext()) { 103 GenericResultWaiter thw = (GenericResultWaiter) wi.next(); 104 105 if (thw.isCompleted()) { 106 Map 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 errorMsg = (String ) 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 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 result, String allParams) { 137 Map newResult = new HashMap (result); 138 List params = StringUtil.split(allParams, ","); 139 Iterator i = params.iterator(); 140 while (i.hasNext()) { 141 Object keyExpr = i.next(); 142 String keyExprStr = (String ) keyExpr; 143 144 if (keyExprStr != null && keyExprStr.trim().toLowerCase().startsWith("name:")) { 145 List couple = StringUtil.split(keyExprStr.trim().substring(5).trim(), "="); 146 Object keyParam = ((String ) couple.get(0)).trim(); 147 Object keyNewParam = ((String ) 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 |