15
15
*/
16
16
package io .serverlessworkflow .utils ;
17
17
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 ;
18
22
import io .serverlessworkflow .api .Workflow ;
19
23
import io .serverlessworkflow .api .actions .Action ;
20
24
import io .serverlessworkflow .api .branches .Branch ;
@@ -157,7 +161,7 @@ public static List<EventDefinition> getWorkflowProducedEvents(Workflow workflow)
157
161
*
158
162
* @return Returns {@code List<EventDefinition>}
159
163
*/
160
- private static List <EventDefinition > getWorkflowEventDefinitions (
164
+ public static List <EventDefinition > getWorkflowEventDefinitions (
161
165
Workflow workflow , EventDefinition .Kind eventKind ) {
162
166
if (!hasStates (workflow )) {
163
167
return null ;
@@ -174,7 +178,7 @@ private static List<EventDefinition> getWorkflowEventDefinitions(
174
178
}
175
179
176
180
/** 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 ) {
178
182
List <String > eventReferences = new ArrayList <>();
179
183
180
184
for (State state : workflow .getStates ()) {
@@ -351,7 +355,7 @@ public static long getNumOfEndStates(Workflow workflow) {
351
355
}
352
356
}
353
357
354
- private static List <Action > getActionsWhichUsesFunctionDefinition (
358
+ public static List <Action > getActionsWhichUsesFunctionDefinition (
355
359
Workflow workflow , String functionDefinitionName ) {
356
360
List <Action > actions = new ArrayList <>();
357
361
for (State state : workflow .getStates ()) {
@@ -419,23 +423,23 @@ private static List<Action> getActionsWhichUsesFunctionDefinition(
419
423
return actions ;
420
424
}
421
425
422
- private static boolean checkIfActionUsesFunctionDefinition (
426
+ public static boolean checkIfActionUsesFunctionDefinition (
423
427
String functionDefinitionName , Action action ) {
424
428
return action != null
425
429
&& action .getFunctionRef () != null
426
430
&& action .getFunctionRef ().getRefName () != null
427
431
&& action .getFunctionRef ().getRefName ().equals (functionDefinitionName );
428
432
}
429
433
430
- private static boolean hasFunctionDefs (Workflow workflow , String functionDefinitionName ) {
434
+ public static boolean hasFunctionDefs (Workflow workflow , String functionDefinitionName ) {
431
435
if (!hasFunctionDefs (workflow )) return false ;
432
436
List <FunctionDefinition > functionDefs = workflow .getFunctions ().getFunctionDefs ();
433
437
return functionDefs .stream ()
434
438
.anyMatch (
435
439
functionDefinition -> functionDefinition .getName ().equals (functionDefinitionName ));
436
440
}
437
441
438
- private static FunctionRef getFunctionRefFromAction (Workflow workflow , String action ) {
442
+ public static FunctionRef getFunctionRefFromAction (Workflow workflow , String action ) {
439
443
if (!hasStates (workflow )) return null ;
440
444
441
445
for (State state : workflow .getStates ()) {
@@ -513,28 +517,28 @@ private static FunctionRef getFunctionRefFromAction(Workflow workflow, String ac
513
517
return null ;
514
518
}
515
519
516
- private static boolean hasFunctionDefs (Workflow workflow ) {
520
+ public static boolean hasFunctionDefs (Workflow workflow ) {
517
521
return workflow != null
518
522
&& workflow .getFunctions () != null
519
523
&& workflow .getFunctions ().getFunctionDefs () != null
520
524
&& !workflow .getFunctions ().getFunctionDefs ().isEmpty ();
521
525
}
522
526
523
527
/** Returns true if workflow has states, otherwise false */
524
- private static boolean hasStates (Workflow workflow ) {
528
+ public static boolean hasStates (Workflow workflow ) {
525
529
return workflow != null && workflow .getStates () != null && !workflow .getStates ().isEmpty ();
526
530
}
527
531
528
532
/** Returns true if workflow has events definitions, otherwise false */
529
- private static boolean hasEventDefs (Workflow workflow ) {
533
+ public static boolean hasEventDefs (Workflow workflow ) {
530
534
return workflow != null
531
535
&& workflow .getEvents () != null
532
536
&& workflow .getEvents ().getEventDefs () != null
533
537
&& !workflow .getEvents ().getEventDefs ().isEmpty ();
534
538
}
535
539
536
540
/** Gets event refs of an action */
537
- private static List <String > getActionEvents (Action action ) {
541
+ public static List <String > getActionEvents (Action action ) {
538
542
List <String > actionEvents = new ArrayList <>();
539
543
540
544
if (action != null && action .getEventRef () != null ) {
@@ -548,4 +552,73 @@ private static List<String> getActionEvents(Action action) {
548
552
549
553
return actionEvents ;
550
554
}
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
+ }
551
624
}
0 commit comments