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.Table;
027    
028    import org.apache.james.mailbox.store.mail.model.Property;
029    
030    @Entity(name="Property")
031    @Table(name="JAMES_MAIL_PROPERTY")
032    public class JPAProperty implements Property {
033    
034        /** The system unique key */
035        @Id
036        @GeneratedValue
037        @Column(name = "PROPERTY_ID", nullable = true)
038        private long id;
039        
040        /** Order within the list of properties */
041        @Basic(optional = false)
042        @Column(name = "PROPERTY_LINE_NUMBER", nullable = false)
043        private int line;
044        
045        /** Local part of the name of this property */
046        @Basic(optional = false)
047        @Column(name = "PROPERTY_LOCAL_NAME", nullable = false, length = 500)
048        private String localName;
049        
050        /** Namespace part of the name of this property */
051        @Basic(optional = false)
052        @Column(name = "PROPERTY_NAME_SPACE", nullable = false, length = 500)
053        private String namespace;
054    
055        /** Value of this property */
056        @Basic(optional = false)
057        @Column(name = "PROPERTY_VALUE", nullable = false, length = 1024)
058        private String value;
059        
060        /**
061         * @deprecated enhancement only
062         */
063        @Deprecated 
064        public JPAProperty() {}
065        
066        /**
067         * Constructs a property.
068         * @param localName not null
069         * @param namespace not null
070         * @param value not null
071         */
072        public JPAProperty(String namespace, String localName, String value, int order) {
073            super();
074            this.localName = localName;
075            this.namespace = namespace;
076            this.value = value;
077            this.line = order;
078        }
079    
080        /**
081         * Constructs a property cloned from the given.
082         * @param property not null
083         */
084        public JPAProperty(Property property, int order) {
085            this(property.getNamespace(), property.getLocalName(), property.getValue(), order);
086        }
087    
088        /**
089         * Create a copy of the give JPAProperty
090         * 
091         * @param property
092         */
093        public JPAProperty(JPAProperty property) {
094            this(property.getNamespace(), property.getLocalName(), property.getValue(), property.getOrder());
095        }
096        
097        /**
098         * Gets the order of this property.
099         * @return order of this property
100         */
101        public int getOrder() {
102            return line;
103        }
104    
105        /**
106         * Gets the local part of the name of the property.
107         * @return not null
108         */
109        public String getLocalName() {
110            return localName;
111        }
112        
113        /**
114         * Gets the namespace for the name.
115         * @return not null
116         */
117        public String getNamespace() {
118            return namespace;
119        }
120        
121        /**
122         * Gets the value for this property.
123         * @return not null
124         */
125        public String getValue() {
126            return value;
127        }
128    
129        @Override
130        public int hashCode() {
131            final int PRIME = 31;
132            int result = 1;
133            result = PRIME * result + (int) (id ^ (id >>> 32));
134            return result;
135        }
136    
137        @Override
138        public boolean equals(Object obj) {
139            if (this == obj)
140                return true;
141            if (obj == null)
142                return false;
143            if (getClass() != obj.getClass())
144                return false;
145            final JPAProperty other = (JPAProperty) obj;
146            if (id != other.id)
147                return false;
148            return true;
149        }
150    
151        /**
152         * Constructs a <code>String</code> with all attributes
153         * in name = value format.
154         *
155         * @return a <code>String</code> representation 
156         * of this object.
157         */
158        public String toString() {
159            final String result = "JPAProperty ( "
160                + "id = " + this.id + " "
161                + "localName = " + this.localName + " "
162                + "namespace = " + this.namespace + " "
163                + "value = " + this.value 
164                + " )";
165        
166            return result;
167        }
168        
169    }