|
19 | 19 | import io.serverlessworkflow.api.actions.Action;
|
20 | 20 | import io.serverlessworkflow.api.branches.Branch;
|
21 | 21 | import io.serverlessworkflow.api.events.EventDefinition;
|
| 22 | +import io.serverlessworkflow.api.events.OnEvents; |
22 | 23 | import io.serverlessworkflow.api.functions.FunctionDefinition;
|
| 24 | +import io.serverlessworkflow.api.functions.FunctionRef; |
23 | 25 | import io.serverlessworkflow.api.interfaces.State;
|
24 | 26 | import io.serverlessworkflow.api.start.Start;
|
25 | 27 | import io.serverlessworkflow.api.states.*;
|
@@ -242,13 +244,83 @@ public static int getWorkflowProducedEventsCount(Workflow workflow) {
|
242 | 244 | }
|
243 | 245 |
|
244 | 246 | /** @return Returns function definition for actions */
|
245 |
| - public static List<FunctionDefinition> getFunctionDefinitionsForAction( |
| 247 | + public static FunctionDefinition getFunctionDefinitionsForAction( |
246 | 248 | Workflow workflow, String action) {
|
247 |
| - if (hasFunctionDefs(workflow)) { |
248 |
| - return workflow.getFunctions().getFunctionDefs().stream() |
249 |
| - .filter(functionDef -> functionDef.getName().equals(action)) |
250 |
| - .collect(Collectors.toList()); |
| 249 | + if (!hasFunctionDefs(workflow)) return null; |
| 250 | + FunctionRef functionRef = getFunctionRefFromAction(workflow, action); |
| 251 | + if (functionRef == null) return null; |
| 252 | + final Optional<FunctionDefinition> functionDefinition = |
| 253 | + workflow.getFunctions().getFunctionDefs().stream() |
| 254 | + .filter(functionDef -> functionDef.getName().equals(functionRef.getRefName())) |
| 255 | + .distinct() |
| 256 | + .findFirst(); |
| 257 | + |
| 258 | + return functionDefinition.isPresent() ? functionDefinition.get() : null; |
| 259 | + } |
| 260 | + |
| 261 | + private static FunctionRef getFunctionRefFromAction(Workflow workflow, String action) { |
| 262 | + if (!hasStates(workflow)) return null; |
| 263 | + |
| 264 | + for (State state : workflow.getStates()) { |
| 265 | + if (state instanceof EventState) { |
| 266 | + EventState eventState = (EventState) state; |
| 267 | + List<OnEvents> onEvents = eventState.getOnEvents(); |
| 268 | + if (onEvents != null) { |
| 269 | + for (OnEvents onEvent : onEvents) { |
| 270 | + if (onEvent != null) { |
| 271 | + List<Action> onEventActions = onEvent.getActions(); |
| 272 | + if (onEventActions != null) { |
| 273 | + for (Action onEventAction : onEventActions) { |
| 274 | + if (onEventAction != null |
| 275 | + && onEventAction.getName() != null |
| 276 | + && onEventAction.getName().equals(action)) |
| 277 | + return onEventAction.getFunctionRef(); |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + } else if (state instanceof CallbackState) { |
| 284 | + CallbackState callbackState = (CallbackState) state; |
| 285 | + final Action callbackStateAction = callbackState.getAction(); |
| 286 | + if (callbackStateAction != null |
| 287 | + && callbackStateAction.getName() != null |
| 288 | + && callbackStateAction.getName().equals(action)) { |
| 289 | + return callbackStateAction.getFunctionRef(); |
| 290 | + } |
| 291 | + |
| 292 | + } else if (state instanceof OperationState) { |
| 293 | + OperationState operationState = (OperationState) state; |
| 294 | + final List<Action> operationStateActions = operationState.getActions(); |
| 295 | + if (operationStateActions != null) { |
| 296 | + for (Action operationStateAction : operationStateActions) { |
| 297 | + if (operationStateAction != null |
| 298 | + && operationStateAction.getName() != null |
| 299 | + && operationStateAction.getName().equals(action)) { |
| 300 | + return operationStateAction.getFunctionRef(); |
| 301 | + } |
| 302 | + } |
| 303 | + } |
| 304 | + } else if (state instanceof ParallelState) { |
| 305 | + ParallelState parallelState = (ParallelState) state; |
| 306 | + List<Branch> parallelStateBranches = parallelState.getBranches(); |
| 307 | + if (parallelStateBranches != null) { |
| 308 | + for (Branch branch : parallelStateBranches) { |
| 309 | + List<Action> branchActions = branch.getActions(); |
| 310 | + if (branchActions != null) { |
| 311 | + for (Action branchAction : branchActions) { |
| 312 | + if (branchAction != null |
| 313 | + && branchAction.getName() != null |
| 314 | + && branchAction.getName().equals(action)) { |
| 315 | + return branchAction.getFunctionRef(); |
| 316 | + } |
| 317 | + } |
| 318 | + } |
| 319 | + } |
| 320 | + } |
| 321 | + } |
251 | 322 | }
|
| 323 | + |
252 | 324 | return null;
|
253 | 325 | }
|
254 | 326 |
|
|
0 commit comments