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.model;
020    
021    import javax.persistence.Basic;
022    import javax.persistence.Column;
023    import javax.persistence.Entity;
024    import javax.persistence.GeneratedValue;
025    import javax.persistence.Id;
026    import javax.persistence.NamedQueries;
027    import javax.persistence.NamedQuery;
028    import javax.persistence.Table;
029    
030    import org.apache.james.mailbox.model.MailboxACL;
031    import org.apache.james.mailbox.model.MailboxPath;
032    import org.apache.james.mailbox.model.SimpleMailboxACL;
033    import org.apache.james.mailbox.store.mail.model.Mailbox;
034    
035    @Entity(name="Mailbox")
036    @Table(name="JAMES_MAILBOX")
037    @NamedQueries({
038        @NamedQuery(name="findMailboxById",
039            query="SELECT mailbox FROM Mailbox mailbox WHERE mailbox.mailbox.mailboxId = :idParam"),
040        @NamedQuery(name="findMailboxByName",
041            query="SELECT mailbox FROM Mailbox mailbox WHERE mailbox.name = :nameParam and mailbox.user is NULL and mailbox.namespace= :namespaceParam"),
042        @NamedQuery(name="findMailboxByNameWithUser",
043            query="SELECT mailbox FROM Mailbox mailbox WHERE mailbox.name = :nameParam and mailbox.user= :userParam and mailbox.namespace= :namespaceParam"),
044        @NamedQuery(name="deleteAllMailboxes",
045            query="DELETE FROM Mailbox mailbox"),
046        @NamedQuery(name="findMailboxWithNameLikeWithUser",
047            query="SELECT mailbox FROM Mailbox mailbox WHERE mailbox.name LIKE :nameParam and mailbox.user= :userParam and mailbox.namespace= :namespaceParam"),
048        @NamedQuery(name="findMailboxWithNameLike",
049            query="SELECT mailbox FROM Mailbox mailbox WHERE mailbox.name LIKE :nameParam and mailbox.user is NULL and mailbox.namespace= :namespaceParam"),
050        @NamedQuery(name="countMailboxesWithNameLikeWithUser",
051            query="SELECT COUNT(mailbox) FROM Mailbox mailbox WHERE mailbox.name LIKE :nameParam and mailbox.user= :userParam and mailbox.namespace= :namespaceParam"),
052        @NamedQuery(name="countMailboxesWithNameLike",
053            query="SELECT COUNT(mailbox) FROM Mailbox mailbox WHERE mailbox.name LIKE :nameParam and mailbox.user is NULL and mailbox.namespace= :namespaceParam"),
054        @NamedQuery(name="listMailboxes",
055            query="SELECT mailbox FROM Mailbox mailbox"),
056        @NamedQuery(name="findHighestModSeq",
057            query="SELECT mailbox.highestModSeq FROM Mailbox mailbox WHERE mailbox.mailboxId = :idParam"),
058        @NamedQuery(name="findLastUid",
059            query="SELECT mailbox.lastUid FROM Mailbox mailbox WHERE mailbox.mailboxId = :idParam")
060    })
061    public class JPAMailbox implements Mailbox<Long> {
062        
063        private static final String TAB = " ";
064    
065        /** The value for the mailboxId field */
066        @Id
067        @GeneratedValue
068        @Column(name = "MAILBOX_ID")
069        private long mailboxId;
070        
071        /** The value for the name field */
072        @Basic(optional = false)
073        @Column(name = "MAILBOX_NAME", nullable = false, length = 200)
074        private String name;
075    
076        /** The value for the uidValidity field */
077        @Basic(optional = false)
078        @Column(name = "MAILBOX_UID_VALIDITY", nullable = false)
079        private long uidValidity;
080    
081        @Basic(optional = false)
082        @Column(name = "USER_NAME", nullable = false, length = 200)
083        private String user;
084        
085        @Basic(optional = false)
086        @Column(name = "MAILBOX_NAMESPACE", nullable = false, length = 200)
087        private String namespace;
088    
089        @Basic(optional = false)
090        @Column(name = "MAILBOX_LAST_UID", nullable = false)
091        private long lastUid;
092        
093        @Basic(optional = false)
094        @Column(name = "MAILBOX_HIGHEST_MODSEQ", nullable = false)
095        private long highestModSeq;
096        
097        /**
098         * JPA only
099         */
100        @Deprecated
101        public JPAMailbox() {
102            super();
103        }
104        
105        public JPAMailbox(MailboxPath path, int uidValidity) {
106            this();
107            this.name = path.getName();
108            this.user = path.getUser();
109            this.namespace = path.getNamespace();
110            this.uidValidity = uidValidity;
111        }
112    
113        /**
114         * @see org.apache.james.mailbox.store.mail.model.Mailbox#getMailboxId()
115         */
116        public Long getMailboxId() {
117            return mailboxId;
118        }
119    
120        /**
121         * @see org.apache.james.mailbox.store.mail.model.Mailbox#getName()
122         */
123        public String getName() {
124            return name;
125        }
126    
127        /**
128         * @see org.apache.james.mailbox.store.mail.model.Mailbox#getUidValidity()
129         */
130        public long getUidValidity() {
131            return uidValidity;
132        }
133        
134        /**
135         * @see org.apache.james.mailbox.store.mail.model.Mailbox#setName(java.lang.String)
136         */
137        public void setName(String name) {
138            this.name = name;
139        }
140    
141        @Override
142        public String toString() {
143            final String retValue = "Mailbox ( "
144                + "mailboxId = " + this.mailboxId + TAB
145                + "name = " + this.name + TAB
146                + "uidValidity = " + this.uidValidity + TAB
147                + " )";
148            return retValue;
149        }
150    
151        @Override
152        public int hashCode() {
153            final int PRIME = 31;
154            int result = 1;
155            result = PRIME * result + (int) (mailboxId ^ (mailboxId >>> 32));
156            return result;
157        }
158    
159        @Override
160        public boolean equals(Object obj) {
161            if (this == obj)
162                return true;
163            if (obj == null)
164                return false;
165            if (getClass() != obj.getClass())
166                return false;
167            final JPAMailbox other = (JPAMailbox) obj;
168            if (mailboxId != other.mailboxId)
169                return false;
170            return true;
171        }
172    
173        /**
174         * @see org.apache.james.mailbox.store.mail.model.Mailbox#getNamespace()
175         */
176        public String getNamespace() {
177            return namespace;
178        }
179    
180        /**
181         * @see org.apache.james.mailbox.store.mail.model.Mailbox#getUser()
182         */
183        public String getUser() {
184            return user;
185        }
186    
187        /**
188         * @see org.apache.james.mailbox.store.mail.model.Mailbox#setNamespace(java.lang.String)
189         */
190        public void setNamespace(String namespace) {
191            this.namespace = namespace;
192        }
193    
194        /**
195         * @see org.apache.james.mailbox.store.mail.model.Mailbox#setUser(java.lang.String)
196         */
197        public void setUser(String user) {
198            this.user = user;
199        }
200    
201        
202        public long getLastUid() {
203            return lastUid;
204        }
205    
206        public long getHighestModSeq() {
207            return highestModSeq;
208        }
209        
210        public long consumeUid() {
211            return ++lastUid;
212        }
213        
214        public long consumeModSeq() {
215            return ++highestModSeq;
216        }
217        
218        /* (non-Javadoc)
219         * @see org.apache.james.mailbox.store.mail.model.Mailbox#getACL()
220         */
221        @Override
222        public MailboxACL getACL() {
223            // TODO ACL support
224            return SimpleMailboxACL.OWNER_FULL_ACL;
225        }
226    
227        /* (non-Javadoc)
228         * @see org.apache.james.mailbox.store.mail.model.Mailbox#setACL(org.apache.james.mailbox.MailboxACL)
229         */
230        @Override
231        public void setACL(MailboxACL acl) {
232            // TODO ACL support
233        }
234        
235    }