1
0

统一自动节点和手动节点的逻辑

This commit is contained in:
2025-01-03 10:34:25 +08:00
parent 07ee530b79
commit 7885f8e87a
10 changed files with 70 additions and 42 deletions

View File

@@ -4,6 +4,7 @@ import com.lanyuanxiaoyao.flowable.core.helper.ListHelper;
import com.lanyuanxiaoyao.flowable.core.helper.MapHelper;
import com.lanyuanxiaoyao.flowable.core.helper.StringHelper;
import com.lanyuanxiaoyao.flowable.core.model.FlowableAction;
import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler;
import com.lanyuanxiaoyao.flowable.core.model.FlowableHistory;
import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance;
import com.lanyuanxiaoyao.flowable.core.model.FlowableListener;
@@ -104,12 +105,7 @@ public abstract class FlowableManager {
}
private void autoAction(FlowableInstance instance, FlowableNode node, Map<String, Object> metadata) {
String actionClass = node.getAutomaticAction();
if (StringHelper.isBlank(actionClass)) {
throw new IllegalArgumentException("自动节点执行器为空");
}
FlowableNode.AutoAction autoAction = createBean(actionClass);
action(instance, node, autoAction.action(instance, node, metadata), "系统自动执行", metadata);
action(instance, node, null, "系统自动执行", metadata);
}
private void action(String instanceId, FlowableAction action, String comment, Map<String, Object> metadata) {
@@ -122,17 +118,25 @@ public abstract class FlowableManager {
if (FlowableInstance.Status.COMPLETED.equals(instance.getStatus()) || FlowableInstance.Status.ERROR.equals(instance.getStatus())) {
throw new IllegalArgumentException("ID为" + instance.getInstanceId() + "的流程已结束,无法操作");
}
String handlerClass = node.getHandler();
if (StringHelper.isBlank(handlerClass)) {
throw new IllegalArgumentException("节点执行器为空");
}
FlowableHandler handler = createBean(handlerClass);
action = handler.handle(action, instance, node, metadata);
if (FlowableAction.TERMINAL.equals(action)) {
saveInstance(instance, FlowableInstance.Status.ERROR, metadata, action, comment);
return;
}
if (Objects.isNull(node.getManualActions())
|| !node.getManualActions().containsKey(action)
|| StringHelper.isBlank(node.getManualActions().get(action))) {
if (Objects.isNull(node.getTargets())
|| !node.getTargets().containsKey(action)
|| StringHelper.isBlank(node.getTargets().get(action))) {
saveInstance(instance, FlowableInstance.Status.COMPLETED, metadata, action, comment);
return;
}
String nextNodeId = node.getManualActions().get(action);
String nextNodeId = node.getTargets().get(action);
FlowableNode nextNode = flowableRepository.getNode(nextNodeId);
instance.setCurrentNodeId(nextNode.getNodeId());
saveInstance(instance, FlowableInstance.Status.RUNNING, metadata, action, comment);

View File

@@ -0,0 +1,20 @@
package com.lanyuanxiaoyao.flowable.core.model;
import java.util.Map;
/**
* 处理器
*
* @author lanyuanxiaoyao
* @version 20250103
*/
public interface FlowableHandler {
FlowableAction handle(FlowableAction action, FlowableInstance instance, FlowableNode node, Map<String, Object> metadata);
class DefaultFlowableHandler implements FlowableHandler {
@Override
public FlowableAction handle(FlowableAction action, FlowableInstance instance, FlowableNode node, Map<String, Object> metadata) {
return action;
}
}
}

View File

@@ -19,25 +19,29 @@ public class FlowableNode {
private final String description;
private final Type type;
private final String automaticAction;
private final Map<FlowableAction, String> manualActions;
private final String handler;
private final Map<FlowableAction, String> targets;
private final List<String> listeners;
private final LocalDateTime createdTime;
private LocalDateTime updatedTime = LocalDateTime.now();
public FlowableNode(String nodeId, String name, String description, Type type, String automaticAction, Map<FlowableAction, String> manualActions) {
this(nodeId, name, description, type, automaticAction, manualActions, ListHelper.empty(), LocalDateTime.now());
public FlowableNode(String nodeId, String name, String description, Map<FlowableAction, String> targets) {
this(nodeId, name, description, Type.MANUAL, FlowableHandler.DefaultFlowableHandler.class.getName(), targets, ListHelper.empty(), LocalDateTime.now());
}
public FlowableNode(String nodeId, String name, String description, Type type, String automaticAction, Map<FlowableAction, String> manualActions, List<String> listeners, LocalDateTime createdTime) {
public FlowableNode(String nodeId, String name, String description, Class<? extends FlowableHandler> actionClass, Map<FlowableAction, String> targets) {
this(nodeId, name, description, Type.AUTOMATIC, actionClass.getName(), targets, ListHelper.empty(), LocalDateTime.now());
}
public FlowableNode(String nodeId, String name, String description, Type type, String handler, Map<FlowableAction, String> targets, List<String> listeners, LocalDateTime createdTime) {
this.nodeId = nodeId;
this.name = name;
this.description = description;
this.type = type;
this.automaticAction = automaticAction;
this.manualActions = manualActions;
this.handler = handler;
this.targets = targets;
this.listeners = listeners;
this.createdTime = createdTime;
}
@@ -46,8 +50,4 @@ public class FlowableNode {
AUTOMATIC,
MANUAL,
}
public interface AutoAction {
FlowableAction action(FlowableInstance instance, FlowableNode node, Map<String, Object> metadata);
}
}