01: package org.jbpm.command;
02:
03: import java.io.Serializable;
04: import java.util.Collection;
05: import java.util.Iterator;
06: import java.util.List;
07:
08: import org.apache.commons.logging.Log;
09: import org.apache.commons.logging.LogFactory;
10: import org.hibernate.Query;
11: import org.jbpm.JbpmContext;
12: import org.jbpm.graph.exe.Token;
13: import org.jbpm.taskmgmt.exe.TaskInstance;
14:
15: public class AbstractCancelCommand implements Serializable {
16:
17: private static final long serialVersionUID = 1L;
18:
19: protected transient JbpmContext jbpmContext = null;
20:
21: protected static final Log log = LogFactory
22: .getLog(AbstractCancelCommand.class);
23:
24: protected void cancelTokens(Collection tokens) {
25: log.info("cancel " + tokens.size() + " tokens");
26: if (tokens != null && tokens.size() > 0) {
27: for (Iterator itr = tokens.iterator(); itr.hasNext();) {
28: cancelToken((Token) itr.next());
29: }
30: }
31: }
32:
33: protected void cancelToken(Token token) {
34: token.end(false); // end the token but dont verify
35: // ParentTermination
36: // if set to token.end() == token.end(true) the parent token is
37: // terminated if there are no children
38: // If we then use that in a "SignalingJoin" the main path of execution
39: // is triggered, but we dont want that!
40:
41: cancelTasks(getTasksForToken(token));
42:
43: log.info("token " + token.getId() + " canceled");
44: }
45:
46: protected List getTasksForToken(Token token) {
47: Query hqlQuery = jbpmContext.getSession().getNamedQuery(
48: "TaskMgmtSession.findTaskInstancesByTokenId");
49: hqlQuery.setLong("tokenId", token.getId());
50: return hqlQuery.list();
51: }
52:
53: protected void cancelTasks(List tasks) {
54: log.info("cancel " + tasks.size() + " tasks");
55: if (tasks != null && tasks.size() > 0) {
56: for (Iterator it = tasks.iterator(); it.hasNext();) {
57: TaskInstance ti = (TaskInstance) it.next();
58:
59: // if the process def doesn't set signal="never", we have to
60: // manually turn off signaling for all tasks;
61: // otherwise, the token will be triggered instead of being
62: // ended.
63: // Do this until http://jira.jboss.com/jira/browse/JBPM-392 is
64: // resolved
65:
66: log.info("cancel task " + ti.getId());
67: ti.setSignalling(false);
68: ti.cancel();
69: }
70: }
71: }
72:
73: }
|