JBPM4.4 撤销任务/回退任务/自由流

http://paomo30000.iteye.com/blog/768033

转处:

    博客分类: 
  • JAVA
JBPM4.4任务撤销操作/回退操作/自由流 
详细操作看注释。 
操作示意图 
JBPM4.4 撤销任务/回退任务/自由流_第1张图片 
task.jpdl.xml 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <process name="TaskAssignee" xmlns="http://jbpm.org/4.3/jpdl">  
  4.   
  5.   <start g="20,20,48,48">  
  6.     <transition g="-23,-17" name="123" to="review"/>  
  7.   </start>  
  8.   
  9.   <task assignee="#{order.owner}" g="96,16,127,52" name="review">  
  10.       <transition g="-59,-17" name="to state1" to="state1"/>  
  11.       <transition g="-53,-17" name="to task1" to="task1"/>  
  12.   </task>  
  13.   <state g="143,142,92,52" name="state1">  
  14.       <transition g="-47,-17" name="to end1" to="end1"/>  
  15.   </state>  
  16.     
  17.    <task assignee="#{order.owner}" g="348,86,92,52" name="task1">  
  18.       <transition name="to end1" to="end1" g="-47,-17"/>  
  19.    </task>  
  20.     <end g="213,298,48,48" name="end1"/>  
  21. </process>  


TestUndoTask.java 
Java代码   收藏代码
  1. package TestPackage;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import java.util.HashMap;  
  6. import java.util.Iterator;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9. import java.util.Set;  
  10.   
  11. import javax.annotation.Resource;  
  12.   
  13. import org.jbpm.api.Configuration;  
  14. import org.jbpm.api.ExecutionService;  
  15. import org.jbpm.api.HistoryService;  
  16. import org.jbpm.api.ProcessDefinition;  
  17. import org.jbpm.api.ProcessEngine;  
  18. import org.jbpm.api.ProcessInstance;  
  19. import org.jbpm.api.RepositoryService;  
  20. import org.jbpm.api.TaskService;  
  21. import org.jbpm.api.history.HistoryActivityInstance;  
  22. import org.jbpm.api.task.Task;  
  23. import org.jbpm.examples.task.assignee.Order;  
  24. import org.jbpm.pvm.internal.env.EnvironmentFactory;  
  25. import org.jbpm.pvm.internal.env.EnvironmentImpl;  
  26. import org.jbpm.pvm.internal.model.ActivityImpl;  
  27. import org.jbpm.pvm.internal.model.ExecutionImpl;  
  28. import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;  
  29. import org.jbpm.pvm.internal.model.TransitionImpl;  
  30. import org.jbpm.pvm.internal.task.TaskImpl;  
  31. import org.junit.*;  
  32. import org.springframework.test.context.ContextConfiguration;  
  33. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;  
  34.   
  35. import IdentitySessionImp.DhbsIdentitySessionImp;  
  36.   
  37. @ContextConfiguration("classpath:applicationContext.xml")  
  38. public class TestUndoTask extends AbstractJUnit4SpringContextTests {  
  39.     private String deploymentId;  
  40.     private ProcessEngine processEngine;  
  41.     private TaskService taskService;  
  42.     private RepositoryService repositoryService;  
  43.     private ExecutionService executionService;  
  44.     private HistoryService historyService;  
  45.       
  46.     public TestUndoTask() {  
  47.     }  
  48.   
  49.     @BeforeClass  
  50.     public static void setUpClass() throws Exception {  
  51.     }  
  52.   
  53.     @AfterClass  
  54.     public static void tearDownClass() throws Exception {  
  55.     }  
  56.   
  57.     @Before  
  58.     public void setUp() {  
  59.         processEngine = Configuration.getProcessEngine();  
  60.         repositoryService = processEngine  
  61.                 .getRepositoryService();  
  62.         executionService = processEngine.getExecutionService();  
  63.         taskService = processEngine.getTaskService();  
  64.         historyService = processEngine.getHistoryService();  
  65.         deploymentId = repositoryService.createDeployment()  
  66.         .addResourceFromClasspath("task.jpdl.xml")  
  67.         .deploy();  
  68.     }  
  69.   
  70.     @After  
  71.     public void tearDown() {  
  72.         repositoryService.deleteDeploymentCascade(deploymentId);  
  73.     }  
  74.   
  75.     /*@Resource 
  76.     private DhbsIdentitySessionImp identitySession;*/  
  77.   
  78.     @Test  
  79.     public void testTaskAssignee() {  
  80.             
  81.         //显示流程定义  
  82.           ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().uniqueResult();  
  83.           System.out.println("显示流程定义  "+pd.getId()+"  "+pd.getKey()+"  "+"  "+pd.getName());  
  84.         Map<String, Object> variables = new HashMap<String, Object>();   
  85.         variables.put("order"new Order("johndoe"));  
  86.         //启动1个实例  
  87.         ProcessInstance processInstance3 = executionService.startProcessInstanceByKey("TaskAssignee", variables);  
  88.         String p3id = processInstance3.getId();  
  89.         //johndoe的任务列表这时有了1条待办任务  
  90.         List<Task> taskList = taskService.findPersonalTasks("johndoe");  
  91.         assertEquals(1, taskList.size());  
  92.         for(Task task : taskList){  
  93.             System.out.println("task.getId() = "+task.getId());  
  94.             System.out.println("task.getName() = "+task.getName());  
  95.         }  
  96.           
  97.         Task task1 = taskList.get(0);  
  98.         //查看第一个待办任务所有流出的节点  
  99.         Set<String> outcomes = taskService.getOutcomes(task1.getId());  
  100.         Iterator it = outcomes.iterator();  
  101.         while(it.hasNext()){  
  102.             String str = (String) it.next();  
  103.             System.out.println("outcome = "+str);  
  104.         }  
  105.         assertEquals("review", task1.getName());  
  106.         assertEquals("johndoe", task1.getAssignee());  
  107.         //流程向下运行  
  108.         Map<String, Object> assignMap = new HashMap<String, Object>();   
  109.         assignMap.put("order"new Order("we"));  
  110.         taskService.completeTask(task1.getId(),"to task1",assignMap);  
  111.         //重新查询才能看到状态改变  
  112.         List<ProcessInstance> piList = executionService.createProcessInstanceQuery().list();  
  113.         for(ProcessInstance pi : piList){  
  114.             System.out.println(pi.findActiveActivityNames());  
  115.         }  
  116.           
  117.         // 查看johndoe的待办任务列表是否空了  
  118.         List<Task> johndoetaskList_ = taskService.findPersonalTasks("johndoe");  
  119.         System.out.println("johndoetaskList0.size() = "+johndoetaskList_.size());  
  120.         assertEquals(0, johndoetaskList_.size());  
  121.           
  122.         //打印在此节点流程的相关信息。这里可以看出getExecutionId()就是流程实例的id即processInstance.getId()  
  123.         List<HistoryActivityInstance> list = historyService.createHistoryActivityInstanceQuery().activityName("state1").list();  
  124.         for(HistoryActivityInstance activity : list){  
  125.             System.out.println("getActivityName() = "+activity.getActivityName());  
  126.             System.out.println("getExecutionId() = "+activity.getExecutionId());  
  127.         }  
  128.           
  129.      // 查看撤销前johndoe的待办任务列表是否为零个  
  130.         List<Task> johndoetaskList0 = taskService.findPersonalTasks("johndoe");  
  131.         System.out.println("查看撤销前johndoe的待办任务列表是否为零个");  
  132.         System.out.println("johndoe_taskList1.size() = "+johndoetaskList0.size());  
  133.         if(johndoetaskList0.size() > 0){  
  134.             TaskImpl before_johndoetask = (TaskImpl)johndoetaskList0.get(0);  
  135.             String before_johndoetask_actName = before_johndoetask.getActivityName();  
  136.             String before_johndoetask_ExecutionId = before_johndoetask.getExecutionId();  
  137.             System.out.println("before_johndoetask_actName = "+before_johndoetask_actName);  
  138.             System.out.println("before_johndoetask_ExecutionId = "+before_johndoetask_ExecutionId);  
  139.             assertEquals(0, johndoetaskList0.size());  
  140.         }  
  141.           
  142.         //查看撤销前we的待办任务列表是否为一个  
  143.         List<Task> wetaskList1 = taskService.findPersonalTasks("we");  
  144.         System.out.println("查看撤销前we的待办任务列表是否为一个");  
  145.         System.out.println("we_taskList1.size() = "+wetaskList1.size());  
  146.         if(wetaskList1.size() > 0){  
  147.             TaskImpl before_wetask = (TaskImpl)wetaskList1.get(0);  
  148.             String before_wetask_actName = before_wetask.getActivityName();  
  149.             String before_wetask_ExecutionId = before_wetask.getId();  
  150.             System.out.println("before_wetask_actName = "+before_wetask_actName);  
  151.             System.out.println("before_wetask_ExecutionId = "+before_wetask_ExecutionId);  
  152.         }  
  153.           
  154.         assertEquals(1, wetaskList1.size());  
  155.         ProcessDefinitionImpl pdi = (ProcessDefinitionImpl)pd;  
  156.         TaskImpl wetask = (TaskImpl)wetaskList1.get(0);  
  157.         //进行撤销操作  
  158.         addOutTransition(pdi,wetask,"review");  
  159.         //重新查询才能看到状态改变  
  160.         List<ProcessInstance> piList1 = executionService.createProcessInstanceQuery().list();  
  161.         for(ProcessInstance pi : piList1){  
  162.             System.out.println(pi.findActiveActivityNames());  
  163.         }  
  164.           
  165.         // 查看撤销后johndoe的待办任务列表是否为一个  
  166.         List<Task> johndoetaskList1 = taskService.findPersonalTasks("johndoe");  
  167.         System.out.println("查看撤销后johndoe的待办任务列表是否为一个");  
  168.         System.out.println("johndoetaskList1.size() = "+johndoetaskList1.size());  
  169.         if(johndoetaskList1.size() > 0){  
  170.             TaskImpl johndoetask = (TaskImpl)johndoetaskList1.get(0);  
  171.             String johndoetask_actName = johndoetask.getActivityName();  
  172.             String johndoetask_ExecutionId = johndoetask.getExecutionId();  
  173.             System.out.println("johndoetask_actName = "+johndoetask_actName);  
  174.             System.out.println("johndoetask_ExecutionId = "+johndoetask_ExecutionId);  
  175.         }  
  176.           
  177.         //assertEquals(1, johndoetaskList1.size());  
  178.         //查看撤销后we的待办任务列表是否变为零个  
  179.         List<Task> wetaskList = taskService.findPersonalTasks("we");  
  180.         System.out.println("查看撤销后we的待办任务列表是否为零个");  
  181.         System.out.println("wetaskList.size() = "+wetaskList.size());  
  182.         if(wetaskList.size() > 0){  
  183.             TaskImpl taskwww = (TaskImpl)wetaskList.get(0);  
  184.             String wetask_actName = taskwww.getActivityName();  
  185.             String wetask_ExecutionId = taskwww.getExecutionId();  
  186.             System.out.println("wetask_actName = "+wetask_actName);  
  187.             System.out.println("wetask_ExecutionId = "+wetask_ExecutionId);  
  188.         }  
  189.       }  
  190.       
  191.     /**  
  192.        * 动态创建连接当前任务节点至名称为destName的节点的Transition  
  193.        * @param taskId 任务节点ID  
  194.        * @param sourceTask 源节点Task 
  195.        * @param destName  目标节点名称  
  196.        */  
  197.       public void addOutTransition(ProcessDefinitionImpl pd,TaskImpl sourceTask,String destName){  
  198.        EnvironmentFactory environmentFactory = (EnvironmentFactory) processEngine;  
  199.        EnvironmentImpl env=null;  
  200.        try {  
  201.            env = environmentFactory.openEnvironment();  
  202.            //取得当前流程的活动定定义  
  203.            ActivityImpl sourceActivity = pd.findActivity(sourceTask.getActivityName());  
  204.            //取得目标的活动定义  
  205.            ActivityImpl destActivity=pd.findActivity(destName);  
  206.            //为两个节点创建连接  
  207.            TransitionImpl transition = sourceActivity.createOutgoingTransition();  
  208.            transition.setName("to" + destName);  
  209.            transition.setDestination(destActivity);  
  210.            sourceActivity.addOutgoingTransition(transition);  
  211.            System.out.println("sourceActivity.getName() = "+sourceActivity.getName());  
  212.            System.out.println("destActivity.getName() = "+destActivity.getName());  
  213.              
  214.            Map<String, Object> variables = new HashMap<String, Object>();   
  215.            variables.put("order"new Order("johndoe"));  
  216.            System.out.println("task1.getId() = "+sourceTask.getId());  
  217.             
  218.            taskService.completeTask(sourceTask.getId(),transition.getName(),variables);  
  219.        }catch(Exception ex){  
  220.            ex.getMessage();  
  221.        }finally{    
  222.            env.close();  
  223.        }  
  224.     }  
  225. }  

你可能感兴趣的:(JBPM4.4)