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 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 private static Class taskInstanceClass = null; 37 38 public static Class getTaskInstanceClass() { 39 if (taskInstanceClass==null) { 40 String taskInstanceClassName = JbpmConfiguration.getString("jbpm.task.instance.class"); 41 taskInstanceClass = ClassLoaderUtil.loadClass(taskInstanceClassName); 42 } 43 return taskInstanceClass; 44 } 45 46 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 63 public TaskInstance createTaskInstance(Task task, Token token) { 64 return createTaskInstance(task, new ExecutionContext(token)); 65 } 66 67 70 public TaskInstance createTaskInstance(Task task, ExecutionContext executionContext) { 71 TaskInstance taskInstance = null; 72 73 taskInstance = instantiateNewTaskInstance(); 75 76 addTaskInstance(taskInstance); 78 79 if (task!=null) taskInstance.setTask(task); 81 82 JbpmSession currentSession = JbpmSession.getCurrentJbpmSession(); 84 if (currentSession!=null) { 85 currentSession.getSession().save(taskInstance); 87 } 88 89 if (executionContext!=null) { 90 Token token = executionContext.getToken(); 91 92 taskInstance.setToken(token); 93 94 try { 95 executionContext.setTask(task); 97 executionContext.setTaskInstance(taskInstance); 98 executionContext.setEventSource(task); 99 100 if (task!=null) { 102 taskInstance.assign(executionContext); 103 } 104 105 taskInstance.create(executionContext); 106 } finally { 107 executionContext.setTask(null); 109 executionContext.setTaskInstance(null); 110 executionContext.setEventSource(null); 111 } 112 113 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 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 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 AssignmentHandler assignmentHandler = (AssignmentHandler) assignmentDelegation.instantiate(); 142 assignmentHandler.assign(assignable, executionContext); 144 } 145 146 } catch (Exception 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 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 e) { 176 e.printStackTrace(); 177 throw new RuntimeException ("couldn't instantiate task instance '"+taskInstanceClass.getName()+"'", e); 178 } 179 return newTaskInstance; 180 } 181 182 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 206 public boolean hasUnfinishedTasks(Token token) { 207 return (getUnfinishedTasks(token).size()>0); 208 } 209 210 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 232 public boolean hasSignallingTasks(ExecutionContext executionContext) { 233 return (getSignallingTasks(executionContext).size()>0); 234 } 235 236 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 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 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 swimlaneName) { 284 return (SwimlaneInstance) (swimlaneInstances!=null ? swimlaneInstances.get(swimlaneName) : null ); 285 } 286 287 289 public TaskMgmtDefinition getTaskMgmtDefinition() { 290 return taskMgmtDefinition; 291 } 292 293 } 295 | Popular Tags |