1
0

调整核心类的创建方式

This commit is contained in:
2025-01-03 16:10:37 +08:00
parent 45ce5a2615
commit 743e2db17d
8 changed files with 76 additions and 91 deletions

View File

@@ -79,13 +79,13 @@ public class FlowableInstance {
@SneakyThrows @SneakyThrows
public com.lanyuanxiaoyao.flowable.core.model.FlowableInstance toFlowableInstance() { public com.lanyuanxiaoyao.flowable.core.model.FlowableInstance toFlowableInstance() {
return new com.lanyuanxiaoyao.flowable.core.model.FlowableInstance( return com.lanyuanxiaoyao.flowable.core.model.FlowableInstance.builder()
instanceId, .instanceId(instanceId)
currentNodeId, .currentNodeId(currentNodeId)
(FlowableMetadata) bytesToObject(this.metadata), .metadata((FlowableMetadata) bytesToObject(this.metadata))
status, .status(status)
createdTime, .createdTime(createdTime)
updatedTime .updatedTime(updatedTime)
); .build();
} }
} }

View File

@@ -66,8 +66,15 @@ public class FlowableNode {
} }
public com.lanyuanxiaoyao.flowable.core.model.FlowableNode toFlowableNode() { 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); return com.lanyuanxiaoyao.flowable.core.model.FlowableNode.builder()
node.setUpdatedTime(updateTime); .nodeId(nodeId)
return node; .name(name)
.description(description)
.type(type)
.handler(handler)
.targets(targets)
.createdTime(createdTime)
.updatedTime(updateTime)
.build();
} }
} }

View File

@@ -25,7 +25,7 @@ public class MapHelper {
return map; return map;
} }
public static Map<String, Object> empty() { public static <K, V> Map<K, V> empty() {
return new HashMap<>(0); return new HashMap<>(0);
} }

View File

@@ -3,6 +3,7 @@ package com.lanyuanxiaoyao.flowable.core.manager;
import com.lanyuanxiaoyao.flowable.core.helper.ListHelper; import com.lanyuanxiaoyao.flowable.core.helper.ListHelper;
import com.lanyuanxiaoyao.flowable.core.helper.MapHelper; import com.lanyuanxiaoyao.flowable.core.helper.MapHelper;
import com.lanyuanxiaoyao.flowable.core.helper.StringHelper; 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.FlowableAction;
import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler; import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler;
import com.lanyuanxiaoyao.flowable.core.model.FlowableHistory; import com.lanyuanxiaoyao.flowable.core.model.FlowableHistory;
@@ -68,7 +69,11 @@ public abstract class FlowableManager {
public String start(String nodeId, Map<String, Object> metadata) { public String start(String nodeId, Map<String, Object> metadata) {
FlowableNode node = repository.getNode(nodeId); 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); repository.saveInstance(instance);
if (FlowableNode.Type.AUTOMATIC.equals(node.getType())) { if (FlowableNode.Type.AUTOMATIC.equals(node.getType())) {

View File

@@ -1,6 +1,8 @@
package com.lanyuanxiaoyao.flowable.core.model; package com.lanyuanxiaoyao.flowable.core.model;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
/** /**
@@ -8,6 +10,8 @@ import lombok.Data;
* @version 20241231 * @version 20241231
*/ */
@Data @Data
@AllArgsConstructor
@Builder
public class FlowableHistory { public class FlowableHistory {
private final String historyId; private final String historyId;
private final String instanceId; private final String instanceId;
@@ -18,12 +22,4 @@ public class FlowableHistory {
public FlowableHistory(String historyId, String instanceId, FlowableAction action, String comment) { public FlowableHistory(String historyId, String instanceId, FlowableAction action, String comment) {
this(historyId, instanceId, action, comment, LocalDateTime.now()); 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;
}
} }

View File

@@ -2,6 +2,8 @@ package com.lanyuanxiaoyao.flowable.core.model;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Map; import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
/** /**
@@ -9,31 +11,20 @@ import lombok.Data;
* @version 20241231 * @version 20241231
*/ */
@Data @Data
@AllArgsConstructor
@Builder
public class FlowableInstance { public class FlowableInstance {
private final String instanceId; private final String instanceId;
private final FlowableMetadata metadata; @Builder.Default
private final LocalDateTime createdTime; private final FlowableMetadata metadata = new FlowableMetadata();
@Builder.Default
private final LocalDateTime createdTime = LocalDateTime.now();
private String currentNodeId; private String currentNodeId;
private Status status; @Builder.Default
private LocalDateTime updatedTime; private Status status = Status.RUNNING;
@Builder.Default
public FlowableInstance(String instanceId, String currentNodeId) { private LocalDateTime updatedTime = LocalDateTime.now();
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;
}
public void addMetadata(Map<String, Object> metadata) { public void addMetadata(Map<String, Object> metadata) {
this.metadata.putAll(metadata); this.metadata.putAll(metadata);

View File

@@ -1,9 +1,11 @@
package com.lanyuanxiaoyao.flowable.core.model; 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.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
/** /**
@@ -13,43 +15,29 @@ import lombok.Data;
* @version 20241231 * @version 20241231
*/ */
@Data @Data
@AllArgsConstructor
@Builder
public class FlowableNode { public class FlowableNode {
private final String nodeId; private final String nodeId;
private final String name; private final String name;
private final String description; private final String description;
private final Type type; @Builder.Default
private final String handler; private final Type type = Type.MANUAL;
private final Map<FlowableAction, String> targets; @Builder.Default
private final String handler = FlowableHandler.DefaultFlowableHandler.class.getName();
@Builder.Default
private final Map<FlowableAction, String> targets = MapHelper.empty();
@Builder.Default
private final String accessor = FlowableAccessor.DefaultFlowableAccessor.class.getName();
private final List<String> listeners; private final List<String> listeners;
private final LocalDateTime createdTime; @Builder.Default
private final LocalDateTime createdTime = LocalDateTime.now();
@Builder.Default
private LocalDateTime updatedTime = LocalDateTime.now(); private LocalDateTime updatedTime = LocalDateTime.now();
public FlowableNode(String nodeId, String name, String description, Map<FlowableAction, String> targets) {
this(nodeId, name, description, Type.MANUAL, FlowableHandler.DefaultFlowableHandler.class, targets);
}
public FlowableNode(String nodeId, String name, String description, Class<? extends FlowableHandler> actionClass, Map<FlowableAction, String> targets) {
this(nodeId, name, description, Type.AUTOMATIC, actionClass, targets);
}
public FlowableNode(String nodeId, String name, String description, Type type, Class<? extends FlowableHandler> actionClass, Map<FlowableAction, String> 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<FlowableAction, String> targets, List<String> 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 { public enum Type {
AUTOMATIC, AUTOMATIC,
MANUAL, MANUAL,

View File

@@ -32,12 +32,12 @@ public abstract class TestFlowableManager {
} }
private FlowableNode createManualNode(String nodeId, Map<FlowableAction, String> nextNodes) { private FlowableNode createManualNode(String nodeId, Map<FlowableAction, String> nextNodes) {
return new FlowableNode( return FlowableNode.builder()
nodeId, .nodeId(nodeId)
UUID.randomUUID().toString(), .name(UUID.randomUUID().toString())
UUID.randomUUID().toString(), .description(UUID.randomUUID().toString())
nextNodes .targets(nextNodes)
); .build();
} }
/** /**
@@ -125,14 +125,12 @@ public abstract class TestFlowableManager {
@Test @Test
public void testSuspend() { public void testSuspend() {
FlowableManager manager = flowableManager(); FlowableManager manager = flowableManager();
FlowableNode node = new FlowableNode( FlowableNode node = FlowableNode.builder()
"edf1b7b0-f45b-4256-b696-ce84857f03f7", .nodeId("50342cb7-2029-4159-bd4c-cbfa4aebe474")
"edf1b7b0-f45b-4256-b696-ce84857f03f7", .name("50342cb7-2029-4159-bd4c-cbfa4aebe474")
"edf1b7b0-f45b-4256-b696-ce84857f03f7", .description("50342cb7-2029-4159-bd4c-cbfa4aebe474")
FlowableNode.Type.MANUAL, .handler(TwoApproveHandler.class.getName())
TwoApproveHandler.class, .build();
null
);
manager.create(node); manager.create(node);
String instanceId = manager.start(node.getNodeId()); String instanceId = manager.start(node.getNodeId());
@@ -148,13 +146,13 @@ public abstract class TestFlowableManager {
@Test @Test
public void testAutomaticNode() { public void testAutomaticNode() {
FlowableManager manager = flowableManager(); FlowableManager manager = flowableManager();
FlowableNode node = new FlowableNode( FlowableNode node = FlowableNode.builder()
"2733d930-7a4b-491e-b1ca-4d5811435e9f", .nodeId("5d60dab0-7691-4543-b753-af7ac02cb7ec")
"自动节点", .name("5d60dab0-7691-4543-b753-af7ac02cb7ec")
"自动节点", .description("5d60dab0-7691-4543-b753-af7ac02cb7ec")
getAutomaticNodeClass(), .type(FlowableNode.Type.AUTOMATIC)
null .handler(getAutomaticNodeClass().getName())
); .build();
manager.create(node); manager.create(node);
String instanceId = manager.start(node.getNodeId()); String instanceId = manager.start(node.getNodeId());
Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus()); Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus());