From 743e2db17d198cf9e6d3f9af8ad388eeaeac9b66 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Fri, 3 Jan 2025 16:10:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=A0=B8=E5=BF=83=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E5=88=9B=E5=BB=BA=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flowable/jpa/entity/FlowableInstance.java | 16 +++---- .../flowable/jpa/entity/FlowableNode.java | 13 ++++-- .../flowable/core/helper/MapHelper.java | 2 +- .../core/manager/FlowableManager.java | 7 ++- .../flowable/core/model/FlowableHistory.java | 12 ++--- .../flowable/core/model/FlowableInstance.java | 33 +++++--------- .../flowable/core/model/FlowableNode.java | 44 +++++++------------ .../flowable/test/TestFlowableManager.java | 40 ++++++++--------- 8 files changed, 76 insertions(+), 91 deletions(-) diff --git a/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/FlowableInstance.java b/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/FlowableInstance.java index cfd6dc5..fea6f4e 100644 --- a/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/FlowableInstance.java +++ b/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/FlowableInstance.java @@ -79,13 +79,13 @@ public class FlowableInstance { @SneakyThrows public com.lanyuanxiaoyao.flowable.core.model.FlowableInstance toFlowableInstance() { - return new com.lanyuanxiaoyao.flowable.core.model.FlowableInstance( - instanceId, - currentNodeId, - (FlowableMetadata) bytesToObject(this.metadata), - status, - createdTime, - updatedTime - ); + return com.lanyuanxiaoyao.flowable.core.model.FlowableInstance.builder() + .instanceId(instanceId) + .currentNodeId(currentNodeId) + .metadata((FlowableMetadata) bytesToObject(this.metadata)) + .status(status) + .createdTime(createdTime) + .updatedTime(updatedTime) + .build(); } } diff --git a/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/FlowableNode.java b/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/FlowableNode.java index 099dcce..c17063b 100644 --- a/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/FlowableNode.java +++ b/adapter/flowable-spring-boot-jpa-starter/src/main/java/com/lanyuanxiaoyao/flowable/jpa/entity/FlowableNode.java @@ -66,8 +66,15 @@ public class FlowableNode { } public com.lanyuanxiaoyao.flowable.core.model.FlowableNode toFlowableNode() { - com.lanyuanxiaoyao.flowable.core.model.FlowableNode node = new com.lanyuanxiaoyao.flowable.core.model.FlowableNode(nodeId, name, description, type, handler, targets, null, createdTime); - node.setUpdatedTime(updateTime); - return node; + return com.lanyuanxiaoyao.flowable.core.model.FlowableNode.builder() + .nodeId(nodeId) + .name(name) + .description(description) + .type(type) + .handler(handler) + .targets(targets) + .createdTime(createdTime) + .updatedTime(updateTime) + .build(); } } diff --git a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/helper/MapHelper.java b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/helper/MapHelper.java index d30e701..6950392 100644 --- a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/helper/MapHelper.java +++ b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/helper/MapHelper.java @@ -25,7 +25,7 @@ public class MapHelper { return map; } - public static Map empty() { + public static Map empty() { return new HashMap<>(0); } 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 63e5667..51da0fc 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 @@ -3,6 +3,7 @@ package com.lanyuanxiaoyao.flowable.core.manager; 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.FlowableAccessor; import com.lanyuanxiaoyao.flowable.core.model.FlowableAction; import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler; import com.lanyuanxiaoyao.flowable.core.model.FlowableHistory; @@ -68,7 +69,11 @@ public abstract class FlowableManager { public String start(String nodeId, Map metadata) { FlowableNode node = repository.getNode(nodeId); - FlowableInstance instance = new FlowableInstance(createId(), node.getNodeId(), new FlowableMetadata(metadata)); + FlowableInstance instance = FlowableInstance.builder() + .instanceId(createId()) + .currentNodeId(node.getNodeId()) + .metadata(new FlowableMetadata(metadata)) + .build(); repository.saveInstance(instance); if (FlowableNode.Type.AUTOMATIC.equals(node.getType())) { diff --git a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableHistory.java b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableHistory.java index 16d6014..d7bb789 100644 --- a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableHistory.java +++ b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableHistory.java @@ -1,6 +1,8 @@ package com.lanyuanxiaoyao.flowable.core.model; import java.time.LocalDateTime; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; /** @@ -8,6 +10,8 @@ import lombok.Data; * @version 20241231 */ @Data +@AllArgsConstructor +@Builder public class FlowableHistory { private final String historyId; private final String instanceId; @@ -18,12 +22,4 @@ public class FlowableHistory { public FlowableHistory(String historyId, String instanceId, FlowableAction action, String comment) { this(historyId, instanceId, action, comment, LocalDateTime.now()); } - - public FlowableHistory(String historyId, String instanceId, FlowableAction action, String comment, LocalDateTime createdTime) { - this.historyId = historyId; - this.instanceId = instanceId; - this.action = action; - this.comment = comment; - this.createdTime = createdTime; - } } diff --git a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableInstance.java b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableInstance.java index 09a6bcb..aa65ae6 100644 --- a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableInstance.java +++ b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableInstance.java @@ -2,6 +2,8 @@ package com.lanyuanxiaoyao.flowable.core.model; import java.time.LocalDateTime; import java.util.Map; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; /** @@ -9,31 +11,20 @@ import lombok.Data; * @version 20241231 */ @Data +@AllArgsConstructor +@Builder public class FlowableInstance { private final String instanceId; - private final FlowableMetadata metadata; - private final LocalDateTime createdTime; + @Builder.Default + private final FlowableMetadata metadata = new FlowableMetadata(); + @Builder.Default + private final LocalDateTime createdTime = LocalDateTime.now(); private String currentNodeId; - private Status status; - private LocalDateTime updatedTime; - - public FlowableInstance(String instanceId, String currentNodeId) { - this(instanceId, currentNodeId, new FlowableMetadata(), Status.RUNNING, LocalDateTime.now(), LocalDateTime.now()); - } - - public FlowableInstance(String instanceId, String currentNodeId, FlowableMetadata metadata) { - this(instanceId, currentNodeId, metadata, Status.RUNNING, LocalDateTime.now(), LocalDateTime.now()); - } - - public FlowableInstance(String instanceId, String currentNodeId, FlowableMetadata metadata, Status status, LocalDateTime createdTime, LocalDateTime updatedTime) { - this.instanceId = instanceId; - this.metadata = metadata; - this.createdTime = createdTime; - this.currentNodeId = currentNodeId; - this.status = status; - this.updatedTime = updatedTime; - } + @Builder.Default + private Status status = Status.RUNNING; + @Builder.Default + private LocalDateTime updatedTime = LocalDateTime.now(); public void addMetadata(Map metadata) { this.metadata.putAll(metadata); 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 e0cf567..24439da 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 @@ -1,9 +1,11 @@ package com.lanyuanxiaoyao.flowable.core.model; -import com.lanyuanxiaoyao.flowable.core.helper.ListHelper; +import com.lanyuanxiaoyao.flowable.core.helper.MapHelper; import java.time.LocalDateTime; import java.util.List; import java.util.Map; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; /** @@ -13,43 +15,29 @@ import lombok.Data; * @version 20241231 */ @Data +@AllArgsConstructor +@Builder public class FlowableNode { private final String nodeId; private final String name; private final String description; - private final Type type; - private final String handler; - private final Map targets; + @Builder.Default + private final Type type = Type.MANUAL; + @Builder.Default + private final String handler = FlowableHandler.DefaultFlowableHandler.class.getName(); + @Builder.Default + private final Map targets = MapHelper.empty(); + @Builder.Default + private final String accessor = FlowableAccessor.DefaultFlowableAccessor.class.getName(); private final List listeners; - private final LocalDateTime createdTime; + @Builder.Default + private final LocalDateTime createdTime = LocalDateTime.now(); + @Builder.Default private LocalDateTime updatedTime = LocalDateTime.now(); - public FlowableNode(String nodeId, String name, String description, Map targets) { - this(nodeId, name, description, Type.MANUAL, FlowableHandler.DefaultFlowableHandler.class, targets); - } - - public FlowableNode(String nodeId, String name, String description, Class actionClass, Map targets) { - this(nodeId, name, description, Type.AUTOMATIC, actionClass, targets); - } - - public FlowableNode(String nodeId, String name, String description, Type type, Class actionClass, Map targets) { - this(nodeId, name, description, type, 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.handler = handler; - this.targets = targets; - this.listeners = listeners; - this.createdTime = createdTime; - } - public enum Type { AUTOMATIC, MANUAL, 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 0864973..832b7ef 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 @@ -32,12 +32,12 @@ public abstract class TestFlowableManager { } private FlowableNode createManualNode(String nodeId, Map nextNodes) { - return new FlowableNode( - nodeId, - UUID.randomUUID().toString(), - UUID.randomUUID().toString(), - nextNodes - ); + return FlowableNode.builder() + .nodeId(nodeId) + .name(UUID.randomUUID().toString()) + .description(UUID.randomUUID().toString()) + .targets(nextNodes) + .build(); } /** @@ -125,14 +125,12 @@ public abstract class TestFlowableManager { @Test public void testSuspend() { FlowableManager manager = flowableManager(); - FlowableNode node = new FlowableNode( - "edf1b7b0-f45b-4256-b696-ce84857f03f7", - "edf1b7b0-f45b-4256-b696-ce84857f03f7", - "edf1b7b0-f45b-4256-b696-ce84857f03f7", - FlowableNode.Type.MANUAL, - TwoApproveHandler.class, - null - ); + FlowableNode node = FlowableNode.builder() + .nodeId("50342cb7-2029-4159-bd4c-cbfa4aebe474") + .name("50342cb7-2029-4159-bd4c-cbfa4aebe474") + .description("50342cb7-2029-4159-bd4c-cbfa4aebe474") + .handler(TwoApproveHandler.class.getName()) + .build(); manager.create(node); String instanceId = manager.start(node.getNodeId()); @@ -148,13 +146,13 @@ public abstract class TestFlowableManager { @Test public void testAutomaticNode() { FlowableManager manager = flowableManager(); - FlowableNode node = new FlowableNode( - "2733d930-7a4b-491e-b1ca-4d5811435e9f", - "自动节点", - "自动节点", - getAutomaticNodeClass(), - null - ); + FlowableNode node = FlowableNode.builder() + .nodeId("5d60dab0-7691-4543-b753-af7ac02cb7ec") + .name("5d60dab0-7691-4543-b753-af7ac02cb7ec") + .description("5d60dab0-7691-4543-b753-af7ac02cb7ec") + .type(FlowableNode.Type.AUTOMATIC) + .handler(getAutomaticNodeClass().getName()) + .build(); manager.create(node); String instanceId = manager.start(node.getNodeId()); Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus());