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.Iterator;
025: import java.util.Map;
026:
027: import org.jbpm.JbpmContext;
028: import org.jbpm.context.exe.ContextInstance;
029: import org.jbpm.graph.def.ProcessDefinition;
030: import org.jbpm.graph.exe.ProcessInstance;
031:
032: /**
033: * Graph command to start a new process and create a task instance if the start
034: * node has a start task definition.
035: *
036: * The result of this command, if requested, is a {@link Long} value containing
037: * the process instance id.
038: *
039: * @author Jim Rigsbee, Tom Baeyens, Bernd Ruecker
040: */
041: public class NewProcessInstanceCommand implements Command {
042:
043: private static final long serialVersionUID = 1L;
044:
045: private String processDefinitionName = null;
046: private long processDefinitionId = 0;
047:
048: // TODO: This is not clear to me, what for do we need that actorId here? // Bernd Ruecker
049: String actorId = null;
050: private Map variables = null;
051: private boolean createStartTask = false;
052:
053: private String key;
054:
055: public NewProcessInstanceCommand() {
056: }
057:
058: public NewProcessInstanceCommand(String processDefinitionName) {
059: this .processDefinitionName = processDefinitionName;
060: }
061:
062: /**
063: * return the id of the newly created process instance.
064: * @throws Exception
065: */
066: public Object execute(JbpmContext jbpmContext) throws Exception {
067:
068: if (actorId != null) {
069: jbpmContext.setActorId(actorId);
070: }
071:
072: ProcessInstance processInstance = null;
073: if (processDefinitionName != null) {
074: processInstance = jbpmContext
075: .newProcessInstance(processDefinitionName);
076: } else {
077: ProcessDefinition processDefinition = jbpmContext
078: .getGraphSession().loadProcessDefinition(
079: processDefinitionId);
080: processInstance = new ProcessInstance(processDefinition);
081: }
082:
083: if (key != null) {
084: processInstance.setKey(key);
085: }
086:
087: Object result = null;
088: if (createStartTask) {
089: result = processInstance.getTaskMgmtInstance()
090: .createStartTaskInstance();
091: } else {
092: result = processInstance;
093: }
094:
095: if (variables != null) {
096: ContextInstance contextInstance = processInstance
097: .getContextInstance();
098: Iterator iter = variables.keySet().iterator();
099: while (iter.hasNext()) {
100: String variableName = (String) iter.next();
101: contextInstance.setVariable(variableName, variables
102: .get(variableName));
103: }
104: }
105:
106: jbpmContext.save(processInstance);
107:
108: return result;
109: }
110:
111: public String getActorId() {
112: return actorId;
113: }
114:
115: public void setActorId(String actorId) {
116: this .actorId = actorId;
117: }
118:
119: public long getProcessId() {
120: return processDefinitionId;
121: }
122:
123: public void setProcessId(long processId) {
124: this .processDefinitionId = processId;
125: }
126:
127: public String getProcessName() {
128: return processDefinitionName;
129: }
130:
131: public void setProcessName(String processName) {
132: this .processDefinitionName = processName;
133: }
134:
135: public boolean isCreateStartTask() {
136: return createStartTask;
137: }
138:
139: public void setCreateStartTask(boolean createStartTask) {
140: this .createStartTask = createStartTask;
141: }
142:
143: public Map getVariables() {
144: return variables;
145: }
146:
147: public void setVariables(Map variables) {
148: this .variables = variables;
149: }
150:
151: public String getKey() {
152: return key;
153: }
154:
155: public void setKey(String key) {
156: this.key = key;
157: }
158: }
|