1 25 package org.ofbiz.workflow.impl; 26 27 import java.sql.Timestamp ; 28 import java.util.Date ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 import org.ofbiz.base.util.Debug; 34 import org.ofbiz.base.util.UtilDateTime; 35 import org.ofbiz.entity.GenericEntityException; 36 import org.ofbiz.entity.GenericValue; 37 import org.ofbiz.workflow.CannotComplete; 38 import org.ofbiz.workflow.InvalidResource; 39 import org.ofbiz.workflow.WfActivity; 40 import org.ofbiz.workflow.WfAssignment; 41 import org.ofbiz.workflow.WfException; 42 import org.ofbiz.workflow.WfResource; 43 44 51 public class WfAssignmentImpl implements WfAssignment { 52 53 public static final String module = WfAssignmentImpl.class.getName(); 54 55 protected WfActivity activity = null; 56 protected WfResource resource = null; 57 protected Timestamp fromDate = null; 58 protected boolean create = false; 59 60 66 public WfAssignmentImpl(WfActivity activity, WfResource resource, Timestamp fromDate, boolean create) throws WfException { 67 this.activity = activity; 68 this.resource = resource; 69 this.fromDate = fromDate; 70 this.create = create; 71 checkAssignment(); 72 } 73 74 private void checkAssignment() throws WfException { 76 String workEffortId = activity.runtimeKey(); 77 String partyId = resource.resourcePartyId(); 78 String roleTypeId = resource.resourceRoleId(); 79 80 if (workEffortId == null) 81 throw new WfException("WorkEffort could not be found for assignment"); 82 if (partyId == null && roleTypeId == null) 83 throw new WfException("Both party and role type IDs cannot be null"); 84 if (fromDate == null) 85 throw new WfException("From date cannot be null"); 86 87 GenericValue value = null; 88 Map fields = new HashMap (); 89 90 fields.put("workEffortId", workEffortId); 91 fields.put("partyId", partyId); 92 fields.put("roleTypeId", roleTypeId); 93 fields.put("fromDate", fromDate); 94 fields.put("statusId", "CAL_SENT"); 95 96 try { 98 if (valueObject() != null) { 99 Debug.logVerbose("[WfAssignment.checkAssignment] : found existing assignment.", module); 100 return; 101 } 102 } catch (WfException e) { 103 Debug.logVerbose("[WfAssignment.checkAssignment] : no existing assignment.", module); 104 } 105 106 if (create) { 107 try { 109 GenericValue v = activity.getDelegator().makeValue("WorkEffortPartyAssignment", fields); 110 111 value = activity.getDelegator().create(v); 112 Debug.logVerbose("[WfAssignment.checkAssignment] : created new party assignment : " + v, module); 113 } catch (GenericEntityException e) { 114 throw new WfException(e.getMessage(), e); 115 } 116 if (value == null) 117 throw new WfException("Could not create the assignement"); 118 } 119 if (value == null) 120 throw new WfException("No existing assignment found or create failed"); 121 } 122 123 126 public void accept() throws WfException { 127 boolean allDelegated = true; 128 boolean acceptAll = activity.getDefinitionObject().get("acceptAllAssignments") != null ? 129 activity.getDefinitionObject().getBoolean("acceptAllAssignments").booleanValue() : false; 130 131 if (!acceptAll) { 132 if (!activity.state().equals("open.not_running.not_started")) { 134 Iterator ai = activity.getIteratorAssignment(); 136 137 while (ai.hasNext() && allDelegated) { 138 WfAssignment a = (WfAssignment) ai.next(); 139 if (!a.equals(this) && !a.status().equals("CAL_DELEGATED")) { 140 allDelegated = false; 141 } 142 } 143 if (!allDelegated) { 145 throw new WfException("Cannot accept; Activity already running with active assignments"); 146 } 147 } else { 148 Debug.logVerbose("[WfAssignment.accept] : setting other assignments to delegated status.", module); 150 Iterator ai = activity.getIteratorAssignment(); 151 152 while (ai.hasNext()) { 153 WfAssignment a = (WfAssignment) ai.next(); 154 if (!this.isEqual(a)) a.delegate(); 155 } 156 } 157 } 158 changeStatus("CAL_ACCEPTED"); 160 } 161 162 165 public void setResult(Map results) throws WfException { 166 activity.setResult(results); 167 } 168 169 172 public void complete() throws WfException { 173 changeStatus("CAL_COMPLETED"); 174 try { 175 activity.complete(); 176 } catch (CannotComplete e) { 177 Debug.logWarning("Activity not complete : " + e.getMessage(), module); 178 } 179 } 180 181 184 public void delegate() throws WfException { 185 if (status().equals("CAL_DELEGATED")) 187 throw new WfException("Assignment has already been delegated"); 188 189 GenericValue valueObject = valueObject(); 191 try { 192 valueObject.set("thruDate", UtilDateTime.nowTimestamp()); 193 valueObject.store(); 194 if (Debug.verboseOn()) Debug.logVerbose("[WfAssignment.delegated()] : set the thru-date.", module); 195 } catch (GenericEntityException e) { 196 e.printStackTrace(); 197 throw new WfException(e.getMessage(), e); 198 } 199 200 changeStatus("CAL_DELEGATED"); 202 } 203 204 207 public void changeStatus(String status) throws WfException { 208 GenericValue valueObject = valueObject(); 210 try { 211 valueObject.set("statusId", status); 212 valueObject.store(); 213 if (Debug.verboseOn()) Debug.logVerbose("[WfAssignment.changeStatus] : changed status to " + status, module); 214 } catch (GenericEntityException e) { 215 e.printStackTrace(); 216 throw new WfException(e.getMessage(), e); 217 } 218 } 219 220 223 public WfActivity activity() throws WfException { 224 return activity; 225 } 226 227 230 public WfResource assignee() throws WfException { 231 return resource; 232 } 233 234 237 public void setAssignee(WfResource newValue) throws WfException, InvalidResource { 238 remove(); 239 this.resource = newValue; 240 this.fromDate = new Timestamp (new Date ().getTime()); 241 checkAssignment(); 242 } 243 244 247 public void remove() throws WfException { 248 try { 249 valueObject().remove(); 250 } catch (GenericEntityException e) { 251 throw new WfException(e.getMessage(), e); 252 } 253 } 254 255 258 public String status() throws WfException { 259 return valueObject().getString("statusId"); 260 } 261 262 265 public Timestamp fromDate() throws WfException { 266 return fromDate; 267 } 268 269 private GenericValue valueObject() throws WfException { 270 GenericValue value = null; 271 Map fields = new HashMap (); 272 273 fields.put("workEffortId", activity.runtimeKey()); 274 fields.put("partyId", resource.resourcePartyId()); 275 fields.put("roleTypeId", resource.resourceRoleId()); 276 fields.put("fromDate", fromDate); 277 try { 278 value = activity.getDelegator().findByPrimaryKey("WorkEffortPartyAssignment", fields); 279 } catch (GenericEntityException e) { 280 throw new WfException(e.getMessage(), e); 281 } 282 if (value == null) 283 throw new WfException("Invalid assignment; no runtime entity"); 284 return value; 285 } 286 287 private boolean isEqual(WfAssignment asgn) throws WfException { 288 if (asgn == null) { 290 return false; 291 } 292 293 if (!this.status().equals(asgn.status())) { 295 return false; 296 } 297 298 WfActivity thisActivity = this.activity(); 300 WfActivity compActivity = asgn.activity(); 301 if ((thisActivity == null && compActivity != null) || (thisActivity != null && compActivity == null)) { 302 return false; 303 } else { 304 String thisKey = thisActivity.runtimeKey(); 305 String compKey = compActivity.runtimeKey(); 306 if ((thisKey != null && compKey == null) || (thisKey == null && compKey != null)) { 307 return false; 308 } else if (thisKey != null && compKey != null && !thisKey.equals(compKey)) { 309 return false; 310 } 311 } 312 313 WfResource thisResource = this.assignee(); 315 WfResource compResource = asgn.assignee(); 316 if ((thisResource == null && compResource != null) || (thisResource != null && compResource == null)) { 317 return false; 318 } else { 319 String thisKey = thisResource.resourceKey(); 320 String compKey = compResource.resourceKey(); 321 if ((thisKey != null && compKey == null) || (thisKey == null && compKey != null)) { 322 return false; 323 } else if (thisKey != null && compKey != null && !thisKey.equals(compKey)) { 324 return false; 325 } 326 } 327 328 return true; 330 } 331 } 332 333 | Popular Tags |