1 package org.enhydra.shark; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.List ; 6 import java.util.Map ; 7 import org.enhydra.shark.api.SharkTransaction; 8 import org.enhydra.shark.api.TransactionException; 9 import org.enhydra.shark.api.client.wfbase.BaseException; 10 import org.enhydra.shark.api.client.wfmodel.NotAssigned; 11 import org.enhydra.shark.api.internal.instancepersistence.AssignmentPersistenceInterface; 12 import org.enhydra.shark.api.internal.instancepersistence.PersistenceException; 13 import org.enhydra.shark.api.internal.instancepersistence.ResourcePersistenceInterface; 14 import org.enhydra.shark.api.internal.working.ObjectFactory; 15 import org.enhydra.shark.api.internal.working.WfAssignmentInternal; 16 import org.enhydra.shark.api.internal.working.WfResourceInternal; 17 18 26 public class WfResourceImpl implements WfResourceInternal { 27 28 private String resourceKey; 29 private String resourceName; 30 private Map assignments; 31 private boolean justCreated=false; 32 33 private boolean isAssignmentMapValid=false; 34 42 protected WfResourceImpl(SharkTransaction t, 43 String resourceKey) throws BaseException { 44 this.justCreated = true; 45 this.assignments = new HashMap (); 46 this.resourceKey = resourceKey; 47 SharkUtilities.addResourceToCache(t,this); 48 try { 49 persist(t); 50 } catch (TransactionException e) { 51 throw new BaseException(e); 52 } 53 } 54 55 56 59 protected WfResourceImpl (ResourcePersistenceInterface po) { 60 restore(po); 61 } 62 63 66 public String resource_key (SharkTransaction t) throws BaseException { 67 return resourceKey; 68 } 69 70 73 public String resource_name (SharkTransaction t) throws BaseException { 74 return resourceName; 75 } 76 77 80 public void release (SharkTransaction t,WfAssignmentInternal from_assigment, String release_info) throws BaseException, NotAssigned { 81 83 if (!getAssignmentsMap(t).containsKey(from_assigment.activityId(t))) { 84 throw new NotAssigned("Can't release assignment "+from_assigment+" - it is not assigned to this resource!"); 85 } 86 } 87 88 public String toString () { 89 return "[Id="+resourceKey+",name="+resourceName+"]"; 90 } 91 92 93 public synchronized void addAssignment (SharkTransaction t,WfAssignmentInternal ass) throws BaseException { 94 if (assignments==null) { 95 assignments=new HashMap (); 96 } 97 assignments.put(ass.activityId(t),ass); 98 } 99 100 public synchronized void removeAssignment (SharkTransaction t,String procId,String actId) throws BaseException { 101 if (assignments!=null) { 102 getAssignmentsMap(t).remove(actId); 103 } 104 } 105 106 public void restoreAssignment (SharkTransaction t,String mgrName,String procId,String actId,boolean isAccepted) throws BaseException { 107 AssignmentPersistenceInterface po=SharkEngineManager 108 .getInstance() 109 .getInstancePersistenceManager().createAssignment(); 110 po.setProcessMgrName(mgrName); 111 po.setProcessId(procId); 112 po.setActivityId(actId); 113 po.setResourceUsername(resourceKey); 114 po.setAccepted(isAccepted); 115 WfAssignmentInternal ass=SharkEngineManager. 116 getInstance(). 117 getObjectFactory(). 118 createAssignment(po,this); 119 synchronized (this) { 120 if (assignments==null) { 121 assignments=new HashMap (); 122 } 123 assignments.put(actId,ass); 124 } 125 } 126 127 protected synchronized Map getAssignmentsMap (SharkTransaction t) throws BaseException { 128 if (null == assignments || !isAssignmentMapValid) { 129 try { 130 assignments=new HashMap (); 131 List l = SharkEngineManager 132 .getInstance() 133 .getInstancePersistenceManager() 134 .getAllValidAssignmentsForResource(resourceKey, t); 135 ObjectFactory objectFactory = 136 SharkEngineManager.getInstance().getObjectFactory(); 137 for (int i=0; i<l.size(); i++) { 138 AssignmentPersistenceInterface po=(AssignmentPersistenceInterface)l.get(i); 139 if (!assignments.containsKey(po.getActivityId())) { 140 WfAssignmentInternal ass=objectFactory.createAssignment(po,this); 141 assignments.put(po.getActivityId(),ass); 142 } 143 } 144 isAssignmentMapValid=true; 145 } catch (Exception ex) { 146 throw new BaseException(ex); 147 } 148 } 149 return assignments; 150 } 151 152 public synchronized List getAssignments (SharkTransaction t) throws BaseException { 153 return new ArrayList (getAssignmentsMap(t).values()); 154 } 155 156 public synchronized WfAssignmentInternal getAssignment (SharkTransaction t,String procId,String actId) throws BaseException { 157 WfAssignmentInternal ass=null; 158 if (assignments!=null) { 159 ass=(WfAssignmentInternal)assignments.get(actId); 160 } 161 if (ass==null) { 162 try { 163 AssignmentPersistenceInterface po=SharkEngineManager 164 .getInstance() 165 .getInstancePersistenceManager().restoreAssignment(actId, resourceKey, t); 166 if (po!=null) { 167 ass=SharkEngineManager. 168 getInstance(). 169 getObjectFactory(). 170 createAssignment(po,this); 171 if (assignments==null) { 172 assignments=new HashMap (); 173 } 174 assignments.put(actId,ass); 175 } 176 } catch (Exception ex) { 177 throw new BaseException(ex); 178 } 179 } 180 181 return (WfAssignmentInternal)assignments.get(actId); 182 } 183 184 188 public boolean equals (Object obj) { 189 if (!(obj instanceof WfResourceImpl)) return false; 190 return ((WfResourceImpl)obj).resourceKey.equals(resourceKey); 191 } 192 193 public void persist(SharkTransaction t) throws TransactionException { 194 try { 195 SharkEngineManager 196 .getInstance() 197 .getInstancePersistenceManager() 198 .persist(createAndFillPersistentObject(), this.justCreated, t); 199 this.justCreated = false; 200 } catch (PersistenceException pe) { 201 throw new TransactionException(pe); 202 } 203 } 204 205 public void delete(SharkTransaction t) throws TransactionException { 206 try { 207 SharkEngineManager 208 .getInstance() 209 .getInstancePersistenceManager() 210 .deleteResource(resourceKey, t); 211 SharkUtilities.removeResourceFromCache(t,this); 212 } catch (Exception ex) { 213 throw new TransactionException("Exception while deleting assignment",ex); 214 } 215 } 216 217 private ResourcePersistenceInterface createAndFillPersistentObject () { 218 ResourcePersistenceInterface po = 219 SharkEngineManager 220 .getInstance() 221 .getInstancePersistenceManager() 222 .createResource(); 223 fillPersistentObject(po); 224 return po; 225 } 226 227 private void fillPersistentObject (ResourcePersistenceInterface po) { 228 po.setUsername(this.resourceKey); 229 po.setName(this.resourceName); 230 } 231 232 private void restore (ResourcePersistenceInterface po) { 233 this.resourceKey=po.getUsername(); 234 this.resourceName=po.getName(); 235 } 236 237 } 238 239 | Popular Tags |