如下图所示,在提单页面,发起人需要手动选择走部门内分组审核还是部门负责人审核,对于这种由发起人选择走不同分支的且分支条件不是来自业务系统的情形,需要在流程模型上设置变量并赋初始化值,下面介绍如何设置及使用。
Flow_1w8hx7u
Flow_06hawzz
Flow_1us3p2e
Flow_1us3p2e
Flow_0dm15h8
Flow_1d5313z
${para_var.indexOf("1") > -1}
Flow_0ydcrpn
Flow_1d5313z
Flow_1y299hk
${para_var.indexOf("0") > -1}
Flow_0dm15h8
Flow_0ydcrpn
Flow_06cclke
Flow_097rgey
Flow_097rgey
Flow_1y299hk
Flow_06cclke
0,1
Flow_1w8hx7u
Flow_06hawzz
Camunda Modeler没有在流程属性上设置全局变量功能,我们这里借助一个Task初始化节点来实现,在初始化节点处设置输出变量para_var,默认值为“0,1”,在流程模型的变量信息处能看到所有输出变量信息。
注:只有输出变量的作用域是全局的,输入变量的作用域是只对当前节点的
包容网关分支条件设置,para_var包含1,则走部门内分组审核,包含0则走部门负责人审核。
创建流程并检查返回变量
@Test
public void createProcess() {
try {
String processDefKey = "Process_Demo11";
String userId = "bx1";
AuthenticationService authenticationService = new AuthenticationService();
// 获取当前流程引擎的名称
String engineName = processEngine.getName();
// 用户无密码登录
UserAuthentication authentication = (UserAuthentication) authenticationService
.createAuthenticate(engineName, userId, null, null);
//设置当前用户为操作人
identityService.setAuthenticatedUserId(authentication.getName());
// 创建流程
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefKey);
//流程创建成功获取变量信息
if (processInstance != null && StringUtils.isNotBlank(processInstance.getId())) {
Map vars = runtimeService.getVariables(processInstance.getId());
logger.info(JSON.toJSONString(vars));
} else {
throw new Exception("创建流程实例失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
查看打印日志,有该流程实例的流程全局变量返回
2021-09-03 21:50:45,885 [main] INFO org.camunda.bpm.DemoApplicationTest:682 - {"starter":"bx1","para_var":"0,1"}
在Cockpit里也能看到,流程发起后初始化节点自动执行了,并且为该流程实例设置了变量para_var
然后通过获取下一节点方法,获取到起草节点后的节点。
[
{
"assignment":"",
"name":"部门内分组审核",
"key":"Activity_1h2epaw"
},
{
"assignment":"",
"name":"部门负责人审核",
"key":"Activity_09moh95"
}
]
起草人在选定好对应的节点和节点处理人后提交到对应分支执行,下面示例将para_var=1来表示实现流转到“部门内分组审核”节点
思考:是否有其他方法实现呢?
@Test
public void submitTask3() {
try {
String userId = "bx1";
String taskId = "a20e9285-0cc4-11ec-b197-00ff0269ba71";
String processInstId = "a1f64f8b-0cc4-11ec-b197-00ff0269ba71";
String nextUserId = "bx2";
AuthenticationService authenticationService = new AuthenticationService();
String engineName = processEngine.getName();
UserAuthentication authentication = (UserAuthentication) authenticationService
.createAuthenticate(engineName, userId, null, null);
logger.info("authentication--------->" + authentication.getName());
identityService.setAuthenticatedUserId(authentication.getName());
List taskList = new ArrayList();
//目标节点的分支变量为1
runtimeService.setVariable(processInstId, "para_var", "1");
//完成任务
taskService.complete(taskId);
taskList = simpleGetTasks(processInstId);
System.out.println(JSON.toJSONString(taskList));
if (taskList != null && taskList.size() == 1) {
//设置目标节点处理人
taskService.setAssignee(taskList.get(0).getId(), nextUserId);
}
taskList = simpleGetTasks(processInstId);
System.out.println(JSON.toJSONString(taskList));
} catch (Exception e) {
e.printStackTrace();
}
}
提交后,流程流转到“部门内分组审核”,对应审批人为"bx2",流程变量para_var为“1”。
任务操作记录能看到是有bx1发给bx2的。
若将para_var修改为0,处理人为“bx3”,查看Cockpit里边,发现流程流转符合预期,流转到“部门负责人审核”节点,任务是由bx1发给bx3的。
@Test
public void submitTask3() {
try {
String userId = "bx1";
String taskId = "7b1fc48b-0cc6-11ec-bd79-00ff0269ba71";
String processInstId = "7afea7f1-0cc6-11ec-bd79-00ff0269ba71";
String nextUserId = "bx3";
AuthenticationService authenticationService = new AuthenticationService();
String engineName = processEngine.getName();
UserAuthentication authentication = (UserAuthentication) authenticationService
.createAuthenticate(engineName, userId, null, null);
logger.info("authentication--------->" + authentication.getName());
identityService.setAuthenticatedUserId(authentication.getName());
List taskList = new ArrayList();
//目标节点的分支变量为1
runtimeService.setVariable(processInstId, "para_var", "0");
//完成任务
taskService.complete(taskId);
taskList = simpleGetTasks(processInstId);
System.out.println(JSON.toJSONString(taskList));
if (taskList != null && taskList.size() == 1) {
//设置目标节点处理人
taskService.setAssignee(taskList.get(0).getId(), nextUserId);
}
taskList = simpleGetTasks(processInstId);
System.out.println(JSON.toJSONString(taskList));
} catch (Exception e) {
e.printStackTrace();
}
}