会签实现多个人同时审批,任意一个人不同意时,会签任务结束,不同意走八戒审批,同意走悟空审批,最后流程结束。流程图如下:
绘制流程图:动态设置审批人,完成条件${(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 list = Arrays.asList("bajie", "wukong");
Map 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 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 getBean(String name) throws BeansException
{
return (T) beanFactory.getBean(name);
}
/**
* 获取类型为requiredType的对象
*
* @param clz
* @return
* @throws org.springframework.beans.BeansException
*
*/
public static T getBean(Class 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 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
Flow_0c7d7xo
Flow_0c7d7xo
Flow_1p2tz1w
${num}
${(pass == 'no')||(nrOfCompletedInstances/nrOfInstances==1)}
Flow_1mknpc1
Flow_0f15fkh
${result== 'N'}
Flow_1p2tz1w
Flow_1mknpc1
Flow_0bp4fi6
Flow_0bp4fi6
Flow_006s5b1
${result== 'Y'}
Flow_006s5b1
Flow_0f15fkh
会签测试数据两人 bajie wukong
悟空批完
两人都审批完后流程高亮显示,绿色为当前登录用户审批过的节点,灰色为审批过的节点,黄色为当前待审批节点,如下:
悟空批完流程结束。
流程图如下
八戒批完,流程结束。
2.八戒同意,悟空不同意,走八戒审批。
八戒批完,流程结束。