1 25 package org.ofbiz.workflow.impl; 26 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.GeneralException; 37 import org.ofbiz.base.util.ObjectType; 38 import org.ofbiz.base.util.UtilMisc; 39 import org.ofbiz.entity.GenericDelegator; 40 import org.ofbiz.entity.GenericEntityException; 41 import org.ofbiz.entity.GenericValue; 42 import org.ofbiz.entity.util.EntityUtil; 43 import org.ofbiz.workflow.CannotChangeRequester; 44 import org.ofbiz.workflow.InvalidRequester; 45 import org.ofbiz.workflow.NotEnabled; 46 import org.ofbiz.workflow.RequesterRequired; 47 import org.ofbiz.workflow.TransitionNotAllowed; 48 import org.ofbiz.workflow.WfException; 49 import org.ofbiz.workflow.WfFactory; 50 import org.ofbiz.workflow.WfProcess; 51 import org.ofbiz.workflow.WfProcessMgr; 52 import org.ofbiz.workflow.WfRequester; 53 import org.ofbiz.workflow.WfUtil; 54 55 63 public class WfProcessMgrImpl implements WfProcessMgr { 64 65 public static final String module = WfProcessMgrImpl.class.getName(); 66 67 protected GenericValue processDef; 68 69 protected String state; protected List processList; 72 protected Map contextSignature = null; 73 protected Map resultSignature = null; 74 protected Map initialContext = null; 75 76 85 public WfProcessMgrImpl(GenericDelegator delegator, String packageId, String packageVersion, 86 String processId, String processVersion) throws WfException { 87 Map finder = UtilMisc.toMap("packageId", packageId, "processId", processId); 88 List order = UtilMisc.toList("-packageVersion", "-processVersion"); 89 90 if (packageVersion != null) finder.put("packageVersion", packageVersion); 91 if (processVersion != null) finder.put("processVersion", processVersion); 92 try { 93 List processes = delegator.findByAnd("WorkflowProcess", finder, order); 94 if (processes.size() == 0) 95 throw new WfException("No process definition found for the specified processId"); 96 else 97 processDef = EntityUtil.getFirst(processes); 98 } catch (GenericEntityException e) { 99 throw new WfException("Problems getting the process definition from the WorkflowProcess entity"); 100 } 101 102 buildSignatures(); 103 buildInitialContext(); 104 processList = new ArrayList (); 105 state = "enabled"; 106 if (Debug.infoOn()) Debug.logInfo("[WfProcessMgr.init] : Create process manager (" + 107 packageId + "[" + packageVersion + "]" + " / " + processId + "[" + processVersion + "]" + ")", module); 108 } 109 110 113 public void setProcessMgrState(String newState) throws WfException, TransitionNotAllowed { 114 if (!newState.equals("enabled") || !newState.equals("disabled")) 115 throw new TransitionNotAllowed(); 116 this.state = newState; 117 } 118 119 122 public List getSequenceProcess(int maxNumber) throws WfException { 123 if (maxNumber > 0) 124 return new ArrayList (processList.subList(0, maxNumber - 1)); 125 return processList; 126 } 127 128 131 public WfProcess createProcess(WfRequester requester) throws WfException, NotEnabled, 132 InvalidRequester, RequesterRequired { 133 if (state.equals("disabled")) 134 throw new NotEnabled(); 135 136 if (requester == null) 137 throw new RequesterRequired(); 138 139 WfProcess process = WfFactory.getWfProcess(processDef, this); 141 142 try { 143 process.setRequester(requester); 144 } catch (CannotChangeRequester ccr) { 145 throw new WfException(ccr.getMessage(), ccr); 146 } 147 processList.add(process); 148 Debug.logVerbose("[WfProcessMgr.createProcess] : Process created.", module); 149 return process; 150 } 151 152 155 public Map contextSignature() throws WfException { 156 return this.contextSignature; 157 } 158 159 162 public int howManyProcess() throws WfException { 163 return processList.size(); 164 } 165 166 169 public List processMgrStateType() throws WfException { 170 String [] list = {"enabled", "disabled"}; 171 return Arrays.asList(list); 172 } 173 174 177 public String category() throws WfException { 178 return processDef.getString("category"); 179 } 180 181 184 public String version() throws WfException { 185 return processDef.getString("version"); 186 } 187 188 191 public String description() throws WfException { 192 return processDef.getString("description"); 193 } 194 195 198 public String name() throws WfException { 199 return processDef.getString("name"); 200 } 201 202 205 public Map resultSignature() throws WfException { 206 return this.resultSignature; 207 } 208 209 213 public Map getInitialContext() { 214 return initialContext; 215 } 216 217 220 public boolean isMemberOfProcess(WfProcess member) throws WfException { 221 return processList.contains(member); 222 } 223 224 227 public Iterator getIteratorProcess() throws WfException { 228 return processList.iterator(); 229 } 230 231 private void buildSignatures() throws WfException { 233 contextSignature = new HashMap (); 234 resultSignature = new HashMap (); 235 Collection params = null; 236 237 try { 238 Map fields = new HashMap (); 239 240 fields.put("packageId", processDef.getString("packageId")); 241 fields.put("packageVersion", processDef.getString("packageVersion")); 242 fields.put("processId", processDef.getString("processId")); 243 fields.put("processVersion", processDef.getString("processVersion")); 244 fields.put("applicationId", "_NA_"); 245 params = processDef.getDelegator().findByAnd("WorkflowFormalParam", fields); 246 247 } catch (GenericEntityException e) { 248 throw new WfException(e.getMessage(), e); 249 } 250 if (params == null) 251 return; 252 253 Iterator i = params.iterator(); 254 while (i.hasNext()) { 255 GenericValue param = (GenericValue) i.next(); 256 String name = param.getString("formalParamId"); 257 String mode = param.getString("modeEnumId"); 258 String type = param.getString("dataTypeEnumId"); 259 260 if (mode.equals("WPM_IN") || mode.equals("WPM_INOUT")) 261 contextSignature.put(name, WfUtil.getJavaType(type)); 262 else if (mode.equals("WPM_OUT") || mode.equals("WPM_INOUT")) 263 resultSignature.put(name, WfUtil.getJavaType(type)); 264 } 265 } 266 267 private void buildInitialContext() throws WfException { 268 GenericDelegator delegator = processDef.getDelegator(); 269 this.initialContext = new HashMap (); 270 List dataFields = new ArrayList (); 271 try { 272 Map fields = new HashMap (); 274 fields.put("packageId", processDef.get("packageId")); 275 fields.put("packageVersion", processDef.get("packageVersion")); 276 277 fields.put("processId", "_NA_"); 279 fields.put("processVersion", "_NA_"); 280 List data1 = delegator.findByAnd("WorkflowDataField", fields); 281 dataFields.addAll(data1); 282 283 fields.put("processId", processDef.get("processId")); 285 fields.put("processVersion", processDef.get("processVersion")); 286 List data2 = delegator.findByAnd("WorkflowDataField", fields); 287 dataFields.addAll(data2); 288 } catch (GenericEntityException e) { 289 throw new WfException(e.getMessage(), e); 290 } 291 if (dataFields == null) 292 return; 293 294 Iterator i = dataFields.iterator(); 295 296 while (i.hasNext()) { 297 GenericValue dataField = (GenericValue) i.next(); 298 String name = dataField.getString("dataFieldName"); 299 String type = dataField.getString("dataTypeEnumId"); 300 String value = dataField.getString("initialValue"); 301 302 try { 303 initialContext.put(name, ObjectType.simpleTypeConvert(value, WfUtil.getJavaType(type), null, null)); 304 } catch (GeneralException e) { 305 throw new WfException(e.getMessage(), e); 306 } 307 } 308 } 309 } 310 311 | Popular Tags |