16
16
package io .serverlessworkflow .utils ;
17
17
18
18
import io .serverlessworkflow .api .Workflow ;
19
- import io .serverlessworkflow .api .end .End ;
19
+ import io .serverlessworkflow .api .actions .Action ;
20
+ import io .serverlessworkflow .api .branches .Branch ;
20
21
import io .serverlessworkflow .api .events .EventDefinition ;
21
22
import io .serverlessworkflow .api .interfaces .State ;
22
23
import io .serverlessworkflow .api .start .Start ;
23
- import io .serverlessworkflow .api .states .CallbackState ;
24
- import io .serverlessworkflow .api .states .DefaultState ;
25
- import io .serverlessworkflow .api .states .EventState ;
26
- import io .serverlessworkflow .api .states .SwitchState ;
27
- import java .util .ArrayList ;
28
- import java .util .HashSet ;
29
- import java .util .List ;
30
- import java .util .Set ;
24
+ import io .serverlessworkflow .api .states .*;
25
+ import java .util .*;
31
26
import java .util .stream .Collectors ;
32
27
33
28
/** Provides common utility methods to provide most often needed answers from a workflow */
34
29
public final class WorkflowUtils {
35
30
private static final int DEFAULT_STARTING_STATE_POSITION = 0 ;
36
31
37
32
/**
38
- * Gets State matching Start state.If start is not present returns first state otherwise returns
39
- * null
33
+ * Gets State matching Start state. If start is not present returns first state. If start is
34
+ * present, returns the matching start State. If matching state is not present, returns null
40
35
*
41
36
* @param workflow workflow
42
37
* @return {@code state} when present else returns {@code null}
43
38
*/
44
39
public static State getStartingState (Workflow workflow ) {
45
- if (workflow == null || workflow . getStates () == null || workflow . getStates (). isEmpty ( )) {
40
+ if (! hasStates ( workflow )) {
46
41
return null ;
47
42
}
48
43
49
44
Start start = workflow .getStart ();
50
45
if (start == null ) {
51
46
return workflow .getStates ().get (DEFAULT_STARTING_STATE_POSITION );
52
47
} else {
53
- return workflow .getStates ().stream ()
54
- .filter (state -> state .getName ().equals (start .getStateName ()))
55
- .findFirst ()
56
- .get ();
48
+ Optional <State > startingState =
49
+ workflow .getStates ().stream ()
50
+ .filter (state -> state .getName ().equals (start .getStateName ()))
51
+ .findFirst ();
52
+ return startingState .orElse (null );
57
53
}
58
54
}
59
55
@@ -65,7 +61,7 @@ public static State getStartingState(Workflow workflow) {
65
61
* @return {@code List<State>}. Returns {@code null} when workflow is null.
66
62
*/
67
63
public static List <State > getStates (Workflow workflow , DefaultState .Type stateType ) {
68
- if (workflow == null || workflow . getStates () == null ) {
64
+ if (! hasStates ( workflow ) ) {
69
65
return null ;
70
66
}
71
67
@@ -99,14 +95,11 @@ public static List<EventDefinition> getDefinedProducedEvents(Workflow workflow)
99
95
*/
100
96
public static List <EventDefinition > getDefinedEvents (
101
97
Workflow workflow , EventDefinition .Kind eventKind ) {
102
- if (workflow == null || workflow .getEvents () == null ) {
103
- return null ;
104
- }
105
- List <EventDefinition > eventDefs = workflow .getEvents ().getEventDefs ();
106
- if (eventDefs == null ) {
98
+ if (!hasEventDefs (workflow )) {
107
99
return null ;
108
100
}
109
101
102
+ List <EventDefinition > eventDefs = workflow .getEvents ().getEventDefs ();
110
103
return eventDefs .stream ()
111
104
.filter (eventDef -> eventDef .getKind () == eventKind )
112
105
.collect (Collectors .toList ());
@@ -141,27 +134,13 @@ public static List<EventDefinition> getWorkflowConsumedEvents(Workflow workflow)
141
134
142
135
/**
143
136
* Gets Produced Events of parent workflow Iterates through states in parent workflow and collects
144
- * all the Produced Events.
137
+ * all the ConsumedEvents. Sub Workflows of the Workflow <strong>are not</strong> considered for
138
+ * getting Consumed Events
145
139
*
146
140
* @return Returns {@code List<EventDefinition>}
147
141
*/
148
142
public static List <EventDefinition > getWorkflowProducedEvents (Workflow workflow ) {
149
- if (workflow == null || workflow .getStates () == null || workflow .getStates ().size () == 0 ) {
150
- return null ;
151
- }
152
- List <EventDefinition > definedProducedEvents =
153
- getDefinedEvents (workflow , EventDefinition .Kind .PRODUCED );
154
- Set <String > uniqueEvents = new HashSet <>();
155
- for (State state : workflow .getStates ()) {
156
- End end = state .getEnd ();
157
- if (end != null && end .getProduceEvents () != null && end .getProduceEvents ().size () != 0 ) {
158
- end .getProduceEvents ()
159
- .forEach (produceEvent -> uniqueEvents .add (produceEvent .getEventRef ()));
160
- }
161
- }
162
- return definedProducedEvents .stream ()
163
- .filter (eventDefinition -> uniqueEvents .contains (eventDefinition .getName ()))
164
- .collect (Collectors .toList ());
143
+ return getWorkflowEventDefinitions (workflow , EventDefinition .Kind .PRODUCED );
165
144
}
166
145
167
146
/**
@@ -172,21 +151,24 @@ public static List<EventDefinition> getWorkflowProducedEvents(Workflow workflow)
172
151
*/
173
152
private static List <EventDefinition > getWorkflowEventDefinitions (
174
153
Workflow workflow , EventDefinition .Kind eventKind ) {
175
- if (workflow == null || workflow . getStates () == null || workflow . getStates (). size () == 0 ) {
154
+ if (! hasStates ( workflow ) ) {
176
155
return null ;
177
156
}
157
+
158
+ List <String > uniqueWorkflowEventsFromStates = getUniqueWorkflowEventsFromStates (workflow );
178
159
List <EventDefinition > definedConsumedEvents = getDefinedEvents (workflow , eventKind );
179
- if (definedConsumedEvents == null ) return null ;
180
- Set <String > uniqEventReferences = new HashSet <>();
181
- List <String > eventReferencesFromState = getWorkflowConsumedEventsFromState (workflow );
182
- uniqEventReferences .addAll (eventReferencesFromState );
160
+ if (definedConsumedEvents == null ) {
161
+ return null ;
162
+ }
183
163
return definedConsumedEvents .stream ()
184
- .filter (x -> uniqEventReferences .contains (x .getName ()))
185
- .collect (Collectors .toList ());
164
+ .filter (definedEvent -> uniqueWorkflowEventsFromStates .contains (definedEvent .getName ()))
165
+ .collect (Collectors .toList ());
186
166
}
187
167
188
- private static List <String > getWorkflowConsumedEventsFromState (Workflow workflow ) {
168
+ /** Returns a list of unique event names from workflow states */
169
+ private static List <String > getUniqueWorkflowEventsFromStates (Workflow workflow ) {
189
170
List <String > eventReferences = new ArrayList <>();
171
+
190
172
for (State state : workflow .getStates ()) {
191
173
if (state instanceof SwitchState ) {
192
174
SwitchState switchState = (SwitchState ) state ;
@@ -198,16 +180,46 @@ private static List<String> getWorkflowConsumedEventsFromState(Workflow workflow
198
180
} else if (state instanceof CallbackState ) {
199
181
CallbackState callbackState = (CallbackState ) state ;
200
182
if (callbackState .getEventRef () != null ) eventReferences .add (callbackState .getEventRef ());
183
+ if (callbackState .getAction () != null && callbackState .getAction ().getEventRef () != null ) {
184
+ eventReferences .addAll (getActionEvents (callbackState .getAction ()));
185
+ }
201
186
} else if (state instanceof EventState ) {
202
187
EventState eventState = (EventState ) state ;
203
188
if (eventState .getOnEvents () != null ) {
204
189
eventState
205
190
.getOnEvents ()
206
- .forEach (onEvents -> eventReferences .addAll (onEvents .getEventRefs ()));
191
+ .forEach (
192
+ onEvents -> {
193
+ eventReferences .addAll (onEvents .getEventRefs ());
194
+ if (onEvents .getActions () != null ) {
195
+ for (Action action : onEvents .getActions ()) {
196
+ eventReferences .addAll (getActionEvents (action ));
197
+ }
198
+ }
199
+ });
200
+ }
201
+ } else if (state instanceof OperationState ) {
202
+ OperationState operationState = (OperationState ) state ;
203
+ if (operationState .getActions () != null ) {
204
+ for (Action action : operationState .getActions ()) {
205
+ eventReferences .addAll (getActionEvents (action ));
206
+ }
207
+ }
208
+ } else if (state instanceof ParallelState ) {
209
+ ParallelState parallelState = (ParallelState ) state ;
210
+ if (parallelState .getBranches () != null ) {
211
+ for (Branch branch : parallelState .getBranches ()) {
212
+ if (branch .getActions () != null ) {
213
+ for (Action action : branch .getActions ()) {
214
+ eventReferences .addAll (getActionEvents (action ));
215
+ }
216
+ }
217
+ }
207
218
}
208
219
}
209
220
}
210
- return eventReferences ;
221
+
222
+ return eventReferences .stream ().distinct ().collect (Collectors .toList ());
211
223
}
212
224
213
225
/**
@@ -227,4 +239,33 @@ public static int getWorkflowProducedEventsCount(Workflow workflow) {
227
239
List <EventDefinition > workflowProducedEvents = getWorkflowProducedEvents (workflow );
228
240
return workflowProducedEvents == null ? 0 : workflowProducedEvents .size ();
229
241
}
242
+
243
+ /** Returns true if workflow has states, otherwise false */
244
+ private static boolean hasStates (Workflow workflow ) {
245
+ return workflow != null && workflow .getStates () != null && !workflow .getStates ().isEmpty ();
246
+ }
247
+
248
+ /** Returns true if workflow has events definitions, otherwise false */
249
+ private static boolean hasEventDefs (Workflow workflow ) {
250
+ return workflow != null
251
+ && workflow .getEvents () != null
252
+ && workflow .getEvents ().getEventDefs () != null
253
+ && !workflow .getEvents ().getEventDefs ().isEmpty ();
254
+ }
255
+
256
+ /** Gets event refs of an action */
257
+ private static List <String > getActionEvents (Action action ) {
258
+ List <String > actionEvents = new ArrayList <>();
259
+
260
+ if (action != null && action .getEventRef () != null ) {
261
+ if (action .getEventRef ().getTriggerEventRef () != null ) {
262
+ actionEvents .add (action .getEventRef ().getTriggerEventRef ());
263
+ }
264
+ if (action .getEventRef ().getResultEventRef () != null ) {
265
+ actionEvents .add (action .getEventRef ().getResultEventRef ());
266
+ }
267
+ }
268
+
269
+ return actionEvents ;
270
+ }
230
271
}
0 commit comments