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.user.model;
020
021import javax.persistence.Basic;
022import javax.persistence.Column;
023import javax.persistence.Entity;
024import javax.persistence.GeneratedValue;
025import javax.persistence.Id;
026import javax.persistence.NamedQueries;
027import javax.persistence.NamedQuery;
028import javax.persistence.Table;
029import javax.persistence.UniqueConstraint;
030
031import org.apache.james.mailbox.store.user.model.Subscription;
032
033/**
034 * A subscription to a mailbox by a user.
035 */
036@Entity(name = "Subscription")
037@Table(
038    name = "JAMES_SUBSCRIPTION",
039    uniqueConstraints = 
040        @UniqueConstraint(
041                columnNames={
042                        "USER_NAME", 
043                        "MAILBOX_NAME"})
044)
045@NamedQueries({
046    @NamedQuery(name = "findFindMailboxSubscriptionForUser",
047        query = "SELECT subscription FROM Subscription subscription WHERE subscription.username = :userParam AND subscription.mailbox = :mailboxParam"),          
048    @NamedQuery(name = "findSubscriptionsForUser",
049        query = "SELECT subscription FROM Subscription subscription WHERE subscription.username = :userParam")                  
050})
051public class JPASubscription implements Subscription {
052
053    private static final String TO_STRING_SEPARATOR = "  ";
054    
055    /** Primary key */
056    @GeneratedValue
057    @Id 
058    @Column(name = "SUBSCRIPTION_ID")
059    private long id;
060    
061    /** Name of the subscribed user */
062    @Basic(optional = false)
063    @Column(name = "USER_NAME", nullable = false, length = 100)
064    private String username;
065    
066    /** Subscribed mailbox */
067    @Basic(optional = false) 
068    @Column(name = "MAILBOX_NAME", nullable = false, length = 100)
069    private String mailbox;
070    
071    /**
072     * Used by JPA
073     */
074    @Deprecated
075    public JPASubscription() {}
076    
077    /**
078     * Constructs a user subscription.
079     * @param username not null
080     * @param mailbox not null
081     */
082    public JPASubscription(String username, String mailbox) {
083        super();
084        this.username = username;
085        this.mailbox = mailbox;
086    }
087
088    /**
089     * @see org.apache.james.mailbox.store.user.model.Subscription#getMailbox()
090     */
091    public String getMailbox() {
092        return mailbox;
093    }
094    
095    /**
096     * @see org.apache.james.mailbox.store.user.model.Subscription#getUser()
097     */
098    public String getUser() {
099        return username;
100    }
101
102    @Override
103    public int hashCode() {
104        final int PRIME = 31;
105        int result = 1;
106        result = PRIME * result + (int) (id ^ (id >>> 32));
107        return result;
108    }
109
110    @Override
111    public boolean equals(Object obj) {
112        if (this == obj)
113            return true;
114        if (obj == null)
115            return false;
116        if (getClass() != obj.getClass())
117            return false;
118        final JPASubscription other = (JPASubscription) obj;
119        if (id != other.id)
120            return false;
121        return true;
122    }
123
124    /**
125     * Renders output suitable for debugging.
126     *
127     * @return output suitable for debugging
128     */
129    public String toString() {
130        final String result = "Subscription ( "
131            + "id = " + this.id + TO_STRING_SEPARATOR
132            + "user = " + this.username + TO_STRING_SEPARATOR
133            + "mailbox = " + this.mailbox + TO_STRING_SEPARATOR
134            + " )";
135        return result;
136    }
137    
138}