提交 12b9718b 编写于 作者: J Joram Barrez

ACT-1842

Add taskService.complete() method that takes an option boolean to determine the scope of variables
上级 3e28c07e
......@@ -189,6 +189,17 @@ public interface TaskService {
* @throws ActivitiObjectNotFoundException when no task exists with the given id.
*/
void complete(String taskId, Map<String, Object> variables);
/**
* Called when the task is successfully executed,
* and the required task paramaters are given by the end-user.
* @param taskId the id of the task to complete, cannot be null.
* @param variables task parameters. May be null or empty.
* @param localScope If true, the provided variables will be stored task-local,
* instead of process instance wide (which is the default for {@link #complete(String, Map)}).
* @throws ActivitiObjectNotFoundException when no task exists with the given id.
*/
void complete(String taskId, Map<String, Object> variables, boolean localScope);
/**
* Changes the assignee of the given task to the given userId.
......
......@@ -171,6 +171,10 @@ public class TaskServiceImpl extends ServiceImpl implements TaskService {
public void complete(String taskId, Map<String, Object> variables) {
commandExecutor.execute(new CompleteTaskCmd(taskId, variables));
}
public void complete(String taskId, Map<String, Object> variables,boolean localScope) {
commandExecutor.execute(new CompleteTaskCmd(taskId, variables, localScope));
}
public void delegateTask(String taskId, String userId) {
commandExecutor.execute(new DelegateTaskCmd(taskId, userId));
......
......@@ -25,15 +25,26 @@ public class CompleteTaskCmd extends NeedsActiveTaskCmd<Void> {
private static final long serialVersionUID = 1L;
protected Map<String, Object> variables;
protected boolean localScope;
public CompleteTaskCmd(String taskId, Map<String, Object> variables) {
super(taskId);
this.variables = variables;
}
public CompleteTaskCmd(String taskId, Map<String, Object> variables, boolean localScope) {
super(taskId);
this.variables = variables;
this.localScope = localScope;
}
protected Void execute(CommandContext commandContext, TaskEntity task) {
if (variables!=null) {
task.setExecutionVariables(variables);
if (localScope) {
task.setVariablesLocal(variables);
} else {
task.setExecutionVariables(variables);
}
}
task.complete();
......
......@@ -596,6 +596,55 @@ public class TaskServiceTest extends PluggableActivitiTestCase {
assertEquals("myValue", variables.get("myParam"));
}
@Deployment(resources = {
"org/activiti/engine/test/api/twoTasksProcess.bpmn20.xml" })
public void testCompleteWithParametersTask2() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("twoTasksProcess");
// Fetch first task
Task task = taskService.createTaskQuery().singleResult();
assertEquals("First task", task.getName());
// Complete first task
Map<String, Object> taskParams = new HashMap<String, Object>();
taskParams.put("myParam", "myValue");
taskService.complete(task.getId(), taskParams, false); // Only difference with previous test
// Fetch second task
task = taskService.createTaskQuery().singleResult();
assertEquals("Second task", task.getName());
// Verify task parameters set on execution
Map<String, Object> variables = runtimeService.getVariables(processInstance.getId());
assertEquals(1, variables.size());
assertEquals("myValue", variables.get("myParam"));
}
@Deployment
public void testCompleteWithTaskLocalParameters() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testTaskLocalVars");
// Fetch first task
Task task = taskService.createTaskQuery().singleResult();
// Complete first task
Map<String, Object> taskParams = new HashMap<String, Object>();
taskParams.put("a", 1);
taskParams.put("b", 1);
taskService.complete(task.getId(), taskParams, true);
// Verify vars are not stored process instance wide
assertNull(runtimeService.getVariable(processInstance.getId(), "a"));
assertNull(runtimeService.getVariable(processInstance.getId(), "b"));
// verify script listener has done its job
assertEquals(new Integer(2), runtimeService.getVariable(processInstance.getId(), "sum"));
// Fetch second task
task = taskService.createTaskQuery().singleResult();
}
public void testSetAssignee() {
User user = identityService.newUser("user");
identityService.saveUser(user);
......
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:activiti="http://activiti.org/bpmn"
targetNamespace="TwoTaskCategory">
<process id="testTaskLocalVars" name="The Two Task Process" activiti:candidateStarterUsers="kermit">
<startEvent id="theStart" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
<userTask id="theTask" name="my task">
<extensionElements>
<activiti:taskListener event="complete" class="org.activiti.engine.impl.bpmn.listener.ScriptTaskListener" >
<activiti:field name="script">
<activiti:string>
var a = task.getVariableLocal('a');
var b = task.getVariableLocal('b');
var sum = new java.lang.Integer(a + b);
task.setVariable('sum', sum);
</activiti:string>
</activiti:field>
<activiti:field name="language" stringValue="JavaScript" />
</activiti:taskListener>
</extensionElements>
</userTask>
<sequenceFlow id="flow2" sourceRef="theTask" targetRef="theSecondTask" />
<userTask id="theSecondTask" name="my task" />
<sequenceFlow id="flow3" sourceRef="theSecondTask" targetRef="theEnd" />
<endEvent id="theEnd" />
</process>
</definitions>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册