001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.processor.idempotent.jpa;
018    
019    import java.util.List;
020    
021    import javax.persistence.EntityManagerFactory;
022    import javax.persistence.Persistence;
023    
024    import org.apache.camel.processor.idempotent.MessageIdRepository;
025    
026    import org.springframework.orm.jpa.JpaTemplate;
027    import org.springframework.orm.jpa.JpaTransactionManager;
028    import org.springframework.transaction.TransactionDefinition;
029    import org.springframework.transaction.TransactionStatus;
030    import org.springframework.transaction.support.TransactionCallback;
031    import org.springframework.transaction.support.TransactionTemplate;
032    
033    /**
034     * @version $Revision: 1.1 $
035     */
036    public class JpaMessageIdRepository implements MessageIdRepository {
037        protected static final String QUERY_STRING = "select x from " + MessageProcessed.class.getName() + " x where x.processorName = ?1 and x.messageId = ?2";
038        private JpaTemplate jpaTemplate;
039        private String processorName;
040        private TransactionTemplate transactionTemplate;
041    
042        public JpaMessageIdRepository(JpaTemplate template, String processorName) {
043            this(template, createTransactionTemplate(template), processorName);
044        }
045    
046        public JpaMessageIdRepository(JpaTemplate template, TransactionTemplate transactionTemplate, String processorName) {
047            this.jpaTemplate = template;
048            this.processorName = processorName;
049            this.transactionTemplate = transactionTemplate;
050        }
051    
052        public static JpaMessageIdRepository jpaMessageIdRepository(String persistenceUnit, String processorName) {
053            EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit);
054            return jpaMessageIdRepository(new JpaTemplate(entityManagerFactory), processorName);
055        }
056    
057        public static JpaMessageIdRepository jpaMessageIdRepository(JpaTemplate jpaTemplate, String processorName) {
058            return new JpaMessageIdRepository(jpaTemplate, processorName);
059        }
060    
061        private static TransactionTemplate createTransactionTemplate(JpaTemplate jpaTemplate) {
062            TransactionTemplate transactionTemplate = new TransactionTemplate();
063            transactionTemplate.setTransactionManager(new JpaTransactionManager(jpaTemplate.getEntityManagerFactory()));
064            transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
065            return transactionTemplate;
066        }
067    
068        public boolean contains(final String messageId) {
069            // Run this in single transaction.
070            Boolean rc = (Boolean)transactionTemplate.execute(new TransactionCallback() {
071                public Object doInTransaction(TransactionStatus arg0) {
072    
073                    List list = jpaTemplate.find(QUERY_STRING, processorName, messageId);
074                    if (list.isEmpty()) {
075                        MessageProcessed processed = new MessageProcessed();
076                        processed.setProcessorName(processorName);
077                        processed.setMessageId(messageId);
078                        jpaTemplate.persist(processed);
079                        jpaTemplate.flush();
080                        return Boolean.FALSE;
081                    } else {
082                        return Boolean.TRUE;
083                    }
084                }
085            });
086            return rc.booleanValue();
087        }
088    }