refactor: Change the way we loop through steps, to make it easier to work with inner steps (e.g. filters).
@@ -49,6 +49,23 @@ import io.syndesis.model.integration.Step;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.URISyntaxException;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
public class DefaultProjectGenerator implements ProjectGenerator {
private static final String MAPPER = "mapper";
@@ -152,9 +169,23 @@ public class DefaultProjectGenerator implements ProjectGenerator {
private byte[] generateFlowYaml(Map<String, byte[]> contents, GenerateProjectRequest request) throws JsonProcessingException {
Flow flow = new Flow();
request.getIntegration().getSteps().ifPresent(steps -> {
- int stepCounter=0;
- for (Step step : steps) {
- stepCounter++;
+ if (steps.isEmpty()) {
+ return;
+ }
+
+ Queue<Step> remaining = new LinkedList<>(steps);
+ Step first = remaining.remove();
+ if (first != null) {
+ visitStep(request, flow, contents, 1, first, remaining);
+ }
+ });
+
+ SyndesisModel syndesisModel = new SyndesisModel();
+ syndesisModel.addFlow(flow);
+ return YAML_OBJECT_MAPPER.writeValueAsBytes(syndesisModel);
+ }
+
+ private void visitStep(GenerateProjectRequest request, Flow flow, Map<String, byte[]> contents, int index, Step step, Queue<Step> remaining) {
if (step.getStepKind().equals(StepKinds.ENDPOINT)) {
step.getAction().ifPresent(action -> {
step.getConnection().ifPresent(connection -> {
@@ -172,36 +203,28 @@ public class DefaultProjectGenerator implements ProjectGenerator {
}
});
});
- continue;
- }
-
- if ( MAPPER.equals(step.getStepKind()) ) {
-
+ } else if ( MAPPER.equals(step.getStepKind())) {
Map<String, String> configuredProperties = step.getConfiguredProperties().get();
- String resourceName = "mapping-step-" + stepCounter + ".json";
+ String resourceName = "mapping-step-" + index + ".json";
byte[] resourceData = utf8(configuredProperties.get("atlasmapping"));
contents.put("src/main/resources/" + resourceName, resourceData);
flow.addStep(new Endpoint("atlas:" + resourceName));
-
- continue;
- }
-
+ } else {
try {
HashMap<String, Object> stepJSON = new HashMap<>(step.getConfiguredProperties().orElse(new HashMap<String, String>()));
stepJSON.put("kind", step.getStepKind());
String json = Json.mapper().writeValueAsString(stepJSON);
flow.addStep(Json.mapper().readValue(json, io.syndesis.integration.model.steps.Step.class));
- continue;
} catch (IOException e) {
e.printStackTrace();
}
}
- });
- SyndesisModel syndesisModel = new SyndesisModel();
- syndesisModel.addFlow(flow);
- return YAML_OBJECT_MAPPER.writeValueAsBytes(syndesisModel);
+ Step next = remaining.isEmpty() ? null : remaining.remove();
+ if (next != null) {
+ visitStep(request, flow, contents, ++index, next, remaining);
+ }
}
private static byte[] utf8(String value) {
