Skip to content

Commit

Permalink
支持执行任务中动态指定后续节点处理人
Browse files Browse the repository at this point in the history
  • Loading branch information
qmdx committed Sep 1, 2024
1 parent f00d9f5 commit 159e0c8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ default void timeout(Long instanceId) {
*/
void updateInstance(FlwInstance flwInstance);

/**
* 根据 流程实例ID 更新流程实例模型内容
*
* @param id 流程实例ID
* @param processModel 流程模型
* @return true 成功 false 失败
*/
boolean updateInstanceModelById(Long id, ProcessModel processModel);

/**
* 级联删除指定流程实例的所有数据
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.aizuda.bpm.engine.entity.FlwInstance;
import com.aizuda.bpm.engine.entity.FlwTask;
import com.aizuda.bpm.engine.entity.FlwTaskActor;
import com.aizuda.bpm.engine.model.ModelHelper;
import com.aizuda.bpm.engine.model.NodeModel;
import com.aizuda.bpm.engine.model.ProcessModel;
import lombok.Getter;
Expand Down Expand Up @@ -143,6 +144,19 @@ public Execution createSubExecution(Execution execution, ProcessModel processMod
public boolean executeNodeModel(FlowLongContext flowLongContext, String nodeKey) {
ProcessModel processModel = this.getProcessModel();
Assert.isNull(processModel, "Process model content cannot be empty");

// 重新加载流程模型内容
ModelHelper.reloadProcessModel(processModel, t -> {

// 更新流程模型
boolean ok = flowLongContext.getRuntimeService().updateInstanceModelById(flwInstance.getId(), t);
Assert.isFalse(ok, "Failed to update process model content");

// 重新构建父节点
t.buildParentNode(processModel.getNodeConfig());
});

// 获取节点模型
NodeModel nodeModel = processModel.getNode(nodeKey);
Assert.isNull(nodeModel, "Not found in the process model, process nodeKey=" + nodeKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*/
package com.aizuda.bpm.engine.impl;

import com.aizuda.bpm.engine.*;
import com.aizuda.bpm.engine.FlowConstants;
import com.aizuda.bpm.engine.QueryService;
import com.aizuda.bpm.engine.RuntimeService;
import com.aizuda.bpm.engine.TaskService;
import com.aizuda.bpm.engine.assist.Assert;
import com.aizuda.bpm.engine.assist.DateUtils;
import com.aizuda.bpm.engine.assist.ObjectUtils;
Expand All @@ -18,7 +21,7 @@
import com.aizuda.bpm.engine.entity.*;
import com.aizuda.bpm.engine.listener.InstanceListener;
import com.aizuda.bpm.engine.model.ConditionNode;
import com.aizuda.bpm.engine.model.DynamicAssignee;
import com.aizuda.bpm.engine.model.ModelHelper;
import com.aizuda.bpm.engine.model.NodeModel;
import com.aizuda.bpm.engine.model.ProcessModel;

Expand Down Expand Up @@ -77,26 +80,8 @@ public FlwInstance createInstance(FlwProcess flwProcess, FlowCreator flowCreator
flwInstance.setProcessId(flwProcess.getId());
flwInstance.setMapVariable(args);

/*
* 处理追加模型逻辑
*/
Map<String, Object> modelData = FlowDataTransfer.get(FlowConstants.processDynamicAssignee);
if (ObjectUtils.isNotEmpty(modelData)) {
ProcessModel processModel = flwProcess.model();
modelData.forEach((key, value) -> {
if (value instanceof DynamicAssignee) {
NodeModel thisNodeModel = processModel.getNode(key);
if (null != thisNodeModel) {
DynamicAssignee dynamicAssignee = (DynamicAssignee) value;
thisNodeModel.setNodeAssigneeList(dynamicAssignee.getAssigneeList());
}
}
});
// 清理父节点
processModel.cleanParentNode(processModel.getNodeConfig());
// 更新模型
flwProcess.setModelContent2Json(processModel);
}
// 重新加载流程模型
ModelHelper.reloadProcessModel(flwProcess.model(), flwProcess::setModelContent2Json);

// 保存实例
this.saveInstance(flwInstance, flwProcess, flowCreator);
Expand Down Expand Up @@ -291,6 +276,18 @@ public void updateInstance(FlwInstance flwInstance) {
instanceDao.updateById(flwInstance);
}

@Override
public boolean updateInstanceModelById(Long id, ProcessModel processModel) {
// 使缓存失效
FlowLongContext.invalidateProcessModel(FlowConstants.processInstanceCacheKey + id);

// 更新流程实例模型
FlwExtInstance extInstance = new FlwExtInstance();
extInstance.setId(id);
extInstance.setModelContent(FlowLongContext.toJson(processModel));
return extInstanceDao.updateById(extInstance);
}

/**
* 级联删除指定流程实例的所有数据
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
*/
package com.aizuda.bpm.engine.model;

import com.aizuda.bpm.engine.FlowConstants;
import com.aizuda.bpm.engine.FlowDataTransfer;
import com.aizuda.bpm.engine.assist.ObjectUtils;
import com.aizuda.bpm.engine.core.enums.NodeSetType;
import com.aizuda.bpm.engine.core.enums.TaskType;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -335,4 +338,32 @@ public static NodeModel getNodeModel(String nodeKey, NodeModel rootNodeModel) {
.orElse(null);
}

/**
* 重新加载流程模型
*
* @param processModel 流程模型
* @param consumer 流程模型消费
*/
public static void reloadProcessModel(ProcessModel processModel, Consumer<ProcessModel> consumer) {
Map<String, Object> modelData = FlowDataTransfer.get(FlowConstants.processDynamicAssignee);
if (ObjectUtils.isNotEmpty(modelData)) {

// 追加动态分配处理人员
modelData.forEach((key, value) -> {
if (value instanceof DynamicAssignee) {
NodeModel thisNodeModel = processModel.getNode(key);
if (null != thisNodeModel) {
DynamicAssignee dynamicAssignee = (DynamicAssignee) value;
thisNodeModel.setNodeAssigneeList(dynamicAssignee.getAssigneeList());
}
}
});

// 清理父节点
processModel.cleanParentNode(processModel.getNodeConfig());

// 更新模型
consumer.accept(processModel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* 测试抄送节点跟条件分支
Expand Down Expand Up @@ -53,4 +50,22 @@ public void test() {

});
}

@Test
public void test2() {
// 执行认领审批分支
Map<String, Object> args = new HashMap<>();
args.put("day", 8);
flowLongEngine.startInstanceById(processId, testCreator, args).ifPresent(instance -> {

// 认领审批并动态执行下一个审批节点处理人员为 test003
FlowDataTransfer.dynamicAssignee(new HashMap<String, Object>() {{
put("k008", DynamicAssignee.assigneeUserList(Collections.singletonList(NodeAssignee.ofFlowCreator(test3Creator))));
}});
executeActiveTasks(instance.getId(), test3Creator);

// 动态处理人员 test003 办理流程结束
executeActiveTasks(instance.getId(), test3Creator);
});
}
}

0 comments on commit 159e0c8

Please sign in to comment.