[ 
https://issues.apache.org/jira/browse/CAMEL-12878?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16649817#comment-16649817
 ] 

ASF GitHub Bot commented on CAMEL-12878:
----------------------------------------

dmvolod closed pull request #2564: CAMEL-12878: camel-jpa: Allow for passing 
named-query parameters via message header
URL: https://github.com/apache/camel/pull/2564
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/components/camel-jpa/src/main/docs/jpa-component.adoc 
b/components/camel-jpa/src/main/docs/jpa-component.adoc
index aca2c481419..8c1cd499517 100644
--- a/components/camel-jpa/src/main/docs/jpa-component.adoc
+++ b/components/camel-jpa/src/main/docs/jpa-component.adoc
@@ -161,7 +161,7 @@ with the following path and query parameters:
 | *sendEmptyMessageWhenIdle* (consumer) | If the polling consumer did not poll 
any files, you can enable this option to send an empty message (no body) 
instead. | false | boolean
 | *skipLockedEntity* (consumer) | To configure whether to use NOWAIT on lock 
and silently skip the entity. | false | boolean
 | *transacted* (consumer) | Whether to run the consumer in transacted mode, by 
which all messages will either commit or rollback, when the entire batch has 
been processed. The default behavior (false) is to commit all the previously 
successfully processed messages, and only rollback the last failed message. | 
false | boolean
-| *exceptionHandler* (consumer) | To let the consumer use a custom 
ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this 
options is not in use. By default the consumer will deal with exceptions, that 
will be logged at WARN or ERROR level and ignored. |  | ExceptionHandler
+| *exceptionHandler* (consumer) | To let the consumer use a custom 
ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this 
option is not in use. By default the consumer will deal with exceptions, that 
will be logged at WARN or ERROR level and ignored. |  | ExceptionHandler
 | *exchangePattern* (consumer) | Sets the exchange pattern when the consumer 
creates an exchange. |  | ExchangePattern
 | *pollStrategy* (consumer) | A pluggable 
org.apache.camel.PollingConsumerPollingStrategy allowing you to provide your 
custom implementation to control error handling usually occurred during the 
poll operation before an Exchange have been created and being routed in Camel. 
|  | PollingConsumerPoll Strategy
 | *flushOnSend* (producer) | Flushes the EntityManager after the entity bean 
has been persisted. | true | boolean
@@ -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 4466b3f97bf..a8467389393 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
@@ -24,6 +24,8 @@
 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 3f3653d8a2b..5ee77029c84 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
@@ -185,14 +185,21 @@ public Object doInTransaction(TransactionStatus status) {
         });
     }
 
+    @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 89772712a38..e4bc79b7f8d 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
@@ -92,7 +92,7 @@ public void process(Exchange e) {
                 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 00000000000..3afb7ee727f
--- /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);
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> camel-jpa: Allow for passing named-query parameters via message header and/or 
> body
> ----------------------------------------------------------------------------------
>
>                 Key: CAMEL-12878
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12878
>             Project: Camel
>          Issue Type: Improvement
>          Components: camel-jpa
>    Affects Versions: 2.22.1
>            Reporter: Jochen Cordes
>            Assignee: Dmitry Volodin
>            Priority: Major
>             Fix For: 2.23.0
>
>
> Right now camel-jpa allows for passing parameters to named-queries via a bean 
> in the registry. For ease of use it should also be possible to pass 
> parameters via message header and/or body (i.e. using a HashMap and 
> key/values for parameter-name and parameter-value).



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to