KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > taskmgmt > exe > TaskMgmtInstance


1 package org.jbpm.taskmgmt.exe;
2
3 import java.util.*;
4
5 import org.jbpm.JbpmConfiguration;
6 import org.jbpm.db.JbpmSession;
7 import org.jbpm.graph.def.DelegationException;
8 import org.jbpm.graph.def.GraphElement;
9 import org.jbpm.graph.exe.*;
10 import org.jbpm.instantiation.ClassLoaderUtil;
11 import org.jbpm.instantiation.Delegation;
12 import org.jbpm.module.exe.*;
13 import org.jbpm.security.Authentication;
14 import org.jbpm.taskmgmt.def.*;
15 import org.jbpm.taskmgmt.log.TaskCreateLog;
16
17 /**
18  * process instance extension for managing tasks on a process instance.
19  */

20 public class TaskMgmtInstance extends ModuleInstance {
21
22   private static final long serialVersionUID = 1L;
23
24   private TaskMgmtDefinition taskMgmtDefinition = null;
25   private Map swimlaneInstances = null;
26   private Set taskInstances = null;
27   
28   public TaskMgmtInstance() {
29   }
30   
31   public TaskMgmtInstance(TaskMgmtDefinition taskMgmtDefinition) {
32     this.taskMgmtDefinition = taskMgmtDefinition;
33   }
34
35   // the task instance class is configurable so that users can extend from TaskInstance to create their custom extensions.
36
private static Class JavaDoc taskInstanceClass = null;
37   
38   public static Class JavaDoc getTaskInstanceClass() {
39     if (taskInstanceClass==null) {
40       String JavaDoc taskInstanceClassName = JbpmConfiguration.getString("jbpm.task.instance.class");
41       taskInstanceClass = ClassLoaderUtil.loadClass(taskInstanceClassName);
42     }
43     return taskInstanceClass;
44   }
45   
46   // task instances ///////////////////////////////////////////////////////////
47

48   public TaskInstance createTaskInstance() {
49     return createTaskInstance(null, (ExecutionContext)null);
50   }
51
52   public TaskInstance createTaskInstance(Task task) {
53     return createTaskInstance(task, (ExecutionContext)null);
54   }
55
56   public TaskInstance createTaskInstance(Token token) {
57     return createTaskInstance(null, new ExecutionContext(token));
58   }
59
60   /**
61    * creates a new task instance on the given token, for the given task.
62    */

63   public TaskInstance createTaskInstance(Task task, Token token) {
64     return createTaskInstance(task, new ExecutionContext(token));
65   }
66
67   /**
68    * creates a new task instance on the given task, in the given execution context.
69    */

70   public TaskInstance createTaskInstance(Task task, ExecutionContext executionContext) {
71     TaskInstance taskInstance = null;
72
73     // instantiate the new task instance
74
taskInstance = instantiateNewTaskInstance();
75
76     // bind the task instance to the TaskMgmtInstance
77
addTaskInstance(taskInstance);
78
79     // initialize the task instance
80
if (task!=null) taskInstance.setTask(task);
81
82     // if there is database session
83
JbpmSession currentSession = JbpmSession.getCurrentJbpmSession();
84     if (currentSession!=null) {
85       // save the new task instance to give it an id
86
currentSession.getSession().save(taskInstance);
87     }
88
89     if (executionContext!=null) {
90       Token token = executionContext.getToken();
91
92       taskInstance.setToken(token);
93       
94       try {
95         // update the executionContext
96
executionContext.setTask(task);
97         executionContext.setTaskInstance(taskInstance);
98         executionContext.setEventSource(task);
99         
100         // if this task instance is created for a task, perform assignment
101
if (task!=null) {
102           taskInstance.assign(executionContext);
103         }
104         
105         taskInstance.create(executionContext);
106       } finally {
107         // clean the executionContext
108
executionContext.setTask(null);
109         executionContext.setTaskInstance(null);
110         executionContext.setEventSource(null);
111       }
112       
113       // log this creation
114
token.addLog(new TaskCreateLog(taskInstance, taskInstance.getActorId()));
115
116     } else {
117       taskInstance.create();
118     }
119     
120     return taskInstance;
121   }
122
123   public SwimlaneInstance getInitializedSwimlaneInstance(ExecutionContext executionContext, Swimlane swimlane) {
124     // initialize the swimlane
125
if (swimlaneInstances==null) swimlaneInstances = new HashMap();
126     SwimlaneInstance swimlaneInstance = (SwimlaneInstance) swimlaneInstances.get(swimlane.getName());
127     if (swimlaneInstance==null) {
128       swimlaneInstance = new SwimlaneInstance(swimlane);
129       addSwimlaneInstance(swimlaneInstance);
130       // assign the swimlaneInstance
131
invokeAssignmentHandler(swimlane.getAssignmentDelegation(), swimlaneInstance, executionContext);
132     }
133
134     return swimlaneInstance;
135   }
136   
137   public void invokeAssignmentHandler(Delegation assignmentDelegation, Assignable assignable, ExecutionContext executionContext) {
138     try {
139       if (assignmentDelegation!=null) {
140         // instantiate the assignment handler
141
AssignmentHandler assignmentHandler = (AssignmentHandler) assignmentDelegation.instantiate();
142         // invoke the assignment handler
143
assignmentHandler.assign(assignable, executionContext);
144       }
145       
146     } catch (Exception JavaDoc exception) {
147       GraphElement graphElement = executionContext.getEventSource();
148       if (graphElement!=null) {
149         graphElement.raiseException(exception, executionContext);
150       } else {
151         throw new DelegationException(exception, executionContext);
152       }
153     }
154   }
155
156
157   /**
158    * creates a task instance on the rootToken, and assigns it
159    * to the currently authenticated user.
160    */

161   public TaskInstance createStartTaskInstance() {
162     TaskInstance taskInstance = null;
163     Task startTask = taskMgmtDefinition.getStartTask();
164     Token rootToken = processInstance.getRootToken();
165     ExecutionContext executionContext = new ExecutionContext(rootToken);
166     taskInstance = createTaskInstance(startTask, executionContext);
167     taskInstance.setActorId(Authentication.getAuthenticatedActorId());
168     return taskInstance;
169   }
170
171   private TaskInstance instantiateNewTaskInstance() {
172     TaskInstance newTaskInstance = null;
173     try {
174       newTaskInstance = (TaskInstance) getTaskInstanceClass().newInstance();
175     } catch (Exception JavaDoc e) {
176       e.printStackTrace();
177       throw new RuntimeException JavaDoc("couldn't instantiate task instance '"+taskInstanceClass.getName()+"'", e);
178     }
179     return newTaskInstance;
180   }
181
182   /**
183    * is true if the given token has task instances that keep the
184    * token from leaving the current node.
185    */

186   public boolean hasBlockingTaskInstances(Token token) {
187     boolean hasBlockingTasks = false;
188     if (taskInstances!=null) {
189       Iterator iter = taskInstances.iterator();
190       while ( (iter.hasNext())
191               && (! hasBlockingTasks)) {
192         TaskInstance taskInstance = (TaskInstance) iter.next();
193         if ( (! taskInstance.hasEnded())
194              && (taskInstance.isBlocking())
195              && (taskInstance.getToken()==token) ) {
196           hasBlockingTasks = true;
197         }
198       }
199     }
200     return hasBlockingTasks;
201   }
202
203   /**
204    * is true if the given token has task instances that are not yet ended.
205    */

206   public boolean hasUnfinishedTasks(Token token) {
207     return (getUnfinishedTasks(token).size()>0);
208   }
209
210   /**
211    * is the collection of {@link TaskInstance}s on the given token that are not ended.
212    */

213   public Collection getUnfinishedTasks(Token token) {
214     Collection unfinishedTasks = new ArrayList();
215     if ( taskInstances != null ) {
216       Iterator iter = taskInstances.iterator();
217       while (iter.hasNext()) {
218         TaskInstance task = (TaskInstance) iter.next();
219         if ( (!task.hasEnded())
220              && (token == task.getToken()) ) {
221           unfinishedTasks.add( task );
222         }
223       }
224     }
225     return unfinishedTasks;
226   }
227
228   /**
229    * is true if there are {@link TaskInstance}s on the given token that can trigger
230    * the token to continue.
231    */

232   public boolean hasSignallingTasks(ExecutionContext executionContext) {
233     return (getSignallingTasks(executionContext).size()>0);
234   }
235
236   /**
237    * is the collection of {@link TaskInstance}s for the given token that can trigger
238    * the token to continue.
239    */

240   public Collection getSignallingTasks(ExecutionContext executionContext) {
241     Collection signallingTasks = new ArrayList();
242     if ( taskInstances != null ) {
243       Iterator iter = taskInstances.iterator();
244       while (iter.hasNext()) {
245         TaskInstance taskInstance = (TaskInstance) iter.next();
246         if (taskInstance.isSignalling()
247             &&(executionContext.getToken()==taskInstance.getToken())) {
248           signallingTasks.add(taskInstance);
249         }
250       }
251     }
252     return signallingTasks;
253   }
254   
255   /**
256    * returns all the taskInstances for the this process instance. This
257    * includes task instances that have been completed previously.
258    */

259   public Collection getTaskInstances() {
260     return taskInstances;
261   }
262   public void addTaskInstance(TaskInstance taskInstance) {
263     if (taskInstances==null) taskInstances = new HashSet();
264     taskInstances.add(taskInstance);
265     taskInstance.setTaskMgmtInstance(this);
266   }
267   public void removeTaskInstance(TaskInstance taskInstance) {
268     if (taskInstances!=null) {
269       taskInstances.remove(taskInstance);
270     }
271   }
272
273   // swimlane instances ///////////////////////////////////////////////////////
274

275   public Map getSwimlaneInstances() {
276     return swimlaneInstances;
277   }
278   public void addSwimlaneInstance( SwimlaneInstance swimlaneInstance ) {
279     if (swimlaneInstances==null) swimlaneInstances = new HashMap();
280     swimlaneInstances.put(swimlaneInstance.getName(), swimlaneInstance);
281     swimlaneInstance.setTaskMgmtInstance(this);
282   }
283   public SwimlaneInstance getSwimlaneInstance(String JavaDoc swimlaneName) {
284     return (SwimlaneInstance) (swimlaneInstances!=null ? swimlaneInstances.get(swimlaneName) : null );
285   }
286   
287   // getters and setters //////////////////////////////////////////////////////
288

289   public TaskMgmtDefinition getTaskMgmtDefinition() {
290     return taskMgmtDefinition;
291   }
292
293   // private static final Log log = LogFactory.getLog(TaskMgmtInstance.class);
294
}
295
Popular Tags