|
| 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 | +} |
0 commit comments