KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > assignment > HistoryRelatedAssignmentManager


1 package org.enhydra.shark.assignment;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.enhydra.shark.Shark;
7 import org.enhydra.shark.api.RootException;
8 import org.enhydra.shark.api.SharkTransaction;
9 import org.enhydra.shark.api.client.wfmodel.WfActivity;
10 import org.enhydra.shark.api.client.wfmodel.WfActivityIterator;
11 import org.enhydra.shark.api.client.wfmodel.WfProcess;
12 import org.enhydra.shark.api.client.wfservice.AdminInterface;
13 import org.enhydra.shark.api.client.wfservice.AdminMisc;
14 import org.enhydra.shark.api.client.wfservice.ExecutionAdministration;
15 import org.enhydra.shark.api.internal.assignment.AssignmentManager;
16 import org.enhydra.shark.api.internal.assignment.PerformerData;
17 import org.enhydra.shark.api.internal.working.CallbackUtilities;
18 import org.enhydra.shark.xpdl.XMLUtil;
19
20 /**
21  * This class provides an extended Assignment Manager implementation via the use
22  * of XPDL activity extended attributes.
23  *
24  * The following extended attributes can be associated with an activity to
25  * affect assignments:
26  *
27  * ReassignToOriginalPerformer - If an activity is executed more than once in a
28  * particular process, then it will only be assigned to the original performer
29  * during subsequent executions. The value of this extended attribute is
30  * ignored.
31  *
32  * AssignToPerformerOfActivity - This extended attribute can be used to force an
33  * activity to be assigned to the performer of a previously-executed activity.
34  * The value of this extended attribute should be the activity definition id in
35  * question.
36  *
37  * DoNotAssignToPerformerOfActivity - This extended attribute can be used to
38  * force an activity NOT to be assigned to the performer of a previously-
39  * executed activity. The value of this extended attribute should be the
40  * activity definition id in question.
41  *
42  * Note that only one of each extended attribute should be associated with any
43  * single activity definition.
44  *
45  * Note that the above names are just the default names of these extended
46  * attributes, and that they can be overriden in the configuration file
47  * (Shark.conf) using the following properties:
48  *
49  * - HistoryRelatedAssignmentManager.extAttrReassignToOriginalPerformer
50  * - HistoryRelatedAssignmentManager.extAttrAssignToPerformerOfActivity
51  * - HistoryRelatedAssignmentManager.extAttrDoNotAssignToPerformerOfActivity
52  *
53  * Finally, note that this class needs to make a connection to the workflow
54  * engine. The username and password used to connect are specified by the
55  * following configuration properties:
56  *
57  * - HistoryRelatedAssignmentManager.username
58  * - HistoryRelatedAssignmentManager.password
59  *
60  * If anybody wishes to extend/modify this class in any way, one obvious
61  * improvment would be to allow multiple copies of each extended attribute to be
62  * assigned to a single activity.
63  *
64  * @author Rich Robinson
65  */

66 public class HistoryRelatedAssignmentManager implements AssignmentManager {
67    private CallbackUtilities cus;
68
69    private String JavaDoc username;
70    private String JavaDoc password;
71    private String JavaDoc extAttrReassignToOriginalPerformer;
72    private String JavaDoc extAttrAssignToPerformerOfActivity;
73    private String JavaDoc extAttrDoNotAssignToPerformerOfActivity;
74
75    public void configure(CallbackUtilities cus) throws RootException {
76       this.cus = cus;
77
78       /*
79        * No default values are provided for username and password for security
80        * reasons - they should always be specified in the configuration file.
81        */

82       username = cus.getProperty("HistoryRelatedAssignmentManager.username");
83       password = cus.getProperty("HistoryRelatedAssignmentManager.password");
84
85       extAttrReassignToOriginalPerformer = cus.getProperty(
86          "HistoryRelatedAssignmentManager.extAttrReassignToOriginalPerformer",
87          "ReassignToOriginalPerformer");
88
89       extAttrAssignToPerformerOfActivity = cus.getProperty(
90          "HistoryRelatedAssignmentManager.extAttrAssignToPerformerOfActivity",
91          "AssignToPerformerOfActivity");
92
93       extAttrDoNotAssignToPerformerOfActivity = cus
94          .getProperty(
95          "HistoryRelatedAssignmentManager.extAttrDoNotAssignToPerformerOfActivity",
96          "DoNotAssignToPerformerOfActivity");
97    }
98
99    public List JavaDoc getAssignments(SharkTransaction t,
100                               String JavaDoc engineName,
101                               String JavaDoc procId,
102                               String JavaDoc actId,
103                               List JavaDoc userIds,
104                               List JavaDoc responsibleIds,
105                               String JavaDoc processRequesterId,
106                               PerformerData xpdlParticipant,
107                               List JavaDoc xpdlResponsibleParticipants) throws RootException {
108       List JavaDoc result = null;
109
110       Shark shark = Shark.getInstance();
111       AdminInterface ai = shark.getAdminInterface();
112       AdminMisc am = ai.getAdminMisc();
113
114       String JavaDoc[][] actExtAttribs = null;
115
116       try {
117          actExtAttribs = am.getActivitiesExtendedAttributeNameValuePairs(t, procId, actId);
118       } catch (Exception JavaDoc ex) {
119       }
120
121       if (actExtAttribs!=null) {
122          if (XMLUtil.getExtendedAttributeValue(actExtAttribs, extAttrReassignToOriginalPerformer)!=null) {
123             result = doReassignToOriginalPerformer(t, engineName, procId,
124                                                    actId, userIds, responsibleIds,
125                                                    processRequesterId, ai, am);
126          }
127          else if (XMLUtil.getExtendedAttributeValue(actExtAttribs,extAttrAssignToPerformerOfActivity)!=null) {
128             result = doAssignToPerformerOfActivity(t, engineName, procId,
129                                                    actId, userIds, responsibleIds,
130                                                    processRequesterId, ai, am, actExtAttribs);
131          }
132          else if (XMLUtil.getExtendedAttributeValue(actExtAttribs,extAttrDoNotAssignToPerformerOfActivity)!=null) {
133             result = doDoNotAssignToPerformerOfActivity(t, engineName, procId,
134                                                         actId, userIds, responsibleIds,
135                                                         processRequesterId, ai, am, actExtAttribs);
136          }
137          else {
138             // If this is not a special case then use the parent implementation.
139
result = getDefaultAssignments(t, engineName, procId, actId,
140                                            userIds, responsibleIds, processRequesterId);
141          }
142       } else {
143          // If there were some problems then use the parent implementation.
144
result = getDefaultAssignments(t, engineName, procId, actId,
145                                         userIds, responsibleIds, processRequesterId);
146       }
147
148       return result;
149    }
150
151    protected List JavaDoc doReassignToOriginalPerformer(SharkTransaction t,
152                                                 String JavaDoc engineName, String JavaDoc procId, String JavaDoc actId, List JavaDoc userIds,
153                                                 List JavaDoc responsibleIds, String JavaDoc processRequesterId,
154                                                 AdminInterface ai, AdminMisc am) throws RootException {
155       String JavaDoc actDefId = am.getActivityDefinitionId(t, procId, actId);
156
157       return getAssignmentsForActDefId(t, engineName, procId, actId, userIds,
158                                        responsibleIds, processRequesterId, ai, am,
159                                        actDefId, true);
160    }
161
162    protected List JavaDoc doAssignToPerformerOfActivity(SharkTransaction t,
163                                                 String JavaDoc engineName, String JavaDoc procId, String JavaDoc actId, List JavaDoc userIds,
164                                                 List JavaDoc responsibleIds, String JavaDoc processRequesterId,
165                                                 AdminInterface ai, AdminMisc am, String JavaDoc[][] actExtAttribs)
166       throws RootException {
167       String JavaDoc actDefId = XMLUtil.getExtendedAttributeValue(actExtAttribs,
168                                                extAttrAssignToPerformerOfActivity);
169
170       return getAssignmentsForActDefId(t, engineName, procId, actId, userIds,
171                                        responsibleIds, processRequesterId, ai, am,
172                                        actDefId, true);
173    }
174
175    protected List JavaDoc doDoNotAssignToPerformerOfActivity(SharkTransaction t,
176                                                      String JavaDoc engineName, String JavaDoc procId, String JavaDoc actId, List JavaDoc userIds,
177                                                      List JavaDoc responsibleIds, String JavaDoc processRequesterId,
178                                                      AdminInterface ai, AdminMisc am, String JavaDoc[][] actExtAttribs)
179       throws RootException {
180       String JavaDoc actDefId = XMLUtil.getExtendedAttributeValue(actExtAttribs,
181                                                extAttrDoNotAssignToPerformerOfActivity);
182
183       List JavaDoc doNotAssignTo = getAssignmentsForActDefId(t, engineName, procId,
184                                                      actId, userIds, responsibleIds, processRequesterId,
185                                                      ai, am, actDefId, false);
186
187       List JavaDoc allAssignments = getDefaultAssignments(t, engineName, procId,
188                                                   actId, userIds, responsibleIds, processRequesterId);
189
190       /*
191        * If we only have one assignment then we CANNOT remove the "do not
192        * assign to" assignment (either it won't be the same assignment, in
193        * which case there's no need to remove it, or it WILL be the same
194        * assignment, and we will be left with an empty assignments list - this
195        * should probably never happen).
196        */

197       if (allAssignments.size() > 1) {
198          allAssignments.removeAll(doNotAssignTo);
199       }
200
201       return allAssignments;
202    }
203
204    /*
205     * Given an activity definition id, this method returns assignments based on
206     * the last resource to execute that activity definition id in this process.
207     */

208    protected List JavaDoc getAssignmentsForActDefId(SharkTransaction t,
209                                             String JavaDoc engineName, String JavaDoc procId, String JavaDoc actId, List JavaDoc userIds,
210                                             List JavaDoc responsibleIds, String JavaDoc processRequesterId,
211                                             AdminInterface ai, AdminMisc am, String JavaDoc actDefId,
212                                             boolean fallbackToDefault) throws RootException {
213       List JavaDoc result = new ArrayList JavaDoc();
214
215       String JavaDoc prevPerformer = getPrevPerformerOfActDefId(t, procId, ai, am,
216                                                         actDefId);
217
218       if (prevPerformer != null) {
219          result.add(prevPerformer);
220       }
221       else if (fallbackToDefault) {
222          /*
223           * If we are falling back to the parent implementation in the case
224           * that there is no resource to assign to, and if our resource list
225           * is empty then we call the super implementation.
226           */

227          result = getDefaultAssignments(t, engineName, procId, actId,
228                                         userIds, responsibleIds, processRequesterId);
229       }
230
231       return result;
232    }
233
234    protected String JavaDoc getPrevPerformerOfActDefId(SharkTransaction t,
235                                                String JavaDoc procId,
236                                                AdminInterface ai,
237                                                AdminMisc am,
238                                                String JavaDoc actDefId)
239       throws RootException {
240       String JavaDoc result = null;
241
242       if (actDefId != null) {
243          ExecutionAdministration ea = null;
244
245          try {
246             ea = ai.getExecutionAdministration();
247             ea.connect(t, username, password, "", "");
248             WfProcess proc = ea.getProcess(t, procId);
249
250             WfActivityIterator wai = proc.get_iterator_step(t);
251
252             String JavaDoc query = "state.equals(\"closed.completed\") && definitionId.equals(\"" + actDefId + "\")";
253
254             wai.set_query_expression(t, query);
255
256             WfActivity[] acts=wai.get_next_n_sequence(t,0);
257             if (acts!=null && acts.length>0) {
258                // find the last one - maybe there was some reassignments
259
long maxTime=-1;
260                WfActivity lastAct=null;
261                for (int i=0; i<acts.length; i++) {
262                   long time=acts[i].last_state_time(t).getTime();
263                   if (time>maxTime) {
264                      maxTime=time;
265                      lastAct=acts[i];
266                   }
267                }
268                String JavaDoc prevActId = lastAct.key(t);
269
270                /*
271                 * Get the resource that finished the previous instance of the
272                 * specified activity.
273                 */

274                result = am.getActivityResourceUsername(t, procId, prevActId);
275             }
276          }
277          finally {
278             ea.disconnect(t);
279          }
280
281       }
282
283       return result;
284    }
285
286    public List JavaDoc getDefaultAssignments(SharkTransaction t, String JavaDoc engineName,
287                                      String JavaDoc procId, String JavaDoc actId, List JavaDoc userIds, List JavaDoc responsibleIds,
288                                      String JavaDoc processRequesterId) throws RootException {
289       List JavaDoc result;
290
291       if (userIds != null && userIds.size() > 0) {
292          result = userIds;
293       }
294       else if (responsibleIds != null && responsibleIds.size() > 0) {
295          result = responsibleIds;
296       }
297       else {
298          result = new ArrayList JavaDoc();
299          result.add(processRequesterId);
300       }
301
302       return result;
303    }
304
305 }
306
Popular Tags