KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark;
2
3 import java.util.List JavaDoc;
4
5 import org.enhydra.shark.api.RootException;
6 import org.enhydra.shark.api.SharkTransaction;
7 import org.enhydra.shark.api.client.wfbase.BaseException;
8 import org.enhydra.shark.api.client.wfmodel.NotAssigned;
9 import org.enhydra.shark.api.client.wfmodel.WfAssignment;
10 import org.enhydra.shark.api.client.wfmodel.WfAssignmentIterator;
11 import org.enhydra.shark.api.client.wfmodel.WfResource;
12 import org.enhydra.shark.api.internal.security.SecurityManager;
13 import org.enhydra.shark.api.internal.working.WfResourceInternal;
14
15 /**
16  * WfResourceWrapper - Workflow Resource Object implementation.
17  * @author Sasa Bojanic
18  * @author Vladimir Puskas
19  */

20 public class WfResourceWrapper implements WfResource {
21
22    private String JavaDoc userAuth;
23    private String JavaDoc username;
24
25    /**
26     * Creates a new WfResource.
27     * @param username uniquely identifies the resource.
28     */

29    protected WfResourceWrapper(String JavaDoc userAuth,String JavaDoc username) {
30       this.userAuth=userAuth;
31       this.username = username;
32    }
33
34    /**
35     * Gets the number of work items currently assigned to this resource.
36     */

37    public int how_many_work_item () throws BaseException {
38       int ret = -1;
39       SharkTransaction t = null;
40       try {
41          t = SharkUtilities.createTransaction();
42          ret = how_many_work_item(t);
43          //SharkUtilities.commitTransaction(t);
44
} catch (RootException e) {
45          //SharkUtilities.rollbackTransaction(t);
46
SharkUtilities.emptyCaches(t);
47          if (e instanceof BaseException)
48             throw (BaseException)e;
49          else
50             throw new BaseException(e);
51       } finally {
52          SharkUtilities.releaseTransaction(t);
53       }
54       return ret;
55    }
56
57    public int how_many_work_item (SharkTransaction t) throws BaseException {
58       SecurityManager JavaDoc sm=SharkEngineManager.getInstance().getSecurityManager();
59       if (sm!=null) {
60          try {
61             sm.check_resource_how_many_work_item(t,
62                                                  username,
63                                                  userAuth);
64          } catch (Exception JavaDoc ex) {
65             throw new BaseException(ex);
66          }
67       }
68
69       try {
70          return SharkEngineManager
71             .getInstance()
72             .getInstancePersistenceManager()
73             .getAllValidAssignmentsForResource(username,t).size();
74       } catch (Exception JavaDoc e) {
75          throw new BaseException(e);
76       }
77    }
78
79    /**
80     * Gets an iterator of work items.
81     */

82    public WfAssignmentIterator get_iterator_work_item () throws BaseException {
83       WfAssignmentIterator ret = null;
84       SharkTransaction t = null;
85       try {
86          t = SharkUtilities.createTransaction();
87          ret = get_iterator_work_item(t);
88          //SharkUtilities.commitTransaction(t);
89
} catch (RootException e) {
90          //SharkUtilities.rollbackTransaction(t);
91
SharkUtilities.emptyCaches(t);
92          if (e instanceof BaseException)
93             throw (BaseException)e;
94          else
95             throw new BaseException(e);
96       } finally {
97          SharkUtilities.releaseTransaction(t);
98       }
99       return ret;
100    }
101
102    public WfAssignmentIterator get_iterator_work_item (SharkTransaction t)
103       throws BaseException {
104       SecurityManager JavaDoc sm=SharkEngineManager.getInstance().getSecurityManager();
105       if (sm!=null) {
106          try {
107             sm.check_resource_get_iterator_work_item(t,
108                                                      username,
109                                                      userAuth);
110          } catch (Exception JavaDoc ex) {
111             throw new BaseException(ex);
112          }
113       }
114
115
116       return SharkEngineManager.getInstance().getObjectFactory().createAssignmentIteratorWrapper(t,userAuth,username);
117    }
118
119    /**
120     * Gets the work items.
121     * @return List of WfAssignment objects.
122     */

123    public WfAssignment[] get_sequence_work_item (int max_number) throws BaseException {
124       WfAssignment[] ret = null;
125       SharkTransaction t = null;
126       try {
127          t = SharkUtilities.createTransaction();
128          ret = get_sequence_work_item(t, max_number);
129          //SharkUtilities.commitTransaction(t);
130
} catch (RootException e) {
131          //SharkUtilities.rollbackTransaction(t);
132
SharkUtilities.emptyCaches(t);
133          if (e instanceof BaseException)
134             throw (BaseException)e;
135          else
136             throw new BaseException(e);
137       } finally {
138          SharkUtilities.releaseTransaction(t);
139       }
140       return ret;
141    }
142
143    public WfAssignment[] get_sequence_work_item(SharkTransaction t,int max_number)
144       throws BaseException {
145       SecurityManager JavaDoc sm=SharkEngineManager.getInstance().getSecurityManager();
146       if (sm!=null) {
147          try {
148             sm.check_resource_get_sequence_work_item(t,
149                                                      username,
150                                                      userAuth);
151          } catch (Exception JavaDoc ex) {
152             throw new BaseException(ex);
153          }
154       }
155
156       List JavaDoc l=SharkUtilities.createAssignmentWrappers(t,userAuth,username);
157       if (max_number > l.size() || max_number<=0) {
158          max_number = l.size();
159       }
160       WfAssignment[] ret = new WfAssignment[l.size()];
161       l.subList(0, max_number).toArray(ret);
162       return ret;
163       
164    }
165
166    /**
167     * Checks if an assignment object is associated with this resource.
168     * @return true if assignment is part of the work list for this resource.
169     */

170    public boolean is_member_of_work_items(WfAssignment member) throws BaseException {
171       boolean ret = false;
172       SharkTransaction t = null;
173       try {
174          t = SharkUtilities.createTransaction();
175          ret = is_member_of_work_items(t, member);
176          //SharkUtilities.commitTransaction(t);
177
} catch (RootException e) {
178          //SharkUtilities.rollbackTransaction(t);
179
SharkUtilities.emptyCaches(t);
180          if (e instanceof BaseException)
181             throw (BaseException)e;
182          else
183             throw new BaseException(e);
184       } finally {
185          SharkUtilities.releaseTransaction(t);
186       }
187       return ret;
188    }
189
190    public boolean is_member_of_work_items(SharkTransaction t,WfAssignment member) throws BaseException {
191       SecurityManager JavaDoc sm=SharkEngineManager.getInstance().getSecurityManager();
192       if (sm!=null) {
193          try {
194             sm.check_resource_is_member_of_work_items(t,
195                                                       username,
196                                                       userAuth);
197          } catch (Exception JavaDoc ex) {
198             throw new BaseException(ex);
199          }
200       }
201
202       String JavaDoc resUn=member.assignee(t).resource_key(t);
203       return resUn.equals(username);
204    }
205
206    /**
207     * Gets the resource username.
208     */

209    public String JavaDoc resource_key () throws BaseException {
210       SecurityManager JavaDoc sm=SharkEngineManager.getInstance().getSecurityManager();
211       if (sm==null) {
212          return username;
213       }
214       String JavaDoc ret = null;
215       SharkTransaction t = null;
216       try {
217          t = SharkUtilities.createTransaction();
218          ret = resource_key(t);
219          //SharkUtilities.commitTransaction(t);
220
} catch (RootException e) {
221          //SharkUtilities.rollbackTransaction(t);
222
SharkUtilities.emptyCaches(t);
223          if (e instanceof BaseException)
224             throw (BaseException)e;
225          else
226             throw new BaseException(e);
227       } finally {
228          SharkUtilities.releaseTransaction(t);
229       }
230       return ret;
231    }
232
233    public String JavaDoc resource_key (SharkTransaction t) throws BaseException {
234       SecurityManager JavaDoc sm=SharkEngineManager.getInstance().getSecurityManager();
235       if (sm!=null) {
236          try {
237             sm.check_resource_resource_key(t,
238                                            username,
239                                            userAuth);
240          } catch (Exception JavaDoc ex) {
241             throw new BaseException(ex);
242          }
243       }
244
245       return username;
246    }
247
248    /**
249     * Gets the resource name.
250     */

251    public String JavaDoc resource_name () throws BaseException {
252       String JavaDoc ret = null;
253       SharkTransaction t = null;
254       try {
255          t = SharkUtilities.createTransaction();
256          ret = resource_name(t);
257          //SharkUtilities.commitTransaction(t);
258
} catch (RootException e) {
259          //SharkUtilities.rollbackTransaction(t);
260
SharkUtilities.emptyCaches(t);
261          if (e instanceof BaseException)
262             throw (BaseException)e;
263          else
264             throw new BaseException(e);
265       } finally {
266          SharkUtilities.releaseTransaction(t);
267       }
268       return ret;
269    }
270
271    public String JavaDoc resource_name (SharkTransaction t) throws BaseException {
272       WfResourceInternal resInternal=WfResourceWrapper.getResourceImpl(t,username);
273       SecurityManager JavaDoc sm=SharkEngineManager.getInstance().getSecurityManager();
274       if (sm!=null) {
275          try {
276             sm.check_resource_resource_name(t,
277                                             username,
278                                             userAuth);
279          } catch (Exception JavaDoc ex) {
280             throw new BaseException(ex);
281          }
282       }
283
284       return resInternal.resource_name(t);
285    }
286
287    /**
288     * Releases the resouce from the assignment.
289     */

290    public void release (WfAssignment from_assigment, String JavaDoc release_info) throws BaseException, NotAssigned {
291       SharkTransaction t = null;
292       try {
293          t = SharkUtilities.createTransaction();
294          release(t,from_assigment,release_info);
295          SharkUtilities.commitTransaction(t);
296       } catch (RootException e) {
297          SharkUtilities.rollbackTransaction(t,e);
298          SharkUtilities.emptyCaches(t);
299          if (e instanceof BaseException)
300             throw (BaseException)e;
301          else
302             throw new BaseException(e);
303       } finally {
304          SharkUtilities.releaseTransaction(t);
305       }
306    }
307
308    public void release (SharkTransaction t,WfAssignment from_assigment, String JavaDoc release_info) throws BaseException, NotAssigned {
309       SecurityManager JavaDoc sm=SharkEngineManager.getInstance().getSecurityManager();
310       if (sm!=null) {
311          try {
312             sm.check_resource_release(t,
313                                       username,
314                                       userAuth);
315          } catch (Exception JavaDoc ex) {
316             throw new BaseException(ex);
317          }
318       }
319    }
320
321    public String JavaDoc toString () {
322       return "[Id="+username+"]";
323    }
324
325    /**
326     * It is assumed that there can't be two or more
327     * resources that have the same resource key.
328     */

329    public boolean equals (Object JavaDoc obj) {
330       if (!(obj instanceof WfResource)) return false;
331       WfResource res=(WfResource)obj;
332       try {
333          if (obj instanceof WfResourceWrapper) {
334             return ((WfResourceWrapper)obj).username.equals(username);
335          } else {
336             return res.resource_key().equals(username);
337          }
338       } catch (Exception JavaDoc ex) {
339          return false;
340       }
341    }
342
343    private static WfResourceInternal getResourceImpl (
344       SharkTransaction t,
345       String JavaDoc username) throws BaseException {
346       WfResourceInternal res=SharkUtilities.getResource(t,username);
347       if (res==null) throw new BaseException("Resource does not exist");
348       return res;
349    }
350 }
351
Popular Tags