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  
  * 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.processor.idempotent.jpa;
 18  
 
 19  
 import java.util.List;
 20  
 
 21  
 import javax.persistence.EntityManagerFactory;
 22  
 import javax.persistence.Persistence;
 23  
 
 24  
 import org.apache.camel.processor.idempotent.MessageIdRepository;
 25  
 
 26  
 import org.springframework.orm.jpa.JpaTemplate;
 27  
 import org.springframework.orm.jpa.JpaTransactionManager;
 28  
 import org.springframework.transaction.TransactionDefinition;
 29  
 import org.springframework.transaction.TransactionStatus;
 30  
 import org.springframework.transaction.support.TransactionCallback;
 31  
 import org.springframework.transaction.support.TransactionTemplate;
 32  
 
 33  
 /**
 34  
  * @version $Revision: 1.1 $
 35  
  */
 36  12
 public class JpaMessageIdRepository implements MessageIdRepository {
 37  1
     protected static final String QUERY_STRING = "select x from " + MessageProcessed.class.getName() + " x where x.processorName = ?1 and x.messageId = ?2";
 38  
     private JpaTemplate jpaTemplate;
 39  
     private String processorName;
 40  
     private TransactionTemplate transactionTemplate;
 41  
 
 42  
     public JpaMessageIdRepository(JpaTemplate template, String processorName) {
 43  1
         this(template, createTransactionTemplate(template), processorName);
 44  1
     }
 45  
 
 46  1
     public JpaMessageIdRepository(JpaTemplate template, TransactionTemplate transactionTemplate, String processorName) {
 47  1
         this.jpaTemplate = template;
 48  1
         this.processorName = processorName;
 49  1
         this.transactionTemplate = transactionTemplate;
 50  1
     }
 51  
 
 52  
     public static JpaMessageIdRepository jpaMessageIdRepository(String persistenceUnit, String processorName) {
 53  0
         EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit);
 54  0
         return jpaMessageIdRepository(new JpaTemplate(entityManagerFactory), processorName);
 55  
     }
 56  
 
 57  
     public static JpaMessageIdRepository jpaMessageIdRepository(JpaTemplate jpaTemplate, String processorName) {
 58  1
         return new JpaMessageIdRepository(jpaTemplate, processorName);
 59  
     }
 60  
 
 61  
     private static TransactionTemplate createTransactionTemplate(JpaTemplate jpaTemplate) {
 62  1
         TransactionTemplate transactionTemplate = new TransactionTemplate();
 63  1
         transactionTemplate.setTransactionManager(new JpaTransactionManager(jpaTemplate.getEntityManagerFactory()));
 64  1
         transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
 65  1
         return transactionTemplate;
 66  
     }
 67  
 
 68  
     public boolean contains(final String messageId) {
 69  
         // Run this in single transaction.
 70  6
         Boolean rc = (Boolean)transactionTemplate.execute(new TransactionCallback() {
 71  6
             public Object doInTransaction(TransactionStatus arg0) {
 72  
 
 73  6
                 List list = jpaTemplate.find(QUERY_STRING, processorName, messageId);
 74  6
                 if (list.isEmpty()) {
 75  0
                     MessageProcessed processed = new MessageProcessed();
 76  0
                     processed.setProcessorName(processorName);
 77  0
                     processed.setMessageId(messageId);
 78  0
                     jpaTemplate.persist(processed);
 79  0
                     jpaTemplate.flush();
 80  0
                     return Boolean.FALSE;
 81  
                 } else {
 82  6
                     return Boolean.TRUE;
 83  
                 }
 84  
             }
 85  
         });
 86  6
         return rc.booleanValue();
 87  
     }
 88  
 }