jbpm4.4 会签实例

开发环境为MyEclipse8.5 jbpm4.4。 

根据会签的一票否决制编写。

 

流程的XML文件 process.jpd.xml

 

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <process name="会签实例" xmlns="http://jbpm.org/4.3/jpdl" description="会签中一个不同意,会签就结束">  
  4.     <start g="110,14,48,48" name="Start">  
  5.         <transition g="-95,-17" name="to JointlySign" to="会签"/>  
  6.     </start>  
  7.     <end g="110,264,48,48" name="End"/>  
  8.     <task g="88,96,92,52" name="会签">  
  9.         <assignment-handler class="test.join.AssignTask">  
  10.         </assignment-handler>  
  11.         <transition g="-71,-17" name="to 执行" to="执行"/>  
  12.         <transition g="-41,-17" name="to 结束" to="End"/>  
  13.     </task>  
  14.     <state g="16,180,92,52" name="执行">  
  15.         <transition g="-41,-17" name="to End" to="End"/>  
  16.     </state>  
  17. </process>  

 

 

 

 

 

AssignTask.java

 

  1. package test.join;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.jbpm.api.Configuration;  
  6. import org.jbpm.api.ProcessEngine;  
  7. import org.jbpm.api.TaskService;  
  8. import org.jbpm.api.model.OpenExecution;  
  9. import org.jbpm.api.task.Assignable;  
  10. import org.jbpm.api.task.AssignmentHandler;  
  11. import org.jbpm.api.task.Participation;  
  12. import org.jbpm.api.task.Task;  
  13. import org.jbpm.pvm.internal.task.OpenTask;  
  14.   
  15. public class AssignTask implements AssignmentHandler{  
  16.   
  17.     private static final long serialVersionUID = 1L;  
  18.     List<String> participants;  
  19.     ProcessEngine processEngine=Configuration.getProcessEngine();  
  20.     TaskService taskService=processEngine.getTaskService();  
  21.       
  22.     @SuppressWarnings("unchecked")  
  23.     public void assign(Assignable assignable, OpenExecution execution) throws Exception {  
  24.         //System.out.println("分配");  
  25.         String pid=execution.getProcessInstance().getId();  
  26.         //System.out.println("pid :"+pid);  
  27.         Task task=taskService.createTaskQuery().processInstanceId(pid).activityName(execution.getName()).uniqueResult();  
  28.         //System.out.println(task.getName());  
  29.         participants=(List<String>)taskService.getVariable(task.getId(), "participants");  
  30.         if(participants!=null){  
  31.             for(String participant:participants){  
  32.                 //System.out.println(participant);  
  33.                 Task subTask=((OpenTask)task).createSubTask();  
  34.                 subTask.setAssignee(participant);  
  35.                 taskService.addTaskParticipatingUser(task.getId(), participant, Participation.CLIENT);  
  36.             }  
  37.         }  
  38.     }  
  39. }  

 

 

Sign.java

 

  1. package test.join;  
  2.   
  3. import org.jbpm.api.TaskService;  
  4. import org.jbpm.api.cmd.Command;  
  5. import org.jbpm.api.cmd.Environment;  
  6. import org.jbpm.api.task.Task;  
  7.   
  8. public class Sign implements Command<Boolean>{  
  9.   
  10.     private static final long serialVersionUID = 1L;  
  11.     public static String VAR_SIGN="同意";  
  12.     private String PASS;  
  13.     private String NOPASS;  
  14.     private String parentTaskId;  
  15.     private Task parentTask;  
  16.     private Task joinTask;  
  17.     String pid;  
  18.     public Sign(String parentTaskId,Task joinTask,String PASS,String NOPASS){  
  19.         this.parentTaskId=parentTaskId;  
  20.         this.PASS=PASS;  
  21.         this.NOPASS=NOPASS;  
  22.         this.joinTask=joinTask;  
  23.     }  
  24.       
  25.     public String getPid(){  
  26.         return pid;  
  27.     }  
  28.       
  29.     public Boolean execute(Environment environment) throws Exception {  
  30.         TaskService taskService=environment.get(TaskService.class);  
  31.         parentTask=taskService.getTask(parentTaskId);  
  32.         pid=parentTask.getExecutionId();  
  33.         String sign=(String)taskService.getVariable(joinTask.getId(), VAR_SIGN);  
  34.         if(sign!=null&&sign.equals("不同意")){  
  35.             taskService.completeTask(joinTask.getId());  
  36.             taskService.completeTask(parentTask.getId(), NOPASS);  
  37.             return true;  
  38.         }  
  39.         taskService.completeTask(joinTask.getId());  
  40.         if(taskService.getSubTasks(parentTaskId).size()==0){  
  41.             taskService.completeTask(parentTaskId,PASS);  
  42.             return true;  
  43.         }  
  44.         return false;  
  45.     }  
  46.   
  47. }  

 

 

 

test.java

 

  1. package test.join;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.jbpm.api.Configuration;  
  9. import org.jbpm.api.ExecutionService;  
  10. import org.jbpm.api.HistoryService;  
  11. import org.jbpm.api.ProcessEngine;  
  12. import org.jbpm.api.ProcessInstance;  
  13. import org.jbpm.api.RepositoryService;  
  14. import org.jbpm.api.TaskService;  
  15. import org.jbpm.api.history.HistoryProcessInstance;  
  16. import org.jbpm.api.task.Task;  
  17.   
  18. public class Test {  
  19.     public static void main(String []args){  
  20.         ProcessEngine processEngine=Configuration.getProcessEngine();  
  21.         ExecutionService executionService=processEngine.getExecutionService();  
  22.         TaskService taskService=processEngine.getTaskService();  
  23.         RepositoryService repositoryService=processEngine.getRepositoryService();  
  24.           
  25.         repositoryService.createDeployment().addResourceFromClasspath("test/join/process.jpdl.xml").deploy();  
  26.           
  27.         Map<String,Object> variables=new HashMap<String,Object>();  
  28.         List<String> pariticipants=new ArrayList<String>();  
  29.         pariticipants.add("yunix");  
  30.         pariticipants.add("snrqtdhuqf");  
  31.         variables.put("participants", pariticipants);  
  32.         ProcessInstance processInstance=executionService.startProcessInstanceByKey("会签实例",variables);  
  33.   
  34.         Task task=taskService.createTaskQuery()  
  35.                             .processInstanceId(processInstance.getId())  
  36.                             .activityName(processInstance.findActiveActivityNames().iterator().next()).uniqueResult();  
  37.           
  38.         //System.out.println(task.getId());  
  39.           
  40.         Task joinTask=taskService.findPersonalTasks("yunix").get(0);  
  41.         Sign sign=new Sign(task.getId(),joinTask,"to 执行","to 结束");  
  42.         processEngine.execute(sign);  
  43.           
  44.         joinTask=taskService.findPersonalTasks("snrqtdhuqf").get(0);  
  45.         sign=new Sign(task.getId(),joinTask,"to 执行","to 结束");  
  46.         processEngine.execute(sign);  
  47.         processInstance=executionService.findProcessInstanceById(processInstance.getId());  
  48.         System.out.println(processInstance.isActive("执行"));  
  49.           
  50.         String executionId = processInstance.findActiveExecutionIn("执行").getId();  
  51.         processInstance=executionService.signalExecutionById(executionId);  
  52.         System.out.println("流程是否结束:"+processInstance.isEnded());  
  53.           
  54.           
  55.           
  56.         processInstance=executionService.startProcessInstanceByKey("会签实例",variables);  
  57.         System.out.println("会签不通过流程开始,流程ID为:"+processInstance.getId()+" ");  
  58.         task=taskService.createTaskQuery()  
  59.                         .processInstanceId(processInstance.getId())  
  60.                         .activityName(processInstance.findActiveActivityNames().iterator().next())  
  61.                         .uniqueResult();  
  62.         joinTask=taskService.findPersonalTasks("yunix").get(0);  
  63.         Map<String,Object> v=new HashMap<String,Object>();  
  64.         v.put(Sign.VAR_SIGN, "不同意");  
  65.         taskService.setVariables(joinTask.getId(), v);  
  66.         sign=new Sign(task.getId(),joinTask,"to 执行","to 结束");  
  67.         System.out.println(processEngine.execute(sign));  
  68.   
  69.         HistoryService historyService=processEngine.getHistoryService();  
  70.         List<HistoryProcessInstance> hProcessInstances = historyService.createHistoryProcessInstanceQuery().processInstanceId(processInstance.getId())  
  71.         .list();  
  72.         for(HistoryProcessInstance h:hProcessInstances){  
  73.             System.out.println("流程"+processInstance.getId()+"状态:"+h.getState());  
  74.         }  
  75.     }  
  76. }  

 

 

测试结果:

true
流程是否结束:true
会签不通过流程开始,流程ID为:会签实例.17 
true
流程会签实例.17状态:ended

 

posted @ 2012-12-15 11:39  eggbucket  阅读(1544)  评论(0编辑  收藏  举报