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;
020    
021    import java.util.Date;
022    
023    import javax.mail.Flags;
024    import javax.mail.internet.SharedInputStream;
025    
026    import org.apache.james.mailbox.MailboxPathLocker;
027    import org.apache.james.mailbox.MailboxSession;
028    import org.apache.james.mailbox.acl.GroupMembershipResolver;
029    import org.apache.james.mailbox.acl.MailboxACLResolver;
030    import org.apache.james.mailbox.exception.MailboxException;
031    import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
032    import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMessage;
033    import org.apache.james.mailbox.store.MailboxEventDispatcher;
034    import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
035    import org.apache.james.mailbox.store.StoreMessageManager;
036    import org.apache.james.mailbox.store.mail.model.Mailbox;
037    import org.apache.james.mailbox.store.mail.model.Message;
038    import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
039    import org.apache.james.mailbox.store.search.MessageSearchIndex;
040    
041    /**
042     * Abstract base class which should be used from JPA implementations.
043     */
044    public class JPAMessageManager extends StoreMessageManager<Long> {
045        
046        public JPAMessageManager(MailboxSessionMapperFactory<Long> mapperFactory, final MessageSearchIndex<Long> index, final MailboxEventDispatcher<Long> dispatcher, final MailboxPathLocker locker, final Mailbox<Long> mailbox, MailboxACLResolver aclResolver, GroupMembershipResolver groupMembershipResolver) throws MailboxException {
047            super(mapperFactory, index, dispatcher, locker, mailbox, aclResolver, groupMembershipResolver);     
048        }
049        
050        @Override
051        protected Message<Long> createMessage(Date internalDate, final int size, int bodyStartOctet, final SharedInputStream content, 
052                final Flags flags, PropertyBuilder propertyBuilder) throws MailboxException{
053    
054            final Message<Long> message = new JPAMessage((JPAMailbox) getMailboxEntity(), internalDate, size, flags, content,  bodyStartOctet,  propertyBuilder);
055            return message;
056        }
057    
058    
059        /**
060         * Support user flags
061         */
062        @Override
063        protected Flags getPermanentFlags(MailboxSession session) {
064            Flags flags =  super.getPermanentFlags(session);
065            flags.add(Flags.Flag.USER);
066            return flags;
067        }
068        
069    }