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.rrt.jpa.model;
020    
021    import java.io.Serializable;
022    
023    import javax.persistence.Column;
024    import javax.persistence.Entity;
025    import javax.persistence.Id;
026    import javax.persistence.IdClass;
027    import javax.persistence.NamedQueries;
028    import javax.persistence.NamedQuery;
029    import javax.persistence.Table;
030    
031    /**
032     * RecipientRewriteTable class for the James Virtual User Table to be used for JPA
033     * persistence.
034     */
035    @Entity(name = "JamesRecipientRewrite")
036    @Table(name = "JAMES_RECIPIENT_REWRITE")
037    @NamedQueries({ 
038        @NamedQuery(name = "selectMappings", query = "SELECT rrt FROM JamesRecipientRewrite rrt WHERE (rrt.user LIKE :user OR rrt.user='*') and (rrt.domain like :domain or rrt.domain='*') ORDER BY rrt.domain DESC"),
039            @NamedQuery(name = "selectUserDomainMapping", query = "SELECT rrt FROM JamesRecipientRewrite rrt WHERE rrt.user=:user AND rrt.domain=:domain"), 
040            @NamedQuery(name = "selectAllMappings", query = "SELECT rrt FROM JamesRecipientRewrite rrt"),
041            @NamedQuery(name = "deleteMapping", query = "DELETE FROM JamesRecipientRewrite rrt WHERE rrt.user=:user AND rrt.domain=:domain AND rrt.targetAddress=:targetAddress"),
042            @NamedQuery(name = "updateMapping", query = "UPDATE JamesRecipientRewrite rrt SET rrt.targetAddress=:targetAddress WHERE rrt.user=:user AND rrt.domain=:domain") })
043    @IdClass(JPARecipientRewrite.RecipientRewriteTableId.class)
044    public class JPARecipientRewrite {
045    
046        public static class RecipientRewriteTableId implements Serializable {
047    
048            private static final long serialVersionUID = 1L;
049    
050            private String user;
051    
052            private String domain;
053    
054            public RecipientRewriteTableId() {
055            }
056    
057            @Override
058            public int hashCode() {
059                final int PRIME = 31;
060                int result = 1;
061                result = PRIME * result + (int) (user.hashCode() ^ (user.hashCode() >>> 32));
062                result = PRIME * result + (int) (domain.hashCode() ^ (domain.hashCode() >>> 32));
063                return result;
064            }
065    
066            @Override
067            public boolean equals(Object obj) {
068                if (this == obj)
069                    return true;
070                if (obj == null)
071                    return false;
072                if (getClass() != obj.getClass())
073                    return false;
074                final RecipientRewriteTableId other = (RecipientRewriteTableId) obj;
075                if (!user.equals(other.user))
076                    return false;
077                if (!domain.equals(other.domain))
078                    return false;
079                return true;
080            }
081        }
082    
083        /**
084         * The name of the user.
085         */
086        @Id
087        @Column(name = "USER_NAME", nullable = false, length = 100)
088        private String user = "";
089    
090        /**
091         * The name of the domain. Column name is chosen to be compatible with the
092         * JDBCRecipientRewriteTableList.
093         */
094        @Id
095        @Column(name = "DOMAIN_NAME", nullable = false, length = 100)
096        private String domain = "";
097    
098        /**
099         * The target address. column name is chosen to be compatible with the
100         * JDBCRecipientRewriteTableList.
101         */
102        @Column(name = "TARGET_ADDRESS", nullable = false, length = 100)
103        private String targetAddress = "";
104        
105        /**
106         * Default no-args constructor to avoid warning during JPA class enhancement.
107         * Do not us this.
108         */
109        @SuppressWarnings(value = "unused" )
110        private JPARecipientRewrite() {
111        }
112    
113        /**
114         * Use this simple constructor to create a new RecipientRewriteTable.
115         * 
116         * @param user
117         *            , domain and their associated targetAddress
118         */
119        public JPARecipientRewrite(String user, String domain, String targetAddress) {
120            this.user = user;
121            this.domain = domain;
122            this.targetAddress = targetAddress;
123        }
124    
125        public String getUser() {
126            return user;
127        }
128    
129        public String getDomain() {
130            return domain;
131        }
132    
133        public String getTargetAddress() {
134            return targetAddress;
135        }
136    
137    }