View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.model;
20  
21  import org.apache.commons.lang.builder.CompareToBuilder;
22  import org.apache.commons.lang.builder.EqualsBuilder;
23  import org.apache.commons.lang.builder.HashCodeBuilder;
24  
25  import java.io.Serializable;
26  import java.util.Comparator;
27  import java.util.Date;
28  
29  /**
30   * This is a POJO Business Domain Object. Hibernate Bean.
31   * <p/>
32   * <p>
33   * All entities are Java Beans and should inherit this class to make sure they
34   * are Serializable and Cloneable and have the following fields : an id, a
35   * creation date and a last modifiation date.
36   * </p>
37   *
38   * @author ready
39   */
40  public abstract class AbstractEntity implements Entity {
41  
42      /**
43       *
44       */
45      private static final long serialVersionUID = 1L;
46  
47      public static final Comparator<Entity> ID_COMPARATOR = new IdComparator();
48  
49      public static final Comparator<AbstractEntity> CREATE_DATE_COMPARATOR = new CreateDateComparator();
50  
51      public static final Comparator<AbstractEntity> LAST_MODIFIED_DATE_COMPARATOR = new LastModifiedDateComparator();
52  
53      /**
54       * System ID
55       */
56      private Integer id;
57  
58      /**
59       * Creation date and time.
60       */
61      private Date createDate = new Date();
62  
63      /**
64       * Last modification date and time.
65       */
66      private Date lastModifiedDate = new Date();
67  
68      /**
69       * Default constructor (required by Hibernate).
70       */
71      public AbstractEntity() {
72      }
73  
74      public Integer getId() {
75          return id;
76      }
77  
78      public void setId(Integer id) {
79          this.id = id;
80      }
81  
82      /**
83       * @return creation time stamp or <tt>null</tt> for transient entities
84       */
85      public Date getCreateDate() {
86          if (null == createDate)
87              createDate = new Date();
88          return new Date(createDate.getTime());
89      }
90  
91      /**
92       * Sets the creation date and time.
93       * <p/>
94       * <p>
95       * The persistence framework automatically sets this property when a new
96       * entity is persisted. <br>
97       * Note that the value is managed by the persistence framework and may be
98       * generated by the database in the future.
99       * </p>
100      * <p/>
101      * <p>
102      * The creation time stamp should never change once initialized.
103      * </p>
104      *
105      * @param dateTime creation time stamp
106      */
107     public void setCreateDate(Date dateTime) {
108         if (null == dateTime)
109             return;
110         this.createDate = new Date(dateTime.getTime());
111     }
112 
113     /**
114      * @return last modification time stamp or <tt>null</tt> for transient
115      *         entities
116      */
117     public Date getLastModifiedDate() {
118         if (null == this.lastModifiedDate)
119             this.lastModifiedDate = new Date();
120         return new Date(lastModifiedDate.getTime());
121     }
122 
123     /**
124      * Sets the last modification date and time.
125      * <p/>
126      * <p>
127      * The persistence framework automatically sets this property to the same
128      * value as the createDate property when persisting a new entity and
129      * automatically updates it when saving an existing one. <br>
130      * Note that the value is managed by the persistence framework and may be
131      * generated by the database in the future.
132      * </p>
133      *
134      * @param dateTime last modification time stamp
135      */
136     public void setLastModifiedDate(Date dateTime) {
137         if (null == dateTime)
138             return;
139         this.lastModifiedDate = new Date(dateTime.getTime());
140     }
141 
142     /**
143      * Returns whether this instance represents a new transient instance.
144      *
145      * @return <tt>true</tt> if <code>id</code> is <tt>null</tt>
146      */
147     public boolean isNew() {
148         return (this.getId() == null);
149     }
150 
151     @Override
152     public Object clone() throws CloneNotSupportedException {
153         return super.clone();
154     }
155 
156     /**
157      * Compares 2 instances by ID.
158      */
159     protected static class IdComparator implements Comparator<Entity>, Serializable {
160 
161         /**
162          *
163          */
164         private static final long serialVersionUID = 1L;
165 
166         public int compare(Entity href="../../../org/itracker/model/Entity.html#Entity">Entity a, Entity b) {
167             return new CompareToBuilder().append(a.getId(), b.getId())
168                     .toComparison();
169         }
170 
171     }
172 
173     protected static class CreateDateComparator implements
174             Comparator<AbstractEntity>, Serializable {
175 
176         /**
177          *
178          */
179         private static final long serialVersionUID = 1L;
180 
181         public int compare(AbstractEntity./../../org/itracker/model/AbstractEntity.html#AbstractEntity">AbstractEntity a, AbstractEntity b) {
182             return new CompareToBuilder().append(a.getCreateDate(),
183                     b.getCreateDate()).toComparison();
184         }
185 
186     }
187 
188     /**
189      * Compares 2 instances by last modified date.
190      */
191     protected static class LastModifiedDateComparator implements
192             Comparator<AbstractEntity>, Serializable {
193 
194         /**
195          *
196          */
197         private static final long serialVersionUID = 1L;
198 
199         public int compare(AbstractEntity./../../org/itracker/model/AbstractEntity.html#AbstractEntity">AbstractEntity a, AbstractEntity b) {
200             return new CompareToBuilder().append(a.getLastModifiedDate(),
201                     b.getLastModifiedDate()).toComparison();
202         }
203 
204     }
205 
206     @Override
207     public final boolean equals(Object obj) {
208 
209         if (this == obj) {
210             return true;
211         }
212         if (isNew() || null == obj) {
213             return false;
214         }
215 
216         if (getClass().equals(obj.getClass())) {
217             Entityref="../../../org/itracker/model/Entity.html#Entity">Entity o = (Entity) obj;
218             return new EqualsBuilder()
219                     .append(getId(), o.getId()).isEquals();
220 
221         }
222 
223         return false;
224 
225     }
226 
227     public final int compareTo(Entity o) {
228         if (this.equals(o)) {
229             return 0;
230         }
231         return new CompareToBuilder().append(getClass(), o.getClass(), AbstractEntity.CLASS_COMPARATOR).append(
232                 getId(), o.getId()).toComparison();
233     }
234 
235     @Override
236     public final int hashCode() {
237         return new HashCodeBuilder().append(getClass()).append(getId()).toHashCode();
238 
239     }
240 
241     private static final Comparator<Class<?>> CLASS_COMPARATOR = new Comparator<Class<?>>() {
242         public int compare(Class<?> o1, Class<?> o2) {
243             return new CompareToBuilder().append(o1.getSimpleName(), o2.getSimpleName()).append(o1.hashCode(), hashCode()).toComparison();
244         }
245     };
246 
247 }