Skip to content

Commit c59554c

Browse files
Closes db insert component (#140)
1 parent e7fe085 commit c59554c

File tree

9 files changed

+284
-35
lines changed

9 files changed

+284
-35
lines changed

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

+35-25
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,6 @@ public DslSnippet translate(int id, SelectiveOutboundRouterType component,
6161
translatorsMap)))
6262
.collect(Collectors.toList());
6363

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-
8264
String whenSubflowMappings = whenStatements
8365
.stream()
8466
.map(item ->
@@ -88,26 +70,54 @@ public DslSnippet translate(int id, SelectiveOutboundRouterType component,
8870
)
8971
.collect(Collectors.joining());
9072

73+
List<DslSnippet> whenStatementDslSnippets = whenStatements
74+
.stream()
75+
.map(item -> DslSnippet.createDSLSnippetFromTopLevelElement(item.getValue()))
76+
.collect(Collectors.toList());
9177

92-
Set<String> requiredImports = whenStatements.stream()
93-
.map(item -> item.getValue().getRequiredImports())
78+
Set<String> requiredImports = whenStatementDslSnippets
79+
.stream()
80+
.map(DslSnippet::getRequiredImports)
9481
.flatMap(Collection::stream)
9582
.collect(Collectors.toSet());
9683

84+
9785
requiredImports.add("org.springframework.util.LinkedMultiValueMap");
9886

99-
Set<Bean> requiredBeans = whenStatements.stream()
100-
.map(item -> item.getValue().getDslSnippets())
101-
.flatMap(Collection::stream)
87+
Set<Bean> requiredBeans = whenStatementDslSnippets.stream()
10288
.map(DslSnippet::getBeans)
10389
.flatMap(Collection::stream)
10490
.collect(Collectors.toSet());
10591

106-
Set<String> requiredDependencies = whenStatements.stream()
107-
.map(item -> item.getValue().getRequiredDependencies())
92+
Set<String> requiredDependencies = whenStatementDslSnippets.stream()
93+
.map(DslSnippet::getRequiredDependencies)
10894
.flatMap(Collection::stream)
10995
.collect(Collectors.toSet());
11096

97+
String otherwiseSubflowMappings = "";
98+
99+
if (component.getOtherwise() != null) {
100+
ChoiceTopLevelElement otherWiseStatement = new ChoiceTopLevelElement(
101+
flowName,
102+
component.getOtherwise().getMessageProcessorOrOutboundEndpoint(),
103+
muleConfigurations,
104+
translatorsMap);
105+
106+
DslSnippet otherWiseDSLSnippet = DslSnippet.createDSLSnippetFromTopLevelElement(otherWiseStatement);
107+
108+
requiredImports.addAll(otherWiseDSLSnippet.getRequiredImports());
109+
requiredDependencies.addAll(otherWiseDSLSnippet.getRequiredDependencies());
110+
requiredBeans.addAll(otherWiseDSLSnippet.getBeans());
111+
112+
otherwiseSubflowMappings = defaultSubflowMapping.replace(
113+
"$OTHERWISE_STATEMENTS",
114+
otherWiseStatement.renderDslSnippet()
115+
);
116+
117+
otherwiseSubflowMappings = " .resolutionRequired(false)\n" +
118+
otherwiseSubflowMappings;
119+
}
120+
111121
return DslSnippet.builder()
112122
.renderedSnippet("/* TODO: LinkedMultiValueMap might not be apt, substitute with right input type*/\n" +
113123
" .<LinkedMultiValueMap<String, String>, String>route(\n" +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.db;
17+
18+
public class DBCommons {
19+
public static String escapeDoubleQuotes(String str) {
20+
return str.replace("\"", "\\\"");
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.db;
17+
18+
import org.mulesoft.schema.mule.db.InsertMessageProcessorType;
19+
import org.springframework.sbm.mule.actions.javadsl.translators.Bean;
20+
import org.springframework.sbm.mule.actions.javadsl.translators.DslSnippet;
21+
import org.springframework.sbm.mule.actions.javadsl.translators.MuleComponentToSpringIntegrationDslTranslator;
22+
import org.springframework.sbm.mule.api.toplevel.configuration.MuleConfigurations;
23+
import org.springframework.stereotype.Component;
24+
25+
import javax.xml.namespace.QName;
26+
import java.util.Map;
27+
import java.util.Set;
28+
29+
@Component
30+
public class InsertTranslator implements MuleComponentToSpringIntegrationDslTranslator<InsertMessageProcessorType> {
31+
@Override
32+
public Class<InsertMessageProcessorType> getSupportedMuleType() {
33+
return InsertMessageProcessorType.class;
34+
}
35+
36+
@Override
37+
public DslSnippet translate(int id,
38+
InsertMessageProcessorType component,
39+
QName name,
40+
MuleConfigurations muleConfigurations,
41+
String flowName,
42+
Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
43+
return DslSnippet.builder()
44+
.renderedSnippet(
45+
" // TODO: payload type might not be always LinkedMultiValueMap please change it to appropriate type \n" +
46+
" // TODO: mule expression language is not converted to java, do it manually. example: #[payload] etc \n" +
47+
" .<LinkedMultiValueMap<String, String>>handle((p, h) -> {\n" +
48+
" jdbcTemplate.execute(\"" + DBCommons.escapeDoubleQuotes(component.getParameterizedQuery()) + "\");\n" +
49+
" return p;\n" +
50+
" })")
51+
.requiredImports(Set.of(
52+
"org.springframework.util.LinkedMultiValueMap",
53+
"org.springframework.jdbc.core.JdbcTemplate"
54+
))
55+
.requiredDependencies(Set.of(
56+
"org.springframework.boot:spring-boot-starter-jdbc:2.5.5",
57+
"org.springframework.integration:spring-integration-jdbc:5.5.4"
58+
))
59+
.beans(Set.of(new Bean("jdbcTemplate", "org.springframework.jdbc.core.JdbcTemplate")))
60+
.build();
61+
}
62+
}

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

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

2525
import javax.xml.namespace.QName;
26-
import java.util.Collections;
2726
import java.util.Map;
2827
import java.util.Set;
2928

29+
import static org.springframework.sbm.mule.actions.javadsl.translators.db.DBCommons.escapeDoubleQuotes;
30+
3031
@Component
3132
public class SelectTranslator implements MuleComponentToSpringIntegrationDslTranslator<SelectMessageProcessorType> {
3233

@@ -56,11 +57,14 @@ public DslSnippet translate(int id, SelectMessageProcessorType component,
5657
"org.springframework.boot:spring-boot-starter-jdbc:2.5.5",
5758
"org.springframework.integration:spring-integration-jdbc:5.5.4"
5859
))
59-
.beans(Set.of(new Bean("jdbcTemplate", "org.springframework.jdbc.core.JdbcTemplate")))
60+
.beans(
61+
Set.of(
62+
new Bean(
63+
"jdbcTemplate",
64+
"org.springframework.jdbc.core.JdbcTemplate"
65+
)
66+
)
67+
)
6068
.build();
6169
}
62-
63-
private String escapeDoubleQuotes(String str) {
64-
return str.replace("\"", "\\\"");
65-
}
6670
}

components/sbm-recipes-mule-to-boot/src/test/java/org/springframework/sbm/mule/actions/JavaDSLActionBaseTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.sbm.mule.actions.javadsl.translators.amqp.AmqpOutboundEndpointTranslator;
2525
import org.springframework.sbm.mule.actions.javadsl.translators.common.ExpressionLanguageTranslator;
2626
import org.springframework.sbm.mule.actions.javadsl.translators.core.*;
27+
import org.springframework.sbm.mule.actions.javadsl.translators.db.InsertTranslator;
2728
import org.springframework.sbm.mule.actions.javadsl.translators.db.SelectTranslator;
2829
import org.springframework.sbm.mule.actions.javadsl.translators.dwl.DwlTransformTranslator;
2930
import org.springframework.sbm.mule.actions.javadsl.translators.http.HttpListenerConfigTypeAdapter;
@@ -78,7 +79,8 @@ public void setup() {
7879
new ChoiceTranslator(),
7980
new SelectTranslator(),
8081
new ForeachTranslator(),
81-
new TransactionalTranslator()
82+
new TransactionalTranslator(),
83+
new InsertTranslator()
8284
);
8385

8486
List<TopLevelElementFactory> topLevelTypeFactories = List.of(

components/sbm-recipes-mule-to-boot/src/test/java/org/springframework/sbm/mule/actions/MuleToJavaDSLChoiceTest.java

+73
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,77 @@ public void nestedChoiceDoesNotError() {
300300
addXMLFileToResource(xmlNestedChoice);
301301
runAction();
302302
}
303+
304+
@Test
305+
public void otherwiseStatementShouldDoImportsBeansAndDependencies() {
306+
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
307+
"<mule xmlns:json=\"http://www.mulesoft.org/schema/mule/json\" xmlns:db=\"http://www.mulesoft.org/schema/mule/db\"\n" +
308+
" xmlns:dw=\"http://www.mulesoft.org/schema/mule/ee/dw\"\n" +
309+
" xmlns:tracking=\"http://www.mulesoft.org/schema/mule/ee/tracking\" xmlns=\"http://www.mulesoft.org/schema/mule/core\" xmlns:doc=\"http://www.mulesoft.org/schema/mule/documentation\"\n" +
310+
" xmlns:spring=\"http://www.springframework.org/schema/beans\" \n" +
311+
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
312+
" xsi:schemaLocation=\"http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd\n" +
313+
"http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd\n" +
314+
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd\n" +
315+
"http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd\n" +
316+
"http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd\n" +
317+
"http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd\">\n" +
318+
" <flow name=\"post:/insert:application/json:cmb-hsbcnet-ss-sa-entitlement-change-request-config\">\n" +
319+
" <choice doc:name=\"Choice\">\n" +
320+
" <when expression=\"#[payload == null || payload.size() == 0]\">\n" +
321+
" <logger message=\"empty details list: change request id #[flowVars.changeRequestId]\" level=\"DEBUG\" doc:name=\"empty details list\"/>\n" +
322+
" </when>\n" +
323+
" <otherwise>\n" +
324+
" <logger message=\"insert details: change request id #[flowVars.changeRequestId]\" level=\"DEBUG\" doc:name=\"insert details\"/>\n" +
325+
" <db:insert config-ref=\"Oracle_Configuration\" bulkMode=\"true\" doc:name=\"Database\">\n" +
326+
" <db:parameterized-query><![CDATA[INSERT INTO ${ORA_SCHEMA}.CHANGE_REQUEST_DETAILS (CHANGE_REQUEST_ID, CR_ATTRIBUTE_ID, SECONDARY_ATTRIBUTE, OLD_VALUE, NEW_VALUE) VALUES (#[flowVars.changeRequestId], #[payload.crAttributeId], #[payload.secondaryAttribute], #[payload.oldValue], #[payload.newValue])]]></db:parameterized-query>\n" +
327+
" </db:insert>\n" +
328+
" </otherwise>\n" +
329+
" </choice>\n" +
330+
" </flow>\n" +
331+
"</mule>\n";
332+
333+
addXMLFileToResource(xml);
334+
runAction();
335+
336+
assertThat(getGeneratedJavaFile()).isEqualTo(
337+
"package com.example.javadsl;\n" +
338+
"import org.springframework.context.annotation.Bean;\n" +
339+
"import org.springframework.context.annotation.Configuration;\n" +
340+
"import org.springframework.http.HttpMethod;\n" +
341+
"import org.springframework.integration.dsl.IntegrationFlow;\n" +
342+
"import org.springframework.integration.dsl.IntegrationFlows;\n" +
343+
"import org.springframework.integration.handler.LoggingHandler;\n" +
344+
"import org.springframework.integration.http.dsl.Http;\n" +
345+
"import org.springframework.jdbc.core.JdbcTemplate;\n" +
346+
"import org.springframework.util.LinkedMultiValueMap;\n" +
347+
"\n" +
348+
"@Configuration\n" +
349+
"public class FlowConfigurations {\n" +
350+
" @Bean\n" +
351+
" IntegrationFlow post__insert_application_json_cmb_hsbcnet_ss_sa_entitlement_change_request_config(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate) {\n" +
352+
" // FIXME: the base path for Http.inboundGateway must be extracted from http:listener in flow containing apikit:router with config-ref=\"cmb-hsbcnet-ss-sa-entitlement-change-request-config\"\n" +
353+
" // FIXME: add all JavaDSL generated components between http:listener and apikit:router with config-ref=\"cmb-hsbcnet-ss-sa-entitlement-change-request-config\" into this flow\n" +
354+
" // FIXME: remove the JavaDSL generated method containing apikit:router with config-ref=\"cmb-hsbcnet-ss-sa-entitlement-change-request-config\"\n" +
355+
" return IntegrationFlows.from(\n" +
356+
" Http.inboundGateway(\"/insert\").requestMapping(r -> r.methods(HttpMethod.POST)))\n" +
357+
" /* TODO: LinkedMultiValueMap might not be apt, substitute with right input type*/\n" +
358+
" .<LinkedMultiValueMap<String, String>, String>route(\n" +
359+
" p -> p.getFirst(\"dataKey\") /*TODO: use apt condition*/,\n" +
360+
" m -> m\n" +
361+
" .subFlowMapping(\"dataValue\" /*TODO: Translate dataValue to #[payload == null || payload.size() == 0]*/,\n" +
362+
" sf -> sf.log(LoggingHandler.Level.DEBUG, \"empty details list: change request id ${flowVars.changeRequestId}\")\n" +
363+
" )\n" +
364+
" .resolutionRequired(false)\n" +
365+
" .defaultSubFlowMapping(sf -> sf.log(LoggingHandler.Level.DEBUG, \"insert details: change request id ${flowVars.changeRequestId}\")\n" +
366+
" // TODO: payload type might not be always LinkedMultiValueMap please change it to appropriate type \n" +
367+
" // TODO: mule expression language is not converted to java, do it manually. example: #[payload] etc \n" +
368+
" .<LinkedMultiValueMap<String, String>>handle((p, h) -> {\n" +
369+
" jdbcTemplate.execute(\"INSERT INTO ${ORA_SCHEMA}.CHANGE_REQUEST_DETAILS (CHANGE_REQUEST_ID, CR_ATTRIBUTE_ID, SECONDARY_ATTRIBUTE, OLD_VALUE, NEW_VALUE) VALUES (#[flowVars.changeRequestId], #[payload.crAttributeId], #[payload.secondaryAttribute], #[payload.oldValue], #[payload.newValue])\");\n" +
370+
" return p;\n" +
371+
" }))\n" +
372+
" )\n" +
373+
" .get();\n" +
374+
" }}");
375+
}
303376
}

0 commit comments

Comments
 (0)