KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.enhydra.shark.api.RootException;
10 import org.enhydra.shark.api.SharkTransaction;
11 import org.enhydra.shark.api.client.wfbase.BaseException;
12 import org.enhydra.shark.api.client.wfmodel.WfActivity;
13 import org.enhydra.shark.api.client.wfmodel.WfActivityIterator;
14 import org.enhydra.shark.api.common.SharkConstants;
15 import org.enhydra.shark.api.internal.instancepersistence.ActivityPersistenceInterface;
16 import org.enhydra.shark.api.internal.instancepersistence.ActivityVariablePersistenceInterface;
17 import org.enhydra.shark.api.internal.instancepersistence.PersistentManagerInterface;
18 import org.enhydra.shark.api.internal.instancepersistence.ProcessPersistenceInterface;
19 import org.enhydra.shark.api.internal.instancepersistence.ProcessVariablePersistenceInterface;
20 import org.enhydra.shark.api.internal.scripting.Evaluator;
21
22 /**
23  * Iterator for activities in the process. The following names may be
24  * used in queries: key, name, priority, description, state,
25  * activitySetDefinitionId, definitionId, activatedTime_ms,
26  * lastStateTime_ms, resourceUsername, accepted, acceptedTime_ms. Also
27  * the names of activity context variables can be used, but the
28  * "context_" prefix should be placed before variable Id, i.e.
29  * "context_myvariable".
30  *
31  * @author Sasa Bojanic
32  * @version 1.0
33  */

34 public class WfActivityIteratorWrapper extends BaseIteratorWrapper implements
35                                                                   WfActivityIterator {
36
37    private String JavaDoc procId;
38
39    protected WfActivityIteratorWrapper(SharkTransaction t,
40                                        String JavaDoc userAuth,
41                                        String JavaDoc procId) throws BaseException {
42       super(userAuth);
43       this.procId = procId;
44    }
45
46    public WfActivity get_next_object() throws BaseException {
47       WfActivity ret = null;
48       SharkTransaction t = null;
49       try {
50          t = SharkUtilities.createTransaction();
51          ret = get_next_object(t);
52          //SharkUtilities.commitTransaction(t);
53
} catch (RootException e) {
54          //SharkUtilities.rollbackTransaction(t);
55
SharkUtilities.emptyCaches(t);
56          if (e instanceof BaseException)
57             throw (BaseException) e;
58          else
59             throw new BaseException(e);
60       } finally {
61          SharkUtilities.releaseTransaction(t);
62       }
63       return ret;
64    }
65
66    public WfActivity get_next_object(SharkTransaction t) throws BaseException {
67       return (WfActivity) super.getNextObject(t);
68    }
69
70    public WfActivity get_previous_object() throws BaseException {
71       WfActivity ret = null;
72       SharkTransaction t = null;
73       try {
74          t = SharkUtilities.createTransaction();
75          ret = get_previous_object(t);
76          //SharkUtilities.commitTransaction(t);
77
} catch (RootException e) {
78          //SharkUtilities.rollbackTransaction(t);
79
SharkUtilities.emptyCaches(t);
80          if (e instanceof BaseException)
81             throw (BaseException) e;
82          else
83             throw new BaseException(e);
84       } finally {
85          SharkUtilities.releaseTransaction(t);
86       }
87       return ret;
88    }
89
90    public WfActivity get_previous_object(SharkTransaction t) throws BaseException {
91       return (WfActivity) super.getPreviousObject(t);
92    }
93
94    public WfActivity[] get_next_n_sequence(int max_number) throws BaseException {
95       WfActivity[] ret = null;
96       SharkTransaction t = null;
97       try {
98          t = SharkUtilities.createTransaction();
99          ret = get_next_n_sequence(t, max_number);
100          //SharkUtilities.commitTransaction(t);
101
} catch (RootException e) {
102          //SharkUtilities.rollbackTransaction(t);
103
SharkUtilities.emptyCaches(t);
104          if (e instanceof BaseException)
105             throw (BaseException) e;
106          else
107             throw new BaseException(e);
108       } finally {
109          SharkUtilities.releaseTransaction(t);
110       }
111       return ret;
112    }
113
114    public WfActivity[] get_next_n_sequence(SharkTransaction t, int max_number) throws BaseException {
115       List JavaDoc l = super.getNextNSequence(t, max_number);
116       WfActivity[] ret = new WfActivity[l.size()];
117       l.toArray(ret);
118       return ret;
119    }
120
121    public WfActivity[] get_previous_n_sequence(int max_number) throws BaseException {
122       WfActivity[] ret = null;
123       SharkTransaction t = null;
124       try {
125          t = SharkUtilities.createTransaction();
126          ret = get_previous_n_sequence(t, max_number);
127          //SharkUtilities.commitTransaction(t);
128
} catch (RootException e) {
129          //SharkUtilities.rollbackTransaction(t);
130
SharkUtilities.emptyCaches(t);
131          if (e instanceof BaseException)
132             throw (BaseException) e;
133          else
134             throw new BaseException(e);
135       } finally {
136          SharkUtilities.releaseTransaction(t);
137       }
138       return ret;
139    }
140
141    public WfActivity[] get_previous_n_sequence(SharkTransaction t,
142                                                int max_number) throws BaseException {
143       List JavaDoc l = super.getPreviousNSequence(t, max_number);
144       WfActivity[] ret = new WfActivity[l.size()];
145       l.toArray(ret);
146       return ret;
147    }
148
149    protected void fillObjectList(SharkTransaction t) throws BaseException {
150       if (objectList != null) return;
151       try {
152          List JavaDoc activities = new ArrayList JavaDoc();
153
154          PersistentManagerInterface ipm = SharkEngineManager.getInstance()
155             .getInstancePersistenceManager();
156
157          String JavaDoc qemod = queryExpression;
158          if (queryExpression.startsWith(SharkConstants.QUERY_STATE_PREFIX)) {
159             qemod = queryExpression.substring(SharkConstants.QUERY_STATE_PREFIX.length());
160          }
161          List JavaDoc l = new ArrayList JavaDoc();
162          if (null != this.sqlWhere) {
163             l.addAll(ipm.getActivitiesWhere(t, sqlWhere));
164          } else if (qemod.equals(queryExpression)
165                     || queryGrammar.equals(SharkConstants.GRAMMAR_PYTHON_SCRIPT)) {
166             l.addAll(ipm.getAllActivitiesForProcess(procId, t));
167          } else {
168             String JavaDoc actState = null;
169             if (qemod.indexOf(SharkConstants.STATE_OPEN_RUNNING) != -1) {
170                actState = SharkConstants.STATE_OPEN_RUNNING;
171             } else if (qemod.indexOf(SharkConstants.STATE_OPEN_NOT_RUNNING_NOT_STARTED) != -1) {
172                actState = SharkConstants.STATE_OPEN_NOT_RUNNING_NOT_STARTED;
173             } else if (qemod.indexOf(SharkConstants.STATE_OPEN_NOT_RUNNING_SUSPENDED) != -1) {
174                actState = SharkConstants.STATE_OPEN_NOT_RUNNING_SUSPENDED;
175             } else if (qemod.indexOf(SharkConstants.STATE_CLOSED_COMPLETED) != -1) {
176                actState = SharkConstants.STATE_CLOSED_COMPLETED;
177             } else if (qemod.indexOf(SharkConstants.STATE_CLOSED_ABORTED) != -1) {
178                actState = SharkConstants.STATE_CLOSED_ABORTED;
179             } else if (qemod.indexOf(SharkConstants.STATE_CLOSED_TERMINATED) != -1) {
180                actState = SharkConstants.STATE_CLOSED_TERMINATED;
181             }
182             l.addAll(ipm.getActivitiesForProcess(procId, actState, t));
183             eval=false;
184          }
185          Evaluator evaluator = SharkEngineManager.getInstance()
186             .getScriptingManager()
187             .getEvaluator(t, queryGrammar);
188          for (int i = 0; i < l.size(); i++) {
189             ActivityPersistenceInterface po = (ActivityPersistenceInterface) l.get(i);
190             boolean toAdd = true;
191             if (eval) {
192                Map JavaDoc context = new HashMap JavaDoc();
193                ProcessPersistenceInterface ppo = null;
194                if (ignoreUsedContext || (!ignoreUsedContext &&
195                      0 <= usedContext.indexOf(SharkConstants.ACT_MGR_PACKAGE_ID))) {
196                   ppo = ipm.restoreProcess(po.getProcessId(), t);
197                   context.put(SharkConstants.ACT_MGR_PACKAGE_ID,
198                               SharkUtilities.getProcessMgrVersion(ppo.getProcessMgrName()));
199                }
200                if (ignoreUsedContext || (!ignoreUsedContext &&
201                      0 <= usedContext.indexOf(SharkConstants.ACT_MGR_PROCESS_DEFINITION_ID))) {
202                   if (null == ppo) ppo = ipm.restoreProcess(po.getProcessId(), t);
203                   context.put(SharkConstants.ACT_MGR_PROCESS_DEFINITION_ID,
204                               SharkUtilities.getProcessMgrProcDefId(ppo.getProcessMgrName()));
205                }
206                if (ignoreUsedContext || (!ignoreUsedContext &&
207                      0 <= usedContext.indexOf(SharkConstants.ACT_MGR_VERSION))) {
208                   if (null == ppo) ppo = ipm.restoreProcess(po.getProcessId(),t);
209                   context.put(SharkConstants.ACT_MGR_PACKAGE_ID,
210                               SharkUtilities.getProcessMgrPkgId(ppo.getProcessMgrName()));
211                }
212                if (ignoreUsedContext || (!ignoreUsedContext &&
213                      0 <= usedContext.indexOf(SharkConstants.ACT_MGR_NAME))) {
214                   if (null == ppo) ppo = ipm.restoreProcess(po.getProcessId(),t);
215                   context.put(SharkConstants.ACT_MGR_NAME,ppo.getProcessMgrName());
216                }
217
218                if (ignoreUsedContext || (!ignoreUsedContext &&
219                      0 <= usedContext.indexOf(SharkConstants.ACT_PROC_KEY))) {
220                   context.put(SharkConstants.ACT_PROC_KEY,po.getProcessId());
221                }
222                if (ignoreUsedContext || (!ignoreUsedContext &&
223                      0 <= usedContext.indexOf(SharkConstants.ACT_PROC_STATE))) {
224                   if (null == ppo) ppo = ipm.restoreProcess(po.getProcessId(),
225                                                             t);
226                   context.put(SharkConstants.ACT_PROC_STATE,
227                               ppo.getState());
228                }
229                if (ignoreUsedContext || (!ignoreUsedContext &&
230                      0 <= usedContext.indexOf(SharkConstants.ACT_PROC_REQUESTER_ID))) {
231                   if (null == ppo) ppo = ipm.restoreProcess(po.getProcessId(),t);
232                   String JavaDoc reqId;
233                   if (ppo.getActivityRequesterId() != null) {
234                      reqId = ppo.getActivityRequesterId();
235                   } else {
236                      reqId = ppo.getResourceRequesterId();
237                   }
238                   context.put(SharkConstants.ACT_PROC_REQUESTER_ID, reqId);
239                }
240                if (queryExpression.indexOf(SharkConstants.ACT_PROC_CONTEXT_)!=-1) {
241                   if (null == ppo) ppo = ipm.restoreProcess(po.getProcessId(),
242                                                             t);
243                   List JavaDoc pc = ipm.getAllVariablesForProcess(ppo.getId(), t);
244                   if (pc != null) {
245                      Iterator JavaDoc iter = pc.iterator();
246                      while (iter.hasNext()) {
247                         ProcessVariablePersistenceInterface pvpo = (ProcessVariablePersistenceInterface) iter.next();
248                         try {
249                            String JavaDoc name = SharkConstants.ACT_PROC_CONTEXT_
250                                          + pvpo.getDefinitionId();
251                            Object JavaDoc value = pvpo.getValue();
252                            context.put(name, value);
253                         } catch (Exception JavaDoc ex) {
254                            throw new BaseException(ex);
255                         }
256                      }
257                   }
258                }
259
260                if (ignoreUsedContext || (!ignoreUsedContext &&
261                      0 <= usedContext.indexOf(SharkConstants.ACT_STATE))) {
262                   context.put(SharkConstants.ACT_STATE, po.getState());
263                }
264                if (ignoreUsedContext || (!ignoreUsedContext &&
265                      0 <= usedContext.indexOf(SharkConstants.ACT_KEY))) {
266                   context.put(SharkConstants.ACT_KEY, po.getId());
267                }
268                if (ignoreUsedContext || (!ignoreUsedContext &&
269                      0 <= usedContext.indexOf(SharkConstants.ACT_NAME))) {
270                   context.put(SharkConstants.ACT_NAME, po.getName());
271                }
272                if (ignoreUsedContext || (!ignoreUsedContext &&
273                      0 <= usedContext.indexOf(SharkConstants.ACT_PRIORITY))) {
274                   context.put(SharkConstants.ACT_PRIORITY,
275                               new Integer JavaDoc(po.getPriority()));
276                }
277                if (ignoreUsedContext || (!ignoreUsedContext &&
278                      0 <= usedContext.indexOf(SharkConstants.ACT_DESCRIPTION))) {
279                   context.put(SharkConstants.ACT_DESCRIPTION,
280                               po.getDescription());
281                }
282                if (ignoreUsedContext || (!ignoreUsedContext &&
283                      0 <= usedContext.indexOf(SharkConstants.ACT_ACTIVITY_SET_DEFINITION_ID))) {
284                   context.put(SharkConstants.ACT_ACTIVITY_SET_DEFINITION_ID,
285                               po.getActivitySetDefinitionId());
286                }
287                if (ignoreUsedContext || (!ignoreUsedContext &&
288                      0 <= usedContext.indexOf(SharkConstants.ACT_DEFINITION_ID))) {
289                   context.put(SharkConstants.ACT_DEFINITION_ID,
290                               po.getActivityDefinitionId());
291                }
292                if (ignoreUsedContext || (!ignoreUsedContext &&
293                      0 <= usedContext.indexOf(SharkConstants.ACT_ACTIVATED_TIME_MS))) {
294                   context.put(SharkConstants.ACT_ACTIVATED_TIME_MS,
295                               new Long JavaDoc(po.getActivatedTime()));
296                }
297                if (ignoreUsedContext || (!ignoreUsedContext &&
298                      0 <= usedContext.indexOf(SharkConstants.ACT_LAST_STATE_TIME_MS))) {
299                   context.put(SharkConstants.ACT_LAST_STATE_TIME_MS,
300                               new Long JavaDoc(po.getLastStateTime()));
301                }
302                if (ignoreUsedContext || (!ignoreUsedContext &&
303                      0 <= usedContext.indexOf(SharkConstants.ACT_RESOURCE_USERNAME))) {
304                   context.put(SharkConstants.ACT_RESOURCE_USERNAME,
305                               po.getResourceUsername());
306                }
307                if (ignoreUsedContext || (!ignoreUsedContext &&
308                      0 <= usedContext.indexOf(SharkConstants.ACT_ACCEPTED))) {
309                   context.put(SharkConstants.ACT_ACCEPTED,
310                               new Boolean JavaDoc(po.getResourceUsername() != null));
311                }
312                if (ignoreUsedContext || (!ignoreUsedContext &&
313                      0 <= usedContext.indexOf(SharkConstants.ACT_ACCEPTED_TIME_MS))) {
314                   context.put(SharkConstants.ACT_ACCEPTED_TIME_MS,
315                               new Long JavaDoc(po.getAcceptedTime()));
316                }
317
318                if (queryExpression.indexOf(SharkConstants.ACT_CONTEXT_) != -1) {
319                   List JavaDoc ac = ipm.getAllVariablesForActivity(po.getId(), t);
320                   if (ac != null) {
321                      Iterator JavaDoc iter = ac.iterator();
322                      while (iter.hasNext()) {
323                         ActivityVariablePersistenceInterface avpo = (ActivityVariablePersistenceInterface) iter.next();
324                         try {
325                            String JavaDoc name = SharkConstants.ACT_CONTEXT_
326                                          + avpo.getDefinitionId();
327                            Object JavaDoc value = avpo.getValue();
328                            context.put(name, value);
329                         } catch (Exception JavaDoc ex) {
330                            throw new BaseException(ex);
331                         }
332                      }
333                   }
334                }
335                toAdd = evaluator.evaluateCondition(t,
336                                                    queryExpression,
337                                                    context);
338             }
339             if (toAdd) {
340                activities.add(SharkEngineManager.getInstance()
341                   .getObjectFactory()
342                   .createActivityWrapper(userAuth,
343                                          po.getProcessMgrName(),
344                                          po.getProcessId(),
345                                          po.getId()));
346             }
347
348          }
349          setObjectList(activities);
350       } catch (Exception JavaDoc ex) {
351          throw new BaseException(ex);
352       }
353    }
354 }
Popular Tags