1 package org.enhydra.shark; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import org.enhydra.shark.api.SharkTransaction; 7 import org.enhydra.shark.api.TransactionException; 8 import org.enhydra.shark.api.client.wfbase.BaseException; 9 import org.enhydra.shark.api.client.wfmodel.CannotAcceptSuspended; 10 import org.enhydra.shark.api.client.wfmodel.InvalidResource; 11 import org.enhydra.shark.api.common.SharkConstants; 12 import org.enhydra.shark.api.internal.instancepersistence.AssignmentPersistenceInterface; 13 import org.enhydra.shark.api.internal.instancepersistence.PersistenceException; 14 import org.enhydra.shark.api.internal.working.PersistenceInterface; 15 import org.enhydra.shark.api.internal.working.WfActivityInternal; 16 import org.enhydra.shark.api.internal.working.WfAssignmentInternal; 17 import org.enhydra.shark.api.internal.working.WfResourceInternal; 18 19 24 public class WfAssignmentImpl implements WfAssignmentInternal { 25 26 private String activityId; 27 private String resourceUsername; 28 29 private String mgrName; 30 private String processId; 31 private boolean isAccepted=false; 32 private WfResourceInternal resource; 33 34 private String oldAssignee=null; 35 36 private boolean justCreated=false; 37 38 43 protected WfAssignmentImpl(SharkTransaction t, 44 WfActivityInternal activity, 45 WfResourceInternal resource) throws BaseException { 46 this.resource=resource; 47 this.justCreated = true; 48 this.activityId=activity.key(t); 49 this.resourceUsername=resource.resource_key(t); 50 this.mgrName=activity.container(t).manager_name(t); 51 this.processId=activity.process_id(t); 52 try { 53 persist(t); 54 } catch (TransactionException tme) { 55 throw new BaseException(tme); 56 } 57 if (SharkEngineManager.getInstance().getEventAuditManager()!=null) { 58 SharkEngineManager 59 .getInstance() 60 .getObjectFactory() 61 .createAssignmentEventAuditWrapper(t,activity,null,resource,false); 62 63 } 64 resource.addAssignment(t,this); 65 } 66 67 70 public WfAssignmentImpl (AssignmentPersistenceInterface po,WfResourceInternal res) { 71 this.resource=res; 72 restore(po); 73 } 74 75 76 80 public WfActivityInternal activity (SharkTransaction t) throws BaseException { 81 return SharkUtilities.getActivity(t,processId,activityId); 82 } 83 84 88 public WfResourceInternal assignee (SharkTransaction t) throws BaseException { 89 if (resource==null) { 90 resource = SharkUtilities.getResource(t,resourceUsername); 91 } 92 return resource; 93 } 94 95 98 public void set_assignee (SharkTransaction t,WfResourceInternal new_value) throws BaseException, InvalidResource { 99 if (new_value==null) { 101 throw new InvalidResource("Can not change resource to null"); 102 } 103 WfActivityInternal act=activity(t); 104 java.util.List assResIds=act.getAssignmentResourceIds(t); 105 boolean reassignToInvalid=false; 106 if (assResIds.contains(new_value.resource_key(t))) { 107 if (act.getResourceUsername(t)==null) { 108 throw new InvalidResource("Such assignment already exists!"); 109 } else { 110 reassignToInvalid=true; 111 } 112 } 113 WfResourceInternal old_value=assignee(t); 114 old_value.removeAssignment(t,processId,activityId); 115 String oldUsername=this.resourceUsername; 116 this.resource=new_value; 117 this.resourceUsername=new_value.resource_key(t); 118 act.updateAssignmentResourceIds(t,oldUsername,this.resourceUsername); 119 oldAssignee=oldUsername; 120 try { 122 if (reassignToInvalid) { 123 SharkEngineManager 124 .getInstance() 125 .getInstancePersistenceManager() 126 .deleteAssignment(activityId, this.resourceUsername, t); 127 } 128 persist(t); 129 } catch (Exception te) { 130 throw new BaseException(te); 131 } 132 if (SharkEngineManager.getInstance().getEventAuditManager()!=null) { 133 SharkEngineManager. 134 getInstance(). 135 getObjectFactory(). 136 createAssignmentEventAuditWrapper(t, 137 act, 138 old_value, 139 new_value, 140 act.accepted_status(t)); 141 } 142 this.resource.addAssignment(t,this); 143 } 144 145 174 175 public final String managerName (SharkTransaction t) throws BaseException { 176 return mgrName; 177 } 178 179 public final String processId(SharkTransaction t) throws BaseException { 180 return processId; 181 } 182 183 public final String activityId(SharkTransaction t) throws BaseException { 184 return activityId; 185 } 186 187 public final String resourceUsername(SharkTransaction t) throws BaseException { 188 return resourceUsername; 189 } 190 191 public String toString () { 192 return "["+activityId+"->"+resourceUsername+"]"; 193 } 194 195 public boolean equals (Object obj) { 196 if (!(obj instanceof WfAssignmentImpl)) return false; 197 WfAssignmentImpl ass=(WfAssignmentImpl)obj; 198 return (ass.activityId.equals(activityId) 199 && ass.resourceUsername.equals(resourceUsername)); 200 } 201 202 public void persist(SharkTransaction t) throws TransactionException { 204 try { 205 if (oldAssignee==null) { 207 SharkEngineManager 208 .getInstance() 209 .getInstancePersistenceManager() 210 .persist(createAndFillPersistentObject(), this.justCreated, t); 211 } else { 212 SharkEngineManager 213 .getInstance() 214 .getInstancePersistenceManager() 215 .persist(createAndFillPersistentObject(), oldAssignee, t); 216 oldAssignee=null; 217 } 218 this.justCreated = false; 219 } catch (PersistenceException pe) { 220 throw new TransactionException(pe); 221 } 222 } 223 224 public void delete(SharkTransaction t) throws TransactionException { 225 try { 226 SharkEngineManager 227 .getInstance() 228 .getInstancePersistenceManager() 229 .deleteAssignment(activityId, resourceUsername, t); 230 assignee(t).removeAssignment(t,processId,activityId); 231 } catch (Exception ex) { 232 throw new TransactionException("Exception while deleting assignment",ex); 233 } 234 } 235 236 private AssignmentPersistenceInterface createAndFillPersistentObject () { 237 AssignmentPersistenceInterface po = 238 SharkEngineManager 239 .getInstance() 240 .getInstancePersistenceManager() 241 .createAssignment(); 242 po.setProcessMgrName(mgrName); 243 po.setProcessId(this.processId); 244 po.setActivityId(this.activityId); 245 po.setResourceUsername(this.resourceUsername); 246 po.setValid(true); 247 po.setAccepted(isAccepted); 248 return po; 249 } 250 251 private void restore (AssignmentPersistenceInterface po) { 252 this.activityId=po.getActivityId(); 253 this.resourceUsername=po.getResourceUsername(); 254 this.processId=po.getProcessId(); 255 this.mgrName=po.getProcessMgrName(); 256 this.isAccepted=po.isAccepted(); 257 } 258 259 } 260 261 | Popular Tags |