|
19 | 19 | import io.serverlessworkflow.api.types.FlowDirective;
|
20 | 20 | import io.serverlessworkflow.api.types.FlowDirectiveEnum;
|
21 | 21 | import io.serverlessworkflow.api.types.TaskBase;
|
| 22 | +import java.time.Instant; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
22 | 25 |
|
23 | 26 | public class TaskContext<T extends TaskBase> {
|
24 | 27 |
|
25 | 28 | private final JsonNode rawInput;
|
26 | 29 | private final T task;
|
| 30 | + private final WorkflowPosition position; |
| 31 | + private final Instant startedAt; |
27 | 32 |
|
28 | 33 | private JsonNode input;
|
29 | 34 | private JsonNode output;
|
30 | 35 | private JsonNode rawOutput;
|
31 | 36 | private FlowDirective flowDirective;
|
| 37 | + private Map<String, Object> contextVariables; |
| 38 | + private Instant completedAt; |
32 | 39 |
|
33 |
| - public TaskContext(JsonNode rawInput, T task) { |
| 40 | + public TaskContext(JsonNode input, WorkflowPosition position) { |
| 41 | + this(input, null, position, Instant.now(), input, input, input, null, new HashMap<>()); |
| 42 | + } |
| 43 | + |
| 44 | + public TaskContext(JsonNode input, TaskContext<?> taskContext, T task) { |
| 45 | + this( |
| 46 | + input, |
| 47 | + task, |
| 48 | + taskContext.position, |
| 49 | + Instant.now(), |
| 50 | + input, |
| 51 | + input, |
| 52 | + input, |
| 53 | + task.getThen(), |
| 54 | + new HashMap<>(taskContext.variables())); |
| 55 | + } |
| 56 | + |
| 57 | + private TaskContext( |
| 58 | + JsonNode rawInput, |
| 59 | + T task, |
| 60 | + WorkflowPosition position, |
| 61 | + Instant startedAt, |
| 62 | + JsonNode input, |
| 63 | + JsonNode output, |
| 64 | + JsonNode rawOutput, |
| 65 | + FlowDirective flowDirective, |
| 66 | + Map<String, Object> contextVariables) { |
34 | 67 | this.rawInput = rawInput;
|
35 |
| - this.input = rawInput; |
36 |
| - this.rawOutput = rawInput; |
37 |
| - this.output = rawInput; |
38 | 68 | this.task = task;
|
39 |
| - this.flowDirective = task.getThen(); |
| 69 | + this.position = position; |
| 70 | + this.startedAt = startedAt; |
| 71 | + this.input = input; |
| 72 | + this.output = output; |
| 73 | + this.rawOutput = rawOutput; |
| 74 | + this.flowDirective = flowDirective; |
| 75 | + this.contextVariables = contextVariables; |
| 76 | + } |
| 77 | + |
| 78 | + public TaskContext<T> copy() { |
| 79 | + return new TaskContext<T>( |
| 80 | + rawInput, |
| 81 | + task, |
| 82 | + position.copy(), |
| 83 | + startedAt, |
| 84 | + input, |
| 85 | + output, |
| 86 | + rawOutput, |
| 87 | + flowDirective, |
| 88 | + new HashMap<>(contextVariables)); |
40 | 89 | }
|
41 | 90 |
|
42 | 91 | public void input(JsonNode input) {
|
43 | 92 | this.input = input;
|
| 93 | + this.rawOutput = input; |
| 94 | + this.output = input; |
44 | 95 | }
|
45 | 96 |
|
46 | 97 | public JsonNode input() {
|
@@ -81,4 +132,24 @@ public FlowDirective flowDirective() {
|
81 | 132 | ? new FlowDirective().withFlowDirectiveEnum(FlowDirectiveEnum.CONTINUE)
|
82 | 133 | : flowDirective;
|
83 | 134 | }
|
| 135 | + |
| 136 | + public Map<String, Object> variables() { |
| 137 | + return contextVariables; |
| 138 | + } |
| 139 | + |
| 140 | + public WorkflowPosition position() { |
| 141 | + return position; |
| 142 | + } |
| 143 | + |
| 144 | + public Instant startedAt() { |
| 145 | + return startedAt; |
| 146 | + } |
| 147 | + |
| 148 | + public void completedAt(Instant instant) { |
| 149 | + this.completedAt = instant; |
| 150 | + } |
| 151 | + |
| 152 | + public Instant completedAt() { |
| 153 | + return completedAt; |
| 154 | + } |
84 | 155 | }
|
0 commit comments