Skip to content

Commit e1340fe

Browse files
Choice support (#110)
* Moving subflow prefix into the renderer function * Implementing otherwise statements * Working example of choice in test * Failing test for subflow calling with in choice
1 parent 388f702 commit e1340fe

29 files changed

+533
-40
lines changed

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/MuleComponentToSpringIntegrationDslTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
import org.springframework.sbm.mule.api.toplevel.configuration.MuleConfigurations;
1919

2020
import javax.xml.namespace.QName;
21+
import java.util.Map;
2122

2223
public interface MuleComponentToSpringIntegrationDslTranslator<T> {
2324
Class<T> getSupportedMuleType();
2425

25-
DslSnippet translate(T component, QName name, MuleConfigurations muleConfigurations, String flowName);
26+
DslSnippet translate(T component, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap);
2627
}

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/UnknownStatementTranslator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222

2323
import javax.xml.namespace.QName;
2424
import java.util.Collections;
25+
import java.util.Map;
2526

26-
public class UnknownStatementTranslator implements MuleComponentToSpringIntegrationDslTranslator {
27+
public class UnknownStatementTranslator implements MuleComponentToSpringIntegrationDslTranslator<Object> {
2728

2829
@Override
2930
public Class getSupportedMuleType() {
3031
return null;
3132
}
3233

33-
@Override
34-
public DslSnippet translate(Object component, QName qname, MuleConfigurations muleConfigurations, String flowName) {
34+
public DslSnippet translate(Object component, QName qname, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
3535
return new DslSnippet(
3636
generateDSLStatement(component, qname),
3737
Collections.emptySet(),

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/amqp/AmqConnectorTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import javax.xml.namespace.QName;
2525
import java.util.Collections;
26+
import java.util.Map;
2627
import java.util.Set;
2728

2829
/**
@@ -40,7 +41,7 @@ public Class getSupportedMuleType() {
4041
public DslSnippet translate(AmqpConnectorType component,
4142
QName name,
4243
MuleConfigurations muleConfigurations,
43-
String flowName) {
44+
String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
4445
return new DslSnippet("// FIXME: <amq:connector/> cannot be translated yet.", Collections.emptySet(), Set.of(), Collections.emptySet());
4546
}
4647
}

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/amqp/AmqpInboundEndpointTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.stereotype.Component;
2424

2525
import javax.xml.namespace.QName;
26+
import java.util.Map;
2627
import java.util.Set;
2728

2829
/***/
@@ -37,7 +38,7 @@ public Class getSupportedMuleType() {
3738
}
3839

3940
@Override
40-
public DslSnippet translate(InboundEndpointType inboundEndpointType, QName name, MuleConfigurations muleConfigurations, String flowName) {
41+
public DslSnippet translate(InboundEndpointType inboundEndpointType, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
4142
String queueName = inboundEndpointType.getQueueName();
4243
String renderedSnippet = snippetTemplate.replace("${queueName}", queueName);
4344
Bean amqpConnectionFactoryBean = new Bean("connectionFactory", "org.springframework.amqp.rabbit.connection.ConnectionFactory");

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/amqp/AmqpOutboundEndpointTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.stereotype.Component;
2424

2525
import javax.xml.namespace.QName;
26+
import java.util.Map;
2627
import java.util.Set;
2728

2829
@Component
@@ -36,7 +37,7 @@ public Class<OutboundEndpointType> getSupportedMuleType() {
3637
}
3738

3839
@Override
39-
public DslSnippet translate(OutboundEndpointType component, QName name, MuleConfigurations muleConfigurations, String flowName) {
40+
public DslSnippet translate(OutboundEndpointType component, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
4041

4142

4243
addExchange(snippet, component.getExchangeName());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm.mule.actions.javadsl.translators.core;
17+
18+
import org.apache.commons.lang3.tuple.ImmutablePair;
19+
import org.mulesoft.schema.mule.core.SelectiveOutboundRouterType;
20+
import org.springframework.sbm.mule.actions.javadsl.translators.Bean;
21+
import org.springframework.sbm.mule.actions.javadsl.translators.DslSnippet;
22+
import org.springframework.sbm.mule.actions.javadsl.translators.MuleComponentToSpringIntegrationDslTranslator;
23+
import org.springframework.sbm.mule.api.toplevel.ChoiceTopLevelElement;
24+
import org.springframework.sbm.mule.api.toplevel.configuration.MuleConfigurations;
25+
import org.springframework.stereotype.Component;
26+
27+
import javax.xml.namespace.QName;
28+
import java.util.*;
29+
import java.util.stream.Collectors;
30+
31+
@Component
32+
public class ChoiceTranslator implements MuleComponentToSpringIntegrationDslTranslator<SelectiveOutboundRouterType> {
33+
34+
@Override
35+
public Class<SelectiveOutboundRouterType> getSupportedMuleType() {
36+
return SelectiveOutboundRouterType.class;
37+
}
38+
39+
private final static String subflowTemplate =
40+
" .subFlowMapping(\"dataValue\" /*TODO: Translate dataValue to $TRANSLATE_EXPRESSION*/,\n" +
41+
" $SUBFLOW_CONTENT\n" +
42+
" )\n";
43+
44+
private final static String defaultSubflowMapping =
45+
" .defaultSubFlowMapping($OTHERWISE_STATEMENTS)\n";
46+
47+
@Override
48+
public DslSnippet translate(SelectiveOutboundRouterType component,
49+
QName name,
50+
MuleConfigurations muleConfigurations,
51+
String flowName,
52+
Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
53+
54+
List<ImmutablePair<String, ChoiceTopLevelElement>> whenStatements = component
55+
.getWhen()
56+
.stream()
57+
.map(item -> new ImmutablePair<>(item.getExpression(), new ChoiceTopLevelElement(
58+
flowName,
59+
item.getMessageProcessorOrOutboundEndpoint(),
60+
muleConfigurations,
61+
translatorsMap)))
62+
.collect(Collectors.toList());
63+
64+
String otherwiseSubflowMappings = "";
65+
66+
if (component.getOtherwise() != null) {
67+
ChoiceTopLevelElement otherWiseStatement = new ChoiceTopLevelElement(
68+
flowName,
69+
component.getOtherwise().getMessageProcessorOrOutboundEndpoint(),
70+
muleConfigurations,
71+
translatorsMap);
72+
73+
otherwiseSubflowMappings = defaultSubflowMapping.replace(
74+
"$OTHERWISE_STATEMENTS",
75+
otherWiseStatement.renderDslSnippet()
76+
);
77+
78+
otherwiseSubflowMappings = " .resolutionRequired(false)\n" +
79+
otherwiseSubflowMappings;
80+
}
81+
82+
String whenSubflowMappings = whenStatements
83+
.stream()
84+
.map(item ->
85+
subflowTemplate
86+
.replace("$TRANSLATE_EXPRESSION", item.getLeft())
87+
.replace("$SUBFLOW_CONTENT", item.getValue().renderDslSnippet())
88+
)
89+
.collect(Collectors.joining());
90+
91+
92+
Set<String> requiredImports = whenStatements.stream()
93+
.map(item -> item.getValue().getRequiredImports())
94+
.flatMap(Collection::stream)
95+
.collect(Collectors.toSet());
96+
97+
requiredImports.add("org.springframework.util.LinkedMultiValueMap");
98+
99+
Set<Bean> requiredBeans = whenStatements.stream()
100+
.map(item -> item.getValue().getDslSnippets())
101+
.flatMap(Collection::stream)
102+
.map(DslSnippet::getBeans)
103+
.flatMap(Collection::stream)
104+
.collect(Collectors.toSet());
105+
106+
Set<String> requiredDependencies = whenStatements.stream()
107+
.map(item -> item.getValue().getRequiredDependencies())
108+
.flatMap(Collection::stream)
109+
.collect(Collectors.toSet());
110+
111+
return new DslSnippet(
112+
"/* TODO: LinkedMultiValueMap might not be apt, substitute with right input type*/\n" +
113+
" .<LinkedMultiValueMap<String, String>, String>route(\n" +
114+
" p -> p.getFirst(\"dataKey\") /*TODO: use apt condition*/,\n" +
115+
" m -> m\n" +
116+
whenSubflowMappings +
117+
otherwiseSubflowMappings +
118+
" )",
119+
requiredImports,
120+
requiredDependencies,
121+
requiredBeans
122+
);
123+
}
124+
}

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/core/FlowRefTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import javax.xml.namespace.QName;
2828
import java.util.Collections;
29+
import java.util.Map;
2930
import java.util.Set;
3031

3132
/**
@@ -46,7 +47,7 @@ public Class getSupportedMuleType() {
4647
}
4748

4849
@Override
49-
public DslSnippet translate(FlowRef component, QName name, MuleConfigurations muleConfigurations, String flowName) {
50+
public DslSnippet translate(FlowRef component, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
5051
String subFlowName = Helper.sanitizeForBeanMethodName(translateToJavaName(component.getName()));
5152
String flowRefSnippet = javaDslFlowRefTemplate.replace(SUBFLOW_NAME, subFlowName);
5253
return new DslSnippet(flowRefSnippet,

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/core/SetPayloadTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import javax.xml.namespace.QName;
2525
import java.util.Collections;
26+
import java.util.Map;
2627

2728
@Component
2829
public class SetPayloadTranslator implements MuleComponentToSpringIntegrationDslTranslator<SetPayloadTransformerType> {
@@ -32,7 +33,7 @@ public Class<SetPayloadTransformerType> getSupportedMuleType() {
3233
}
3334

3435
@Override
35-
public DslSnippet translate(SetPayloadTransformerType component, QName name, MuleConfigurations muleConfigurations, String flowName) {
36+
public DslSnippet translate(SetPayloadTransformerType component, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
3637
String valueWithoutNewLines = component.getValue().replace("\n", "");
3738
String withEscapedChars = valueWithoutNewLines.replace("\"", "\\\"");
3839
return new DslSnippet(".handle((p, h) -> " + "\"" + withEscapedChars + "\")", Collections.emptySet());

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/core/SetPropertyTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import javax.xml.namespace.QName;
2626
import java.util.Collections;
27+
import java.util.Map;
2728

2829
@Component
2930
public class SetPropertyTranslator implements MuleComponentToSpringIntegrationDslTranslator<SetPropertyType> {
@@ -33,7 +34,7 @@ public Class<SetPropertyType> getSupportedMuleType() {
3334
}
3435

3536
@Override
36-
public DslSnippet translate(SetPropertyType component, QName name, MuleConfigurations muleConfigurations, String flowName) {
37+
public DslSnippet translate(SetPropertyType component, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
3738
return new DslSnippet(
3839
".enrichHeaders(h -> h.header(\"" + component.getPropertyName() + "\", \"" + component.getValue() + "\"))",
3940
Collections.emptySet()

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/core/TransformerTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.stereotype.Component;
2424

2525
import javax.xml.namespace.QName;
26+
import java.util.Map;
2627
import java.util.Set;
2728

2829
@Component
@@ -33,7 +34,7 @@ public Class<AbstractTransformerType> getSupportedMuleType() {
3334
}
3435

3536
@Override
36-
public DslSnippet translate(AbstractTransformerType component, QName name, MuleConfigurations muleConfigurations, String flowName) {
37+
public DslSnippet translate(AbstractTransformerType component, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
3738
if (name.getLocalPart().equals("byte-array-to-string-transformer")) {
3839
return new DslSnippet(".transform(new ObjectToStringTransformer())", Set.of("org.springframework.integration.transformer.ObjectToStringTransformer"));
3940
} else {

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/dwl/DwlTransformTranslator.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import javax.xml.namespace.QName;
2727
import java.util.Collections;
28+
import java.util.Map;
2829
import java.util.Set;
2930

3031
@Component
@@ -55,7 +56,8 @@ public DslSnippet translate(
5556
TransformMessageType component,
5657
QName name,
5758
MuleConfigurations muleConfigurations,
58-
String flowName
59+
String flowName,
60+
Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap
5961
) {
6062

6163
if (component.getSetPayload() != null) {

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/http/HttpListenerTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import javax.xml.namespace.QName;
2626
import java.util.Collections;
27+
import java.util.Map;
2728
import java.util.Set;
2829

2930
/**
@@ -44,7 +45,7 @@ public Class<ListenerType> getSupportedMuleType() {
4445
}
4546

4647
@Override
47-
public DslSnippet translate(ListenerType component, QName name, MuleConfigurations muleConfigurations, String flowName) {
48+
public DslSnippet translate(ListenerType component, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
4849
/*
4950
* In the connector component on your flow, the only required fields are the Path
5051
* (the path-absolute URL defining the resource location), which by default is /,

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/http/HttpRequestTranslator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Class getSupportedMuleType() {
5454
public DslSnippet translate(RequestType component,
5555
QName name,
5656
MuleConfigurations muleConfigurations,
57-
String flowName) {
57+
String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
5858

5959
RequestConfigType config = getRequestConfiguration(component, muleConfigurations);
6060
return new DslSnippet(

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/logging/LoggingTranslator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Class getSupportedMuleType() {
4646
return LoggerType.class;
4747
}
4848

49-
public DslSnippet translate(LoggerType loggerType, QName name, MuleConfigurations muleConfigurations, String flowName) {
49+
public DslSnippet translate(LoggerType loggerType, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
5050

5151
StringBuffer sb = new StringBuffer();
5252

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/wmq/WmqInboundEndpointTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.stereotype.Component;
2424

2525
import javax.xml.namespace.QName;
26+
import java.util.Map;
2627
import java.util.Set;
2728

2829
@Component
@@ -33,7 +34,7 @@ public Class<InboundEndpointType> getSupportedMuleType() {
3334
}
3435

3536
@Override
36-
public DslSnippet translate(InboundEndpointType component, QName name, MuleConfigurations muleConfigurations, String flowName) {
37+
public DslSnippet translate(InboundEndpointType component, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
3738
return new DslSnippet(
3839
"return IntegrationFlows.from(Jms.inboundAdapter(connectionFactory).destination(\"" + component.getQueue() +"\")).handle((p, h) -> p)",
3940
Set.of("org.springframework.integration.jms.dsl.Jms"),

components/sbm-recipes-mule-to-boot/src/main/java/org/springframework/sbm/mule/actions/javadsl/translators/wmq/WmqOutboundEndpointTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.stereotype.Component;
2424

2525
import javax.xml.namespace.QName;
26+
import java.util.Map;
2627
import java.util.Set;
2728

2829
@Component
@@ -33,7 +34,7 @@ public Class<OutboundEndpointType> getSupportedMuleType() {
3334
}
3435

3536
@Override
36-
public DslSnippet translate(OutboundEndpointType component, QName name, MuleConfigurations muleConfigurations, String flowName) {
37+
public DslSnippet translate(OutboundEndpointType component, QName name, MuleConfigurations muleConfigurations, String flowName, Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
3738
return new DslSnippet(
3839
".handle(Jms.outboundAdapter(connectionFactory).destination(\"" +component.getQueue()+"\"))",
3940
Set.of("org.springframework.integration.jms.dsl.Jms"),

0 commit comments

Comments
 (0)