前言
1、从缓存里获取流程,这样修改流程,不会破坏原来的流程定义;
2、新增流程节点,不是在原来流程上,而是以新增节点为开始节点,重新构建一个流程。
考虑一、用到缓存,那就得考虑缓存是否持久化,系统重启后是否还存在?
activity对应的缓存流程,如果你更改了缓存,项目启动后,是不会保留更改后的缓存,所以持久化需要我们另外处理。
新增一个表,用于记录最新的流程连线(每次新增节点,都催生新的连线,保留最新连线即可),在程序重启时,加载最新流程并刷新缓存。
考虑二、新增节点会按最新的流程连线,那么新增节点该什么时候添加呢?
新节点连线到原流程节点(目标节点)时,当该节点处于代办状态时,转换为按新增节点为代办任务,把目标节点删掉,包括历史节点信息
一、添加新节点
1、从缓存中获取流程process
2、构建新的流程节点,可满足同时添加多个节点
3、新增节点最后一个节点,连线到原流程目标节点
4、更新缓存process
5、删掉原流程的代办任务,把新增节点作为代办任务(采用任意节点跳过功能)
6、记录新增节点信息,当流程重新启动时,加载刷新缓存
public class AddNodeService {
public void addUserTask(AddUserTaskDto dto) {
if (CollectionUtils.isEmpty(dto.getTaskModelList())) {
log.error("加签节点没有提供增加节点");
return;
}
ManagementService managementService = dto.getProcessEngine().getManagementService();
managementService.executeCommand(new GetProcessCmd(dto.getProcessDefinitionId()));
//确保这里一定可以从缓存中拿到流程定义相关信息
ProcessDefinitionCacheEntry processDefinitionCacheEntry = managementService.executeCommand(new GetProcessDefinitionCacheEntryCmd(dto.getProcessDefinitionId()));
Process process = processDefinitionCacheEntry.getProcess();
List<UserTask> userTasks = new ArrayList<>(dto.getTaskModelList().size());
// 批量生成任务,将用户自定义的相关流程信息转为activity的userTask TODO 如果添加条件,当条件不满足时,目前只能跳过节点,不能直接结束
for (ProcessTask taskModel : dto.getTaskModelList()) {
UserTask userTask = GenerateActivityUtils.transformation(taskModel);
if (Objects.isNull(userTask)) continue;
userTasks.add(userTask);
process.addFlowElement(userTask);
}
for (int i = 0; i < userTasks.size(); i++) {
UserTask userTask = userTasks.get(i);
SequenceFlow sequenceFlow;
if (i == userTasks.size() - 1) {
//是最后一个任务节点
String lastNodeId = dto.getTargetNodeId();
sequenceFlow = GenerateActivityUtils.createSequenceFlow(userTask.getId() + "---->>>>" + lastNodeId, userTask.getId() + "---->>>>" + lastNodeId,
userTask.getId(), lastNodeId, userTask.getSkipExpression());
sequenceFlow.setTargetFlowElement(process.getFlowElement(lastNodeId));
userTask.setOutgoingFlows(Lists.newArrayList(sequenceFlow));
} else {
//不是最后一个节点
sequenceFlow = GenerateActivityUtils.createSequenceFlow(userTask.getId() + "--->>>" + userTasks.get(i + 1).getId(), userTask.getId() + "--->>>" + userTasks.get(i + 1).getId(), userTask.getId(),
userTasks.get(i + 1).getId(), userTask.getSkipExpression());
sequenceFlow.setTargetFlowElement(userTasks.get(i + 1));
userTask.setOutgoingFlows(Lists.newArrayList(sequenceFlow));
}
process.addFlowElement(sequenceFlow);
}
//更新缓存,确保缓存中存储的是最新的流程数据
processDefinitionCacheEntry.setProcess(process);
if (dto.isFire()) {
//新增节点时,加签第一个节点ID,即将为待办节点
String addTodoNodeId = dto.getTaskModelList().get(0).getTaskId();
managementService.executeCommand(new JumpNodeCmd(dto.getTaskId(), addTodoNodeId, dto.getVars()));
}
if (dto.isPersistenceDataToDataBase()) {
new AddNodeDao().persistenceDataToDataBase(dto.getProcessDefinitionId(), dto.getProcessInstanceId(), dto.getTargetNodeId(), dto.getTaskModelList(), dto.getProcessEngine());
}
}
}
``
获取缓存类GetProcessDefinitionCacheEntryCmd:
```java
public class GetProcessDefinitionCacheEntryCmd implements Command<ProcessDefinitionCacheEntry> {
String processDefinitionId;
public GetProcessDefinitionCacheEntryCmd(String processDefinitionId) {
this.processDefinitionId = processDefinitionId;
}
@Override
public ProcessDefinitionCacheEntry execute(CommandContext commandContext) {
DeploymentManager deploymentManager = commandContext.getProcessEngineConfiguration().getDeploymentManager();
ProcessDefinitionCacheEntry processDefinitionCacheEntry = deploymentManager.getProcessDefinitionCache().get(processDefinitionId);
return processDefinitionCacheEntry;
}
}
获取流程实例:
public class GetProcessCmd implements Command<Process> {
private String processDefinitionId;
public GetProcessCmd(String processDefinitionId) {
this.processDefinitionId = processDefinitionId;
}
@Override
public Process execute(CommandContext commandContext) {
BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processDefinitionId);
if (CollectionUtils.isEmpty(bpmnModel.getProcesses())) {
log.error("未找不到任何流程");
return new Process();
}
return bpmnModel.getProcesses().get(0);
}
}
把新增节点持久化到数据库
详细请看《activity自定义增删查改》
如果你对工作流感兴趣,想了解更多,请点击
Java工作流管理系统(activity6.0)