Coverage Report - org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository
 
Classes in this File Line Coverage Branch Coverage Complexity
JpaMessageIdRepository
71% 
100% 
1.286
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.processor.idempotent.jpa;
 19  
 
 20  
 import org.apache.camel.processor.idempotent.MessageIdRepository;
 21  
 import org.springframework.orm.jpa.JpaCallback;
 22  
 import org.springframework.orm.jpa.JpaTemplate;
 23  
 import org.springframework.orm.jpa.JpaTransactionManager;
 24  
 import org.springframework.transaction.TransactionDefinition;
 25  
 import org.springframework.transaction.TransactionStatus;
 26  
 import org.springframework.transaction.support.TransactionCallback;
 27  
 import org.springframework.transaction.support.TransactionTemplate;
 28  
 
 29  
 import javax.persistence.EntityManager;
 30  
 import javax.persistence.EntityManagerFactory;
 31  
 import javax.persistence.Persistence;
 32  
 import javax.persistence.PersistenceException;
 33  
 
 34  
 import java.util.List;
 35  
 
 36  
 /**
 37  
  * @version $Revision: 1.1 $
 38  
  */
 39  12
 public class JpaMessageIdRepository implements MessageIdRepository {
 40  1
     protected static final String QUERY_STRING = "select x from " + MessageProcessed.class.getName() + " x where x.processorName = ?1 and x.messageId = ?2";
 41  
     private JpaTemplate jpaTemplate;
 42  
     private String processorName;
 43  
         private TransactionTemplate transactionTemplate;
 44  
 
 45  
     public static JpaMessageIdRepository jpaMessageIdRepository(String persistenceUnit, String processorName) {
 46  0
         EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit);
 47  0
         return jpaMessageIdRepository(new JpaTemplate(entityManagerFactory), processorName);
 48  
     }
 49  
 
 50  
     public static JpaMessageIdRepository jpaMessageIdRepository(JpaTemplate jpaTemplate, String processorName) {
 51  1
         return new JpaMessageIdRepository(jpaTemplate, processorName);
 52  
     }
 53  
 
 54  
     public JpaMessageIdRepository(JpaTemplate template, String processorName) {
 55  1
         this(template, createTransactionTemplate(template), processorName);
 56  1
     }
 57  
 
 58  1
     public JpaMessageIdRepository(JpaTemplate template, TransactionTemplate transactionTemplate, String processorName) {
 59  1
         this.jpaTemplate = template;
 60  1
         this.processorName = processorName;
 61  1
         this.transactionTemplate=transactionTemplate;
 62  1
     }
 63  
     
 64  
     static private TransactionTemplate createTransactionTemplate(JpaTemplate jpaTemplate) {
 65  1
             TransactionTemplate transactionTemplate = new TransactionTemplate();
 66  1
         transactionTemplate.setTransactionManager(new JpaTransactionManager(jpaTemplate.getEntityManagerFactory()));
 67  1
         transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
 68  1
         return transactionTemplate;
 69  
     }
 70  
 
 71  
     public boolean contains(final String messageId) {
 72  
             // Run this in single transaction.
 73  6
             Boolean rc = (Boolean) transactionTemplate.execute(new TransactionCallback(){
 74  6
                         public Object doInTransaction(TransactionStatus arg0) {
 75  
                                 
 76  6
                         List list = jpaTemplate.find(QUERY_STRING, processorName, messageId);
 77  6
                         if (list.isEmpty()) {
 78  0
                             MessageProcessed processed = new MessageProcessed();
 79  0
                             processed.setProcessorName(processorName);
 80  0
                             processed.setMessageId(messageId);
 81  0
                             jpaTemplate.persist(processed);
 82  0
                             jpaTemplate.flush();
 83  0
                             return Boolean.FALSE;
 84  
                         }
 85  
                         else {
 86  6
                             return Boolean.TRUE;
 87  
                         }
 88  
                         }
 89  
                 });
 90  6
             return rc.booleanValue();
 91  
     }
 92  
 }