From 7885f8e87af586db2b2291872ab1afacaba6c9c2 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Fri, 3 Jan 2025 10:34:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=87=AA=E5=8A=A8=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E5=92=8C=E6=89=8B=E5=8A=A8=E8=8A=82=E7=82=B9=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flowable/jpa/SpringFlowableManager.java | 5 ++-- .../flowable/jpa/entity/JpaFlowableNode.java | 12 +++++----- .../flowable/jpa/SimpleAutoAction.java | 5 ++-- .../jpa/TestSpringFlowableManager.java | 3 ++- .../core/manager/FlowableManager.java | 24 +++++++++++-------- .../flowable/core/model/FlowableHandler.java | 20 ++++++++++++++++ .../flowable/core/model/FlowableNode.java | 22 ++++++++--------- .../flowable/test/TestFlowableManager.java | 8 +++---- ...AutoAction.java => SimpleAutoHandler.java} | 8 +++++-- .../test/TestSimpleFlowableManager.java | 5 ++-- 10 files changed, 70 insertions(+), 42 deletions(-) create mode 100644 flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableHandler.java rename flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/{SimpleAutoAction.java => SimpleAutoHandler.java} (51%) diff --git a/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/SpringFlowableManager.java b/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/SpringFlowableManager.java index 79af33a..d8411ce 100644 --- a/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/SpringFlowableManager.java +++ b/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/SpringFlowableManager.java @@ -26,15 +26,14 @@ public class SpringFlowableManager extends FlowableManager { @Override protected T createBean(String classpath) { Class clazz = Class.forName(classpath); - T targetObject = null; + T targetObject; try { targetObject = (T) applicationContext.getBean(clazz); } catch (Exception springException) { - log.warn("{} not found in spring context", springException); try { targetObject = (T) clazz.newInstance(); } catch (Exception javaException) { - throw new IllegalArgumentException(javaException); + throw new IllegalArgumentException(javaException.initCause(springException)); } } return targetObject; diff --git a/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/JpaFlowableNode.java b/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/JpaFlowableNode.java index 50d7319..7c46d74 100644 --- a/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/JpaFlowableNode.java +++ b/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/JpaFlowableNode.java @@ -47,12 +47,12 @@ public class JpaFlowableNode { @Enumerated(EnumType.STRING) @Column(nullable = false) private FlowableNode.Type type; - private String automaticAction; + private String handler; @ElementCollection(fetch = javax.persistence.FetchType.EAGER) @MapKeyColumn(name = "action") @Column(name = "nodeId") - @CollectionTable(name = "flowable_node_manual_actions", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) - private Map manualActions; + @CollectionTable(name = "flowable_node_targets", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) + private Map targets; @CreatedDate private LocalDateTime createdTime; @@ -64,12 +64,12 @@ public class JpaFlowableNode { this.name = node.getName(); this.description = node.getDescription(); this.type = node.getType(); - this.automaticAction = node.getAutomaticAction(); - this.manualActions = node.getManualActions(); + this.handler = node.getHandler(); + this.targets = node.getTargets(); } public FlowableNode toFlowableNode() { - FlowableNode node = new FlowableNode(nodeId, name, description, type, automaticAction, manualActions, null, createdTime); + FlowableNode node = new FlowableNode(nodeId, name, description, type, handler, targets, null, createdTime); node.setUpdatedTime(updateTime); return node; } diff --git a/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/SimpleAutoAction.java b/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/SimpleAutoAction.java index 501d494..36a62d9 100644 --- a/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/SimpleAutoAction.java +++ b/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/SimpleAutoAction.java @@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.flowable.jpa; import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager; import com.lanyuanxiaoyao.flowable.core.model.FlowableAction; +import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler; import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance; import com.lanyuanxiaoyao.flowable.core.model.FlowableNode; import java.util.Map; @@ -13,12 +14,12 @@ import lombok.extern.slf4j.Slf4j; * @version 20250102 */ @Slf4j -public class SimpleAutoAction implements FlowableNode.AutoAction { +public class SimpleAutoAction implements FlowableHandler { @Resource private FlowableManager flowableManager; @Override - public FlowableAction action(FlowableInstance instance, FlowableNode node, Map metadata) { + public FlowableAction handle(FlowableAction action, FlowableInstance instance, FlowableNode node, Map metadata) { log.info("Initial with spring: {}", flowableManager.listNodes()); return FlowableAction.APPROVE; } diff --git a/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/TestSpringFlowableManager.java b/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/TestSpringFlowableManager.java index 5c9e272..eae35e1 100644 --- a/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/TestSpringFlowableManager.java +++ b/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/TestSpringFlowableManager.java @@ -1,6 +1,7 @@ package com.lanyuanxiaoyao.flowable.jpa; import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager; +import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler; import com.lanyuanxiaoyao.flowable.test.TestFlowableManager; import javax.annotation.Resource; import org.springframework.boot.test.context.SpringBootTest; @@ -20,7 +21,7 @@ public class TestSpringFlowableManager extends TestFlowableManager { } @Override - protected Class getAutomaticNodeClass() { + protected Class getAutomaticNodeClass() { return SimpleAutoAction.class; } } diff --git a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/manager/FlowableManager.java b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/manager/FlowableManager.java index 74414c3..29dab60 100644 --- a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/manager/FlowableManager.java +++ b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/manager/FlowableManager.java @@ -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 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 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); diff --git a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableHandler.java b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableHandler.java new file mode 100644 index 0000000..ddb5a03 --- /dev/null +++ b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableHandler.java @@ -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 metadata); + + class DefaultFlowableHandler implements FlowableHandler { + @Override + public FlowableAction handle(FlowableAction action, FlowableInstance instance, FlowableNode node, Map metadata) { + return action; + } + } +} diff --git a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableNode.java b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableNode.java index fa6dfdd..4b91992 100644 --- a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableNode.java +++ b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableNode.java @@ -19,25 +19,29 @@ public class FlowableNode { private final String description; private final Type type; - private final String automaticAction; - private final Map manualActions; + private final String handler; + private final Map targets; private final List listeners; private final LocalDateTime createdTime; private LocalDateTime updatedTime = LocalDateTime.now(); - public FlowableNode(String nodeId, String name, String description, Type type, String automaticAction, Map manualActions) { - this(nodeId, name, description, type, automaticAction, manualActions, ListHelper.empty(), LocalDateTime.now()); + public FlowableNode(String nodeId, String name, String description, Map 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 manualActions, List listeners, LocalDateTime createdTime) { + public FlowableNode(String nodeId, String name, String description, Class actionClass, Map 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 targets, List 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 metadata); - } } diff --git a/flowable-example/src/main/java/com/lanyuanxiaoyao/flowable/test/TestFlowableManager.java b/flowable-example/src/main/java/com/lanyuanxiaoyao/flowable/test/TestFlowableManager.java index 12e9389..5794d6f 100644 --- a/flowable-example/src/main/java/com/lanyuanxiaoyao/flowable/test/TestFlowableManager.java +++ b/flowable-example/src/main/java/com/lanyuanxiaoyao/flowable/test/TestFlowableManager.java @@ -3,6 +3,7 @@ package com.lanyuanxiaoyao.flowable.test; import com.lanyuanxiaoyao.flowable.core.helper.MapHelper; import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager; import com.lanyuanxiaoyao.flowable.core.model.FlowableAction; +import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler; import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance; import com.lanyuanxiaoyao.flowable.core.model.FlowableNode; import java.util.Map; @@ -34,8 +35,6 @@ public abstract class TestFlowableManager { nodeId, UUID.randomUUID().toString(), UUID.randomUUID().toString(), - FlowableNode.Type.MANUAL, - null, nextNodes ); } @@ -122,7 +121,7 @@ public abstract class TestFlowableManager { Assertions.assertThrows(IllegalArgumentException.class, () -> manager.approve(instanceId)); } - protected abstract Class getAutomaticNodeClass(); + protected abstract Class getAutomaticNodeClass(); @Test public void testAutomaticNode() { @@ -131,8 +130,7 @@ public abstract class TestFlowableManager { "2733d930-7a4b-491e-b1ca-4d5811435e9f", "自动节点", "自动节点", - FlowableNode.Type.AUTOMATIC, - getAutomaticNodeClass().getName(), + getAutomaticNodeClass(), null ); manager.create(node); diff --git a/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleAutoAction.java b/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleAutoHandler.java similarity index 51% rename from flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleAutoAction.java rename to flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleAutoHandler.java index 3835584..d282bbf 100644 --- a/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleAutoAction.java +++ b/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleAutoHandler.java @@ -1,17 +1,21 @@ package com.lanyuanxiaoyao.flowable.test; import com.lanyuanxiaoyao.flowable.core.model.FlowableAction; +import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler; import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance; import com.lanyuanxiaoyao.flowable.core.model.FlowableNode; import java.util.Map; +import lombok.extern.slf4j.Slf4j; /** * @author lanyuanxiaoyao * @version 20250102 */ -public class SimpleAutoAction implements FlowableNode.AutoAction { +@Slf4j +public class SimpleAutoHandler implements FlowableHandler { @Override - public FlowableAction action(FlowableInstance instance, FlowableNode node, Map metadata) { + public FlowableAction handle(FlowableAction action, FlowableInstance instance, FlowableNode node, Map metadata) { + log.info("Simple handler initial"); return FlowableAction.APPROVE; } } diff --git a/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/TestSimpleFlowableManager.java b/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/TestSimpleFlowableManager.java index cc5adcb..aab23f8 100644 --- a/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/TestSimpleFlowableManager.java +++ b/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/TestSimpleFlowableManager.java @@ -1,6 +1,7 @@ package com.lanyuanxiaoyao.flowable.test; import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager; +import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler; /** * @author lanyuanxiaoyao @@ -13,7 +14,7 @@ public class TestSimpleFlowableManager extends TestFlowableManager { } @Override - protected Class getAutomaticNodeClass() { - return SimpleAutoAction.class; + protected Class getAutomaticNodeClass() { + return SimpleAutoHandler.class; } }