Coverage Report - org.apache.camel.component.jpa.JpaProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
JpaProducer
100% 
100% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.component.jpa;
 18  
 
 19  
 import java.util.Iterator;
 20  
 
 21  
 import javax.persistence.EntityManager;
 22  
 import javax.persistence.PersistenceException;
 23  
 
 24  
 import org.apache.camel.Exchange;
 25  
 import org.apache.camel.Expression;
 26  
 import org.apache.camel.converter.ObjectConverter;
 27  
 import org.apache.camel.impl.DefaultProducer;
 28  
 
 29  
 import org.springframework.orm.jpa.JpaCallback;
 30  
 
 31  
 /**
 32  
  * @version $Revision: 563665 $
 33  
  */
 34  
 public class JpaProducer extends DefaultProducer<Exchange> {
 35  
     private final TransactionStrategy template;
 36  
     private final Expression<Exchange> expression;
 37  
 
 38  
     public JpaProducer(JpaEndpoint endpoint, Expression<Exchange> expression) {
 39  3
         super(endpoint);
 40  3
         this.expression = expression;
 41  3
         this.template = endpoint.createTransactionStrategy();
 42  3
     }
 43  
 
 44  
     public void process(Exchange exchange) {
 45  3
         final Object values = expression.evaluate(exchange);
 46  3
         if (values != null) {
 47  3
             template.execute(new JpaCallback() {
 48  3
                 public Object doInJpa(EntityManager entityManager) throws PersistenceException {
 49  3
                     Iterator iter = ObjectConverter.iterator(values);
 50  6
                     while (iter.hasNext()) {
 51  3
                         Object value = iter.next();
 52  3
                         entityManager.persist(value);
 53  3
                     }
 54  3
                     return null;
 55  
                 }
 56  
             });
 57  
         }
 58  3
     }
 59  
 }