001: package org.jbpm.command;
002:
003: import java.util.Date;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009: import org.hibernate.Query;
010: import org.jbpm.JbpmContext;
011: import org.jbpm.graph.def.ProcessDefinition;
012: import org.jbpm.graph.exe.ProcessInstance;
013: import org.jbpm.graph.exe.Token;
014:
015: /**
016: * a bunch of processes is signalled with this command. you can specify the
017: * tokens either
018: * <li> by a array of token ids
019: * <li> or by processName, processVersion (optional, without all versions),
020: * stateName
021: *
022: * transitionName speicifies the transition to take (if null, the default
023: * transition is taken).
024: *
025: * This command can be for example useful, if you have a lot of processes to
026: * check some information a not jBPM task has altered before (maybe in batch
027: * too).
028: *
029: * CURRENTLY EXPERIMENTAL!
030: *
031: * @author Bernd Rucker (bernd.ruecker@camunda.com)
032: */
033: public class BatchSignalCommand implements Command {
034:
035: private static final long serialVersionUID = -4330623193546102772L;
036:
037: private static Log log = LogFactory
038: .getLog(BatchSignalCommand.class);
039:
040: private long[] tokenIds = null;
041:
042: private String processName = null;
043:
044: private String stateName = null;
045:
046: /**
047: * if set, only tokens which are started after this date are signaled
048: * (interessting to implement some timeout for example)
049: */
050: private Date inStateAtLeastSince = null;
051:
052: private long processVersion = 0;
053:
054: private String transitionName = null;
055:
056: public Object execute(JbpmContext jbpmContext) throws Exception {
057: log.debug("executing " + this );
058:
059: // batch tokens
060: if (tokenIds != null && tokenIds.length > 0) {
061: for (int i = 0; i < tokenIds.length; i++) {
062: Token token = jbpmContext
063: .loadTokenForUpdate(tokenIds[i]);
064: signalToken(token);
065: }
066: }
067:
068: // search for tokens in process/state
069: if (processName != null && stateName != null) {
070: Query query = jbpmContext.getSession().getNamedQuery(
071: "GraphSession.findTokensForProcessInNode");
072: query.setString("processDefinitionName", processName);
073: query.setString("nodeName", stateName);
074:
075: Iterator iter = query.list().iterator();
076: while (iter.hasNext()) {
077: Token t = (Token) iter.next();
078: if (inStateAtLeastSince == null
079: || t.getNodeEnter().before(inStateAtLeastSince))
080: signalToken(t);
081: }
082: }
083:
084: return null;
085: }
086:
087: private void signalToken(Token token) {
088: log.debug("signal token " + token);
089: if (transitionName == null) {
090: token.signal();
091: } else {
092: token.signal(transitionName);
093: }
094: }
095:
096: public String getProcessName() {
097: return processName;
098: }
099:
100: public void setProcessName(String processName) {
101: this .processName = processName;
102: }
103:
104: public long getProcessVersion() {
105: return processVersion;
106: }
107:
108: public void setProcessVersion(long processVersion) {
109: this .processVersion = processVersion;
110: }
111:
112: public String getStateName() {
113: return stateName;
114: }
115:
116: public void setStateName(String stateName) {
117: this .stateName = stateName;
118: }
119:
120: public long[] getTokenIds() {
121: return tokenIds;
122: }
123:
124: public void setTokenIds(long[] tokenIds) {
125: this .tokenIds = tokenIds;
126: }
127:
128: public String getTransitionName() {
129: return transitionName;
130: }
131:
132: public void setTransitionName(String transitionName) {
133: this .transitionName = transitionName;
134: }
135:
136: public Date getInStateAtLeastSince() {
137: return inStateAtLeastSince;
138: }
139:
140: public void setInStateAtLeastSince(Date inStateAtLeastSince) {
141: this.inStateAtLeastSince = inStateAtLeastSince;
142: }
143:
144: }
|