Skip to content

140 db insert #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 7, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,6 @@ public DslSnippet translate(int id, SelectiveOutboundRouterType component,
translatorsMap)))
.collect(Collectors.toList());

String otherwiseSubflowMappings = "";

if (component.getOtherwise() != null) {
ChoiceTopLevelElement otherWiseStatement = new ChoiceTopLevelElement(
flowName,
component.getOtherwise().getMessageProcessorOrOutboundEndpoint(),
muleConfigurations,
translatorsMap);

otherwiseSubflowMappings = defaultSubflowMapping.replace(
"$OTHERWISE_STATEMENTS",
otherWiseStatement.renderDslSnippet()
);

otherwiseSubflowMappings = " .resolutionRequired(false)\n" +
otherwiseSubflowMappings;
}

String whenSubflowMappings = whenStatements
.stream()
.map(item ->
Expand All @@ -88,26 +70,54 @@ public DslSnippet translate(int id, SelectiveOutboundRouterType component,
)
.collect(Collectors.joining());

List<DslSnippet> whenStatementDslSnippets = whenStatements
.stream()
.map(item -> DslSnippet.createDSLSnippetFromTopLevelElement(item.getValue()))
.collect(Collectors.toList());

Set<String> requiredImports = whenStatements.stream()
.map(item -> item.getValue().getRequiredImports())
Set<String> requiredImports = whenStatementDslSnippets
.stream()
.map(DslSnippet::getRequiredImports)
.flatMap(Collection::stream)
.collect(Collectors.toSet());


requiredImports.add("org.springframework.util.LinkedMultiValueMap");

Set<Bean> requiredBeans = whenStatements.stream()
.map(item -> item.getValue().getDslSnippets())
.flatMap(Collection::stream)
Set<Bean> requiredBeans = whenStatementDslSnippets.stream()
.map(DslSnippet::getBeans)
.flatMap(Collection::stream)
.collect(Collectors.toSet());

Set<String> requiredDependencies = whenStatements.stream()
.map(item -> item.getValue().getRequiredDependencies())
Set<String> requiredDependencies = whenStatementDslSnippets.stream()
.map(DslSnippet::getRequiredDependencies)
.flatMap(Collection::stream)
.collect(Collectors.toSet());

String otherwiseSubflowMappings = "";

if (component.getOtherwise() != null) {
ChoiceTopLevelElement otherWiseStatement = new ChoiceTopLevelElement(
flowName,
component.getOtherwise().getMessageProcessorOrOutboundEndpoint(),
muleConfigurations,
translatorsMap);

DslSnippet otherWiseDSLSnippet = DslSnippet.createDSLSnippetFromTopLevelElement(otherWiseStatement);

requiredImports.addAll(otherWiseDSLSnippet.getRequiredImports());
requiredDependencies.addAll(otherWiseDSLSnippet.getRequiredDependencies());
requiredBeans.addAll(otherWiseDSLSnippet.getBeans());

otherwiseSubflowMappings = defaultSubflowMapping.replace(
"$OTHERWISE_STATEMENTS",
otherWiseStatement.renderDslSnippet()
);

otherwiseSubflowMappings = " .resolutionRequired(false)\n" +
otherwiseSubflowMappings;
}

return DslSnippet.builder()
.renderedSnippet("/* TODO: LinkedMultiValueMap might not be apt, substitute with right input type*/\n" +
" .<LinkedMultiValueMap<String, String>, String>route(\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.mule.actions.javadsl.translators.db;

public class DBCommons {
public static String escapeDoubleQuotes(String str) {
return str.replace("\"", "\\\"");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.mule.actions.javadsl.translators.db;

import org.mulesoft.schema.mule.db.InsertMessageProcessorType;
import org.springframework.sbm.mule.actions.javadsl.translators.Bean;
import org.springframework.sbm.mule.actions.javadsl.translators.DslSnippet;
import org.springframework.sbm.mule.actions.javadsl.translators.MuleComponentToSpringIntegrationDslTranslator;
import org.springframework.sbm.mule.api.toplevel.configuration.MuleConfigurations;
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;
import java.util.Map;
import java.util.Set;

@Component
public class InsertTranslator implements MuleComponentToSpringIntegrationDslTranslator<InsertMessageProcessorType> {
@Override
public Class<InsertMessageProcessorType> getSupportedMuleType() {
return InsertMessageProcessorType.class;
}

@Override
public DslSnippet translate(int id,
InsertMessageProcessorType component,
QName name,
MuleConfigurations muleConfigurations,
String flowName,
Map<Class, MuleComponentToSpringIntegrationDslTranslator> translatorsMap) {
return DslSnippet.builder()
.renderedSnippet(
" // TODO: payload type might not be always LinkedMultiValueMap please change it to appropriate type \n" +
" // TODO: mule expression language is not converted to java, do it manually. example: #[payload] etc \n" +
" .<LinkedMultiValueMap<String, String>>handle((p, h) -> {\n" +
" jdbcTemplate.execute(\"" + DBCommons.escapeDoubleQuotes(component.getParameterizedQuery()) + "\");\n" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevent SQL injection?
Mule db:parameterized-query should result in a PreparedStatement

" return p;\n" +
" })")
.requiredImports(Set.of(
"org.springframework.util.LinkedMultiValueMap",
"org.springframework.jdbc.core.JdbcTemplate"
))
.requiredDependencies(Set.of(
"org.springframework.boot:spring-boot-starter-jdbc:2.5.5",
"org.springframework.integration:spring-integration-jdbc:5.5.4"
))
.beans(Set.of(new Bean("jdbcTemplate", "org.springframework.jdbc.core.JdbcTemplate")))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import static org.springframework.sbm.mule.actions.javadsl.translators.db.DBCommons.escapeDoubleQuotes;

@Component
public class SelectTranslator implements MuleComponentToSpringIntegrationDslTranslator<SelectMessageProcessorType> {

Expand Down Expand Up @@ -56,11 +57,14 @@ public DslSnippet translate(int id, SelectMessageProcessorType component,
"org.springframework.boot:spring-boot-starter-jdbc:2.5.5",
"org.springframework.integration:spring-integration-jdbc:5.5.4"
))
.beans(Set.of(new Bean("jdbcTemplate", "org.springframework.jdbc.core.JdbcTemplate")))
.beans(
Set.of(
new Bean(
"jdbcTemplate",
"org.springframework.jdbc.core.JdbcTemplate"
)
)
)
.build();
}

private String escapeDoubleQuotes(String str) {
return str.replace("\"", "\\\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.sbm.mule.actions.javadsl.translators.amqp.AmqpOutboundEndpointTranslator;
import org.springframework.sbm.mule.actions.javadsl.translators.common.ExpressionLanguageTranslator;
import org.springframework.sbm.mule.actions.javadsl.translators.core.*;
import org.springframework.sbm.mule.actions.javadsl.translators.db.InsertTranslator;
import org.springframework.sbm.mule.actions.javadsl.translators.db.SelectTranslator;
import org.springframework.sbm.mule.actions.javadsl.translators.dwl.DwlTransformTranslator;
import org.springframework.sbm.mule.actions.javadsl.translators.http.HttpListenerConfigTypeAdapter;
Expand Down Expand Up @@ -78,7 +79,8 @@ public void setup() {
new ChoiceTranslator(),
new SelectTranslator(),
new ForeachTranslator(),
new TransactionalTranslator()
new TransactionalTranslator(),
new InsertTranslator()
);

List<TopLevelElementFactory> topLevelTypeFactories = List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,77 @@ public void nestedChoiceDoesNotError() {
addXMLFileToResource(xmlNestedChoice);
runAction();
}

@Test
public void otherwiseStatementShouldDoImportsBeansAndDependencies() {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<mule xmlns:json=\"http://www.mulesoft.org/schema/mule/json\" xmlns:db=\"http://www.mulesoft.org/schema/mule/db\"\n" +
" xmlns:dw=\"http://www.mulesoft.org/schema/mule/ee/dw\"\n" +
" 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" +
" xmlns:spring=\"http://www.springframework.org/schema/beans\" \n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd\n" +
"http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd\n" +
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd\n" +
"http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd\n" +
"http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd\n" +
"http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd\">\n" +
" <flow name=\"post:/insert:application/json:cmb-hsbcnet-ss-sa-entitlement-change-request-config\">\n" +
" <choice doc:name=\"Choice\">\n" +
" <when expression=\"#[payload == null || payload.size() == 0]\">\n" +
" <logger message=\"empty details list: change request id #[flowVars.changeRequestId]\" level=\"DEBUG\" doc:name=\"empty details list\"/>\n" +
" </when>\n" +
" <otherwise>\n" +
" <logger message=\"insert details: change request id #[flowVars.changeRequestId]\" level=\"DEBUG\" doc:name=\"insert details\"/>\n" +
" <db:insert config-ref=\"Oracle_Configuration\" bulkMode=\"true\" doc:name=\"Database\">\n" +
" <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" +
" </db:insert>\n" +
" </otherwise>\n" +
" </choice>\n" +
" </flow>\n" +
"</mule>\n";

addXMLFileToResource(xml);
runAction();

assertThat(getGeneratedJavaFile()).isEqualTo(
"package com.example.javadsl;\n" +
"import org.springframework.context.annotation.Bean;\n" +
"import org.springframework.context.annotation.Configuration;\n" +
"import org.springframework.http.HttpMethod;\n" +
"import org.springframework.integration.dsl.IntegrationFlow;\n" +
"import org.springframework.integration.dsl.IntegrationFlows;\n" +
"import org.springframework.integration.handler.LoggingHandler;\n" +
"import org.springframework.integration.http.dsl.Http;\n" +
"import org.springframework.jdbc.core.JdbcTemplate;\n" +
"import org.springframework.util.LinkedMultiValueMap;\n" +
"\n" +
"@Configuration\n" +
"public class FlowConfigurations {\n" +
" @Bean\n" +
" IntegrationFlow post__insert_application_json_cmb_hsbcnet_ss_sa_entitlement_change_request_config(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate) {\n" +
" // 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" +
" // 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" +
" // FIXME: remove the JavaDSL generated method containing apikit:router with config-ref=\"cmb-hsbcnet-ss-sa-entitlement-change-request-config\"\n" +
" return IntegrationFlows.from(\n" +
" Http.inboundGateway(\"/insert\").requestMapping(r -> r.methods(HttpMethod.POST)))\n" +
" /* TODO: LinkedMultiValueMap might not be apt, substitute with right input type*/\n" +
" .<LinkedMultiValueMap<String, String>, String>route(\n" +
" p -> p.getFirst(\"dataKey\") /*TODO: use apt condition*/,\n" +
" m -> m\n" +
" .subFlowMapping(\"dataValue\" /*TODO: Translate dataValue to #[payload == null || payload.size() == 0]*/,\n" +
" sf -> sf.log(LoggingHandler.Level.DEBUG, \"empty details list: change request id ${flowVars.changeRequestId}\")\n" +
" )\n" +
" .resolutionRequired(false)\n" +
" .defaultSubFlowMapping(sf -> sf.log(LoggingHandler.Level.DEBUG, \"insert details: change request id ${flowVars.changeRequestId}\")\n" +
" // TODO: payload type might not be always LinkedMultiValueMap please change it to appropriate type \n" +
" // TODO: mule expression language is not converted to java, do it manually. example: #[payload] etc \n" +
" .<LinkedMultiValueMap<String, String>>handle((p, h) -> {\n" +
" 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" +
" return p;\n" +
" }))\n" +
" )\n" +
" .get();\n" +
" }}");
}
}
Loading