1 package org.jbpm.graph.node; 2 3 import java.util.*; 4 5 import org.dom4j.Element; 6 import org.jbpm.graph.def.*; 7 import org.jbpm.graph.exe.*; 8 import org.jbpm.jpdl.xml.*; 9 import org.jbpm.taskmgmt.def.*; 10 import org.jbpm.taskmgmt.exe.*; 11 12 17 public class TaskNode extends Node implements Parsable { 18 19 private static final long serialVersionUID = 1L; 20 21 24 public static final int SIGNAL_UNSYNCHRONIZED = 0; 25 28 public static final int SIGNAL_NEVER = 1; 29 33 public static final int SIGNAL_FIRST = 2; 34 38 public static final int SIGNAL_FIRST_WAIT = 3; 39 43 public static final int SIGNAL_LAST = 4; 44 48 public static final int SIGNAL_LAST_WAIT = 5; 49 50 public static int parseSignal(String text) { 51 if ("unsynchronized".equalsIgnoreCase(text)) { 52 return SIGNAL_UNSYNCHRONIZED; 53 } else if ("never".equalsIgnoreCase(text)) { 54 return SIGNAL_NEVER; 55 } else if ("first".equalsIgnoreCase(text)) { 56 return SIGNAL_FIRST; 57 } else if ("first-wait".equalsIgnoreCase(text)) { 58 return SIGNAL_FIRST_WAIT; 59 } else if ("last-wait".equalsIgnoreCase(text)) { 60 return SIGNAL_LAST_WAIT; 61 } else { return SIGNAL_LAST; 63 } 64 } 65 66 public static String signalToString(int signal) { 67 if (signal==SIGNAL_UNSYNCHRONIZED) { 68 return "unsynchronized"; 69 } else if (signal==SIGNAL_NEVER) { 70 return "never"; 71 } else if (signal==SIGNAL_FIRST) { 72 return "first"; 73 } else if (signal==SIGNAL_FIRST_WAIT) { 74 return "first-wait"; 75 } else if (signal==SIGNAL_LAST) { 76 return "last"; 77 } else if (signal==SIGNAL_LAST_WAIT) { 78 return "last-wait"; 79 } else { 80 return null; 81 } 82 } 83 84 private long id = 0; 85 private Set tasks = null; 86 private int signal = SIGNAL_LAST; 87 private boolean createTasks = true; 88 89 public TaskNode() { 90 } 91 92 public TaskNode(String name) { 93 super(name); 94 } 95 96 public void read(Element element, JpdlXmlReader jpdlReader) { 97 String signalText = element.attributeValue("signal"); 99 if (signalText!=null) { 100 signal = parseSignal(signalText); 101 } 102 103 String createTasksText = element.attributeValue("create-tasks"); 105 if (createTasksText!=null) { 106 if (("no".equalsIgnoreCase(createTasksText)) 107 || ("false".equalsIgnoreCase(createTasksText)) ) { 108 createTasks = false; 109 } 110 } 111 112 jpdlReader.readTasks(element, this); 114 } 115 116 public void addTask(Task task) { 117 if (tasks==null) tasks = new HashSet(); 118 tasks.add(task); 119 task.setTaskNode(this); 120 } 121 122 125 public void execute(ExecutionContext executionContext) { 126 127 TaskMgmtInstance tmi = getTaskMgmtInstance(executionContext.getToken()); 128 129 if ( (createTasks) 131 && (tasks!=null) ) { 132 Iterator iter = tasks.iterator(); 133 while (iter.hasNext()) { 134 Task task = (Task) iter.next(); 135 executionContext.setTask(task); 136 tmi.createTaskInstance(task, executionContext); 137 } 138 } 139 140 boolean continueExecution = false; 142 switch (signal) { 143 case SIGNAL_UNSYNCHRONIZED: 144 continueExecution = true; 145 break; 146 case SIGNAL_FIRST_WAIT: 147 case SIGNAL_LAST_WAIT: 148 case SIGNAL_NEVER: 149 continueExecution = false; 150 break; 151 case SIGNAL_FIRST: 152 case SIGNAL_LAST: 153 continueExecution = tmi.getSignallingTasks(executionContext).isEmpty(); 154 } 155 156 if (continueExecution) { 157 leave(executionContext); 158 } 159 } 160 161 public void leave(ExecutionContext executionContext, Transition transition) { 162 TaskMgmtInstance tmi = getTaskMgmtInstance(executionContext.getToken()); 163 if (tmi.hasBlockingTaskInstances(executionContext.getToken()) ) { 164 throw new IllegalStateException ("task-node '"+name+"' still has blocking tasks"); 165 } 166 removeTaskInstanceSynchronization(executionContext.getToken()); 167 super.leave(executionContext, transition); 168 } 169 170 173 public boolean completionTriggersSignal(TaskInstance taskInstance) { 174 boolean completionTriggersSignal = false; 175 if ( (signal==SIGNAL_FIRST) 176 || (signal==SIGNAL_FIRST_WAIT) ) { 177 completionTriggersSignal = true; 178 } else if ( ( (signal==SIGNAL_LAST) 179 || (signal==SIGNAL_LAST_WAIT) ) 180 && (isLastToComplete(taskInstance) ) ){ 181 completionTriggersSignal = true; 182 } 183 return completionTriggersSignal; 184 } 185 186 private boolean isLastToComplete(TaskInstance taskInstance) { 187 Token token = taskInstance.getToken(); 188 TaskMgmtInstance tmi = getTaskMgmtInstance(token); 189 190 boolean isLastToComplete = true; 191 Iterator iter = tmi.getTaskInstances().iterator(); 192 while ( iter.hasNext() 193 && (isLastToComplete) ) { 194 TaskInstance other = (TaskInstance) iter.next(); 195 if ( (other.getToken()==token) 196 && (other!=taskInstance) 197 && (!other.hasEnded()) ) { 198 isLastToComplete = false; 199 } 200 } 201 202 return isLastToComplete; 203 } 204 205 public void removeTaskInstanceSynchronization(Token token) { 206 TaskMgmtInstance tmi = getTaskMgmtInstance(token); 207 Collection taskInstances = tmi.getTaskInstances(); 208 if (taskInstances!=null) { 209 Iterator iter = taskInstances.iterator(); 210 while (iter.hasNext()) { 211 TaskInstance taskInstance = (TaskInstance) iter.next(); 212 if (taskInstance.isSignalling() 214 &&(token==taskInstance.getToken())) { 215 taskInstance.setSignalling(false); 216 } 217 if (taskInstance.isBlocking() 219 &&(token==taskInstance.getToken())) { 220 taskInstance.setBlocking(false); 221 } 222 } 223 } 224 } 225 226 private TaskMgmtInstance getTaskMgmtInstance(Token token) { 227 return (TaskMgmtInstance) token.getProcessInstance().getInstance(TaskMgmtInstance.class); 228 } 229 230 233 237 public Map getTasksMap() { 238 Map tasksMap = new HashMap(); 239 if (tasks!=null) { 240 Iterator iter = tasks.iterator(); 241 while (iter.hasNext()) { 242 Task task = (Task) iter.next(); 243 tasksMap.put(task.getName(), task); 244 } 245 } 246 return tasksMap; 247 } 248 249 253 public Task getTask(String taskName) { 254 return (Task) getTasksMap().get(taskName); 255 } 256 257 public long getId() { 258 return id; 259 } 260 public Set getTasks() { 261 return tasks; 262 } 263 public int getSignal() { 264 return signal; 265 } 266 public boolean getCreateTasks() { 267 return createTasks; 268 } 269 } 270 | Popular Tags |