Jbpm4.4会签

前言

有次面试过程中被问到了JBPM如何实现会签,于是回来后查了写相关资料,整理了一下,跟大家一起分享下。

 

正题

一、会签概述

会签就是多人同时对一个任务进行操作,那么针对会签有一下几种情况:

1)会签中某一个人不同意,就马上退回节点到申请;

2)全部会签结束后,如果有不同意的,则退回节点到申请;

3)全部会签结束后,超过半数不同意的,退回节点到申请。

 

二、jbpm4.4实现会签

我们知道工作流要支持串行和并行,所以它肯定也支持我们的会签(并行)。今天主要以第一种情况“一票否决制”来进行编写。

 

1.实现思路

在会签节点任务创建后,通过程序按会签人数创建出对应的会签子任务,对应到具体的审批人上。

即提交到会签后,获取到会签的task,然后针对这个task创建subtask。

 

 

2.流程图

流程图很简单,就是一个Task来表示会签节点,由于我们的思路是在会签节点上创建子节点,所以具体的实现需要从我们的代码中进行查看。

Jbpm4.4会签_第1张图片

process.jpd.xml

<?xml version="1.0" encoding="UTF-8"?>
<process name="会签实例" xmlns="http://jbpm.org/4.3/jpdl" description="会签中一个不同意,会签就结束">
 <start g="110,14,48,48" name="Start">
  <transition g="-95,-17" name="to JointlySign" to="会签"/>
 </start>
 <end g="110,264,48,48" name="End"/>
 <task g="88,96,92,52" name="会签">
  <assignment-handler class="com.baihe.testjbpm2.AssignTask">
  </assignment-handler>
  <transition g="-71,-17" name="to 执行" to="执行"/>
  <transition g="-41,-17" name="to 结束" to="End"/>
 </task>
 <state g="16,180,92,52" name="执行">
  <transition g="-41,-17" name="to End" to="End"/>
 </state>
</process>



3.实现代码

环境:MyEclipse8.5+jbpm4.4

AssignTask.java

package com.baihe.testjbpm2;
import java.util.List;     
import org.jbpm.api.Configuration;   
import org.jbpm.api.ProcessEngine;   
import org.jbpm.api.TaskService;   
import org.jbpm.api.model.OpenExecution;   
import org.jbpm.api.task.Assignable;   
import org.jbpm.api.task.AssignmentHandler;   
import org.jbpm.api.task.Participation;   
import org.jbpm.api.task.Task;   
import org.jbpm.pvm.internal.task.OpenTask;   
  
public class AssignTask implements AssignmentHandler{   
  
    private static final long serialVersionUID = 1L;   
    List<String> participants;   
    ProcessEngine processEngine=Configuration.getProcessEngine();   
    TaskService taskService=processEngine.getTaskService();   
       
    @SuppressWarnings("unchecked")   
    public void assign(Assignable assignable, OpenExecution execution) throws Exception {   
        //System.out.println("分配");   
        String pid=execution.getProcessInstance().getId();   
        //System.out.println("pid :"+pid);   
        Task task=taskService.createTaskQuery().processInstanceId(pid).activityName(execution.getName()).uniqueResult();   
        //System.out.println(task.getName());   
        participants=(List<String>)taskService.getVariable(task.getId(), "participants");   
        if(participants!=null){   
            for(String participant:participants){   
                //System.out.println(participant);   
                Task subTask=((OpenTask)task).createSubTask();   
                subTask.setAssignee(participant);   
                taskService.addTaskParticipatingUser(task.getId(), participant, Participation.CLIENT);   
            }   
        }   
    }   
} 


Sign.java

package com.baihe.testjbpm2;

import org.jbpm.api.TaskService;   
import org.jbpm.api.cmd.Command;   
import org.jbpm.api.cmd.Environment;   
import org.jbpm.api.task.Task;   
  
public class Sign implements Command<Boolean>{   
  
    private static final long serialVersionUID = 1L;   
    public static String VAR_SIGN="同意";   
    private String PASS;   
    private String NOPASS;   
    private String parentTaskId;   
    private Task parentTask;   
    private Task joinTask;   
    String pid;   
    public Sign(String parentTaskId,Task joinTask,String PASS,String NOPASS){   
        this.parentTaskId=parentTaskId;   
        this.PASS=PASS;   
        this.NOPASS=NOPASS;   
        this.joinTask=joinTask;   
    }   
       
    public String getPid(){   
        return pid;   
    }   
       
    public Boolean execute(Environment environment) throws Exception {   
        TaskService taskService=environment.get(TaskService.class);   
        parentTask=taskService.getTask(parentTaskId);   
        pid=parentTask.getExecutionId();   
        String sign=(String)taskService.getVariable(joinTask.getId(), VAR_SIGN);   
        if(sign!=null&&sign.equals("不同意")){   
            taskService.completeTask(joinTask.getId());   
            taskService.completeTask(parentTask.getId(), NOPASS);   
            return true;   
        }   
        taskService.completeTask(joinTask.getId());   
        if(taskService.getSubTasks(parentTaskId).size()==0){   
            taskService.completeTask(parentTaskId,PASS);   
            return true;   
        }   
        return false;   
    }   
  
}


test.java

package com.baihe.testjbpm2;

import java.util.ArrayList;   
import java.util.HashMap;   
import java.util.List;   
import java.util.Map;   
  
import org.jbpm.api.Configuration;   
import org.jbpm.api.ExecutionService;   
import org.jbpm.api.HistoryService;   
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.HistoryProcessInstance;   
import org.jbpm.api.task.Task;   
  
public class Test {   
    public static void main(String []args){   
        ProcessEngine processEngine=Configuration.getProcessEngine();   
        ExecutionService executionService=processEngine.getExecutionService();   
        TaskService taskService=processEngine.getTaskService();   
        RepositoryService repositoryService=processEngine.getRepositoryService();   
           
        repositoryService.createDeployment().addResourceFromClasspath("com/baihe/jbpm/process.jpdl.xml").deploy();   
           
        Map<String,Object> variables=new HashMap<String,Object>();   
        List<String> pariticipants=new ArrayList<String>();   
        pariticipants.add("yunix");   
        pariticipants.add("snrqtdhuqf");   
        variables.put("participants", pariticipants);   
        ProcessInstance processInstance=executionService.startProcessInstanceByKey("会签实例",variables);   
  
        Task task=taskService.createTaskQuery()   
                            .processInstanceId(processInstance.getId())   
                            .activityName(processInstance.findActiveActivityNames().iterator().next()).uniqueResult();   
           
        //System.out.println(task.getId());   
           
        Task joinTask=taskService.findPersonalTasks("yunix").get(0);   
        Sign sign=new Sign(task.getId(),joinTask,"to 执行","to 结束");   
        processEngine.execute(sign);   
           
        joinTask=taskService.findPersonalTasks("snrqtdhuqf").get(0);   
        sign=new Sign(task.getId(),joinTask,"to 执行","to 结束");   
        processEngine.execute(sign);   
        processInstance=executionService.findProcessInstanceById(processInstance.getId());   
        System.out.println(processInstance.isActive("执行"));   
           
        String executionId = processInstance.findActiveExecutionIn("执行").getId();   
        processInstance=executionService.signalExecutionById(executionId);   
        System.out.println("流程是否结束:"+processInstance.isEnded());   
           
           
           
        processInstance=executionService.startProcessInstanceByKey("会签实例",variables);   
        System.out.println("会签不通过流程开始,流程ID为:"+processInstance.getId()+" ");   
        task=taskService.createTaskQuery()   
                        .processInstanceId(processInstance.getId())   
                        .activityName(processInstance.findActiveActivityNames().iterator().next())   
                        .uniqueResult();   
        joinTask=taskService.findPersonalTasks("yunix").get(0);   
        Map<String,Object> v=new HashMap<String,Object>();   
        v.put(Sign.VAR_SIGN, "不同意");   
       taskService.setVariables(joinTask.getId(), v);   
       sign=new Sign(task.getId(),joinTask,"to 执行","to 结束");   
        System.out.println(processEngine.execute(sign));   
  
        HistoryService historyService=processEngine.getHistoryService();   
        List<HistoryProcessInstance> hProcessInstances = historyService.createHistoryProcessInstanceQuery().processInstanceId(processInstance.getId())   
        .list();   
        for(HistoryProcessInstance h:hProcessInstances){   
            System.out.println("流程"+processInstance.getId()+"状态:"+h.getState());   
        }   
    }   
} 


测试结果:

true

流程是否结束:true

会签不通过流程开始,流程ID为:会签实例.17

true

流程会签实例.17状态:ended

 

注:本文属于转载,原文地址:http://blog.163.com/wex660@126/blog/static/24153065201142301621118/

 

 

你可能感兴趣的:(Jbpm4.4会签)