1、功能需求
會簽實作多個人同時審批,任意一個人不同意時,會簽任務結束,不同意走八戒審批,同意走悟空審批,最後流程結束。流程圖如下:
繪制流程圖:動态設定審批人,完成條件${(pass == 'no')||(nrOfCompletedInstances/nrOfInstances==1)}
添加表單字段控件FormProperty_29f662k-_!string-_!審批意見-_!請輸入-_!s,需和前端約定,控件解析格式
添加執行監聽器,任務結束時調用。
網關後添加表達式判斷
開啟流程執行個體,對流程圖中參數指派
//啟動流程執行個體
@GetMapping(value = "/startProcess")
public AjaxResponse startProcess(@RequestParam("processDefinitionKey") String processDefinitionKey,
@RequestParam("instanceName") String instanceName,
@RequestParam("instanceVariable") String instanceVariable) {
try {
if (GlobalConfig.Test) {
securityUtil.logInAs("bajie");
} else {
securityUtil.logInAs(SecurityContextHolder.getContext().getAuthentication().getName());
}
List<String> list = Arrays.asList("bajie", "wukong");
Map<String, Object> map = new HashMap<>();
//設定會簽人員
map.put("countersigner", list);
map.put("num", list.size());
//初始化pass變量
map.put("pass", "yes");
ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
.start()
//開啟流程,參數為流程圖的key,如此接口綁定的流程圖為countersign(bpmnjs流程圖中的編号)
.withProcessDefinitionKey(processDefinitionKey)
.withName(instanceName)
//.withVariable("content", instanceVariable)
//.withVariable("參數2", "參數2的值")
.withVariables(map)
.withBusinessKey("自定義BusinessKey")
.build());
return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
GlobalConfig.ResponseCode.SUCCESS.getDesc(), processInstance.getName() + ";" + processInstance.getId());
} catch (Exception e) {
return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.ERROR.getCode(),
"建立流程執行個體失敗", e.toString());
}
}
監聽器相關代碼
package com.imooc.activitiweb.listener;
import com.imooc.activitiweb.util.SpringUtils;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.task.Task;
import java.util.Map;
/**
* 消息通知任務監聽器
*/
public class FileCheckExecutionListener implements ExecutionListener {
@Override
public void notify(DelegateExecution execution) {
//修改審批狀态
Map<String, Object> variables = execution.getVariables();
//在執行監聽器中通過,執行執行個體id擷取目前任務資訊
TaskService taskService = SpringUtils.getBean(TaskService.class);
Task task = taskService.createTaskQuery().processInstanceId(execution.getProcessInstanceId()).executionId(execution.getId()).singleResult();
if (task == null) {
return;
}
//擷取目前任務執行人
String assignee = task.getAssignee();
System.out.println(assignee);
//通過流程圖中表單字段id擷取表單字段值,即使用者點選審批,在輸入框中填入的'yes' 或'no'
String s = "FormProperty_29f662k";
Object o = variables.get(s);
//若為no,設定result參數變量為n,pass為no,pass在流程啟動時預設初值為yes
if (o != null && o.toString().equals("no")) {
//會簽結束,設定參數result為N,下個任務為申請
execution.setVariable("pass", "no");
execution.setVariable("result", "N");
} else {
//全部審批完
Integer complete = (Integer) execution.getParent().getVariable("nrOfCompletedInstances");
Integer all = (Integer) execution.getParent().getVariable("nrOfInstances");
execution.setVariable("nrOfCompletedInstances", complete);
//說明都完成了并且沒有人拒絕
if ((complete + 1) / all == 1) {
execution.setVariable("result", "Y");
}
}
}
}
工具類
package com.imooc.activitiweb.util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* spring工具類 友善在非spring管理環境中擷取bean
*
* @author piesat
*/
@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{
/** Spring應用上下文環境 */
private static ConfigurableListableBeanFactory beanFactory;
private static ApplicationContext applicationContext;
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
{
SpringUtils.beanFactory = beanFactory;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
SpringUtils.applicationContext = applicationContext;
}
/**
* 擷取對象
*
* @param name
* @return Object 一個以所給名字注冊的bean的執行個體
* @throws org.springframework.beans.BeansException
*
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException
{
return (T) beanFactory.getBean(name);
}
/**
* 擷取類型為requiredType的對象
*
* @param clz
* @return
* @throws org.springframework.beans.BeansException
*
*/
public static <T> T getBean(Class<T> clz) throws BeansException
{
T result = (T) beanFactory.getBean(clz);
return result;
}
/**
* 如果BeanFactory包含一個與所給名稱比對的bean定義,則傳回true
*
* @param name
* @return boolean
*/
public static boolean containsBean(String name)
{
return beanFactory.containsBean(name);
}
/**
* 判斷以給定名字注冊的bean定義是一個singleton還是一個prototype。 如果與給定名字相應的bean定義沒有被找到,将會抛出一個異常(NoSuchBeanDefinitionException)
*
* @param name
* @return boolean
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
*
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
{
return beanFactory.isSingleton(name);
}
/**
* @param name
* @return Class 注冊對象的類型
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
*
*/
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
{
return beanFactory.getType(name);
}
/**
* 如果給定的bean名字在bean定義中有别名,則傳回這些别名
*
* @param name
* @return
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
*
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
{
return beanFactory.getAliases(name);
}
/**
* 擷取aop代理對象
*
* @param invoker
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getAopProxy(T invoker)
{
return (T) AopContext.currentProxy();
}
/**
* 擷取目前的環境配置,無配置傳回null
*
* @return 目前的環境配置
*/
public static String[] getActiveProfiles()
{
return applicationContext.getEnvironment().getActiveProfiles();
}
/**
* 擷取目前的環境配置,當有多個環境配置時,隻擷取第一個
*
* @return 目前的環境配置
*/
/* public static String getActiveProfile()
{
final String[] activeProfiles = getActiveProfiles();
return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
}*/
}
流程圖xml
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:activiti="http://activiti.org/bpmn" id="sample-diagram" targetNamespace="http://activiti.org/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
<bpmn2:process id="countersign" name="會簽多執行個體測試" isExecutable="true">
<bpmn2:startEvent id="StartEvent_1" name="開始" activiti:formKey="StartEvent_1">
<bpmn2:outgoing>Flow_0c7d7xo</bpmn2:outgoing>
</bpmn2:startEvent>
<bpmn2:userTask id="Activity_1mfnzrp" name="會簽審批" activiti:formKey="Activity_1mfnzrp" activiti:assignee="${assignee}">
<bpmn2:extensionElements>
<activiti:formProperty id="FormProperty_29f662k-_!string-_!審批意見-_!請輸入-_!s" type="string" />
<activiti:executionListener class="com.imooc.activitiweb.listener.FileCheckExecutionListener" event="end" />
</bpmn2:extensionElements>
<bpmn2:incoming>Flow_0c7d7xo</bpmn2:incoming>
<bpmn2:outgoing>Flow_1p2tz1w</bpmn2:outgoing>
<bpmn2:multiInstanceLoopCharacteristics activiti:collection="countersigner" activiti:elementVariable="assignee">
<bpmn2:loopCardinality xsi:type="bpmn2:tFormalExpression">${num}</bpmn2:loopCardinality>
<bpmn2:completionCondition xsi:type="bpmn2:tFormalExpression">${(pass == 'no')||(nrOfCompletedInstances/nrOfInstances==1)}</bpmn2:completionCondition>
</bpmn2:multiInstanceLoopCharacteristics>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="Flow_1p2tz1w" sourceRef="Activity_1mfnzrp" targetRef="Gateway_1946bhz" />
<bpmn2:userTask id="Activity_13x0ps8" name="八戒" activiti:formKey="Activity_13x0ps8" activiti:assignee="bajie">
<bpmn2:incoming>Flow_1mknpc1</bpmn2:incoming>
<bpmn2:outgoing>Flow_0f15fkh</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="Flow_1mknpc1" name="不同意" sourceRef="Gateway_1946bhz" targetRef="Activity_13x0ps8">
<bpmn2:conditionExpression xsi:type="bpmn2:tFormalExpression">${result== 'N'}</bpmn2:conditionExpression>
</bpmn2:sequenceFlow>
<bpmn2:exclusiveGateway id="Gateway_1946bhz">
<bpmn2:incoming>Flow_1p2tz1w</bpmn2:incoming>
<bpmn2:outgoing>Flow_1mknpc1</bpmn2:outgoing>
<bpmn2:outgoing>Flow_0bp4fi6</bpmn2:outgoing>
</bpmn2:exclusiveGateway>
<bpmn2:sequenceFlow id="Flow_0c7d7xo" sourceRef="StartEvent_1" targetRef="Activity_1mfnzrp" />
<bpmn2:userTask id="Activity_0c7dfe8" name="悟空" activiti:formKey="Activity_0c7dfe8" activiti:assignee="wukong">
<bpmn2:incoming>Flow_0bp4fi6</bpmn2:incoming>
<bpmn2:outgoing>Flow_006s5b1</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="Flow_0bp4fi6" name="同意" sourceRef="Gateway_1946bhz" targetRef="Activity_0c7dfe8">
<bpmn2:conditionExpression xsi:type="bpmn2:tFormalExpression">${result== 'Y'}</bpmn2:conditionExpression>
</bpmn2:sequenceFlow>
<bpmn2:endEvent id="Event_0fb32y5" name="結束">
<bpmn2:incoming>Flow_006s5b1</bpmn2:incoming>
<bpmn2:incoming>Flow_0f15fkh</bpmn2:incoming>
</bpmn2:endEvent>
<bpmn2:sequenceFlow id="Flow_006s5b1" sourceRef="Activity_0c7dfe8" targetRef="Event_0fb32y5" />
<bpmn2:sequenceFlow id="Flow_0f15fkh" sourceRef="Activity_13x0ps8" targetRef="Event_0fb32y5" />
</bpmn2:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="countersigned">
<bpmndi:BPMNEdge id="Flow_0c7d7xo_di" bpmnElement="Flow_0c7d7xo">
<di:waypoint x="308" y="250" />
<di:waypoint x="430" y="250" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1mknpc1_di" bpmnElement="Flow_1mknpc1">
<di:waypoint x="670" y="275" />
<di:waypoint x="670" y="340" />
<bpmndi:BPMNLabel>
<dc:Bounds x="669" y="305" width="33" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1p2tz1w_di" bpmnElement="Flow_1p2tz1w">
<di:waypoint x="530" y="250" />
<di:waypoint x="645" y="250" />
<bpmndi:BPMNLabel>
<dc:Bounds x="577" y="232" width="22" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0bp4fi6_di" bpmnElement="Flow_0bp4fi6">
<di:waypoint x="695" y="250" />
<di:waypoint x="810" y="250" />
<bpmndi:BPMNLabel>
<dc:Bounds x="741" y="232" width="23" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_006s5b1_di" bpmnElement="Flow_006s5b1">
<di:waypoint x="910" y="250" />
<di:waypoint x="1032" y="250" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0f15fkh_di" bpmnElement="Flow_0f15fkh">
<di:waypoint x="720" y="380" />
<di:waypoint x="1050" y="380" />
<di:waypoint x="1050" y="268" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="272" y="232" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="279" y="275" width="22" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1mfnzrp_di" bpmnElement="Activity_1mfnzrp">
<dc:Bounds x="430" y="210" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_13x0ps8_di" bpmnElement="Activity_13x0ps8">
<dc:Bounds x="620" y="340" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Gateway_0tt4kuq_di" bpmnElement="Gateway_1946bhz" isMarkerVisible="true">
<dc:Bounds x="645" y="225" width="50" height="50" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0c7dfe8_di" bpmnElement="Activity_0c7dfe8">
<dc:Bounds x="810" y="210" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0fb32y5_di" bpmnElement="Event_0fb32y5">
<dc:Bounds x="1032" y="232" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="1039" y="202" width="22" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn2:definitions>
2.實作效果
會簽測試資料兩人 bajie wukong
1.兩人都同意
悟空批完
兩人都審批完後流程高亮顯示,綠色為目前登入使用者審批過的節點,灰色為審批過的節點,黃色為目前待審批節點,如下:
悟空批完流程結束。
2.兩人中任意一人不同意,走八戒審批
- 悟空登入,審批不同意,此時八戒未審批
流程圖如下
八戒批完,流程結束。
2.八戒同意,悟空不同意,走八戒審批。
八戒批完,流程結束。