|
| 1 | +/* |
| 2 | + * Copyright 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 | + |
| 17 | +package org.springframework.integration.cassandra.config.xml; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.w3c.dom.Element; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 24 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 25 | +import org.springframework.beans.factory.support.ManagedMap; |
| 26 | +import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; |
| 27 | +import org.springframework.beans.factory.xml.ParserContext; |
| 28 | +import org.springframework.integration.config.xml.IntegrationNamespaceUtils; |
| 29 | +import org.springframework.util.CollectionUtils; |
| 30 | +import org.springframework.util.StringUtils; |
| 31 | +import org.springframework.util.xml.DomUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * The {@code int-cassandra} namespace XML parser helper. |
| 35 | + * |
| 36 | + * @author Filippo Balicchia |
| 37 | + * @author Artem Bilan |
| 38 | + * |
| 39 | + * @since 6.0 |
| 40 | + */ |
| 41 | +public final class CassandraParserUtils { |
| 42 | + |
| 43 | + public static void processOutboundTypeAttributes(Element element, ParserContext parserContext, |
| 44 | + BeanDefinitionBuilder builder) { |
| 45 | + |
| 46 | + String cassandraTemplate = element.getAttribute("cassandra-template"); |
| 47 | + String mode = element.getAttribute("mode"); |
| 48 | + String ingestQuery = element.getAttribute("ingest-query"); |
| 49 | + String query = element.getAttribute("query"); |
| 50 | + |
| 51 | + if (!StringUtils.hasText(cassandraTemplate)) { |
| 52 | + parserContext.getReaderContext().error("cassandra-template is required", element); |
| 53 | + } |
| 54 | + |
| 55 | + builder.addConstructorArgReference(cassandraTemplate); |
| 56 | + if (StringUtils.hasText(mode)) { |
| 57 | + builder.addConstructorArgValue(mode); |
| 58 | + } |
| 59 | + |
| 60 | + BeanDefinition statementExpressionDef = IntegrationNamespaceUtils |
| 61 | + .createExpressionDefIfAttributeDefined("statement-expression", element); |
| 62 | + |
| 63 | + if (statementExpressionDef != null) { |
| 64 | + builder.addPropertyValue("statementExpression", statementExpressionDef); |
| 65 | + } |
| 66 | + |
| 67 | + if (!areMutuallyExclusive(query, statementExpressionDef, ingestQuery)) { |
| 68 | + parserContext.getReaderContext() |
| 69 | + .error("'query', 'ingest-query', 'statement-expression' are mutually exclusive", element); |
| 70 | + } |
| 71 | + |
| 72 | + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "write-options"); |
| 73 | + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ingest-query"); |
| 74 | + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "query"); |
| 75 | + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "async"); |
| 76 | + |
| 77 | + List<Element> parameterExpressions = DomUtils.getChildElementsByTagName(element, "parameter-expression"); |
| 78 | + if (!CollectionUtils.isEmpty(parameterExpressions)) { |
| 79 | + ManagedMap<String, Object> parameterExpressionsMap = new ManagedMap<>(); |
| 80 | + for (Element parameterExpressionElement : parameterExpressions) { |
| 81 | + String name = parameterExpressionElement.getAttribute(AbstractBeanDefinitionParser.NAME_ATTRIBUTE); |
| 82 | + BeanDefinition expression = |
| 83 | + IntegrationNamespaceUtils.createExpressionDefIfAttributeDefined( |
| 84 | + IntegrationNamespaceUtils.EXPRESSION_ATTRIBUTE, parameterExpressionElement); |
| 85 | + if (expression != null) { |
| 86 | + parameterExpressionsMap.put(name, expression); |
| 87 | + } |
| 88 | + } |
| 89 | + builder.addPropertyValue("parameterExpressions", parameterExpressionsMap); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + public static boolean areMutuallyExclusive(String query, BeanDefinition statementExpressionDef, |
| 95 | + String ingestQuery) { |
| 96 | + |
| 97 | + return !StringUtils.hasText(query) && statementExpressionDef == null && !StringUtils.hasText(ingestQuery) |
| 98 | + || !(StringUtils.hasText(query) && statementExpressionDef != null && StringUtils.hasText(ingestQuery)) |
| 99 | + && (StringUtils.hasText(query) ^ statementExpressionDef != null) ^ StringUtils.hasText(ingestQuery); |
| 100 | + } |
| 101 | + |
| 102 | + private CassandraParserUtils() { |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments