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 ****************************************************************/
019package org.apache.james.mailbox.jpa;
020
021import org.apache.james.mailbox.MailboxPathLocker;
022import org.apache.james.mailbox.MailboxSession;
023import org.apache.james.mailbox.acl.GroupMembershipResolver;
024import org.apache.james.mailbox.acl.MailboxACLResolver;
025import org.apache.james.mailbox.exception.MailboxException;
026import org.apache.james.mailbox.jpa.mail.JPAMailboxMapper;
027import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
028import org.apache.james.mailbox.model.MailboxPath;
029import org.apache.james.mailbox.store.Authenticator;
030import org.apache.james.mailbox.store.StoreMailboxManager;
031import org.apache.james.mailbox.store.mail.model.Mailbox;
032import org.apache.james.mailbox.store.transaction.TransactionalMapper;
033
034/**
035 * JPA implementation of {@link StoreMailboxManager}
036 */
037public abstract class JPAMailboxManager extends StoreMailboxManager<Long> {
038    
039    public JPAMailboxManager(JPAMailboxSessionMapperFactory mailboxSessionMapperFactory,
040            final Authenticator authenticator, final MailboxPathLocker locker, MailboxACLResolver aclResolver, GroupMembershipResolver groupMembershipResolver) {
041        super(mailboxSessionMapperFactory, authenticator, locker, aclResolver, groupMembershipResolver);
042    }
043    
044    @Override
045    protected Mailbox<Long> doCreateMailbox(MailboxPath path, MailboxSession session) throws MailboxException {
046        return  new JPAMailbox(path, randomUidValidity());
047    }
048
049    /**
050     * Delete all mailboxes 
051     * 
052     * @param mailboxSession
053     * @throws MailboxException
054     */
055    public void deleteEverything(MailboxSession mailboxSession) throws MailboxException {
056        final JPAMailboxMapper mapper = (JPAMailboxMapper) getMapperFactory().getMailboxMapper(mailboxSession);
057        mapper.execute(new TransactionalMapper.VoidTransaction() {
058            public void runVoid() throws MailboxException {
059                mapper.deleteAllMemberships(); 
060            }
061        });
062        mapper.execute(new TransactionalMapper.VoidTransaction() {
063            public void runVoid() throws MailboxException {
064                mapper.deleteAllMailboxes(); 
065            }
066        });
067    }
068
069}