1    /*
2     * Copyright 2006 :torweg free software group
3     * 
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.util.entity;
19   
20   import java.io.Serializable;
21   
22   import javax.persistence.Column;
23   import javax.persistence.GeneratedValue;
24   import javax.persistence.Id;
25   import javax.persistence.MappedSuperclass;
26   import javax.xml.bind.annotation.XmlAccessType;
27   import javax.xml.bind.annotation.XmlAccessorType;
28   import javax.xml.bind.annotation.XmlAttribute;
29   
30   import org.hibernate.search.annotations.Field;
31   
32   /**
33    * The most basic default implementation for entities used in <em>pulse</em>,
34    * containing a generated id and {@code #equals(Object)}, {@code #hashCode()}
35    * and {@code #compareTo(AbstractBasicEntity)} methods.
36    * 
37    * @author Christian Schatt, Daniel Dietz, Thomas Weber
38    * @version $Revision: 1408 $
39    */
40   @MappedSuperclass
41   @XmlAccessorType(XmlAccessType.NONE)
42   public abstract class AbstractBasicEntity implements Serializable {
43   
44       /**
45        * serialVersionUID.
46        */
47       private static final long serialVersionUID = 729837579566298988L;
48   
49       /**
50        * the primary key.
51        * <p>
52        * For entities using Hibernate<sup>TM</sup> search, the id is annotated
53        * with {@code @Field}.
54        * </p>
55        */
56       @Id
57       @GeneratedValue
58       @Field
59       @Column(unique = true, nullable = false, updatable = false)
60       @XmlAttribute(name = "id")
61       private Long id;
62   
63   
64       /**
65        * Returns the unique, generated Id of this {@code AbstractBasicEntity}
66        * .
67        * 
68        * @return the Id of this {@code AbstractBasicEntity}
69        */
70       public final Long getId() {
71           return this.id;
72       }
73   
74       /**
75        * For JAXB only.
76        * <p>
77        * The FQN for evaluation in XSL:
78        * <pre>
79        * return getClass().getCanonicalName();
80        * </pre>
81        * </p>
82        * 
83        * @return the FQN
84        */
85       @XmlAttribute(name = "class")
86       @Deprecated
87       protected final String getFullQualifiedClassNameJAXB() {
88           return getClass().getCanonicalName();
89       }
90   
91       /**
92        * Compares this {@code AbstractBasicEntity} to the parameter object,
93        * and either returns an int as the result of the comparison, or throws a
94        * {@code ClassCastException}.
95        * 
96        * @param object
97        *            the {@code AbstractBasicEntity} to be compared to this
98        *            {@code AbstractBasicEntity}
99        * @return -1, 0, or 1 as this {@code AbstractBasicEntity} is less
100       *         than, equal to, or greater than the parameter object
101       */
102      public final int compareTo(final AbstractBasicEntity object) {
103          if (!this.getClass().isAssignableFrom(object.getClass())) {
104              throw new ClassCastException(getClass()
105                      + " is not assignable from " + object.getClass() + ".");
106          }
107          if ((this.getId() == null) && (object.getId() == null)) {
108              return Integer.valueOf(this.hashCode())
109                      .compareTo(object.hashCode());
110          } else if (this.getId() == null) {
111              return -1;
112          } else if (object.getId() == null) {
113              return 1;
114          }
115          return this.getId().compareTo(object.getId());
116      }
117  
118      /**
119       * Checks if this {@code AbstractBasicEntity} is equal to the parameter
120       * object.
121       * 
122       * @param object
123       *            the {@code Object} to be checked for equality with this
124       *            {@code AbstractBasicEntity}
125       * @return {@code true} if the parameter object is not null and equals
126       *         this {@code AbstractBasicEntity}, else {@code false}
127       */
128      @Override
129      public boolean equals(final Object object) {
130          if ((object == null)
131                  || !this.getClass().isAssignableFrom(object.getClass())) {
132              return false;
133          }
134          AbstractBasicEntity bObject = (AbstractBasicEntity) object;
135          if ((this.getId() == null) && (bObject.getId() == null)) {
136              return super.equals(object);
137          } else if ((this.getId() == null) || (bObject.getId() == null)) {
138              return false;
139          }
140          return this.getId().equals(bObject.getId());
141      }
142  
143      /**
144       * Returns a hashCode for this {@code AbstractBasicEntity}.
145       * 
146       * @return a hashCode for this {@code AbstractBasicEntity}
147       * 
148       * @see org.torweg.pulse.util.entity.AbstractBasicEntity#hashCode()
149       */
150      @Override
151      public int hashCode() {
152          if (this.getId() == null) {
153              return super.hashCode();
154          }
155          return this.getId().intValue();
156      }
157  
158  }
159