@@ -258,6 +258,97 @@ public static FunctionDefinition getFunctionDefinitionsForAction(
258258 return functionDefinition .isPresent () ? functionDefinition .get () : null ;
259259 }
260260
261+ /** @return : Returns @{code List<Action>} which uses a function defintion */
262+ public static List <Action > getActionsForFunctionDefinition (
263+ Workflow workflow , String functionDefinitionName ) {
264+ if (!hasFunctionDefs (workflow , functionDefinitionName )) return null ;
265+ return getActionsWhichUsesFunctionDefinition (workflow , functionDefinitionName );
266+ }
267+
268+ private static List <Action > getActionsWhichUsesFunctionDefinition (
269+ Workflow workflow , String functionDefinitionName ) {
270+ List <Action > actions = new ArrayList <>();
271+ for (State state : workflow .getStates ()) {
272+ if (state instanceof EventState ) {
273+ EventState eventState = (EventState ) state ;
274+ List <OnEvents > onEvents = eventState .getOnEvents ();
275+ if (onEvents != null ) {
276+ for (OnEvents onEvent : onEvents ) {
277+ if (onEvent != null ) {
278+ List <Action > onEventActions = onEvent .getActions ();
279+ if (onEventActions != null ) {
280+ for (Action onEventAction : onEventActions ) {
281+ if (doesActionUsesFuntionDefinition (functionDefinitionName , onEventAction ))
282+ actions .add (onEventAction );
283+ }
284+ }
285+ }
286+ }
287+ }
288+ } else if (state instanceof CallbackState ) {
289+ CallbackState callbackState = (CallbackState ) state ;
290+ final Action callbackStateAction = callbackState .getAction ();
291+ if (doesActionUsesFuntionDefinition (functionDefinitionName , callbackStateAction )) {
292+ actions .add (callbackStateAction );
293+ }
294+
295+ } else if (state instanceof OperationState ) {
296+ OperationState operationState = (OperationState ) state ;
297+ final List <Action > operationStateActions = operationState .getActions ();
298+ if (operationStateActions != null ) {
299+ for (Action operationStateAction : operationStateActions ) {
300+ if (doesActionUsesFuntionDefinition (functionDefinitionName , operationStateAction )) {
301+ actions .add (operationStateAction );
302+ }
303+ }
304+ }
305+ } else if (state instanceof ParallelState ) {
306+ ParallelState parallelState = (ParallelState ) state ;
307+ List <Branch > parallelStateBranches = parallelState .getBranches ();
308+ if (parallelStateBranches != null ) {
309+ for (Branch branch : parallelStateBranches ) {
310+ List <Action > branchActions = branch .getActions ();
311+ if (branchActions != null ) {
312+ for (Action branchAction : branchActions ) {
313+ if (doesActionUsesFuntionDefinition (functionDefinitionName , branchAction )) {
314+ actions .add (branchAction );
315+ }
316+ }
317+ }
318+ }
319+ }
320+ } else if (state instanceof ForEachState ) {
321+ ForEachState forEachState = (ForEachState ) state ;
322+ List <Action > forEachStateActions = forEachState .getActions ();
323+ if (forEachStateActions != null ) {
324+ for (Action forEachStateAction : forEachStateActions ) {
325+ if (doesActionUsesFuntionDefinition (functionDefinitionName , forEachStateAction )) {
326+ actions .add (forEachStateAction );
327+ }
328+ }
329+ }
330+ }
331+ }
332+
333+ return actions ;
334+ }
335+
336+ private static boolean doesActionUsesFuntionDefinition (
337+ String functionDefinitionName , Action forEachStateAction ) {
338+ return forEachStateAction != null
339+ && forEachStateAction .getFunctionRef () != null
340+ && forEachStateAction .getFunctionRef ().getRefName () != null
341+ && forEachStateAction .getFunctionRef ().getRefName ().equals (functionDefinitionName );
342+ }
343+
344+ private static boolean hasFunctionDefs (Workflow workflow , String functionDefinitionName ) {
345+ if (!hasFunctionDefs (workflow )) return false ;
346+ List <FunctionDefinition > functionDefs = workflow .getFunctions ().getFunctionDefs ();
347+ return functionDefs .stream ()
348+ .anyMatch (
349+ functionDefinition -> functionDefinition .getName ().equals (functionDefinitionName ));
350+ }
351+
261352 private static FunctionRef getFunctionRefFromAction (Workflow workflow , String action ) {
262353 if (!hasStates (workflow )) return null ;
263354
0 commit comments