1515 */
1616package io .serverlessworkflow .utils ;
1717
18+ import com .fasterxml .jackson .databind .JsonNode ;
19+ import com .fasterxml .jackson .databind .ObjectMapper ;
20+ import com .fasterxml .jackson .databind .node .ArrayNode ;
21+ import com .fasterxml .jackson .databind .node .ObjectNode ;
1822import io .serverlessworkflow .api .Workflow ;
1923import io .serverlessworkflow .api .actions .Action ;
2024import io .serverlessworkflow .api .branches .Branch ;
@@ -157,7 +161,7 @@ public static List<EventDefinition> getWorkflowProducedEvents(Workflow workflow)
157161 *
158162 * @return Returns {@code List<EventDefinition>}
159163 */
160- private static List <EventDefinition > getWorkflowEventDefinitions (
164+ public static List <EventDefinition > getWorkflowEventDefinitions (
161165 Workflow workflow , EventDefinition .Kind eventKind ) {
162166 if (!hasStates (workflow )) {
163167 return null ;
@@ -174,7 +178,7 @@ private static List<EventDefinition> getWorkflowEventDefinitions(
174178 }
175179
176180 /** Returns a list of unique event names from workflow states */
177- private static List <String > getUniqueWorkflowEventsFromStates (Workflow workflow ) {
181+ public static List <String > getUniqueWorkflowEventsFromStates (Workflow workflow ) {
178182 List <String > eventReferences = new ArrayList <>();
179183
180184 for (State state : workflow .getStates ()) {
@@ -351,7 +355,7 @@ public static long getNumOfEndStates(Workflow workflow) {
351355 }
352356 }
353357
354- private static List <Action > getActionsWhichUsesFunctionDefinition (
358+ public static List <Action > getActionsWhichUsesFunctionDefinition (
355359 Workflow workflow , String functionDefinitionName ) {
356360 List <Action > actions = new ArrayList <>();
357361 for (State state : workflow .getStates ()) {
@@ -419,23 +423,23 @@ private static List<Action> getActionsWhichUsesFunctionDefinition(
419423 return actions ;
420424 }
421425
422- private static boolean checkIfActionUsesFunctionDefinition (
426+ public static boolean checkIfActionUsesFunctionDefinition (
423427 String functionDefinitionName , Action action ) {
424428 return action != null
425429 && action .getFunctionRef () != null
426430 && action .getFunctionRef ().getRefName () != null
427431 && action .getFunctionRef ().getRefName ().equals (functionDefinitionName );
428432 }
429433
430- private static boolean hasFunctionDefs (Workflow workflow , String functionDefinitionName ) {
434+ public static boolean hasFunctionDefs (Workflow workflow , String functionDefinitionName ) {
431435 if (!hasFunctionDefs (workflow )) return false ;
432436 List <FunctionDefinition > functionDefs = workflow .getFunctions ().getFunctionDefs ();
433437 return functionDefs .stream ()
434438 .anyMatch (
435439 functionDefinition -> functionDefinition .getName ().equals (functionDefinitionName ));
436440 }
437441
438- private static FunctionRef getFunctionRefFromAction (Workflow workflow , String action ) {
442+ public static FunctionRef getFunctionRefFromAction (Workflow workflow , String action ) {
439443 if (!hasStates (workflow )) return null ;
440444
441445 for (State state : workflow .getStates ()) {
@@ -513,28 +517,28 @@ private static FunctionRef getFunctionRefFromAction(Workflow workflow, String ac
513517 return null ;
514518 }
515519
516- private static boolean hasFunctionDefs (Workflow workflow ) {
520+ public static boolean hasFunctionDefs (Workflow workflow ) {
517521 return workflow != null
518522 && workflow .getFunctions () != null
519523 && workflow .getFunctions ().getFunctionDefs () != null
520524 && !workflow .getFunctions ().getFunctionDefs ().isEmpty ();
521525 }
522526
523527 /** Returns true if workflow has states, otherwise false */
524- private static boolean hasStates (Workflow workflow ) {
528+ public static boolean hasStates (Workflow workflow ) {
525529 return workflow != null && workflow .getStates () != null && !workflow .getStates ().isEmpty ();
526530 }
527531
528532 /** Returns true if workflow has events definitions, otherwise false */
529- private static boolean hasEventDefs (Workflow workflow ) {
533+ public static boolean hasEventDefs (Workflow workflow ) {
530534 return workflow != null
531535 && workflow .getEvents () != null
532536 && workflow .getEvents ().getEventDefs () != null
533537 && !workflow .getEvents ().getEventDefs ().isEmpty ();
534538 }
535539
536540 /** Gets event refs of an action */
537- private static List <String > getActionEvents (Action action ) {
541+ public static List <String > getActionEvents (Action action ) {
538542 List <String > actionEvents = new ArrayList <>();
539543
540544 if (action != null && action .getEventRef () != null ) {
@@ -548,4 +552,73 @@ private static List<String> getActionEvents(Action action) {
548552
549553 return actionEvents ;
550554 }
555+
556+ /**
557+ * Merges two JsonNode
558+ *
559+ * @param mainNode
560+ * @param updateNode
561+ * @return merged JsonNode
562+ */
563+ public static JsonNode mergeNodes (JsonNode mainNode , JsonNode updateNode ) {
564+
565+ Iterator <String > fieldNames = updateNode .fieldNames ();
566+ while (fieldNames .hasNext ()) {
567+
568+ String fieldName = fieldNames .next ();
569+ JsonNode jsonNode = mainNode .get (fieldName );
570+ // if field exists and is an embedded object
571+ if (jsonNode != null && jsonNode .isObject ()) {
572+ mergeNodes (jsonNode , updateNode .get (fieldName ));
573+ } else {
574+ if (mainNode instanceof ObjectNode ) {
575+ // Overwrite field
576+ JsonNode value = updateNode .get (fieldName );
577+ ((ObjectNode ) mainNode ).put (fieldName , value );
578+ }
579+ }
580+ }
581+
582+ return mainNode ;
583+ }
584+
585+ /**
586+ * Adds node as field
587+ *
588+ * @param mainNode
589+ * @param toAddNode
590+ * @param fieldName
591+ * @return original, main node with field added
592+ */
593+ public static JsonNode addNode (JsonNode mainNode , JsonNode toAddNode , String fieldName ) {
594+ ((ObjectNode ) mainNode ).put (fieldName , toAddNode );
595+ return mainNode ;
596+ }
597+
598+ /**
599+ * Adds array with name
600+ *
601+ * @param mainNode
602+ * @param toAddArray
603+ * @param arrayName
604+ * @return original, main node with array added
605+ */
606+ public static JsonNode addArray (JsonNode mainNode , ArrayNode toAddArray , String arrayName ) {
607+ ((ObjectNode ) mainNode ).put (arrayName , toAddArray );
608+ return mainNode ;
609+ }
610+
611+ /**
612+ * Adds a object field
613+ *
614+ * @param mainNode
615+ * @param toAddValue
616+ * @param fieldName
617+ * @return original, main node with field added
618+ */
619+ public static JsonNode addFieldValue (JsonNode mainNode , Object toAddValue , String fieldName ) {
620+ ObjectMapper mapper = new ObjectMapper ();
621+ ((ObjectNode ) mainNode ).put (fieldName , mapper .valueToTree (toAddValue ));
622+ return mainNode ;
623+ }
551624}
0 commit comments