KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark;
2
3 import java.util.*;
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.WfProcess;
9 import org.enhydra.shark.api.client.wfmodel.WfProcessIterator;
10 import org.enhydra.shark.api.common.SharkConstants;
11 import org.enhydra.shark.api.internal.instancepersistence.*;
12 import org.enhydra.shark.api.internal.scripting.Evaluator;
13
14 /**
15  * Iterator for process managers processes. The following names may be
16  * used in queries: key, name, priority, description, state,
17  * requesterId, createdTime_ms, startTime_ms, lastStateTime_ms, activeActivitiesNo. Also
18  * the names of process context variables can be used, but the
19  * "context_" prefix should be placed before variable Id, i.e.
20  * "context_myvariable".
21  *
22  * @author Sasa Bojanic
23  * @version 1.0
24  */

25 public class WfProcessIteratorWrapper extends BaseIteratorWrapper implements
26                                                                  WfProcessIterator {
27
28    private String JavaDoc mgrName;
29
30    private String JavaDoc procId;
31
32    private String JavaDoc actId;
33
34    private String JavaDoc username;
35
36    protected WfProcessIteratorWrapper(SharkTransaction t,
37                                       String JavaDoc userAuth,
38                                       String JavaDoc mgrName) throws BaseException {
39       super(userAuth);
40       this.mgrName = mgrName;
41    }
42
43    protected WfProcessIteratorWrapper(SharkTransaction t,
44                                       String JavaDoc userAuth,
45                                       String JavaDoc procId,
46                                       String JavaDoc actId) throws BaseException {
47       super(userAuth);
48       this.procId = procId;
49       this.actId = actId;
50    }
51
52    protected WfProcessIteratorWrapper(SharkTransaction t,
53                                       String JavaDoc userAuth,
54                                       String JavaDoc resUname,
55                                       boolean ext) throws BaseException {
56       super(userAuth);
57       this.username = resUname;
58    }
59
60    protected WfProcessIteratorWrapper(SharkTransaction t,
61                                       String JavaDoc userAuth,
62                                       List collection) throws BaseException {
63       super(userAuth);
64    }
65
66    public WfProcess get_next_object() throws BaseException {
67       WfProcess ret = null;
68       SharkTransaction t = null;
69       try {
70          t = SharkUtilities.createTransaction();
71          ret = get_next_object(t);
72          //SharkUtilities.commitTransaction(t);
73
} catch (RootException e) {
74          //SharkUtilities.rollbackTransaction(t);
75
SharkUtilities.emptyCaches(t);
76          if (e instanceof BaseException)
77             throw (BaseException) e;
78          else
79             throw new BaseException(e);
80       } finally {
81          SharkUtilities.releaseTransaction(t);
82       }
83       return ret;
84    }
85
86    public WfProcess get_next_object(SharkTransaction t) throws BaseException {
87       return (WfProcess) super.getNextObject(t);
88    }
89
90    public WfProcess get_previous_object() throws BaseException {
91       WfProcess ret = null;
92       SharkTransaction t = null;
93       try {
94          t = SharkUtilities.createTransaction();
95          ret = get_previous_object(t);
96          //SharkUtilities.commitTransaction(t);
97
} catch (RootException e) {
98          //SharkUtilities.rollbackTransaction(t);
99
SharkUtilities.emptyCaches(t);
100          if (e instanceof BaseException)
101             throw (BaseException) e;
102          else
103             throw new BaseException(e);
104       } finally {
105          SharkUtilities.releaseTransaction(t);
106       }
107       return ret;
108    }
109
110    public WfProcess get_previous_object(SharkTransaction t) throws BaseException {
111       return (WfProcess) super.getPreviousObject(t);
112    }
113
114    public WfProcess[] get_next_n_sequence(int max_number) throws BaseException {
115       WfProcess[] ret = null;
116       SharkTransaction t = null;
117       try {
118          t = SharkUtilities.createTransaction();
119          ret = get_next_n_sequence(t, max_number);
120          //SharkUtilities.commitTransaction(t);
121
} catch (RootException e) {
122          //SharkUtilities.rollbackTransaction(t);
123
SharkUtilities.emptyCaches(t);
124          if (e instanceof BaseException)
125             throw (BaseException) e;
126          else
127             throw new BaseException(e);
128       } finally {
129          SharkUtilities.releaseTransaction(t);
130       }
131       return ret;
132    }
133
134    public WfProcess[] get_next_n_sequence(SharkTransaction t, int max_number) throws BaseException {
135       List l = super.getNextNSequence(t, max_number);
136       WfProcess[] ret = new WfProcess[l.size()];
137       l.toArray(ret);
138       return ret;
139    }
140
141    public WfProcess[] get_previous_n_sequence(int max_number) throws BaseException {
142       WfProcess[] ret = null;
143       SharkTransaction t = null;
144       try {
145          t = SharkUtilities.createTransaction();
146          ret = get_previous_n_sequence(t, max_number);
147          //SharkUtilities.commitTransaction(t);
148
} catch (RootException e) {
149          //SharkUtilities.rollbackTransaction(t);
150
SharkUtilities.emptyCaches(t);
151          if (e instanceof BaseException)
152             throw (BaseException) e;
153          else
154             throw new BaseException(e);
155       } finally {
156          SharkUtilities.releaseTransaction(t);
157       }
158       return ret;
159    }
160
161    public WfProcess[] get_previous_n_sequence(SharkTransaction t,
162                                               int max_number) throws BaseException {
163       List l = super.getPreviousNSequence(t, max_number);
164       WfProcess[] ret = new WfProcess[l.size()];
165       l.toArray(ret);
166       return ret;
167    }
168
169    protected void fillObjectList(SharkTransaction t) throws BaseException {
170       if (objectList != null) return;
171       try {
172          List processes = new ArrayList();
173          PersistentManagerInterface ipm = SharkEngineManager.getInstance()
174             .getInstancePersistenceManager();
175          List l = new ArrayList();
176          if (null!=sqlWhere) {
177             l.addAll(ipm.getProcessesWhere(t, sqlWhere));
178          } else if (mgrName != null) {
179             l.addAll(ipm.getAllProcessesForMgr(mgrName, t));
180          } else if (procId != null && actId != null) {
181             ActivityPersistenceInterface apo=ipm.restoreActivity(actId, t);
182             String JavaDoc performerId = apo.getSubflowProcessId();
183             if (performerId != null) {
184                processes.add(SharkEngineManager.getInstance()
185                   .getObjectFactory()
186                   .createProcessWrapper(userAuth, apo.getProcessMgrName(), performerId));
187             }
188          } else if (username != null) {
189             List ids = ipm.getResourceRequestersProcessIds(username, t);
190             for (int i = 0; i < ids.size(); i++) {
191                String JavaDoc pId=(String JavaDoc)ids.get(i);
192                ProcessPersistenceInterface ppo = ipm.restoreProcess(pId, t);
193                // TODO: decide if we have to check here about external
194
// requesters (or maybe already in instance persistence
195
// layer). This is all due to a change of persisting
196
// external requesters
197
processes.add(SharkEngineManager.getInstance()
198                   .getObjectFactory()
199                   .createProcessWrapper(userAuth, ppo.getProcessMgrName(), ppo.getId()));
200             }
201          }
202          Evaluator evaluator = SharkEngineManager.getInstance()
203             .getScriptingManager()
204             .getEvaluator(t, queryGrammar);
205
206          for (int i = 0; i < l.size(); i++) {
207             ProcessPersistenceInterface po = (ProcessPersistenceInterface) l.get(i);
208             boolean toAdd = true;
209             if (eval) {
210                Map context = new HashMap();
211
212                if (ignoreUsedContext || (!ignoreUsedContext &&
213                      0 <= usedContext.indexOf(SharkConstants.PROC_MGR_PACKAGE_ID))) {
214                   context.put(SharkConstants.PROC_MGR_PACKAGE_ID,
215                               SharkUtilities.getProcessMgrPkgId(po.getProcessMgrName()));
216                }
217                if (ignoreUsedContext || (!ignoreUsedContext &&
218                      0 <= usedContext.indexOf(SharkConstants.PROC_MGR_PROCESS_DEFINITION_ID))) {
219                   context.put(SharkConstants.PROC_MGR_PROCESS_DEFINITION_ID,
220                               SharkUtilities.getProcessMgrProcDefId(po.getProcessMgrName()));
221                }
222                if (ignoreUsedContext || (!ignoreUsedContext &&
223                      0 <= usedContext.indexOf(SharkConstants.PROC_MGR_VERSION))) {
224                   context.put(SharkConstants.PROC_MGR_VERSION,
225                               SharkUtilities.getProcessMgrVersion(po.getProcessMgrName()));
226                }
227                if (ignoreUsedContext || (!ignoreUsedContext &&
228                      0 <= usedContext.indexOf(SharkConstants.PROC_MGR_NAME))) {
229                   context.put(SharkConstants.PROC_MGR_NAME,
230                               po.getProcessMgrName());
231                }
232
233                if (ignoreUsedContext || (!ignoreUsedContext &&
234                      0 <= usedContext.indexOf(SharkConstants.PROC_STATE))) {
235                   context.put(SharkConstants.PROC_STATE, po.getState());
236                }
237                if (ignoreUsedContext || (!ignoreUsedContext &&
238                      0 <= usedContext.indexOf(SharkConstants.PROC_KEY))) {
239                   context.put(SharkConstants.PROC_KEY, po.getId());
240                }
241                if (ignoreUsedContext || (!ignoreUsedContext &&
242                      0 <= usedContext.indexOf(SharkConstants.PROC_NAME))) {
243                   context.put(SharkConstants.PROC_NAME, po.getName());
244                }
245                if (ignoreUsedContext || (!ignoreUsedContext &&
246                      0 <= usedContext.indexOf(SharkConstants.PROC_PRIORITY))) {
247                   context.put(SharkConstants.PROC_PRIORITY,
248                               new Integer JavaDoc(po.getPriority()));
249                }
250                if (ignoreUsedContext || (!ignoreUsedContext &&
251                      0 <= usedContext.indexOf(SharkConstants.PROC_DESCRIPTION))) {
252                   context.put(SharkConstants.PROC_DESCRIPTION,
253                               po.getDescription());
254                }
255                if (ignoreUsedContext || (!ignoreUsedContext &&
256                      0 <= usedContext.indexOf(SharkConstants.PROC_REQUESTER_ID))) {
257                   context.put(SharkConstants.PROC_REQUESTER_ID,
258                               po.getActivityRequesterId());
259                }
260                if (ignoreUsedContext || (!ignoreUsedContext &&
261                      0 <= usedContext.indexOf(SharkConstants.PROC_REQUESTER_RESOURCE))) {
262                   context.put(SharkConstants.PROC_REQUESTER_RESOURCE,
263                               po.getResourceRequesterId());
264                }
265                if (ignoreUsedContext || (!ignoreUsedContext &&
266                      0 <= usedContext.indexOf(SharkConstants.PROC_CREATED_TIME_MS))) {
267                   context.put(SharkConstants.PROC_CREATED_TIME_MS,
268                               new Long JavaDoc(po.getCreatedTime()));
269                }
270                if (ignoreUsedContext || (!ignoreUsedContext &&
271                      0 <= usedContext.indexOf(SharkConstants.PROC_START_TIME_MS))) {
272                   context.put(SharkConstants.PROC_START_TIME_MS,
273                               new Long JavaDoc(po.getStartedTime()));
274                }
275                if (ignoreUsedContext || (!ignoreUsedContext &&
276                      0 <= usedContext.indexOf(SharkConstants.PROC_LAST_STATE_TIME_MS))) {
277                   context.put(SharkConstants.PROC_LAST_STATE_TIME_MS,
278                               new Long JavaDoc(po.getLastStateTime()));
279                }
280                if (queryExpression.indexOf(SharkConstants.PROC_ACTIVE_ACTIVITIES_NO) != -1) {
281                   context.put(SharkConstants.PROC_ACTIVE_ACTIVITIES_NO,
282                               new Long JavaDoc(ipm.getAllActiveActivitiesForProcess(po.getId(),
283                                                                             t)
284                                  .size()));
285                }
286                if (queryExpression.indexOf(SharkConstants.PROC_CONTEXT_) != -1) {
287                   List pc = ipm.getAllVariablesForProcess(po.getId(), t);
288                   if (pc != null) {
289                      Iterator iter = pc.iterator();
290                      while (iter.hasNext()) {
291                         ProcessVariablePersistenceInterface pvpo = (ProcessVariablePersistenceInterface) iter.next();
292                         try {
293                            String JavaDoc name = SharkConstants.PROC_CONTEXT_
294                                          + pvpo.getDefinitionId();
295                            Object JavaDoc value = pvpo.getValue();
296                            context.put(name, value);
297                         } catch (Exception JavaDoc ex) {
298                            throw new BaseException(ex);
299                         }
300                      }
301                   }
302                }
303
304                toAdd = evaluator.evaluateCondition(t,
305                                                    queryExpression,
306                                                    context);
307             }
308             if (toAdd) {
309                processes.add(SharkEngineManager.getInstance()
310                   .getObjectFactory()
311                   .createProcessWrapper(userAuth, po.getProcessMgrName(), po.getId()));
312             }
313
314          }
315          setObjectList(processes);
316       } catch (Exception JavaDoc ex) {
317          throw new BaseException(ex);
318       }
319    }
320
321 }
Popular Tags