diff --git a/kamelet-support/src/main/java/io/kaoto/backend/model/deployment/kamelet/step/ChoiceFlowStep.java b/kamelet-support/src/main/java/io/kaoto/backend/model/deployment/kamelet/step/ChoiceFlowStep.java index 1957373c5..887c13089 100644 --- a/kamelet-support/src/main/java/io/kaoto/backend/model/deployment/kamelet/step/ChoiceFlowStep.java +++ b/kamelet-support/src/main/java/io/kaoto/backend/model/deployment/kamelet/step/ChoiceFlowStep.java @@ -51,20 +51,31 @@ public ChoiceFlowStep(final Step step, final KamelPopulator kameletPopulator) { List choices = new LinkedList<>(); if (step.getBranches() != null) { + Branch tentativeOtherwise = null; for (Branch b : step.getBranches()) { if (b.getCondition() != null || String.valueOf(b.getIdentifier()).startsWith("when-") - || choice.getOtherwise() != null) { + || tentativeOtherwise != null) { + if ("otherwise".equalsIgnoreCase(b.getIdentifier())) { + Branch tmp = tentativeOtherwise; + tentativeOtherwise = setOtherwiseBranch(kameletPopulator, choice, b); + b = tmp; + } choices.add(processChoice(b, kameletPopulator)); } else { - var otherwise = new Otherwise(); - otherwise.setSteps(kameletPopulator.processSteps(b)); - choice.setOtherwise(otherwise); + tentativeOtherwise = setOtherwiseBranch(kameletPopulator, choice, b); } } } choice.setChoice(choices); } + private static Branch setOtherwiseBranch(KamelPopulator kameletPopulator, SuperChoice choice, Branch b) { + var otherwise = new Otherwise(); + otherwise.setSteps(kameletPopulator.processSteps(b)); + choice.setOtherwise(otherwise); + return b; + } + private Choice processChoice(final Branch b, final KamelPopulator kameletPopulator) { Choice choice = new Choice(); @@ -83,7 +94,7 @@ private Choice processChoice(final Branch b, final KamelPopulator kameletPopulat @Override public Map getRepresenterProperties() { Map properties = new HashMap<>(); - properties.put(CHOICE_LABEL, this.getChoice()); + properties.put(CHOICE_LABEL, this.getChoice().getRepresenterProperties()); return properties; } diff --git a/kamelet-support/src/main/java/io/kaoto/backend/model/deployment/kamelet/step/choice/SuperChoice.java b/kamelet-support/src/main/java/io/kaoto/backend/model/deployment/kamelet/step/choice/SuperChoice.java index bbaf2ce6a..0d825cac2 100644 --- a/kamelet-support/src/main/java/io/kaoto/backend/model/deployment/kamelet/step/choice/SuperChoice.java +++ b/kamelet-support/src/main/java/io/kaoto/backend/model/deployment/kamelet/step/choice/SuperChoice.java @@ -9,7 +9,7 @@ import io.kaoto.backend.model.step.Branch; import io.kaoto.backend.model.step.Step; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -31,7 +31,7 @@ public SuperChoice(Step step, final KamelPopulator kameletPopulator) { if (step.getBranches() != null) { for (Branch b : step.getBranches()) { - if (b.getCondition() != null) { + if (b.getCondition() != null || String.valueOf(b.getIdentifier()).startsWith("when-")) { Choice choice = new Choice(); choice.setSteps(kameletPopulator.processSteps(b)); switch (b.getConditionSyntax()) { @@ -66,8 +66,9 @@ protected void processBranches(final Step step, final StepCatalog catalog, step.setBranches(new LinkedList<>()); if (getChoice() != null) { + int i = 1; for (var flow : getChoice()) { - Branch branch = createBranch(getChoiceIdentifier(flow), flow.getSteps(), kameletStepParserService); + Branch branch = createBranch(getChoiceIdentifier(flow, i++), flow.getSteps(), kameletStepParserService); branch.setConditionSyntax(getChoiceConditionSyntax(flow)); branch.setCondition(getChoiceCondition(flow)); kameletStepParserService.setValueOnStepProperty(step, @@ -83,13 +84,17 @@ protected void processBranches(final Step step, final StepCatalog catalog, } } - private String getChoiceIdentifier(final Choice flow) { - return getChoiceCondition(flow); + private String getChoiceIdentifier(final Choice flow, Integer i) { + String id = getChoiceCondition(flow); + if (id == null || id.isBlank()) { + id = "when-" + i; + } + return id; } private String getChoiceCondition(final Choice flow) { var res = ""; - switch (getChoiceConditionSyntax(flow)){ + switch (getChoiceConditionSyntax(flow)) { case JQ: res = flow.getJq(); break; @@ -117,7 +122,7 @@ private Branch.ConditionSyntax getChoiceConditionSyntax(final Choice flow) { @Override public Map getRepresenterProperties() { - Map properties = new HashMap<>(); + Map properties = new LinkedHashMap<>(); if (this.getChoice() != null) { properties.put(WHEN_LABEL, this.getChoice()); } @@ -129,7 +134,6 @@ public Map getRepresenterProperties() { } - @Override protected void assignProperty(final Parameter parameter) { //We don't have properties