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,21 @@ 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 );
183
160
return definedConsumedEvents .stream ()
184
- .filter (x -> uniqEventReferences .contains (x .getName ()))
161
+ .filter (definedEvent -> uniqueWorkflowEventsFromStates .contains (definedEvent .getName ()))
185
162
.collect (Collectors .toList ());
186
163
}
187
164
188
- private static List <String > getWorkflowConsumedEventsFromState (Workflow workflow ) {
165
+ /** Returns a list of unique event names from workflow states */
166
+ private static List <String > getUniqueWorkflowEventsFromStates (Workflow workflow ) {
189
167
List <String > eventReferences = new ArrayList <>();
168
+
190
169
for (State state : workflow .getStates ()) {
191
170
if (state instanceof SwitchState ) {
192
171
SwitchState switchState = (SwitchState ) state ;
@@ -198,16 +177,46 @@ private static List<String> getWorkflowConsumedEventsFromState(Workflow workflow
198
177
} else if (state instanceof CallbackState ) {
199
178
CallbackState callbackState = (CallbackState ) state ;
200
179
if (callbackState .getEventRef () != null ) eventReferences .add (callbackState .getEventRef ());
180
+ if (callbackState .getAction () != null && callbackState .getAction ().getEventRef () != null ) {
181
+ eventReferences .addAll (getActionEvents (callbackState .getAction ()));
182
+ }
201
183
} else if (state instanceof EventState ) {
202
184
EventState eventState = (EventState ) state ;
203
185
if (eventState .getOnEvents () != null ) {
204
186
eventState
205
187
.getOnEvents ()
206
- .forEach (onEvents -> eventReferences .addAll (onEvents .getEventRefs ()));
188
+ .forEach (
189
+ onEvents -> {
190
+ eventReferences .addAll (onEvents .getEventRefs ());
191
+ if (onEvents .getActions () != null ) {
192
+ for (Action action : onEvents .getActions ()) {
193
+ eventReferences .addAll (getActionEvents (action ));
194
+ }
195
+ }
196
+ });
197
+ }
198
+ } else if (state instanceof OperationState ) {
199
+ OperationState operationState = (OperationState ) state ;
200
+ if (operationState .getActions () != null ) {
201
+ for (Action action : operationState .getActions ()) {
202
+ eventReferences .addAll (getActionEvents (action ));
203
+ }
204
+ }
205
+ } else if (state instanceof ParallelState ) {
206
+ ParallelState parallelState = (ParallelState ) state ;
207
+ if (parallelState .getBranches () != null ) {
208
+ for (Branch branch : parallelState .getBranches ()) {
209
+ if (branch .getActions () != null ) {
210
+ for (Action action : branch .getActions ()) {
211
+ eventReferences .addAll (getActionEvents (action ));
212
+ }
213
+ }
214
+ }
207
215
}
208
216
}
209
217
}
210
- return eventReferences ;
218
+
219
+ return eventReferences .stream ().distinct ().collect (Collectors .toList ());
211
220
}
212
221
213
222
/**
@@ -227,4 +236,33 @@ public static int getWorkflowProducedEventsCount(Workflow workflow) {
227
236
List <EventDefinition > workflowProducedEvents = getWorkflowProducedEvents (workflow );
228
237
return workflowProducedEvents == null ? 0 : workflowProducedEvents .size ();
229
238
}
239
+
240
+ /** Returns true if workflow has states, otherwise false */
241
+ private static boolean hasStates (Workflow workflow ) {
242
+ return workflow != null && workflow .getStates () != null && !workflow .getStates ().isEmpty ();
243
+ }
244
+
245
+ /** Returns true if workflow has events definitions, otherwise false */
246
+ private static boolean hasEventDefs (Workflow workflow ) {
247
+ return workflow != null
248
+ && workflow .getEvents () != null
249
+ && workflow .getEvents ().getEventDefs () != null
250
+ && !workflow .getEvents ().getEventDefs ().isEmpty ();
251
+ }
252
+
253
+ /** Gets event refs of an action */
254
+ private static List <String > getActionEvents (Action action ) {
255
+ List <String > actionEvents = new ArrayList <>();
256
+
257
+ if (action != null && action .getEventRef () != null ) {
258
+ if (action .getEventRef ().getTriggerEventRef () != null ) {
259
+ actionEvents .add (action .getEventRef ().getTriggerEventRef ());
260
+ }
261
+ if (action .getEventRef ().getResultEventRef () != null ) {
262
+ actionEvents .add (action .getEventRef ().getResultEventRef ());
263
+ }
264
+ }
265
+
266
+ return actionEvents ;
267
+ }
230
268
}
0 commit comments