KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > WfResourceImpl


1 package org.enhydra.shark;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
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 /**
19  * WfResourceImpl - Workflow Resource Object implementation.
20  * Some methods are doubled. One version is used by the client, and
21  * when he executes it, the object is persisted, and the other is
22  * used by the persistence layer.
23  * @author Sasa Bojanic, Vladimir Puskas
24  * @version 1.0.1
25  */

26 public class WfResourceImpl implements WfResourceInternal {
27
28    private String JavaDoc resourceKey;
29    private String JavaDoc resourceName;
30    private Map JavaDoc assignments;
31    private boolean justCreated=false;
32
33    private boolean isAssignmentMapValid=false;
34    /**
35     * Creates a new WfResource.
36     * @param resourceKey uniquely identifies the resource, it's a username
37     * for logging on engine.
38     * @param t SharkTransaction.
39     *
40     * @exception BaseException
41     */

42    protected WfResourceImpl(SharkTransaction t,
43                             String JavaDoc resourceKey) throws BaseException {
44       this.justCreated = true;
45       this.assignments = new HashMap JavaDoc();
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    /**
57     * Used to create object when restoring it from database.
58     */

59    protected WfResourceImpl (ResourcePersistenceInterface po) {
60       restore(po);
61    }
62
63    /**
64     * Gets the resource username.
65     */

66    public String JavaDoc resource_key (SharkTransaction t) throws BaseException {
67       return resourceKey;
68    }
69
70    /**
71     * Gets the resource name.
72     */

73    public String JavaDoc resource_name (SharkTransaction t) throws BaseException {
74       return resourceName;
75    }
76
77    /**
78     * Releases the resouce from the assignment.
79     */

80    public void release (SharkTransaction t,WfAssignmentInternal from_assigment, String JavaDoc release_info) throws BaseException, NotAssigned {
81       // TODO: check OMG docu for this method
82

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 JavaDoc 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 JavaDoc();
96       }
97       assignments.put(ass.activityId(t),ass);
98    }
99
100    public synchronized void removeAssignment (SharkTransaction t,String JavaDoc procId,String JavaDoc actId) throws BaseException {
101       if (assignments!=null) {
102          getAssignmentsMap(t).remove(actId);
103       }
104    }
105
106    public void restoreAssignment (SharkTransaction t,String JavaDoc mgrName,String JavaDoc procId,String JavaDoc 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 JavaDoc();
122          }
123          assignments.put(actId,ass);
124       }
125    }
126
127    protected synchronized Map JavaDoc getAssignmentsMap (SharkTransaction t) throws BaseException {
128       if (null == assignments || !isAssignmentMapValid) {
129          try {
130             assignments=new HashMap JavaDoc();
131             List JavaDoc 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 JavaDoc ex) {
146             throw new BaseException(ex);
147          }
148       }
149       return assignments;
150    }
151
152    public synchronized List JavaDoc getAssignments (SharkTransaction t) throws BaseException {
153       return new ArrayList JavaDoc(getAssignmentsMap(t).values());
154    }
155
156    public synchronized WfAssignmentInternal getAssignment (SharkTransaction t,String JavaDoc procId,String JavaDoc 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 JavaDoc();
173                }
174                assignments.put(actId,ass);
175             }
176          } catch (Exception JavaDoc ex) {
177             throw new BaseException(ex);
178          }
179       }
180       
181       return (WfAssignmentInternal)assignments.get(actId);
182    }
183
184    /**
185     * It is assumed that there can't be two or more
186     * resources that have the same resource key.
187     */

188    public boolean equals (Object JavaDoc 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 JavaDoc 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