1 25 package org.ofbiz.workflow.impl; 26 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Locale ; 33 34 import org.ofbiz.base.util.Debug; 35 import org.ofbiz.entity.GenericEntityException; 36 import org.ofbiz.entity.GenericValue; 37 import org.ofbiz.service.GenericRequester; 38 import org.ofbiz.service.GenericServiceException; 39 import org.ofbiz.service.ModelService; 40 import org.ofbiz.workflow.InvalidPerformer; 41 import org.ofbiz.workflow.SourceNotAvailable; 42 import org.ofbiz.workflow.WfEventAudit; 43 import org.ofbiz.workflow.WfException; 44 import org.ofbiz.workflow.WfProcess; 45 import org.ofbiz.workflow.WfProcessMgr; 46 import org.ofbiz.workflow.WfRequester; 47 48 55 public class WfRequesterImpl implements WfRequester { 56 57 public static final String module = WfRequesterImpl.class.getName(); 58 59 protected Map performers = null; 60 61 64 public WfRequesterImpl() { 65 this.performers = new HashMap (); 66 } 67 68 71 public void registerProcess(WfProcess process, Map context, GenericRequester requester) throws WfException { 72 if (process == null) 73 throw new WfException("Process cannot be null"); 74 if (context == null) 75 throw new WfException("Context should not be null"); 76 77 performers.put(process, requester); 78 WfProcessMgr mgr = process.manager(); 79 80 try { 82 if (Debug.verboseOn()) Debug.logVerbose("Validating w/ signature: " + mgr.contextSignature(), module); 83 ModelService.validate(mgr.contextSignature(), context, true, null, ModelService.IN_PARAM, Locale.getDefault()); 84 } catch (GenericServiceException e) { 85 throw new WfException("Context passed does not validate against defined signature: ", e); 86 } 87 88 Map localContext = new HashMap (context); 90 localContext.putAll(mgr.getInitialContext()); 91 process.setProcessContext(localContext); 92 93 GenericValue processDefinition = process.getDefinitionObject(); 95 String sourceReferenceField = processDefinition.getString("sourceReferenceField"); 96 if (context.containsKey(sourceReferenceField)) { 97 GenericValue processObj = process.getRuntimeObject(); 98 if (processObj != null) { 99 try { 100 processObj.set("sourceReferenceId", localContext.get(sourceReferenceField)); 101 processObj.store(); 102 } catch (GenericEntityException e) { 103 throw new WfException("Cannot set sourceReferenceId on the process runtime object", e); 104 } 105 } 106 } 107 108 } 109 110 113 public int howManyPerformer() throws WfException { 114 return performers.size(); 115 } 116 117 120 public Iterator getIteratorPerformer() throws WfException { 121 return performers.keySet().iterator(); 122 } 123 124 127 public List getSequencePerformer(int maxNumber) throws WfException { 128 if (maxNumber > 0) 129 return new ArrayList (performers.keySet()).subList(0, (maxNumber - 1)); 130 return new ArrayList (performers.keySet()); 131 } 132 133 136 public boolean isMemberOfPerformer(WfProcess member) throws WfException { 137 return performers.containsKey(member); 138 } 139 140 143 public synchronized void receiveEvent(WfEventAudit event) throws WfException, InvalidPerformer { 144 WfProcess process = null; 146 147 try { 148 process = (WfProcess) event.source(); 149 } catch (SourceNotAvailable sna) { 150 throw new InvalidPerformer("Could not get the performer", sna); 151 } catch (ClassCastException cce) { 152 throw new InvalidPerformer("Not a valid process object", cce); 153 } 154 if (process == null) 155 throw new InvalidPerformer("No performer specified"); 156 if (!performers.containsKey(process)) 157 throw new InvalidPerformer("Performer not assigned to this requester"); 158 159 GenericRequester req = null; 160 161 if (performers.containsKey(process)) 162 req = (GenericRequester) performers.get(process); 163 if (req != null) 164 req.receiveResult(process.result()); 165 } 166 } 167 168 | Popular Tags |