001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    package org.apache.james.mailbox.jpa.mail;
020    
021    import javax.persistence.EntityManager;
022    import javax.persistence.EntityManagerFactory;
023    import javax.persistence.PersistenceException;
024    
025    import org.apache.james.mailbox.MailboxPathLocker;
026    import org.apache.james.mailbox.MailboxSession;
027    import org.apache.james.mailbox.exception.MailboxException;
028    import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
029    import org.apache.james.mailbox.store.mail.AbstractLockingModSeqProvider;
030    import org.apache.james.mailbox.store.mail.model.Mailbox;
031    
032    public class JPAModSeqProvider extends AbstractLockingModSeqProvider<Long>{
033    
034        private EntityManagerFactory factory;
035    
036        public JPAModSeqProvider(MailboxPathLocker locker, EntityManagerFactory factory) {
037            super(locker);
038            this.factory = factory;
039        }
040    
041        @Override
042        public long highestModSeq(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
043            EntityManager manager = null;
044            try {
045                manager = factory.createEntityManager();
046                manager.getTransaction().begin();
047                long highest = (Long) manager.createNamedQuery("findHighestModSeq").setParameter("idParam", mailbox.getMailboxId()).getSingleResult();
048                manager.getTransaction().commit();
049                return highest;
050            } catch (PersistenceException e) {
051                if (manager != null && manager.getTransaction().isActive()) {
052                    manager.getTransaction().rollback();
053                }
054                throw new MailboxException("Unable to get highest mod-sequence for mailbox " + mailbox, e);
055            } finally {
056                if (manager != null) {
057                    manager.close();
058                }
059            }
060        }
061    
062        @Override
063        protected long lockedNextModSeq(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
064            EntityManager manager = null;
065            try {
066                manager = factory.createEntityManager();
067                manager.getTransaction().begin();
068                JPAMailbox m = manager.find(JPAMailbox.class, mailbox.getMailboxId());
069                long modSeq = m.consumeModSeq();
070                manager.persist(m);
071                manager.getTransaction().commit();
072                return modSeq;
073            } catch (PersistenceException e) {
074                if (manager != null && manager.getTransaction().isActive()) {
075                    manager.getTransaction().rollback();
076                }
077                throw new MailboxException("Unable to save highest mod-sequence for mailbox " + mailbox, e);
078            } finally {
079                if (manager != null) {
080                    manager.close();
081                }
082            }
083        }
084    
085    }