Skip to content

Commit 377fbd0

Browse files
committed
fix script
1 parent 8727e2f commit 377fbd0

10 files changed

Lines changed: 57 additions & 85 deletions

File tree

flow-engine-framework/src/main/java/com/codingapi/flow/error/ErrorThrow.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,10 @@ public class ErrorThrow {
2424
*/
2525
private List<IFlowOperator> operators;
2626

27-
public static Builder builder() {
28-
return new Builder();
29-
}
30-
27+
/**
28+
* 是否为节点
29+
*/
3130
public boolean isNode() {
32-
return node != null;
33-
}
34-
35-
public static class Builder {
36-
private IFlowNode node;
37-
private List<IFlowOperator> operators;
38-
39-
public Builder node(IFlowNode node) {
40-
this.node = node;
41-
return this;
42-
}
43-
44-
public Builder operators(IFlowOperator... operators) {
45-
this.operators = List.of(operators);
46-
return this;
47-
}
48-
49-
public Builder operators(List<IFlowOperator> operators) {
50-
this.operators = operators;
51-
return this;
52-
}
53-
54-
public ErrorThrow build() {
55-
ErrorThrow errorThrow = new ErrorThrow();
56-
errorThrow.setNode(node);
57-
errorThrow.setOperators(operators);
58-
return errorThrow;
59-
}
31+
return node!=null;
6032
}
6133
}

flow-engine-framework/src/main/java/com/codingapi/flow/script/ScriptDefaultConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def run(request){
4545
// @SCRIPT_TITLE 回退至开始节点
4646
// @SCRIPT_META {"type":"node","node":"START"}
4747
def run(request){
48-
return $bind.createErrorThrow(request.getStartNode());
48+
return request.getStartNode().getId();
4949
}
5050
""";
5151

flow-engine-framework/src/main/java/com/codingapi/flow/script/node/ErrorTriggerScript.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.codingapi.flow.script.node;
22

33
import com.codingapi.flow.error.ErrorThrow;
4+
import com.codingapi.flow.operator.IFlowOperator;
45
import com.codingapi.flow.script.ScriptDefaultConstants;
56
import com.codingapi.flow.script.request.GroovyScriptRequest;
67
import com.codingapi.flow.script.runtime.ScriptRuntimeContext;
78
import com.codingapi.flow.session.FlowSession;
89
import lombok.AllArgsConstructor;
910
import lombok.Getter;
1011

12+
import java.util.ArrayList;
13+
import java.util.List;
14+
1115

1216
/**
1317
* 异常触发脚本
@@ -20,7 +24,31 @@ public class ErrorTriggerScript {
2024

2125
public ErrorThrow execute(FlowSession session) {
2226
GroovyScriptRequest request = new GroovyScriptRequest(session);
23-
return ScriptRuntimeContext.getInstance().run(script, ErrorThrow.class, request);
27+
Object value = ScriptRuntimeContext.getInstance().run(script, Object.class, request);
28+
if(value instanceof String){
29+
String nodeId = (String) value;
30+
ErrorThrow errorThrow = new ErrorThrow();
31+
errorThrow.setNode(session.getNode(nodeId));
32+
return errorThrow;
33+
}
34+
if(value instanceof List){
35+
List<Object> userIds =(List<Object>) value;
36+
List<Long> operatorIds = new ArrayList<>();
37+
for(Object userId:userIds){
38+
operatorIds.add(Long.parseLong(String.valueOf(userId)));
39+
}
40+
ErrorThrow errorThrow = new ErrorThrow();
41+
errorThrow.setOperators(session.getRepositoryHolder().findOperatorByIds(operatorIds));
42+
return errorThrow;
43+
}
44+
45+
long userId = Long.parseLong(String.valueOf(value));
46+
ErrorThrow errorThrow = new ErrorThrow();
47+
List<IFlowOperator> operatorList = new ArrayList<>();
48+
operatorList.add(session.getRepositoryHolder().getOperatorById(userId));
49+
errorThrow.setOperators(operatorList);
50+
return errorThrow;
51+
2452
}
2553

2654
/**

flow-engine-framework/src/main/java/com/codingapi/flow/script/request/GroovyScriptBind.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.codingapi.flow.script.request;
22

3-
import com.codingapi.flow.error.ErrorThrow;
4-
import com.codingapi.flow.node.IFlowNode;
53
import com.codingapi.flow.operator.IFlowOperator;
64
import com.codingapi.flow.record.FlowRecord;
75
import com.codingapi.flow.script.runtime.FlowScriptContext;
@@ -12,26 +10,14 @@
1210
/**
1311
* 流程groovy脚本绑定对象 $bind
1412
* def run(request){
15-
* $bind.createErrorThrow(operator);
13+
* $bind.getRecordById(1);
1614
* }
1715
*/
1816
@AllArgsConstructor
1917
public class GroovyScriptBind {
2018

2119
private final FlowScriptContext context;
2220

23-
public ErrorThrow createErrorThrow(IFlowNode node) {
24-
return context.createErrorThrow(node);
25-
}
26-
27-
public ErrorThrow createErrorThrow(List<Long> userIds) {
28-
return context.createErrorThrow(userIds);
29-
}
30-
31-
public ErrorThrow createErrorThrow(long... userIds) {
32-
return context.createErrorThrow(userIds);
33-
}
34-
3521
public <T> T getBean(Class<T> clazz) {
3622
return context.getBean(clazz);
3723
}

flow-engine-framework/src/main/java/com/codingapi/flow/script/request/GroovyScriptRequest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ public class GroovyScriptRequest {
3030
@Getter
3131
private String workflowCode;
3232

33-
/**
34-
* 流程编号
35-
*/
36-
@Getter
37-
private String workCode;
38-
3933
/**
4034
* 当前节点名称
4135
*/
@@ -111,7 +105,6 @@ public GroovyScriptRequest(FlowSession session) {
111105

112106
// 提取流程编号(从record获取)
113107
if (session.getCurrentRecord() != null) {
114-
this.workCode = session.getCurrentRecord().getWorkCode();
115108
this.submitOperatorId = session.getSubmitOperatorId();
116109
this.submitOperatorName = session.getSubmitOperatorName();
117110
}
@@ -132,6 +125,20 @@ public IFlowNode getNode(String nodeId){
132125
return flowSession.getNode(nodeId);
133126
}
134127

128+
/**
129+
* 是否流程管理员
130+
*/
131+
public boolean isFlowManager(){
132+
return this.currentOperator.isFlowManager();
133+
}
134+
135+
/**
136+
* 是否模拟测试
137+
*/
138+
public boolean isMock(){
139+
return this.flowSession.isMock();
140+
}
141+
135142

136143
/**
137144
* 流程创建者Id

flow-engine-framework/src/main/java/com/codingapi/flow/script/runtime/FlowScriptContext.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package com.codingapi.flow.script.runtime;
22

3-
import com.codingapi.flow.error.ErrorThrow;
4-
import com.codingapi.flow.node.IFlowNode;
53
import com.codingapi.flow.operator.IFlowOperator;
64
import com.codingapi.flow.record.FlowRecord;
75
import lombok.Getter;
86
import lombok.Setter;
97

10-
import java.util.Arrays;
118
import java.util.List;
129

1310
/**
@@ -27,24 +24,6 @@ private FlowScriptContext() {
2724
private IBeanFactory beanFactory;
2825

2926

30-
public ErrorThrow createErrorThrow(IFlowNode node) {
31-
return ErrorThrow.builder()
32-
.node(node)
33-
.build();
34-
}
35-
36-
public ErrorThrow createErrorThrow(List<Long> userIds) {
37-
List<IFlowOperator> operators = beanFactory.findOperatorsByIds(userIds);
38-
return ErrorThrow.builder()
39-
.operators(operators)
40-
.build();
41-
}
42-
43-
public ErrorThrow createErrorThrow(long... userIds) {
44-
List<Long> userIdList = Arrays.stream(userIds).boxed().toList();
45-
return this.createErrorThrow(userIdList);
46-
}
47-
4827
public <T> T getBean(Class<T> clazz) {
4928
return beanFactory.getBean(clazz);
5029
}
@@ -57,7 +36,6 @@ public <T> List<T> getBeans(Class<T> clazz) {
5736
return beanFactory.getBeans(clazz);
5837
}
5938

60-
6139
public FlowRecord getRecordById(long id) {
6240
return beanFactory.getRecordById(id);
6341
}
@@ -66,7 +44,6 @@ public IFlowOperator getOperatorById(long userId) {
6644
return beanFactory.getOperatorById(userId);
6745
}
6846

69-
7047
public List<IFlowOperator> findOperatorsByIds(List<Long> ids) {
7148
return beanFactory.findOperatorsByIds(ids);
7249
}

flow-engine-framework/src/test/java/com/codingapi/flow/script/ErrorTriggerScriptTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class ErrorTriggerScriptTest {
3333
void execute() {
3434
User user = new User(1, "lorne");
3535

36+
factory.userGateway.save(user);
37+
3638
FlowForm form = FlowFormBuilder.builder()
3739
.name("请假流程")
3840
.code("leave")
@@ -63,7 +65,7 @@ void execute() {
6365
.addPermission("leave", "reason", PermissionType.READ)
6466
.build()
6567
))
66-
.addStrategy(new OperatorLoadStrategy("def run(request){return [request.getCreatedOperator()]}"))
68+
.addStrategy(new OperatorLoadStrategy("def run(request){return [request.getCreatedOperatorId()]}"))
6769
.build()
6870
)
6971
.build();
@@ -91,7 +93,7 @@ void execute() {
9193

9294
String script = """
9395
def run(request){
94-
return $bind.createErrorThrow(request.getCreatedOperator());
96+
return request.getCreatedOperatorId();
9597
}
9698
""";
9799

flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowMockSampleServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,7 +2674,7 @@ void errorTest() {
26742674
.addPermission("leave", "reason", PermissionType.WRITE)
26752675
.build()))
26762676
.addStrategy(new OperatorLoadStrategy("def run(request){return [-1]}"))
2677-
.addStrategy(new ErrorTriggerStrategy("def run(request){ return $bind.createErrorThrow(3); }"))
2677+
.addStrategy(new ErrorTriggerStrategy("def run(request){ return 3; }"))
26782678
.build()
26792679
)
26802680
.build();

flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowSampleServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ void errorOperatorTest() {
26672667
.addPermission("leave", "reason", PermissionType.WRITE)
26682668
.build()))
26692669
.addStrategy(new OperatorLoadStrategy("def run(request){return [-1]}"))
2670-
.addStrategy(new ErrorTriggerStrategy("def run(request){ return $bind.createErrorThrow(3); }"))
2670+
.addStrategy(new ErrorTriggerStrategy("def run(request){ return 3; }"))
26712671
.build()
26722672
)
26732673
.build();
@@ -2773,7 +2773,7 @@ void errorNodeTest() {
27732773
.addPermission("leave", "reason", PermissionType.WRITE)
27742774
.build()))
27752775
.addStrategy(new OperatorLoadStrategy("def run(request){return [-1]}"))
2776-
.addStrategy(new ErrorTriggerStrategy("def run(request){ return $bind.createErrorThrow(request.getNode('" + startNode.getId() + "')); }"))
2776+
.addStrategy(new ErrorTriggerStrategy("def run(request){ return '" + startNode.getId() + "'; }"))
27772777
.build()
27782778
)
27792779
.build();

0 commit comments

Comments
 (0)