This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch sandbox/camel-3.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit c818b2939c2a35fecc19844bd353c070d709ed73 Author: Dmitry Volodin <[email protected]> AuthorDate: Fri Oct 12 18:25:30 2018 +0300 CAMEL-12878: camel-jpa: Allow for passing named-query parameters via message header --- .../camel-jpa/src/main/docs/jpa-component.adoc | 2 + .../apache/camel/component/jpa/JpaConstants.java | 2 + .../apache/camel/component/jpa/JpaProducer.java | 11 ++- .../org/apache/camel/component/jpa/JpaTest.java | 2 +- .../JpaProducerWithQueryParametersHeaderTest.java | 92 ++++++++++++++++++++++ 5 files changed, 106 insertions(+), 3 deletions(-) diff --git a/components/camel-jpa/src/main/docs/jpa-component.adoc b/components/camel-jpa/src/main/docs/jpa-component.adoc index 27bc625..8c1cd49 100644 --- a/components/camel-jpa/src/main/docs/jpa-component.adoc +++ b/components/camel-jpa/src/main/docs/jpa-component.adoc @@ -229,6 +229,8 @@ reason why the support for this header has been dropped. |`CamelEntityManager` |`EntityManager` |*Camel 2.12: JPA consumer / Camel 2.12.2: JPA producer:* The JPA `EntityManager` object being used by `JpaConsumer` or `JpaProducer`. +|`CamelJpaParameters` |`Map<String, Object>` |*Camel 2.23: JPA producer:* Alternative way for passing query parameters as an Exchange header. + |======================================================================= ### Configuring EntityManagerFactory diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConstants.java b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConstants.java index 4636a42..5df05d6 100644 --- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConstants.java +++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConstants.java @@ -22,6 +22,8 @@ package org.apache.camel.component.jpa; public final class JpaConstants { public static final String ENTITY_MANAGER = "CamelEntityManager"; + + public static final String JPA_PARAMETERS_HEADER = "CamelJpaParameters"; /** * @deprecated use {@link #ENTITY_MANAGER} diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java index 55ab57f..f68de3a 100644 --- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java +++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java @@ -180,14 +180,21 @@ public class JpaProducer extends DefaultProducer { }); } + @SuppressWarnings("unchecked") private void configureParameters(Query query, Exchange exchange) { int maxResults = getEndpoint().getMaximumResults(); if (maxResults > 0) { query.setMaxResults(maxResults); } - // setup the parameter + // setup the parameters + Map<String, Object> params = null; if (parameters != null) { - parameters.forEach((key, value) -> { + params = parameters; + } else { + params = (Map<String, Object>)exchange.getIn().getHeader(JpaConstants.JPA_PARAMETERS_HEADER); + } + if (params != null) { + params.forEach((key, value) -> { Object resolvedValue = value; if (value instanceof String) { resolvedValue = SimpleLanguage.expression((String)value).evaluate(exchange, Object.class); diff --git a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java index 774fa75..afc8c5a 100644 --- a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java +++ b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java @@ -87,7 +87,7 @@ public class JpaTest extends Assert { LOG.info("Received exchange: " + e.getIn()); receivedExchange = e; // should have a EntityManager - EntityManager entityManager = e.getIn().getHeader(JpaConstants.ENTITYMANAGER, EntityManager.class); + EntityManager entityManager = e.getIn().getHeader(JpaConstants.ENTITY_MANAGER, EntityManager.class); assertNotNull("Should have a EntityManager as header", entityManager); latch.countDown(); } diff --git a/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerWithQueryParametersHeaderTest.java b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerWithQueryParametersHeaderTest.java new file mode 100644 index 0000000..3afb7ee --- /dev/null +++ b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerWithQueryParametersHeaderTest.java @@ -0,0 +1,92 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.processor.jpa; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jpa.JpaConstants; +import org.apache.camel.examples.Customer; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.util.ServiceHelper; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JpaProducerWithQueryParametersHeaderTest extends Assert { + + protected static final Logger LOG = LoggerFactory.getLogger(JpaProducerWithQueryParametersHeaderTest.class); + + protected DefaultCamelContext camelContext; + protected ProducerTemplate template; + + @Test + @SuppressWarnings("rawtypes") + public void testProducerWithNamedQuery() throws Exception { + template.sendBody("direct:deleteCustomers", ""); + Customer c1 = new Customer(); + c1.setName("Willem"); + template.sendBody("direct:addCustomer", c1); + Customer c2 = new Customer(); + c2.setName("Dummy"); + template.sendBody("direct:addCustomer", c2); + + Map<String, Object> params = new HashMap<>(); + params.put("custName", "${body}"); + + Object answer = template.requestBodyAndHeader("direct:namedQuery", "Willem", JpaConstants.JPA_PARAMETERS_HEADER, params); + List list = (List)answer; + assertEquals(1, list.size()); + assertEquals("Willem", ((Customer)list.get(0)).getName()); + + answer = template.requestBody("direct:deleteCustomers", ""); + assertEquals(2, ((Integer)answer).intValue()); + } + + @Before + public void setUp() throws Exception { + camelContext = new DefaultCamelContext(); + + camelContext.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:namedQuery") + .to("jpa://" + Customer.class.getName() + "?namedQuery=findAllCustomersWithName"); + + from("direct:addCustomer") + .to("jpa://" + Customer.class.getName()); + from("direct:deleteCustomers") + .to("jpa://" + Customer.class.getName() + "?query=delete from " + Customer.class.getName()); + + } + }); + + template = camelContext.createProducerTemplate(); + ServiceHelper.startServices(template, camelContext); + } + + @After + public void tearDown() throws Exception { + ServiceHelper.stopServices(template, camelContext); + } +}
