KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > workflow > impl > WfExecutionObjectImpl


1 /*
2  * $Id: WfExecutionObjectImpl.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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.workflow.impl;
26
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.sql.Timestamp JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37
38 import org.ofbiz.base.util.BshUtil;
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.ObjectType;
41 import org.ofbiz.base.util.UtilMisc;
42 import org.ofbiz.base.util.string.FlexibleStringExpander;
43 import org.ofbiz.entity.GenericDelegator;
44 import org.ofbiz.entity.GenericEntityException;
45 import org.ofbiz.entity.GenericValue;
46 import org.ofbiz.entity.serialize.SerializeException;
47 import org.ofbiz.entity.serialize.XmlSerializer;
48 import org.ofbiz.service.DispatchContext;
49 import org.ofbiz.service.GenericDispatcher;
50 import org.ofbiz.service.GenericServiceException;
51 import org.ofbiz.service.LocalDispatcher;
52 import org.ofbiz.workflow.AlreadySuspended;
53 import org.ofbiz.workflow.CannotResume;
54 import org.ofbiz.workflow.CannotStop;
55 import org.ofbiz.workflow.CannotSuspend;
56 import org.ofbiz.workflow.EvaluationException;
57 import org.ofbiz.workflow.HistoryNotAvailable;
58 import org.ofbiz.workflow.InvalidData;
59 import org.ofbiz.workflow.InvalidState;
60 import org.ofbiz.workflow.NotRunning;
61 import org.ofbiz.workflow.NotSuspended;
62 import org.ofbiz.workflow.TransitionCondition;
63 import org.ofbiz.workflow.TransitionNotAllowed;
64 import org.ofbiz.workflow.UpdateNotAllowed;
65 import org.ofbiz.workflow.WfException;
66 import org.ofbiz.workflow.WfExecutionObject;
67 import org.ofbiz.workflow.WfUtil;
68
69 /**
70  * WfExecutionObjectImpl - Workflow Execution Object implementation
71  *
72  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
73  * @author David Ostrovsky (d.ostrovsky@gmx.de)
74  * @version $Rev: 5462 $
75  * @since 2.0
76  */

77 public abstract class WfExecutionObjectImpl implements WfExecutionObject {
78
79     public static final String JavaDoc module = WfExecutionObjectImpl.class.getName();
80     public static final String JavaDoc dispatcherName = "WFDispatcher";
81
82     protected String JavaDoc packageId = null;
83     protected String JavaDoc packageVersion = null;
84     protected String JavaDoc processId = null;
85     protected String JavaDoc processVersion = null;
86     protected String JavaDoc activityId = null;
87     protected String JavaDoc workEffortId = null;
88     protected GenericDelegator delegator = null;
89     protected List JavaDoc history = null;
90
91     public WfExecutionObjectImpl(GenericValue valueObject, String JavaDoc parentId) throws WfException {
92         this.packageId = valueObject.getString("packageId");
93         this.packageVersion = valueObject.getString("packageVersion");
94         this.processId = valueObject.getString("processId");
95         this.processVersion = valueObject.getString("processVersion");
96         if (valueObject.getEntityName().equals("WorkflowActivity")) {
97             this.activityId = valueObject.getString("activityId");
98         } else {
99             this.activityId = null;
100         }
101         this.delegator = valueObject.getDelegator();
102         createRuntime(parentId);
103     }
104
105     public WfExecutionObjectImpl(GenericDelegator delegator, String JavaDoc workEffortId) throws WfException {
106         this.delegator = delegator;
107         this.workEffortId = workEffortId;
108         this.packageId = getRuntimeObject().getString("workflowPackageId");
109         this.packageVersion = getRuntimeObject().getString("workflowPackageVersion");
110         this.processId = getRuntimeObject().getString("workflowProcessId");
111         this.processVersion = getRuntimeObject().getString("workflowProcessVersion");
112         this.activityId = getRuntimeObject().getString("workflowActivityId");
113         this.history = null;
114         if (Debug.verboseOn()) Debug.logVerbose(" Package ID: " + packageId + " V: " + packageVersion, module);
115         if (Debug.verboseOn()) Debug.logVerbose(" Process ID: " + processId + " V: " + processVersion, module);
116         if (Debug.verboseOn()) Debug.logVerbose("Activity ID: " + activityId, module);
117     }
118     
119     // creates the stored runtime workeffort data.
120
private void createRuntime(String JavaDoc parentId) throws WfException {
121         GenericValue valueObject = getDefinitionObject();
122         GenericValue dataObject = null;
123
124         workEffortId = getDelegator().getNextSeqId("WorkEffort").toString();
125         Map JavaDoc dataMap = new HashMap JavaDoc();
126         String JavaDoc weType = activityId != null ? "ACTIVITY" : "WORK_FLOW";
127
128         dataMap.put("workEffortId", workEffortId);
129         dataMap.put("workEffortTypeId", weType);
130         dataMap.put("workEffortParentId", parentId);
131         dataMap.put("workflowPackageId", packageId);
132         dataMap.put("workflowPackageVersion", packageVersion);
133         dataMap.put("workflowProcessId", processId);
134         dataMap.put("workflowProcessVersion", processVersion);
135         dataMap.put("workEffortName", valueObject.getString("objectName"));
136         dataMap.put("description", valueObject.getString("description"));
137         dataMap.put("createdDate", new Timestamp JavaDoc((new Date JavaDoc()).getTime()));
138         dataMap.put("estimatedStartDate", dataMap.get("createdDate"));
139         dataMap.put("lastModifiedDate", dataMap.get("createdDate"));
140         dataMap.put("priority", valueObject.getLong("objectPriority"));
141         dataMap.put("currentStatusId", WfUtil.getOFBStatus("open.not_running.not_started"));
142         if (activityId != null)
143             dataMap.put("workflowActivityId", activityId);
144         if (activityId != null && parentId != null) {
145             GenericValue parentWorkEffort = getWorkEffort(parentId);
146             if (parentWorkEffort != null && parentWorkEffort.get("sourceReferenceId") != null)
147                 dataMap.put("sourceReferenceId", parentWorkEffort.getString("sourceReferenceId"));
148         }
149
150         try {
151             dataObject = getDelegator().makeValue("WorkEffort", dataMap);
152             getDelegator().create(dataObject);
153             
154             String JavaDoc objectId = activityId != null ? activityId : processId;
155             if (Debug.verboseOn()) Debug.logVerbose("Created new runtime object [" + objectId + "] (Workeffort: " + runtimeKey() + ")", module);
156         } catch (GenericEntityException e) {
157             throw new WfException(e.getMessage(), e);
158         }
159     }
160     
161     protected void parseDescriptions(Map JavaDoc parseContext) throws WfException {
162         GenericValue runtime = getRuntimeObject();
163         String JavaDoc name = runtime.getString("workEffortName");
164         String JavaDoc desc = runtime.getString("description");
165         String JavaDoc nameExp = FlexibleStringExpander.expandString(name, parseContext);
166         String JavaDoc descExp = FlexibleStringExpander.expandString(desc, parseContext);
167         
168         boolean changed = false;
169         if (nameExp != null && !nameExp.equals(name)) {
170             changed = true;
171             runtime.set("workEffortName", nameExp);
172         }
173         if (descExp != null && !descExp.equals(desc)) {
174             changed = true;
175             runtime.set("description", descExp);
176         }
177         
178         if (changed) {
179             try {
180                 runtime.store();
181             } catch (GenericEntityException e) {
182                 throw new WfException(e.getMessage(), e);
183             }
184         }
185     }
186
187     /**
188      * @see org.ofbiz.workflow.WfExecutionObject#name()
189      */

190     public String JavaDoc name() throws WfException {
191         return getRuntimeObject().getString("workEffortName");
192     }
193    
194     /**
195      * @see org.ofbiz.workflow.WfExecutionObject#setName(java.lang.String)
196      */

197     public void setName(String JavaDoc newValue) throws WfException {
198         GenericValue dataObject = getRuntimeObject();
199
200         try {
201             dataObject.set("workEffortName", newValue);
202             dataObject.store();
203         } catch (GenericEntityException e) {
204             throw new WfException(e.getMessage(), e);
205         }
206     }
207    
208     /**
209      * @see org.ofbiz.workflow.WfExecutionObject#setPriority(long)
210      */

211     public void setPriority(long newValue) throws WfException {
212         GenericValue dataObject = getRuntimeObject();
213
214         try {
215             dataObject.set("priority", new Long JavaDoc(newValue));
216             dataObject.store();
217         } catch (GenericEntityException e) {
218             throw new WfException(e.getMessage(), e);
219         }
220     }
221
222     /**
223      * @see org.ofbiz.workflow.WfExecutionObject#priority()
224      */

225     public long priority() throws WfException {
226         if (getRuntimeObject().get("priority") != null)
227             return getRuntimeObject().getLong("priority").longValue();
228         return 0; // change to default priority value
229
}
230
231     /**
232      * @see org.ofbiz.workflow.WfExecutionObject#state()
233      */

234     public String JavaDoc state() throws WfException {
235         GenericValue statusObj = null;
236         String JavaDoc stateStr = null;
237
238         try {
239             statusObj = getRuntimeObject().getRelatedOne("CurrentStatusItem");
240         } catch (GenericEntityException e) {
241             throw new WfException(e.getMessage(), e);
242         }
243         if (statusObj != null)
244             stateStr = statusObj.getString("statusCode");
245
246         if (stateStr == null)
247             throw new WfException("Stored state is not a valid type.");
248             
249         if (Debug.verboseOn()) Debug.logVerbose("Current state: " + stateStr, module);
250         return stateStr;
251     }
252
253     /**
254      * @see org.ofbiz.workflow.WfExecutionObject#validStates()
255      */

256     public List JavaDoc validStates() throws WfException {
257         String JavaDoc statesArr[] = {"open.running", "open.not_running.not_started", "open.not_running.suspended",
258                 "closed.completed", "closed.terminated", "closed.aborted"};
259         ArrayList JavaDoc possibleStates = new ArrayList JavaDoc(Arrays.asList(statesArr));
260         String JavaDoc currentState = state();
261
262         if (currentState.startsWith("closed"))
263             return new ArrayList JavaDoc();
264         if (!currentState.startsWith("open"))
265             throw new WfException("Currently in an unknown state.");
266         if (currentState.equals("open.running")) {
267             possibleStates.remove("open.running");
268             possibleStates.remove("open.not_running.not_started");
269             return possibleStates;
270         }
271         if (currentState.equals("open.not_running.not_started")) {
272             possibleStates.remove("open.not_running.not_started");
273             possibleStates.remove("open.not_running.suspended");
274             possibleStates.remove("closed.completed");
275             possibleStates.remove("closed.terminated");
276             return possibleStates;
277         }
278         if (currentState.equals("open.not_running.suspended")) {
279             possibleStates.remove("open.not_running.suspended");
280             possibleStates.remove("open.not_running.not_started");
281             possibleStates.remove("closed.complete");
282             possibleStates.remove("closed.terminated");
283             return possibleStates;
284         }
285         return new ArrayList JavaDoc();
286     }
287
288     /**
289      * @see org.ofbiz.workflow.WfExecutionObject#howManyHistory()
290      */

291     public int howManyHistory() throws WfException, HistoryNotAvailable {
292         if (history.size() < 1)
293             throw new HistoryNotAvailable();
294         return history.size();
295     }
296
297     /**
298      * @see org.ofbiz.workflow.WfExecutionObject#abort()
299      */

300     public void abort() throws WfException, CannotStop, NotRunning {
301         Debug.logInfo("Aborting current state : " + state(), module);
302         String JavaDoc stateStr = "closed.aborted";
303         
304         if (!state().startsWith("open")) {
305             throw new NotRunning();
306         }
307         
308         if (!validStates().contains(stateStr)) {
309             throw new CannotStop();
310         }
311                 
312         changeState(stateStr);
313     }
314
315     /**
316      * @see org.ofbiz.workflow.WfExecutionObject#whileOpenType()
317      */

318     public List JavaDoc whileOpenType() throws WfException {
319         String JavaDoc[] list = {"running", "not_running"};
320
321         return Arrays.asList(list);
322     }
323
324     /**
325      * @see org.ofbiz.workflow.WfExecutionObject#whyNotRunningType()
326      */

327     public List JavaDoc whyNotRunningType() throws WfException {
328         String JavaDoc[] list = {"not_started", "suspended"};
329
330         return Arrays.asList(list);
331     }
332
333     /**
334      * @see org.ofbiz.workflow.WfExecutionObject#runtimeKey()
335      */

336     public String JavaDoc runtimeKey() throws WfException {
337         return getRuntimeObject().getString("workEffortId");
338     }
339
340     /**
341      * @see org.ofbiz.workflow.WfExecutionObject#key()
342      */

343     public String JavaDoc key() throws WfException {
344         if (activityId != null)
345             return activityId;
346         else
347             return processId;
348     }
349
350     /**
351      * @see org.ofbiz.workflow.WfExecutionObject#isMemberOfHistory(org.ofbiz.workflow.WfExecutionObject)
352      */

353     public boolean isMemberOfHistory(WfExecutionObject member) throws WfException {
354         return false;
355     }
356
357     /**
358      * @see org.ofbiz.workflow.WfExecutionObject#setProcessContext(java.util.Map)
359      */

360     public void setProcessContext(Map JavaDoc newValue) throws WfException, InvalidData, UpdateNotAllowed {
361         setSerializedData(newValue);
362     }
363
364     /**
365      * @see org.ofbiz.workflow.WfExecutionObject#setProcessContext(java.lang.String)
366      */

367     public void setProcessContext(String JavaDoc contextKey) throws WfException, InvalidData, UpdateNotAllowed {
368         GenericValue dataObject = getRuntimeObject();
369
370         try {
371             dataObject.set("runtimeDataId", contextKey);
372             dataObject.store();
373         } catch (GenericEntityException e) {
374             throw new WfException(e.getMessage(), e);
375         }
376     }
377
378     /**
379      * @see org.ofbiz.workflow.WfExecutionObject#contextKey()
380      */

381     public String JavaDoc contextKey() throws WfException {
382         if (getRuntimeObject().get("runtimeDataId") == null)
383             return null;
384         else
385             return getRuntimeObject().getString("runtimeDataId");
386     }
387  
388     /**
389      * @see org.ofbiz.workflow.WfExecutionObject#processContext()
390      */

391     public Map JavaDoc processContext() throws WfException {
392         return getContext();
393     }
394
395     /**
396      * @see org.ofbiz.workflow.WfExecutionObject#workflowStateType()
397      */

398     public List JavaDoc workflowStateType() throws WfException {
399         String JavaDoc[] list = {"open", "closed"};
400         return Arrays.asList(list);
401     }
402
403     /**
404      * @see org.ofbiz.workflow.WfExecutionObject#terminate()
405      */

406     public void terminate() throws WfException, CannotStop, NotRunning {
407         String JavaDoc stateStr = "closed.terminated";
408
409         if (!state().equals("open.running"))
410             throw new NotRunning();
411         if (!validStates().contains(stateStr))
412             throw new CannotStop();
413         changeState(stateStr);
414     }
415
416     /**
417      * @see org.ofbiz.workflow.WfExecutionObject#setDescription(java.lang.String)
418      */

419     public void setDescription(String JavaDoc newValue) throws WfException {
420         GenericValue valueObject = getDefinitionObject();
421
422         try {
423             valueObject.set("description", newValue);
424             valueObject.store();
425         } catch (GenericEntityException e) {
426             throw new WfException(e.getMessage(), e);
427         }
428     }
429
430     /**
431      * @see org.ofbiz.workflow.WfExecutionObject#description()
432      */

433     public String JavaDoc description() throws WfException {
434         return getDefinitionObject().getString("description");
435     }
436
437     /**
438      * @see org.ofbiz.workflow.WfExecutionObject#lastStateTime()
439      */

440     public Timestamp JavaDoc lastStateTime() throws WfException {
441         GenericValue dataObject = getRuntimeObject();
442
443         if (dataObject == null || dataObject.get("lastStatusUpdate") == null)
444             throw new WfException("No runtime object or status has never been set.");
445         return dataObject.getTimestamp("lastStatusUpdate");
446     }
447
448     /**
449      * @see org.ofbiz.workflow.WfExecutionObject#getSequenceHistory(int)
450      */

451     public List JavaDoc getSequenceHistory(int maxNumber) throws WfException,
452             HistoryNotAvailable {
453         return history;
454     }
455
456     /**
457      * @see org.ofbiz.workflow.WfExecutionObject#getIteratorHistory(java.lang.String, java.util.Map)
458      */

459     public Iterator JavaDoc getIteratorHistory(String JavaDoc query,
460         Map JavaDoc namesInQuery) throws WfException, HistoryNotAvailable {
461         return history.iterator();
462     }
463
464     /**
465      * @see org.ofbiz.workflow.WfExecutionObject#resume()
466      */

467     public void resume() throws WfException, CannotResume, NotRunning, NotSuspended {
468         if (!state().equals("open.not_running.suspended")) {
469             if (state().equals("open.not_running.not_started")) {
470                 throw new NotRunning();
471             } else if (state().startsWith("closed")) {
472                 throw new CannotResume();
473             } else {
474                 throw new NotSuspended();
475             }
476         } else {
477             changeState("open.running");
478         }
479     }
480
481     /**
482      * @see org.ofbiz.workflow.WfExecutionObject#howClosedType()
483      */

484     public List JavaDoc howClosedType() throws WfException {
485         String JavaDoc[] list = {"completed", "terminated", "aborted"};
486
487         return Arrays.asList(list);
488     }
489
490     /**
491      * @see org.ofbiz.workflow.WfExecutionObject#changeState(java.lang.String)
492      */

493     public void changeState(String JavaDoc newState) throws WfException, InvalidState, TransitionNotAllowed {
494         // Test is transaction is allowed???
495
GenericValue dataObject = getRuntimeObject();
496
497         if (validStates().contains(newState)) {
498             try {
499                 long now = (new Date JavaDoc()).getTime();
500
501                 dataObject.set("currentStatusId", WfUtil.getOFBStatus(newState));
502                 dataObject.set("lastStatusUpdate", new Timestamp JavaDoc(now));
503                 dataObject.store();
504             } catch (GenericEntityException e) {
505                 throw new WfException(e.getMessage(), e);
506             }
507         } else {
508             throw new InvalidState();
509         }
510     }
511
512     /**
513      * @see org.ofbiz.workflow.WfExecutionObject#suspend()
514      */

515     public void suspend() throws WfException, CannotSuspend, NotRunning, AlreadySuspended {
516         changeState("open.not_running.suspended");
517     }
518
519     /**
520      * @see org.ofbiz.workflow.WfExecutionObject#getDelegator()
521      */

522     public GenericDelegator getDelegator() throws WfException {
523         return delegator;
524     }
525
526     /**
527      * @see org.ofbiz.workflow.WfExecutionObject#getDefinitionObject()
528      */

529     public GenericValue getDefinitionObject() throws WfException {
530         String JavaDoc entityName = activityId != null ? "WorkflowActivity" : "WorkflowProcess";
531         GenericValue value = null;
532         Map JavaDoc fields = UtilMisc.toMap("packageId", packageId, "packageVersion", packageVersion, "processId", processId,
533                 "processVersion", processVersion);
534
535         if (activityId != null)
536             fields.put("activityId", activityId);
537         try {
538             value = getDelegator().findByPrimaryKey(entityName, fields);
539         } catch (GenericEntityException e) {
540             throw new WfException(e.getMessage(), e);
541         }
542         return value;
543     }
544
545     public GenericValue getRuntimeObject() throws WfException {
546         GenericValue value = null;
547
548         try {
549             value = getDelegator().findByPrimaryKey("WorkEffort",
550                         UtilMisc.toMap("workEffortId", workEffortId));
551         } catch (GenericEntityException e) {
552             throw new WfException(e.getMessage(), e);
553         }
554         return value;
555     }
556
557     /**
558      * Getter for this type of execution object.
559      * @return String
560      */

561     public abstract String JavaDoc executionObjectType();
562
563     /**
564      * Updates the runtime data entity
565      * @param field The field name of the entity (resultDataId,contextDataId)
566      * @param value The value to serialize and set
567      * @throws WfException
568      */

569     protected void setSerializedData(Map JavaDoc value) throws WfException, InvalidData {
570         GenericValue runtimeData = null;
571         GenericValue dataObject = getRuntimeObject();
572
573         try {
574             if (dataObject.get("runtimeDataId") == null) {
575                 String JavaDoc seqId = getDelegator().getNextSeqId("RuntimeData").toString();
576
577                 runtimeData = getDelegator().makeValue("RuntimeData",
578                             UtilMisc.toMap("runtimeDataId", seqId));
579                 getDelegator().create(runtimeData);
580                 dataObject.set("runtimeDataId", seqId);
581                 dataObject.store();
582             } else {
583                 runtimeData = dataObject.getRelatedOne("RuntimeData");
584             }
585             // String serialized = XmlSerializer.serialize(value);
586
// System.out.println(serialized);
587

588             runtimeData.set("runtimeInfo", XmlSerializer.serialize(value));
589             runtimeData.store();
590         } catch (GenericEntityException e) {
591             throw new WfException(e.getMessage(), e);
592         } catch (SerializeException e) {
593             throw new InvalidData(e.getMessage(), e);
594         } catch (FileNotFoundException JavaDoc e) {
595             throw new InvalidData(e.getMessage(), e);
596         } catch (IOException JavaDoc e) {
597             throw new InvalidData(e.getMessage(), e);
598         }
599     }
600
601     /**
602      * Get an instance of the local dispatcher
603      * @return LocalDispatcher instance for use with this workflow
604      * @throws WfException
605      */

606     protected LocalDispatcher getDispatcher() throws WfException {
607         try {
608             return GenericDispatcher.getLocalDispatcher(dispatcherName, getDelegator());
609         } catch (GenericServiceException e) {
610             throw new WfException("No workflow service dispatcher", e);
611         }
612     }
613
614     private Map JavaDoc getContext() throws WfException {
615         GenericValue dataObject = getRuntimeObject();
616         String JavaDoc contextXML = null;
617         Map JavaDoc context = null;
618
619         if (dataObject.get("runtimeDataId") == null)
620             return context;
621         try {
622             GenericValue runtimeData = dataObject.getRelatedOne("RuntimeData");
623
624             contextXML = runtimeData.getString("runtimeInfo");
625         } catch (GenericEntityException e) {
626             throw new WfException(e.getMessage(), e);
627         }
628         // De-serialize the context
629
if (contextXML != null) {
630             try {
631                 context = (Map JavaDoc) XmlSerializer.deserialize(contextXML, getDelegator());
632             } catch (SerializeException e) {
633                 throw new WfException(e.getMessage(), e);
634             } catch (IOException JavaDoc e) {
635                 throw new WfException(e.getMessage(), e);
636             } catch (Exception JavaDoc e) {
637                 throw new WfException(e.getMessage(), e);
638             }
639         }
640         return context;
641     }
642     
643     private GenericValue getWorkEffort(String JavaDoc workEffortId) throws WfException {
644         GenericValue we = null;
645         try {
646             we = getDelegator().findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffortId));
647         } catch (GenericEntityException e) {
648             throw new WfException("Problem getting WorkEffort entity (" + workEffortId + ")", e);
649         }
650         return we;
651     }
652             
653     /**
654      * Evaluate a condition expression using an implementation of TransitionCondition
655      * @param className The class name of the TransitionCondition implementation
656      * @param expression The expression to evaluate
657      * @return The result of the evaluation (True/False)
658      * @throws WfException
659      */

660     protected boolean evalConditionClass(String JavaDoc className, String JavaDoc expression, Map JavaDoc context, Map JavaDoc attrs) throws WfException {
661         // attempt to load and instance of the class
662
Object JavaDoc conditionObject = null;
663         try {
664             conditionObject = ObjectType.getInstance(className);
665         } catch (ClassNotFoundException JavaDoc e) {
666             Debug.logError(e, "Cannot load class " + className, module);
667             return false;
668         } catch (InstantiationException JavaDoc e) {
669             Debug.logError(e, "Cannot get instance of class " + className, module);
670             return false;
671         } catch (IllegalAccessException JavaDoc e) {
672             Debug.logError(e, "Cannot access class " + className, module);
673             return false;
674         }
675                                 
676         // make sure we implement the TransitionCondition interface
677
if (!ObjectType.instanceOf(conditionObject, "org.ofbiz.workflow.TransitionCondition")) {
678             Debug.logError("Class " + className + " is not an instance of TransitionCondition", module);
679             return false;
680         }
681         
682         // cast to the interface
683
TransitionCondition cond = (TransitionCondition) conditionObject;
684         
685         // trim up the expression if it isn't empty
686
if (expression != null)
687             expression = expression.trim();
688         
689         // get a DispatchContext object to pass over to the eval
690
DispatchContext dctx = this.getDispatcher().getDispatchContext();
691         
692         // evaluate the condition
693
Boolean JavaDoc evaluation = null;
694         try {
695             evaluation = cond.evaluateCondition(context, attrs, expression, dctx);
696         } catch (EvaluationException e) {
697             throw new WfException("Problems evaluating condition", e);
698         }
699         
700         return evaluation.booleanValue();
701     }
702
703     /**
704      * Evaluate a condition expression using BeanShell
705      * @param expression The expression to evaluate
706      * @param context The context to use in evaluation
707      * @return The result of the evaluation (True/False)
708      * @throws WfException
709      */

710     protected boolean evalBshCondition(String JavaDoc expression, Map JavaDoc context) throws WfException {
711         if (expression == null || expression.length() == 0) {
712             Debug.logVerbose("Null or empty expression, returning true.", module);
713             return true;
714         }
715         
716         Object JavaDoc o = null;
717         try {
718             o = BshUtil.eval(expression.trim(), context);
719         } catch (bsh.EvalError e) {
720             throw new WfException("Bsh evaluation error.", e);
721         }
722
723         if (o == null)
724             return false;
725         else if (o instanceof Number JavaDoc)
726             return (((Number JavaDoc) o).doubleValue() == 0) ? false : true;
727         else
728             return (!o.toString().equalsIgnoreCase("true")) ? false : true;
729     }
730 }
731
732
Popular Tags