KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
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 /**
20  * WfAssignmentImpl - Workflow Assignment Object implementation
21  * @author Sasa Bojanic
22  * @author Vladimir Puskas
23  */

24 public class WfAssignmentImpl implements WfAssignmentInternal {
25
26    private String JavaDoc activityId;
27    private String JavaDoc resourceUsername;
28
29    private String JavaDoc mgrName;
30    private String JavaDoc processId;
31    private boolean isAccepted=false;
32    private WfResourceInternal resource;
33
34    private String JavaDoc oldAssignee=null;
35
36    private boolean justCreated=false;
37
38    /**
39     * Creates new WfAssignment.
40     * @param activity The Activity object for this assignment.
41     * @param resource The WfResource object this is assigned to.
42     */

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    /**
68     * Used to create object when restoring it from database.
69     */

70    public WfAssignmentImpl (AssignmentPersistenceInterface po,WfResourceInternal res) {
71       this.resource=res;
72       restore(po);
73    }
74
75
76    /**
77     * Gets the activity object of this assignment.
78     * @return WfActivity The activity object of this assignment.
79     */

80    public WfActivityInternal activity (SharkTransaction t) throws BaseException {
81       return SharkUtilities.getActivity(t,processId,activityId);
82    }
83
84    /**
85     * Gets the assignee (resource) of this assignment.
86     * @return WfResource The assignee of this assignment.
87     */

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    /**
96     * Sets the assignee of this assignment.
97     */

98    public void set_assignee (SharkTransaction t,WfResourceInternal new_value) throws BaseException, InvalidResource {
99       // do not allow reassignment to the same resource
100
if (new_value==null) {
101          throw new InvalidResource("Can not change resource to null");
102       }
103       WfActivityInternal act=activity(t);
104       java.util.List JavaDoc 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 JavaDoc 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       // persist the change to assignee
121
try {
122          if (reassignToInvalid) {
123             SharkEngineManager
124             .getInstance()
125             .getInstancePersistenceManager()
126             .deleteAssignment(activityId, this.resourceUsername, t);
127          }
128          persist(t);
129       } catch (Exception JavaDoc 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    /*public void set_accepted_status (SharkTransaction t,boolean accept) throws BaseException, CannotAcceptSuspended {
146       WfActivityInternal act=activity(t);
147       if (act.state(t).startsWith(SharkConstants.STATEPREFIX_CLOSED)) {
148          throw new BaseException("activity state is closed"); // activity is closed
149       }
150
151       if (accept && act.state(t).equals(SharkConstants.STATE_OPEN_NOT_RUNNING_SUSPENDED)) {
152          throw new CannotAcceptSuspended("Can't accept suspended activity"); // activity is suspended
153       }
154
155
156       if (act.accepted_status(t) && accept &&
157           !act.state(t).equals(SharkConstants.STATE_OPEN_NOT_RUNNING_SUSPENDED)) {
158          //System.err.println("Activity "+activity.key()+" is already accepted");
159          //System.err.println("AOA, AS="+activity.getAccepted());
160          throw new BaseException("assignment already accepted and activity is NOT suspended"); // activity is already accepted
161       }
162
163       WfResourceInternal res=assignee(t);
164
165       act.set_accepted_status(t,accept,resourceUsername);
166       //addToPersistenceList(act);
167    }
168
169    public boolean get_accepted_status (SharkTransaction t) throws BaseException {
170       WfActivityInternal actInt=((WfActivityInternal)activity(t));
171       boolean isAccepted=actInt.accepted_status(t) && actInt.getResourceUsername(t).equals(resourceUsername);
172       return isAccepted;
173    }*/

174
175    public final String JavaDoc managerName (SharkTransaction t) throws BaseException {
176       return mgrName;
177    }
178    
179    public final String JavaDoc processId(SharkTransaction t) throws BaseException {
180       return processId;
181    }
182
183    public final String JavaDoc activityId(SharkTransaction t) throws BaseException {
184       return activityId;
185    }
186
187    public final String JavaDoc resourceUsername(SharkTransaction t) throws BaseException {
188       return resourceUsername;
189    }
190
191    public String JavaDoc toString () {
192       return "["+activityId+"->"+resourceUsername+"]";
193    }
194
195    public boolean equals (Object JavaDoc 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    // Interface used for Persistence
203
public void persist(SharkTransaction t) throws TransactionException {
204       try {
205          //System.err.println("The ass "+this+" is being persisted:");
206
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 JavaDoc 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