001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.command;
023:
024: import java.util.Map;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.jbpm.JbpmContext;
029: import org.jbpm.JbpmException;
030: import org.jbpm.graph.exe.Token;
031: import org.jbpm.graph.exe.ProcessInstance;
032:
033: /**
034: * Signals a token. After signalling the token is returned
035: *
036: * @author ??, Bernd Ruecker
037: */
038: public class SignalCommand implements Command {
039:
040: private static final long serialVersionUID = 1L;
041:
042: private long tokenId = 0;
043:
044: private String transitionName = null;
045:
046: /**
047: * if given, it is checked if the state is as expected. If not, a exception is thrown
048: * Ignored if null
049: */
050: private String expectedStateName = null;
051:
052: private Token previousToken = null;
053:
054: private ProcessInstance previousProcessInstance = null;
055:
056: private Map variables;
057:
058: public SignalCommand() {
059: }
060:
061: public SignalCommand(long tokenId, String transitionName) {
062: this .tokenId = tokenId;
063: this .transitionName = transitionName;
064: }
065:
066: public Object execute(JbpmContext jbpmContext) {
067: log.debug("executing " + this );
068: if (previousProcessInstance != null) {
069:
070: if (variables != null && variables.size() > 0)
071: previousProcessInstance.getContextInstance()
072: .addVariables(variables);
073:
074: if (transitionName == null) {
075: previousProcessInstance.signal();
076: } else {
077: previousProcessInstance.signal(transitionName);
078: }
079: return previousProcessInstance.getRootToken();
080: } else {
081: Token token = getToken(jbpmContext);
082:
083: if (expectedStateName != null
084: && !expectedStateName.equals(token.getNode()
085: .getName()))
086: throw new JbpmException(
087: "token is not in expected state '"
088: + expectedStateName + "' but in '"
089: + token.getNode().getName() + "'");
090:
091: if (variables != null && variables.size() > 0)
092: token.getProcessInstance().getContextInstance()
093: .addVariables(variables);
094:
095: if (transitionName == null) {
096: token.signal();
097: } else {
098: token.signal(transitionName);
099: }
100: return token;
101: }
102: }
103:
104: protected Token getToken(JbpmContext jbpmContext) {
105: if (previousToken != null) {
106: return previousToken;
107: }
108: return jbpmContext.loadTokenForUpdate(tokenId);
109: }
110:
111: public long getTokenId() {
112: return tokenId;
113: }
114:
115: public void setTokenId(long tokenId) {
116: this .tokenId = tokenId;
117: }
118:
119: public String getTransitionName() {
120: return transitionName;
121: }
122:
123: public void setTransitionName(String transitionName) {
124: this .transitionName = transitionName;
125: }
126:
127: private static Log log = LogFactory.getLog(SignalCommand.class);
128:
129: public Map getVariables() {
130: return variables;
131: }
132:
133: public void setVariables(Map variables) {
134: this .variables = variables;
135: }
136:
137: public String getExpectedStateName() {
138: return expectedStateName;
139: }
140:
141: public void setExpectedStateName(String expectedStateName) {
142: this.expectedStateName = expectedStateName;
143: }
144: }
|