JBPM4.4任务撤销操作/回退操作/自由流
详细操作看注释。
操作示意图
task.jpdl.xml
<?xml version="1.0" encoding="UTF-8"?>
<process name="TaskAssignee" xmlns="http://jbpm.org/4.3/jpdl">
<start g="20,20,48,48">
<transition g="-23,-17" name="123" to="review"/>
</start>
<task assignee="#{order.owner}" g="96,16,127,52" name="review">
<transition g="-59,-17" name="to state1" to="state1"/>
<transition g="-53,-17" name="to task1" to="task1"/>
</task>
<state g="143,142,92,52" name="state1">
<transition g="-47,-17" name="to end1" to="end1"/>
</state>
<task assignee="#{order.owner}" g="348,86,92,52" name="task1">
<transition name="to end1" to="end1" g="-47,-17"/>
</task>
<end g="213,298,48,48" name="end1"/>
</process>
<?xml version="1.0" encoding="UTF-8"?>
<process name="TaskAssignee" xmlns="http://jbpm.org/4.3/jpdl">
<start g="20,20,48,48">
<transition g="-23,-17" name="123" to="review"/>
</start>
<task assignee="#{order.owner}" g="96,16,127,52" name="review">
<transition g="-59,-17" name="to state1" to="state1"/>
<transition g="-53,-17" name="to task1" to="task1"/>
</task>
<state g="143,142,92,52" name="state1">
<transition g="-47,-17" name="to end1" to="end1"/>
</state>
<task assignee="#{order.owner}" g="348,86,92,52" name="task1">
<transition name="to end1" to="end1" g="-47,-17"/>
</task>
<end g="213,298,48,48" name="end1"/>
</process>
TestUndoTask.java
package TestPackage;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.history.HistoryActivityInstance;
import org.jbpm.api.task.Task;
import org.jbpm.examples.task.assignee.Order;
import org.jbpm.pvm.internal.env.EnvironmentFactory;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.model.ActivityImpl;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
import org.jbpm.pvm.internal.model.TransitionImpl;
import org.jbpm.pvm.internal.task.TaskImpl;
import org.junit.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import IdentitySessionImp.DhbsIdentitySessionImp;
@ContextConfiguration("classpath:applicationContext.xml")
public class TestUndoTask extends AbstractJUnit4SpringContextTests {
private String deploymentId;
private ProcessEngine processEngine;
private TaskService taskService;
private RepositoryService repositoryService;
private ExecutionService executionService;
private HistoryService historyService;
public TestUndoTask() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
processEngine = Configuration.getProcessEngine();
repositoryService = processEngine
.getRepositoryService();
executionService = processEngine.getExecutionService();
taskService = processEngine.getTaskService();
historyService = processEngine.getHistoryService();
deploymentId = repositoryService.createDeployment()
.addResourceFromClasspath("task.jpdl.xml")
.deploy();
}
@After
public void tearDown() {
repositoryService.deleteDeploymentCascade(deploymentId);
}
/*@Resource
private DhbsIdentitySessionImp identitySession;*/
@Test
public void testTaskAssignee() {
//显示流程定义
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().uniqueResult();
System.out.println("显示流程定义 "+pd.getId()+" "+pd.getKey()+" "+" "+pd.getName());
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("order", new Order("johndoe"));
//启动1个实例
ProcessInstance processInstance3 = executionService.startProcessInstanceByKey("TaskAssignee", variables);
String p3id = processInstance3.getId();
//johndoe的任务列表这时有了1条待办任务
List<Task> taskList = taskService.findPersonalTasks("johndoe");
assertEquals(1, taskList.size());
for(Task task : taskList){
System.out.println("task.getId() = "+task.getId());
System.out.println("task.getName() = "+task.getName());
}
Task task1 = taskList.get(0);
//查看第一个待办任务所有流出的节点
Set<String> outcomes = taskService.getOutcomes(task1.getId());
Iterator it = outcomes.iterator();
while(it.hasNext()){
String str = (String) it.next();
System.out.println("outcome = "+str);
}
assertEquals("review", task1.getName());
assertEquals("johndoe", task1.getAssignee());
//流程向下运行
Map<String, Object> assignMap = new HashMap<String, Object>();
assignMap.put("order", new Order("we"));
taskService.completeTask(task1.getId(),"to task1",assignMap);
//重新查询才能看到状态改变
List<ProcessInstance> piList = executionService.createProcessInstanceQuery().list();
for(ProcessInstance pi : piList){
System.out.println(pi.findActiveActivityNames());
}
// 查看johndoe的待办任务列表是否空了
List<Task> johndoetaskList_ = taskService.findPersonalTasks("johndoe");
System.out.println("johndoetaskList0.size() = "+johndoetaskList_.size());
assertEquals(0, johndoetaskList_.size());
//打印在此节点流程的相关信息。这里可以看出getExecutionId()就是流程实例的id即processInstance.getId()
List<HistoryActivityInstance> list = historyService.createHistoryActivityInstanceQuery().activityName("state1").list();
for(HistoryActivityInstance activity : list){
System.out.println("getActivityName() = "+activity.getActivityName());
System.out.println("getExecutionId() = "+activity.getExecutionId());
}
// 查看撤销前johndoe的待办任务列表是否为零个
List<Task> johndoetaskList0 = taskService.findPersonalTasks("johndoe");
System.out.println("查看撤销前johndoe的待办任务列表是否为零个");
System.out.println("johndoe_taskList1.size() = "+johndoetaskList0.size());
if(johndoetaskList0.size() > 0){
TaskImpl before_johndoetask = (TaskImpl)johndoetaskList0.get(0);
String before_johndoetask_actName = before_johndoetask.getActivityName();
String before_johndoetask_ExecutionId = before_johndoetask.getExecutionId();
System.out.println("before_johndoetask_actName = "+before_johndoetask_actName);
System.out.println("before_johndoetask_ExecutionId = "+before_johndoetask_ExecutionId);
assertEquals(0, johndoetaskList0.size());
}
//查看撤销前we的待办任务列表是否为一个
List<Task> wetaskList1 = taskService.findPersonalTasks("we");
System.out.println("查看撤销前we的待办任务列表是否为一个");
System.out.println("we_taskList1.size() = "+wetaskList1.size());
if(wetaskList1.size() > 0){
TaskImpl before_wetask = (TaskImpl)wetaskList1.get(0);
String before_wetask_actName = before_wetask.getActivityName();
String before_wetask_ExecutionId = before_wetask.getId();
System.out.println("before_wetask_actName = "+before_wetask_actName);
System.out.println("before_wetask_ExecutionId = "+before_wetask_ExecutionId);
}
assertEquals(1, wetaskList1.size());
ProcessDefinitionImpl pdi = (ProcessDefinitionImpl)pd;
TaskImpl wetask = (TaskImpl)wetaskList1.get(0);
//进行撤销操作
addOutTransition(pdi,wetask,"review");
//重新查询才能看到状态改变
List<ProcessInstance> piList1 = executionService.createProcessInstanceQuery().list();
for(ProcessInstance pi : piList1){
System.out.println(pi.findActiveActivityNames());
}
// 查看撤销后johndoe的待办任务列表是否为一个
List<Task> johndoetaskList1 = taskService.findPersonalTasks("johndoe");
System.out.println("查看撤销后johndoe的待办任务列表是否为一个");
System.out.println("johndoetaskList1.size() = "+johndoetaskList1.size());
if(johndoetaskList1.size() > 0){
TaskImpl johndoetask = (TaskImpl)johndoetaskList1.get(0);
String johndoetask_actName = johndoetask.getActivityName();
String johndoetask_ExecutionId = johndoetask.getExecutionId();
System.out.println("johndoetask_actName = "+johndoetask_actName);
System.out.println("johndoetask_ExecutionId = "+johndoetask_ExecutionId);
}
//assertEquals(1, johndoetaskList1.size());
//查看撤销后we的待办任务列表是否变为零个
List<Task> wetaskList = taskService.findPersonalTasks("we");
System.out.println("查看撤销后we的待办任务列表是否为零个");
System.out.println("wetaskList.size() = "+wetaskList.size());
if(wetaskList.size() > 0){
TaskImpl taskwww = (TaskImpl)wetaskList.get(0);
String wetask_actName = taskwww.getActivityName();
String wetask_ExecutionId = taskwww.getExecutionId();
System.out.println("wetask_actName = "+wetask_actName);
System.out.println("wetask_ExecutionId = "+wetask_ExecutionId);
}
}
/**
* 动态创建连接当前任务节点至名称为destName的节点的Transition
* @param taskId 任务节点ID
* @param sourceTask 源节点Task
* @param destName 目标节点名称
*/
public void addOutTransition(ProcessDefinitionImpl pd,TaskImpl sourceTask,String destName){
EnvironmentFactory environmentFactory = (EnvironmentFactory) processEngine;
EnvironmentImpl env=null;
try {
env = environmentFactory.openEnvironment();
//取得当前流程的活动定定义
ActivityImpl sourceActivity = pd.findActivity(sourceTask.getActivityName());
//取得目标的活动定义
ActivityImpl destActivity=pd.findActivity(destName);
//为两个节点创建连接
TransitionImpl transition = sourceActivity.createOutgoingTransition();
transition.setName("to" + destName);
transition.setDestination(destActivity);
sourceActivity.addOutgoingTransition(transition);
System.out.println("sourceActivity.getName() = "+sourceActivity.getName());
System.out.println("destActivity.getName() = "+destActivity.getName());
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("order", new Order("johndoe"));
System.out.println("task1.getId() = "+sourceTask.getId());
taskService.completeTask(sourceTask.getId(),transition.getName(),variables);
}catch(Exception ex){
ex.getMessage();
}finally{
env.close();
}
}
}