工作流Activiti实现审批人灵活配置:
我主要采用的是使用监听器进行审批人员的添加,由于本次工程中使用了四种审批模式:
1. 组任务
2. 会签串行
3. 会签并行
4. 个人任务
其中会签串行和并行都是采用多实例方式,会签并行是相当于流程运行到这个任务,Activiti会自动创建多个实例任务,互不影响,类似物理学中电路的并行;而会签串行是流程运行到这个任务,Activiti会创建一个任务,等待这个任务完成,在创建一个新的任务,有多少审批人或者条件满足时,串行任务完成结束。
针对会签任务,其实就是多实例任务,由于在任务创建之前就需要确定任务数,与配置的审批人数相对应,我就选择在用户任务之前的线上加上监听器,将线的ID设置为下一任务节点的审批角色名称,这样就可以实现审批人灵活配置。
具体监听器代码如下:
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.pvm.PvmProcessElement;
/**
* 任务监听器
* @author MuYi
* @date 2017年11月15日下午6:51:43
*/
public class ParallelTaskListener implements ExecutionListener {
private static final long serialVersionUID = 1L;
// 获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
@Override
public void notify(DelegateExecution execution) throws Exception {
// 获取下一任务节点的角色名
ExecutionEntity s=(ExecutionEntity)execution;
PvmProcessElement ss=s.getEventSource();
String nextRoleName =ss.getId();
String[] str = execution.getProcessBusinessKey().split("\\.");
// 得到流程信息
String flowEnglishName = str[0];
String flowId = "";
String flowIdsql = " select flowId from t_flow where flowEnglishName = '" + flowEnglishName + "'";
ResultSet rst = DBHelper.select(flowIdsql);
while (rst.next()) {
flowId = rst.getString("flowId");
}
// 获得部门ID
String flowDataId = str[1];
String requestIdSql ="select requestId from "+flowEnglishName+" where id ='"+flowDataId+"'";
String requestId = "";
ResultSet rs = DBHelper.select(requestIdSql);
while (rs.next()) {
requestId = rs.getString("requestId");
}
String deptId = "";
String deptIdSql ="select departmentId from t_sys_user where id = '"+requestId+"'";
ResultSet rs1 = DBHelper.select(deptIdSql);
while (rs1.next()) {
deptId = rs1.getString("departmentId");
}
// 获取下一任务节点的人员ID
String getUserSql ="select userId from t_sys_user_role where roleId =(select id from t_sys_role where roleName = '"+nextRoleName +"'and id in(select roleId from t_flow_of_roles where flowRolesConfigId =(select flowRolesConfigId from t_flow_config where deptId = '"+deptId+"' and flowId ='"+ flowId+"')))order by sort";
List<String> assigneeList = new ArrayList<String>();
try {
ResultSet rs3 = DBHelper.select(getUserSql);
while (rs3.next()) {
assigneeList.add(rs3.getString("userId"));
}
} catch (Exception e) {
e.printStackTrace();
}
// 作为流程参数传入流程
//多实例任务所需参数
execution.setVariable("assigneeList", assigneeList);
execution.setVariable("assigneeListSize", assigneeList.size());
execution.setVariable("num", 0);
//组任务所需参数
execution.setVariable("users", assigneeList);
//个人任务参数
execution.setVariable("user", requestId);
}
}
解释几点:
- 其中DBHelper类是我写的数据库连接帮助类,可以去我的博客里面找C3P0那篇文章查看详细信息,
- assigneeList 就是并行多实例任务的审批人集合
- 幷签多实例具体bpmn图配置
- 组任务具体bpmn图配置
- 个人任务bpmn图配置
由于是初次了解activiti,有不详尽完善之处还望大家海涵…
本文介绍了如何使用工作流Activiti实现审批人灵活配置,包括组任务、会签串行、会签并行和个人任务四种模式。通过监听器在任务创建前设置审批角色,确保审批人配置的灵活性。并提供了多实例任务的配置示例和相关BPMN图。
1万+

被折叠的 条评论
为什么被折叠?



