KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > instance > EntityPersistentMgr


1 /*
2  * $Id: EntityPersistentMgr.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.shark.instance;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.UtilMisc;
36 import org.ofbiz.base.util.UtilValidate;
37 import org.ofbiz.entity.GenericDelegator;
38 import org.ofbiz.entity.GenericEntityException;
39 import org.ofbiz.entity.GenericValue;
40 import org.ofbiz.entity.util.EntityListIterator;
41 import org.ofbiz.entity.model.DynamicViewEntity;
42 import org.ofbiz.entity.model.ModelKeyMap;
43 import org.ofbiz.entity.condition.EntityCondition;
44 import org.ofbiz.entity.condition.EntityConditionList;
45 import org.ofbiz.entity.condition.EntityExpr;
46 import org.ofbiz.entity.condition.EntityJoinOperator;
47 import org.ofbiz.entity.condition.EntityOperator;
48 import org.ofbiz.entity.condition.EntityComparisonOperator;
49 import org.ofbiz.shark.container.SharkContainer;
50
51 import org.enhydra.shark.api.RootException;
52 import org.enhydra.shark.api.SharkTransaction;
53 import org.enhydra.shark.api.internal.instancepersistence.*;
54 import org.enhydra.shark.api.internal.working.CallbackUtilities;
55
56 /**
57  * Shark Persistance Manager Implementation
58  *
59  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
60  * @version $Rev: 5462 $
61  * @since 3.1
62  */

63 public class EntityPersistentMgr implements PersistentManagerInterface {
64
65     public static final String JavaDoc module = EntityPersistentMgr.class.getName();
66
67     protected CallbackUtilities callBackUtil = null;
68
69     public void configure(CallbackUtilities callBackUtil) throws RootException {
70         this.callBackUtil = callBackUtil;
71     }
72
73     public void shutdownDatabase() throws PersistenceException {
74     }
75
76     // store methods
77
public void persist(ProcessMgrPersistenceInterface processMgr, SharkTransaction trans) throws PersistenceException {
78         try {
79             ((ProcessMgr) processMgr).store();
80         } catch (GenericEntityException e) {
81             throw new PersistenceException(e);
82         }
83     }
84
85     public void persist(ProcessPersistenceInterface process, SharkTransaction trans) throws PersistenceException {
86         try {
87             ((Process JavaDoc) process).store();
88         } catch (GenericEntityException e) {
89             throw new PersistenceException(e);
90         }
91     }
92
93     public void persistExternalRequester(String JavaDoc processId, Object JavaDoc req, SharkTransaction trans) throws PersistenceException {
94         ProcessPersistenceInterface proc = this.restoreProcess(processId, trans);
95         proc.setExternalRequester(req);
96         this.persist(proc, trans);
97     }
98
99     public void persist(ActivityPersistenceInterface activity, SharkTransaction trans) throws PersistenceException {
100         try {
101             ((Activity) activity).store();
102         } catch (GenericEntityException e) {
103             throw new PersistenceException(e);
104         }
105     }
106
107     public void persist(ResourcePersistenceInterface resource, SharkTransaction trans) throws PersistenceException {
108         try {
109             ((Resource) resource).store();
110         } catch (GenericEntityException e) {
111             throw new PersistenceException(e);
112         }
113     }
114
115     public void persist(AssignmentPersistenceInterface assignment, SharkTransaction trans) throws PersistenceException {
116         try {
117             ((Assignment) assignment).store();
118         } catch (GenericEntityException e) {
119             throw new PersistenceException(e);
120         }
121     }
122
123     public void persist(AssignmentPersistenceInterface assignment, String JavaDoc oldResUname, SharkTransaction trans) throws PersistenceException {
124         // TODO: look into this, if it is used only for re-assigning then have it expire/create new assignment
125
persist(assignment, trans);
126     }
127
128     public void persist(ProcessVariablePersistenceInterface processVariable, SharkTransaction trans) throws PersistenceException {
129         try {
130             ((ProcessVariable) processVariable).store();
131         } catch (GenericEntityException e) {
132             throw new PersistenceException(e);
133         }
134     }
135
136     public void persist(ActivityVariablePersistenceInterface activityVariable, SharkTransaction trans) throws PersistenceException {
137         try {
138             ((ActivityVariable) activityVariable).store();
139         } catch (GenericEntityException e) {
140             throw new PersistenceException(e);
141         }
142     }
143
144     public void persist(AndJoinEntryInterface andJoin, SharkTransaction trans) throws PersistenceException {
145         try {
146             ((AndJoinEntry) andJoin).store();
147         } catch (GenericEntityException e) {
148             throw new PersistenceException(e);
149         }
150     }
151
152     public void persist(DeadlinePersistenceInterface dpe, SharkTransaction ti) throws PersistenceException {
153         try {
154             ((Deadline) dpe).store();
155         } catch (GenericEntityException e) {
156             throw new PersistenceException(e);
157         }
158     }
159
160     // restore methods
161
public ProcessMgrPersistenceInterface restoreProcessMgr(String JavaDoc mgrName, SharkTransaction trans) throws PersistenceException {
162         return ProcessMgr.getInstance(this, mgrName);
163     }
164
165     public ProcessPersistenceInterface restoreProcess(String JavaDoc processId, SharkTransaction trans) throws PersistenceException {
166         return Process.getInstance(this, processId);
167     }
168
169     public ActivityPersistenceInterface restoreActivity(String JavaDoc activityId, SharkTransaction trans) throws PersistenceException {
170         return Activity.getInstance(this, activityId);
171     }
172
173     public ResourcePersistenceInterface restoreResource(String JavaDoc resourceId, SharkTransaction trans) throws PersistenceException {
174         return Resource.getInstance(this, resourceId);
175     }
176
177     public AssignmentPersistenceInterface restoreAssignment(String JavaDoc activityId, String JavaDoc userName, SharkTransaction trans) throws PersistenceException {
178         return Assignment.getInstance(this, activityId, userName);
179     }
180
181     public boolean restore(ProcessVariablePersistenceInterface processVariablePersistenceInterface, SharkTransaction trans) throws PersistenceException {
182         if (Debug.verboseOn()) Debug.log(":: ProcessVariablePersistenceInterface ::", module);
183         if (processVariablePersistenceInterface == null) {
184             return false;
185         }
186         try {
187             ((ProcessVariable) processVariablePersistenceInterface).reload();
188         } catch (GenericEntityException e) {
189             Debug.logError(e, module);
190             throw new PersistenceException(e);
191         }
192         return true;
193     }
194
195     public boolean restore(ActivityVariablePersistenceInterface activityVariablePersistenceInterface, SharkTransaction trans) throws PersistenceException {
196         if (Debug.verboseOn()) Debug.log(":: ActivityVariablePersistenceInterface ::", module);
197         if (activityVariablePersistenceInterface == null) {
198             return false;
199         }
200         try {
201             ((ActivityVariable) activityVariablePersistenceInterface).reload();
202         } catch (GenericEntityException e) {
203             Debug.logError(e, module);
204             throw new PersistenceException(e);
205         }
206         return true;
207     }
208
209     // remove/delete methods
210
public void deleteProcessMgr(String JavaDoc mgrName, SharkTransaction trans) throws PersistenceException {
211         if (Debug.infoOn()) Debug.log(":: deleteProcessMgr ::", module);
212         try {
213             ((ProcessMgr) restoreProcessMgr(mgrName, trans)).remove();
214         } catch (GenericEntityException e) {
215             throw new PersistenceException(e);
216         }
217     }
218
219     public void deleteProcess(String JavaDoc processId, boolean admin, SharkTransaction trans) throws PersistenceException {
220         if (admin) {
221             if (Debug.infoOn()) Debug.log(":: deleteProcess ::", module);
222             Process JavaDoc process = (Process JavaDoc) this.restoreProcess(processId, trans);
223             List JavaDoc activities = this.getAllActivitiesForProcess(processId, trans);
224             if (activities != null) {
225                 Iterator JavaDoc i = activities.iterator();
226                 while (i.hasNext()) {
227                     Activity activity = (Activity) i.next();
228                     try {
229                         activity.remove();
230                     } catch (GenericEntityException e) {
231                         throw new PersistenceException(e);
232                     }
233                 }
234             }
235
236             try {
237                 process.remove();
238             } catch (GenericEntityException e) {
239                 throw new PersistenceException(e);
240             }
241         }
242     }
243
244     public void deleteActivity(String JavaDoc activityId, SharkTransaction trans) throws PersistenceException {
245         if (Debug.infoOn()) Debug.log(":: deleteActivity ::", module);
246         Activity activity = (Activity) this.restoreActivity(activityId, trans);
247         List JavaDoc assignments = this.getAllAssignmentsForActivity(activityId, trans);
248         if (assignments != null) {
249             Iterator JavaDoc i = assignments.iterator();
250             while (i.hasNext()) {
251                 Assignment assignment = (Assignment) i.next();
252                 try {
253                     assignment.remove();
254                 } catch (GenericEntityException e) {
255                     throw new PersistenceException(e);
256                 }
257             }
258
259             try {
260                 activity.remove();
261             } catch (GenericEntityException e) {
262                 throw new PersistenceException(e);
263             }
264         }
265     }
266
267     public void deleteResource(String JavaDoc userName, SharkTransaction trans) throws PersistenceException {
268         if (Debug.infoOn()) Debug.log(":: deleteResource ::", module);
269         try {
270             ((Resource) restoreResource(userName, trans)).remove();
271         } catch (GenericEntityException e) {
272             throw new PersistenceException(e);
273         }
274     }
275
276     public void deleteAssignment(String JavaDoc activityId, String JavaDoc userName, SharkTransaction trans) throws PersistenceException {
277         if (Debug.infoOn()) Debug.log(":: deleteAssignment ::", module);
278         try {
279             ((Assignment) restoreAssignment(activityId, userName, trans)).remove();
280         } catch (GenericEntityException e) {
281             throw new PersistenceException(e);
282         }
283     }
284
285     public void deleteAndJoinEntries(String JavaDoc procId, String JavaDoc asDefId, String JavaDoc aDefId, SharkTransaction trans) throws PersistenceException {
286         if (Debug.infoOn()) Debug.log(":: deleteAndJoinEntries ::", module);
287         GenericDelegator delegator = SharkContainer.getDelegator();
288         try {
289             delegator.removeByAnd("WfAndJoin", UtilMisc.toMap("processId", procId,
290                     "activitySetDefId", asDefId, "activityDefId", aDefId));
291         } catch (GenericEntityException e) {
292             throw new PersistenceException(e);
293         }
294     }
295
296     public void deleteDeadlines(String JavaDoc procId, SharkTransaction trans) throws PersistenceException {
297         GenericDelegator delegator = SharkContainer.getDelegator();
298         try {
299             delegator.removeByAnd("WfDeadline", UtilMisc.toMap("processId", procId));
300         } catch (GenericEntityException e) {
301             throw new PersistenceException(e);
302         }
303     }
304
305     public void deleteDeadlines(String JavaDoc procId, String JavaDoc actId, SharkTransaction trans) throws PersistenceException {
306         GenericDelegator delegator = SharkContainer.getDelegator();
307         try {
308             delegator.removeByAnd("WfDeadline", UtilMisc.toMap("processId", procId, "activityId", actId));
309         } catch (GenericEntityException e) {
310             throw new PersistenceException(e);
311         }
312     }
313
314     public void delete(ProcessVariablePersistenceInterface processVariable, SharkTransaction trans) throws PersistenceException {
315         // don't delete variables
316
}
317
318     public void delete(ActivityVariablePersistenceInterface activityVariable, SharkTransaction trans) throws PersistenceException {
319         // don't delete variables
320
}
321
322     public List JavaDoc getProcessMgrsWhere(SharkTransaction trans, String JavaDoc sqlWhere) throws PersistenceException {
323         if (sqlWhere != null) {
324             Debug.log("Attempt to call getProcessMgrsWhere() - " + sqlWhere, module);
325             throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")");
326         }
327         return this.getAllProcessMgrs(trans);
328     }
329
330     public List JavaDoc getAllProcessMgrs(SharkTransaction trans) throws PersistenceException {
331         if (Debug.verboseOn()) Debug.log(":: getAllProcessMgrs ::", module);
332         GenericDelegator delegator = SharkContainer.getDelegator();
333         List JavaDoc createdList = new ArrayList JavaDoc();
334         List JavaDoc lookupList = null;
335         try {
336             lookupList = delegator.findAll("WfProcessMgr");
337         } catch (GenericEntityException e) {
338             Debug.logError(e, module);
339             throw new PersistenceException(e);
340         }
341         if (lookupList != null && lookupList.size() > 0) {
342             Iterator JavaDoc i = lookupList.iterator();
343             while (i.hasNext()) {
344                 GenericValue v = (GenericValue) i.next();
345                 createdList.add(ProcessMgr.getInstance(this, v));
346             }
347         }
348         if (Debug.verboseOn()) Debug.log("ProcessMgr : " + createdList.size(), module);
349         return createdList;
350     }
351
352     public List JavaDoc getResourcesWhere(SharkTransaction trans, String JavaDoc sqlWhere) throws PersistenceException {
353         if (sqlWhere != null) {
354             Debug.log("Attempt to call getResourcesWhere() - " + sqlWhere, module);
355             throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")");
356         }
357         return this.getAllResources(trans);
358     }
359
360     public List JavaDoc getAllResources(SharkTransaction trans) throws PersistenceException {
361         if (Debug.verboseOn()) Debug.log(":: getAllResources ::", module);
362         GenericDelegator delegator = SharkContainer.getDelegator();
363         List JavaDoc createdList = new ArrayList JavaDoc();
364         List JavaDoc lookupList = null;
365         try {
366             lookupList = delegator.findAll("WfResource");
367         } catch (GenericEntityException e) {
368             throw new PersistenceException(e);
369         }
370         if (lookupList != null && lookupList.size() > 0) {
371             Iterator JavaDoc i = lookupList.iterator();
372             while (i.hasNext()) {
373                 GenericValue v = (GenericValue) i.next();
374                 createdList.add(Resource.getInstance(this, v));
375             }
376         }
377         return createdList;
378     }
379
380     public List JavaDoc getAllAssignments(SharkTransaction trans) throws PersistenceException {
381         if (Debug.verboseOn()) Debug.log(":: getAllAssignments ::", module);
382         GenericDelegator delegator = SharkContainer.getDelegator();
383         List JavaDoc createdList = new ArrayList JavaDoc();
384         List JavaDoc lookupList = null;
385         try {
386             lookupList = delegator.findAll("WfAssignment");
387         } catch (GenericEntityException e) {
388             throw new PersistenceException(e);
389         }
390         if (lookupList != null && lookupList.size() > 0) {
391             Iterator JavaDoc i = lookupList.iterator();
392             while (i.hasNext()) {
393                 GenericValue v = (GenericValue) i.next();
394                 createdList.add(Assignment.getInstance(this, v));
395             }
396         }
397         return createdList;
398     }
399
400     public List JavaDoc getAllProcesses(SharkTransaction trans) throws PersistenceException {
401         if (Debug.verboseOn()) Debug.log(":: getAllProcesses ::", module);
402         GenericDelegator delegator = SharkContainer.getDelegator();
403         List JavaDoc createdList = new ArrayList JavaDoc();
404         List JavaDoc lookupList = null;
405         try {
406             lookupList = delegator.findAll("WfProcess");
407         } catch (GenericEntityException e) {
408             throw new PersistenceException(e);
409         }
410         if (lookupList != null && lookupList.size() > 0) {
411             Iterator JavaDoc i = lookupList.iterator();
412             while (i.hasNext()) {
413                 GenericValue v = (GenericValue) i.next();
414                 createdList.add(Process.getInstance(this, v));
415             }
416         }
417         return createdList;
418     }
419
420     public List JavaDoc getAllActivities(SharkTransaction trans) throws PersistenceException {
421         if (Debug.verboseOn()) Debug.log(":: getAllActivities ::", module);
422         GenericDelegator delegator = SharkContainer.getDelegator();
423         List JavaDoc createdList = new ArrayList JavaDoc();
424         List JavaDoc lookupList = null;
425         try {
426             lookupList = delegator.findAll("WfActivity");
427         } catch (GenericEntityException e) {
428             throw new PersistenceException(e);
429         }
430         if (lookupList != null && lookupList.size() > 0) {
431             Iterator JavaDoc i = lookupList.iterator();
432             while (i.hasNext()) {
433                 GenericValue v = (GenericValue) i.next();
434                 createdList.add(Activity.getInstance(this, v));
435             }
436         }
437         return createdList;
438     }
439
440     public List JavaDoc getAllProcessesForMgr(String JavaDoc mgrName, SharkTransaction trans) throws PersistenceException {
441         if (Debug.verboseOn()) Debug.log(":: getAllProcessesForMgr ::", module);
442         return this.getProcessesForMgr(mgrName, null, trans);
443     }
444
445     public List JavaDoc getProcessesForMgr(String JavaDoc mgrName, String JavaDoc state, SharkTransaction trans) throws PersistenceException {
446         if (Debug.verboseOn()) Debug.log(":: getProcessesForMgr ::", module);
447         GenericDelegator delegator = SharkContainer.getDelegator();
448         List JavaDoc createdList = new ArrayList JavaDoc();
449         List JavaDoc lookupList = null;
450
451         Map JavaDoc findBy = UtilMisc.toMap("mgrName", mgrName);
452         if (state != null) {
453             findBy.put("currentState", state);
454         }
455
456         try {
457             lookupList = delegator.findByAnd("WfProcess", findBy);
458         } catch (GenericEntityException e) {
459             throw new PersistenceException(e);
460         }
461         if (lookupList != null && lookupList.size() > 0) {
462             Iterator JavaDoc i = lookupList.iterator();
463             while (i.hasNext()) {
464                 GenericValue v = (GenericValue) i.next();
465                 createdList.add(Process.getInstance(this, v));
466             }
467         }
468         return createdList;
469     }
470
471     public List JavaDoc getAllRunningProcesses(SharkTransaction trans) throws PersistenceException {
472         GenericDelegator delegator = SharkContainer.getDelegator();
473         List JavaDoc runningStates = UtilMisc.toList("open.running");
474         List JavaDoc order = UtilMisc.toList("startedTime");
475         List JavaDoc createdList = new ArrayList JavaDoc();
476         List JavaDoc lookupList = null;
477         try {
478             lookupList = delegator.findByCondition("WfProcess",
479                     makeStateListCondition("currentState", runningStates, EntityOperator.EQUALS, EntityOperator.OR), null, order);
480         } catch (GenericEntityException e) {
481             throw new PersistenceException(e);
482         }
483         if (!UtilValidate.isEmpty(lookupList)) {
484             Iterator JavaDoc i = lookupList.iterator();
485             while (i.hasNext()) {
486                 GenericValue v = (GenericValue) i.next();
487                 createdList.add(Process.getInstance(this, v));
488             }
489         }
490         return createdList;
491     }
492
493     protected List JavaDoc findFinishedProcesses(SharkTransaction trans, String JavaDoc packageId, String JavaDoc processDefId, String JavaDoc packageVer, Date JavaDoc finishedBefore) throws PersistenceException {
494         GenericDelegator delegator = SharkContainer.getDelegator();
495         List JavaDoc finsihedStates = UtilMisc.toList("closed.completed", "closed.terminated", "closed.aborted");
496         List JavaDoc order = UtilMisc.toList("lastStateTime");
497         List JavaDoc createdList = new ArrayList JavaDoc();
498         List JavaDoc lookupList = null;
499
500         try {
501             EntityCondition stateCond = this.makeStateListCondition("currentState", finsihedStates, EntityOperator.EQUALS, EntityOperator.OR);
502             EntityCondition cond = this.makeProcessFilterCondition(stateCond, packageId, processDefId, packageVer, finishedBefore);
503             lookupList = delegator.findByCondition("WfProcess", cond, null, order);
504         } catch (GenericEntityException e) {
505             throw new PersistenceException(e);
506         }
507         if (!UtilValidate.isEmpty(lookupList)) {
508             Iterator JavaDoc i = lookupList.iterator();
509             while (i.hasNext()) {
510                 GenericValue v = (GenericValue) i.next();
511                 createdList.add(Process.getInstance(this, v));
512             }
513         }
514         return createdList;
515     }
516
517     public List JavaDoc getAllFinishedProcesses(SharkTransaction trans) throws PersistenceException {
518         return this.findFinishedProcesses(trans, null, null, null, null);
519     }
520
521     public List JavaDoc getAllFinishedProcesses(SharkTransaction trans, Date JavaDoc finishedBefore) throws PersistenceException {
522         return this.findFinishedProcesses(trans, null, null, null, finishedBefore);
523     }
524
525     public List JavaDoc getAllFinishedProcesses(SharkTransaction trans, String JavaDoc packageId) throws PersistenceException {
526         return this.findFinishedProcesses(trans, packageId, null, null, null);
527     }
528
529     public List JavaDoc getAllFinishedProcesses(SharkTransaction trans, String JavaDoc packageId, String JavaDoc processDefId) throws PersistenceException {
530         return this.findFinishedProcesses(trans, packageId, processDefId, null, null);
531     }
532
533     public List JavaDoc getAllFinishedProcesses(SharkTransaction trans, String JavaDoc packageId, String JavaDoc processDefId, String JavaDoc packageVer) throws PersistenceException {
534         return this.findFinishedProcesses(trans, packageId, processDefId, packageVer, null);
535     }
536
537     private EntityCondition makeStateListCondition(String JavaDoc field, List JavaDoc states, EntityComparisonOperator operator, EntityJoinOperator jop) throws GenericEntityException {
538         if (states != null) {
539             if (states.size() > 1) {
540                 List JavaDoc exprs = new LinkedList JavaDoc();
541                 Iterator JavaDoc i = states.iterator();
542                 while (i.hasNext()) {
543                     exprs.add(new EntityExpr(field, operator, i.next()));
544                 }
545                 return new EntityConditionList(exprs, jop);
546             } else {
547                 return new EntityExpr(field, operator, states.get(0));
548             }
549         } else {
550             throw new GenericEntityException("Cannot create entity condition from list :" + states);
551         }
552     }
553
554     private EntityCondition makeProcessFilterCondition(EntityCondition cond, String JavaDoc packageId, String JavaDoc processDefId, String JavaDoc packageVer, Date JavaDoc finishBefore) {
555         EntityCondition newCond = null;
556         List JavaDoc exprs = new LinkedList JavaDoc();
557         if (packageId != null) {
558             exprs.add(new EntityExpr("packageId", EntityOperator.EQUALS, packageId));
559         }
560         if (processDefId != null) {
561             exprs.add(new EntityExpr("definitionId", EntityOperator.EQUALS, processDefId));
562         }
563         if (packageVer != null) {
564             exprs.add(new EntityExpr("packageVer", EntityOperator.EQUALS, packageVer));
565         }
566         if (finishBefore != null) {
567             exprs.add(new EntityExpr("lastStateTime", EntityOperator.LESS_THAN, finishBefore));
568         }
569
570         if (exprs.size() > 0) {
571             newCond = new EntityConditionList(exprs, EntityJoinOperator.AND);
572         }
573
574         if (newCond != null) {
575             if (cond != null) {
576                 return new EntityConditionList(UtilMisc.toList(cond, newCond), EntityJoinOperator.AND);
577             } else {
578                 return newCond;
579             }
580         } else {
581             return cond;
582         }
583     }
584
585     protected List JavaDoc findProcessActivities(String JavaDoc processId, List JavaDoc states, EntityComparisonOperator operator, SharkTransaction trans) throws PersistenceException {
586         GenericDelegator delegator = SharkContainer.getDelegator();
587         List JavaDoc createdList = new ArrayList JavaDoc();
588         List JavaDoc order = UtilMisc.toList("lastStateTime");
589         List JavaDoc lookupList = null;
590         try {
591             EntityCondition cond = null;
592             EntityCondition proc = new EntityExpr("processId", EntityOperator.EQUALS, processId);
593
594             if (states != null) {
595                 EntityCondition stateCond = this.makeStateListCondition("currentState", states, operator, EntityOperator.OR);
596                 cond = new EntityConditionList(UtilMisc.toList(proc, stateCond), EntityOperator.AND);
597             } else {
598                 cond = proc;
599             }
600             lookupList = delegator.findByCondition("WfActivity", cond, null, order);
601         } catch (GenericEntityException e) {
602             throw new PersistenceException(e);
603         }
604         if (lookupList != null && lookupList.size() > 0) {
605             Iterator JavaDoc i = lookupList.iterator();
606             while (i.hasNext()) {
607                 GenericValue v = (GenericValue) i.next();
608                 createdList.add(Activity.getInstance(this, v));
609             }
610         }
611         return createdList;
612     }
613
614     public List JavaDoc getAllActivitiesForProcess(String JavaDoc processId, SharkTransaction trans) throws PersistenceException {
615         return this.findProcessActivities(processId, null, null, trans);
616     }
617
618     public List JavaDoc getActivitiesForProcess(String JavaDoc processId, String JavaDoc actState, SharkTransaction trans) throws PersistenceException {
619         return this.findProcessActivities(processId, UtilMisc.toList(actState), null, trans);
620     }
621
622     public List JavaDoc getAllFinishedActivitiesForProcess(String JavaDoc processId, SharkTransaction trans) throws PersistenceException {
623         List JavaDoc finishedStates = UtilMisc.toList("closed.completed", "closed.terminated", "closed.aborted");
624         return this.findProcessActivities(processId, finishedStates, EntityOperator.EQUALS, trans);
625     }
626
627     public List JavaDoc getAllActiveActivitiesForProcess(String JavaDoc processId, SharkTransaction trans) throws PersistenceException {
628         List JavaDoc finishedStates = UtilMisc.toList("closed.completed", "closed.terminated", "closed.aborted");
629         return this.findProcessActivities(processId, finishedStates, EntityOperator.NOT_EQUAL, trans);
630     }
631
632     /**
633     * Returns all assignments for the resource, no matter if its activity
634     * is in "closed" state (or some of its sub-states).
635     */

636     public List JavaDoc getAllAssignmentsForResource(String JavaDoc user, SharkTransaction trans) throws PersistenceException {
637         GenericDelegator delegator = SharkContainer.getDelegator();
638         List JavaDoc createdList = new ArrayList JavaDoc();
639         List JavaDoc lookupList = null;
640         try {
641             lookupList = delegator.findByAnd("WfAssignment", UtilMisc.toMap("userName", user));
642         } catch (GenericEntityException e) {
643             throw new PersistenceException(e);
644         }
645         if (lookupList != null && lookupList.size() > 0) {
646             Iterator JavaDoc i = lookupList.iterator();
647             while (i.hasNext()) {
648                 GenericValue v = (GenericValue) i.next();
649                 createdList.add(Assignment.getInstance(this, v));
650             }
651         }
652         return createdList;
653     }
654
655     /**
656     * Returns all assignments which activity is not in "closed" state, or some
657     * of its sub-states.
658     */

659     public List JavaDoc getAllAssignmentsForNotClosedActivitiesForResource(String JavaDoc user, SharkTransaction trans) throws PersistenceException {
660         List JavaDoc allAssignments = getAllAssignmentsForResource(user, trans);
661         List JavaDoc notClosed = new ArrayList JavaDoc();
662         Iterator JavaDoc i = allAssignments.iterator();
663         while (i.hasNext()) {
664             Assignment as = (Assignment) i.next();
665             Activity at = Activity.getInstance(this, as.getActivityId());
666             if (!at.getState().startsWith("closed")) {
667                 notClosed.add(as);
668             }
669         }
670         return notClosed;
671     }
672
673     /**
674     * Returns only the assignments that can be currently executed by the resource
675     * with a given username. This means the ones which activity is not finished
676     * and not accepted (it doesn't have getResourceUsername() field set), and the
677     * ones which activity is accepted by this resource (its getResourceUsername()
678     * field is set to the resource with given username).
679     */

680     public List JavaDoc getAllValidAssignmentsForResource(String JavaDoc user, SharkTransaction trans) throws PersistenceException {
681         List JavaDoc allAssignments = getAllAssignmentsForResource(user, trans);
682         List JavaDoc valid = new ArrayList JavaDoc();
683         Iterator JavaDoc i = allAssignments.iterator();
684         while (i.hasNext()) {
685             Assignment as = (Assignment) i.next();
686             Activity at = Activity.getInstance(this, as.getActivityId());
687             if (!at.getState().startsWith("closed")) {
688                 if (at.getResourceUsername() == null || user.equals(at.getResourceUsername())) {
689                     valid.add(as);
690                 }
691             }
692         }
693         return valid;
694     }
695
696     /**
697     * Returns all assignments that are ever created for that activity, no
698     * matter if activity is already in "closed" state or some of its sub-states.
699     */

700     public List JavaDoc getAllAssignmentsForActivity(String JavaDoc activityId, SharkTransaction trans) throws PersistenceException {
701         GenericDelegator delegator = SharkContainer.getDelegator();
702         List JavaDoc createdList = new ArrayList JavaDoc();
703         List JavaDoc lookupList = null;
704         try {
705             lookupList = delegator.findByAnd("WfAssignment", UtilMisc.toMap("activityId", activityId));
706         } catch (GenericEntityException e) {
707             throw new PersistenceException(e);
708         }
709         if (lookupList != null && lookupList.size() > 0) {
710             Iterator JavaDoc i = lookupList.iterator();
711             while (i.hasNext()) {
712                 GenericValue v = (GenericValue) i.next();
713                 createdList.add(Assignment.getInstance(this, v));
714             }
715         }
716         return createdList;
717     }
718
719     /**
720     * If activity is in "closed" state, or some of its sub-states, returns an
721     * empty list, otherwise returns all assignments that are ever created for
722     * that activity.
723     */

724     public List JavaDoc getAllAssignmentsForNotClosedActivity(String JavaDoc activityId, SharkTransaction trans) throws PersistenceException {
725         Activity at = Activity.getInstance(this, activityId);
726         if (at.getState().startsWith("closed")) {
727             return new ArrayList JavaDoc();
728         } else {
729             return getAllAssignmentsForActivity(activityId, trans);
730         }
731     }
732
733     /**
734     * If activity is in "closed" state, or some of its sub-states, returns an
735     * empty list, otherwise it returns either all assignments that are ever
736     * created for that activity if activity is not accepted, or just the
737     * assignment for the resource that accepted activity.
738     */

739     public List JavaDoc getAllValidAssignmentsForActivity(String JavaDoc activityId, SharkTransaction trans) throws PersistenceException {
740         Activity at = Activity.getInstance(this, activityId);
741         if (at.getState().startsWith("closed")) {
742             return new ArrayList JavaDoc();
743         }
744
745         List JavaDoc assignments = getAllAssignmentsForActivity(activityId, trans);
746         if (at.getResourceUsername() == null) {
747             return assignments;
748         }
749
750         List JavaDoc valid = new ArrayList JavaDoc();
751         Iterator JavaDoc i = assignments.iterator();
752         while (i.hasNext()) {
753             Assignment as = (Assignment) i.next();
754             if (at.getResourceUsername().equals(as.getResourceUsername())) {
755                 valid.add(as);
756             }
757         }
758         return valid;
759     }
760
761     public List JavaDoc getAllVariablesForProcess(String JavaDoc processId, SharkTransaction trans) throws PersistenceException {
762         if (Debug.verboseOn()) Debug.log(":: getAllVariablesForProcess ::", module);
763         GenericDelegator delegator = SharkContainer.getDelegator();
764         List JavaDoc createdList = new ArrayList JavaDoc();
765         List JavaDoc lookupList = null;
766         try {
767             lookupList = delegator.findByAnd("WfProcessVariable", UtilMisc.toMap("processId", processId));
768         } catch (GenericEntityException e) {
769             Debug.logError(e, module);
770             throw new PersistenceException(e);
771         }
772         if (lookupList != null && lookupList.size() > 0) {
773             Debug.log("Lookup list contains : " + lookupList.size(), module);
774             Iterator JavaDoc i = lookupList.iterator();
775             while (i.hasNext()) {
776                 GenericValue v = (GenericValue) i.next();
777                 createdList.add(ProcessVariable.getInstance(this, v));
778             }
779         } else {
780             Debug.log("Lookup list empty", module);
781         }
782
783         if (Debug.verboseOn()) Debug.log("Returning list : " + createdList.size(), module);
784         //Debug.log(new Exception(), "Stack Trace", module);
785
return createdList;
786     }
787
788     public List JavaDoc getAllVariablesForActivity(String JavaDoc activityId, SharkTransaction trans) throws PersistenceException {
789         if (Debug.verboseOn()) Debug.log(":: getAllVariablesForActivity ::", module);
790         GenericDelegator delegator = SharkContainer.getDelegator();
791         List JavaDoc createdList = new ArrayList JavaDoc();
792         List JavaDoc lookupList = null;
793         try {
794             lookupList = delegator.findByAnd("WfActivityVariable", UtilMisc.toMap("activityId", activityId));
795         } catch (GenericEntityException e) {
796             throw new PersistenceException(e);
797         }
798         if (lookupList != null && lookupList.size() > 0) {
799             Iterator JavaDoc i = lookupList.iterator();
800             while (i.hasNext()) {
801                 GenericValue v = (GenericValue) i.next();
802                 createdList.add(ActivityVariable.getInstance(this, v));
803             }
804         }
805         return createdList;
806     }
807
808     public List JavaDoc getResourceRequestersProcessIds(String JavaDoc userName, SharkTransaction trans) throws PersistenceException {
809         GenericDelegator delegator = SharkContainer.getDelegator();
810         List JavaDoc idList = new ArrayList JavaDoc();
811         List JavaDoc lookupList = null;
812         try {
813             lookupList = delegator.findByAnd("WfProcess", UtilMisc.toMap("resourceReqId", userName));
814         } catch (GenericEntityException e) {
815             throw new PersistenceException(e);
816         }
817         if (!UtilValidate.isEmpty(lookupList)) {
818             Iterator JavaDoc i = lookupList.iterator();
819             while (i.hasNext()) {
820                 GenericValue v = (GenericValue) i.next();
821                 idList.add(v.getString("processId"));
822             }
823         }
824         return idList;
825     }
826
827     public List JavaDoc getAndJoinEntries(String JavaDoc procId, String JavaDoc asDefId, String JavaDoc aDefId, SharkTransaction trans) throws PersistenceException {
828         List JavaDoc createdList = new ArrayList JavaDoc();
829         List JavaDoc lookupList = getAndJoinValues(procId, asDefId, aDefId);
830
831         if (lookupList != null && lookupList.size() > 0) {
832             Iterator JavaDoc i = lookupList.iterator();
833             while (i.hasNext()) {
834                 GenericValue v = (GenericValue) i.next();
835                 createdList.add(AndJoinEntry.getInstance(this, v));
836             }
837         }
838         return createdList;
839     }
840
841     public int howManyAndJoinEntries(String JavaDoc procId, String JavaDoc asDefId, String JavaDoc aDefId, SharkTransaction trans) throws PersistenceException {
842         List JavaDoc lookupList = getAndJoinValues(procId, asDefId, aDefId);
843         return lookupList.size();
844     }
845
846     public List JavaDoc getAllDeadlinesForProcess(String JavaDoc procId, SharkTransaction trans) throws PersistenceException {
847         List JavaDoc lookupList = getDeadlineValues(UtilMisc.toList(new EntityExpr("processId", EntityOperator.EQUALS, procId)));
848         return getDealineObjects(lookupList);
849     }
850
851     public List JavaDoc getAllDeadlinesForProcess(String JavaDoc procId, long timeLimit, SharkTransaction trans) throws PersistenceException {
852         List JavaDoc lookupList = getDeadlineValues(UtilMisc.toList(new EntityExpr("processId", EntityOperator.EQUALS, procId),
853                 new EntityExpr("timeLimit", EntityOperator.LESS_THAN, new Long JavaDoc(timeLimit))));
854         return getDealineObjects(lookupList);
855     }
856
857     public List JavaDoc getAllIdsForProcessesWithExpiriedDeadlines(long l, SharkTransaction trans) throws PersistenceException {
858         if (Debug.infoOn()) Debug.log(":: getAllIdsForProcessesWithExpiriedDeadlines ::", module);
859         GenericDelegator delegator = SharkContainer.getDelegator();
860         List JavaDoc processIds = new ArrayList JavaDoc();
861
862         DynamicViewEntity view = new DynamicViewEntity();
863         view.addMemberEntity("WFDL", "WfDeadline");
864         view.addMemberEntity("WFPR", "WfProcess");
865         view.addMemberEntity("WFAC", "WfActivity");
866         view.addAlias("WFPR", "currentState", "processState", null, null, null, null);
867         view.addAlias("WFAC", "currentState", "activityState", null, null, null, null);
868         view.addViewLink("WFDL", "WFPR", Boolean.FALSE, ModelKeyMap.makeKeyMapList("processId"));
869         view.addViewLink("WFDL", "WFAC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("activityId"));
870
871         EntityListIterator eli = null;
872         try {
873             EntityCondition procState = new EntityExpr("processState", EntityOperator.EQUALS, "open.running");
874             EntityCondition actState1 = new EntityExpr("activityState", EntityOperator.EQUALS, "open.not_running.not_started");
875             EntityCondition actState2 = new EntityExpr("activityState", EntityOperator.EQUALS, "open.running");
876
877             EntityCondition actState = new EntityConditionList(UtilMisc.toList(actState1, actState2), EntityOperator.OR);
878             EntityCondition timeCond = new EntityExpr("timeLimit", EntityOperator.LESS_THAN, new Long JavaDoc(l));
879
880             EntityCondition cond = new EntityConditionList(UtilMisc.toList(timeCond, procState, actState), EntityOperator.AND);
881             eli = delegator.findListIteratorByCondition(view, cond, null, null, null, null);
882             GenericValue v = null;
883             while ((v = (GenericValue) eli.next()) != null) {
884                 processIds.add(v.getString("processId"));
885             }
886         } catch (GenericEntityException e) {
887             throw new PersistenceException(e);
888         } finally {
889             if (eli != null) {
890                 try {
891                     eli.close();
892                 } catch (GenericEntityException e) {
893                     throw new PersistenceException(e);
894                 }
895             }
896         }
897         return processIds;
898     }
899
900     public List JavaDoc getAllDeadlinesForActivity(String JavaDoc procId, String JavaDoc actId, SharkTransaction trans) throws PersistenceException {
901         List JavaDoc lookupList = getDeadlineValues(UtilMisc.toList(new EntityExpr("processId", EntityOperator.EQUALS, procId),
902                 new EntityExpr("activityId", EntityOperator.EQUALS, actId)));
903         return getDealineObjects(lookupList);
904     }
905
906     public List JavaDoc getAllDeadlinesForActivity(String JavaDoc procId, String JavaDoc actId, long timeLimit, SharkTransaction trans) throws PersistenceException {
907         List JavaDoc lookupList = getDeadlineValues(UtilMisc.toList(new EntityExpr("processId", EntityOperator.EQUALS, procId),
908                 new EntityExpr("activityId", EntityOperator.EQUALS, actId),
909                 new EntityExpr("timeLimit", EntityOperator.LESS_THAN, new Long JavaDoc(timeLimit))));
910         return getDealineObjects(lookupList);
911     }
912
913     public int getExecuteCount(String JavaDoc procId, String JavaDoc asDefId, String JavaDoc aDefId, SharkTransaction trans) throws PersistenceException {
914         GenericDelegator delegator = SharkContainer.getDelegator();
915         long count = 0;
916         try {
917             count = delegator.findCountByAnd("WfActivity", UtilMisc.toMap("processId", procId, "setDefinitionId", asDefId, "definitionId", aDefId));
918         } catch (GenericEntityException e) {
919             throw new PersistenceException(e);
920         }
921
922         return (int) count;
923     }
924
925     private List JavaDoc getAndJoinValues(String JavaDoc processId, String JavaDoc activitySetDefId, String JavaDoc activityDefId) throws PersistenceException {
926         GenericDelegator delegator = SharkContainer.getDelegator();
927         List JavaDoc lookupList = null;
928         try {
929             lookupList = delegator.findByAnd("WfAndJoin", UtilMisc.toMap("processId", processId,
930                     "activitySetDefId", activitySetDefId, "activityDefId", activityDefId));
931         } catch (GenericEntityException e) {
932             throw new PersistenceException(e);
933         }
934         if (lookupList == null) {
935             lookupList = new ArrayList JavaDoc();
936         }
937         return lookupList;
938     }
939
940     private List JavaDoc getDeadlineValues(List JavaDoc exprList) throws PersistenceException {
941         GenericDelegator delegator = SharkContainer.getDelegator();
942         List JavaDoc lookupList = null;
943         if (exprList == null) {
944             lookupList = new ArrayList JavaDoc();
945         } else {
946             try {
947                 lookupList = delegator.findByAnd("WfDeadline", exprList);
948             } catch (GenericEntityException e) {
949                 throw new PersistenceException(e);
950             }
951             if (lookupList == null) {
952                 lookupList = new ArrayList JavaDoc();
953             }
954         }
955         return lookupList;
956     }
957
958     private List JavaDoc getDealineObjects(List JavaDoc deadlineValues) {
959         List JavaDoc deadlines = new ArrayList JavaDoc();
960         if (deadlineValues != null) {
961             Iterator JavaDoc i = deadlineValues.iterator();
962             while (i.hasNext()) {
963                 GenericValue v = (GenericValue) i.next();
964                 deadlines.add(Deadline.getInstance(this, v));
965             }
966         }
967         return deadlines;
968     }
969
970     // create methods
971
public ActivityPersistenceInterface createActivity() {
972         return new Activity(this, SharkContainer.getDelegator());
973     }
974
975     public ProcessPersistenceInterface createProcess() {
976         return new Process JavaDoc(this, SharkContainer.getDelegator());
977     }
978
979     public ProcessMgrPersistenceInterface createProcessMgr() {
980         return new ProcessMgr(this, SharkContainer.getDelegator());
981     }
982
983     public AssignmentPersistenceInterface createAssignment() {
984         return new Assignment(this, SharkContainer.getDelegator());
985     }
986
987     public ResourcePersistenceInterface createResource() {
988         return new Resource(this, SharkContainer.getDelegator());
989     }
990
991     public ProcessVariablePersistenceInterface createProcessVariable() {
992         return new ProcessVariable(this, SharkContainer.getDelegator());
993     }
994
995     public ActivityVariablePersistenceInterface createActivityVariable() {
996         return new ActivityVariable(this, SharkContainer.getDelegator());
997     }
998
999     public AndJoinEntryInterface createAndJoinEntry() {
1000        return new AndJoinEntry(this, SharkContainer.getDelegator());
1001    }
1002
1003    public DeadlinePersistenceInterface createDeadline() {
1004        return new Deadline(this, SharkContainer.getDelegator());
1005    }
1006
1007    public synchronized String JavaDoc getNextId(String JavaDoc string) throws PersistenceException {
1008        GenericDelegator delegator = SharkContainer.getDelegator();
1009        return delegator.getNextSeqId("SharkWorkflowSeq").toString();
1010    }
1011
1012    public List JavaDoc getAssignmentsWhere(SharkTransaction trans, String JavaDoc sqlWhere) throws PersistenceException {
1013        if (sqlWhere != null) {
1014            Debug.log("Attempt to call getAssignmentsWhere() - " + sqlWhere, module);
1015            throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")");
1016        }
1017        return this.getAllAssignments(trans);
1018    }
1019
1020    public List JavaDoc getProcessesWhere(SharkTransaction trans, String JavaDoc sqlWhere) throws PersistenceException {
1021        if (sqlWhere != null) {
1022            Debug.log("Attempt to call getProcessesWhere() - " + sqlWhere, module);
1023            throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")");
1024        }
1025        return this.getAllProcesses(trans);
1026    }
1027
1028    public List JavaDoc getActivitiesWhere(SharkTransaction trans, String JavaDoc sqlWhere) throws PersistenceException {
1029        if (sqlWhere != null) {
1030            Debug.log("Attempt to call getActivitiesWhere() - " + sqlWhere, module);
1031            throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")");
1032        }
1033        return this.getAllActivities(trans);
1034    }
1035}
1036
Popular Tags