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 java.io.Serializable;
22  import java.util.Comparator;
23  import java.util.Date;
24  
25  import org.apache.commons.lang.builder.CompareToBuilder;
26  import org.apache.commons.lang.builder.EqualsBuilder;
27  import org.apache.commons.lang.builder.HashCodeBuilder;
28  
29  /**
30   * This is a POJO Business Domain Object. Hibernate Bean.
31   * 
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  	/** System ID */
54  	private Integer id;
55  
56  	/** Creation date and time. */
57  	private Date createDate = new Date();
58  
59  	/** Last modification date and time. */
60  	private Date lastModifiedDate = new Date();
61  
62  	/**
63  	 * Default constructor (required by Hibernate).
64  	 */
65  	public AbstractEntity() {
66  	}
67  
68  	public Integer getId() {
69  		return id;
70  	}
71  
72  	public void setId(Integer id) {
73  		this.id = id;
74  	}
75  
76  	/**
77  	 * @return creation time stamp or <tt>null</tt> for transient entities
78  	 */
79  	public Date getCreateDate() {
80  		if (null == createDate)
81  			createDate = new Date();
82  		return new Date(createDate.getTime());
83  	}
84  
85  	/**
86  	 * Sets the creation date and time.
87  	 * 
88  	 * <p>
89  	 * The persistence framework automatically sets this property when a new
90  	 * entity is persisted. <br>
91  	 * Note that the value is managed by the persistence framework and may be
92  	 * generated by the database in the future.
93  	 * </p>
94  	 * 
95  	 * <p>
96  	 * The creation time stamp should never change once initialized.
97  	 * </p>
98  	 * 
99  	 * @param dateTime
100 	 *            creation time stamp
101 	 */
102 	public void setCreateDate(Date dateTime) {
103 		if (null == dateTime)
104 			return;
105 		this.createDate = new Date(dateTime.getTime());
106 	}
107 
108 	/**
109 	 * @return last modification time stamp or <tt>null</tt> for transient
110 	 *         entities
111 	 */
112 	public Date getLastModifiedDate() {
113 		if (null == this.lastModifiedDate)
114 			this.lastModifiedDate = new Date();
115 		return new Date(lastModifiedDate.getTime());
116 	}
117 
118 	/**
119 	 * Sets the last modification date and time.
120 	 * 
121 	 * <p>
122 	 * The persistence framework automatically sets this property to the same
123 	 * value as the createDate property when persisting a new entity and
124 	 * automatically updates it when saving an existing one. <br>
125 	 * Note that the value is managed by the persistence framework and may be
126 	 * generated by the database in the future.
127 	 * </p>
128 	 * 
129 	 * @param dateTime
130 	 *            last modification time stamp
131 	 */
132 	public void setLastModifiedDate(Date dateTime) {
133 		if (null == dateTime)
134 			return;
135 		this.lastModifiedDate = new Date(dateTime.getTime());
136 	}
137 
138 	/**
139 	 * Returns whether this instance represents a new transient instance.
140 	 * 
141 	 * @return <tt>true</tt> if <code>id</code> is <tt>null</tt>
142 	 */
143 	public boolean isNew() {
144 		return (this.getId() == null);
145 	}
146 
147 	@Override
148 	public Object clone() throws CloneNotSupportedException {
149 		return super.clone();
150 	}
151 
152 	/**
153 	 * Compares 2 instances by ID.
154 	 */
155 	protected static class IdComparator implements Comparator<Entity>, Serializable {
156 		
157 		/**
158 		 * 
159 		 */
160 		private static final long serialVersionUID = 1L;
161 
162 		public int compare(Entity a, Entity b) {
163 			return new CompareToBuilder().append(a.getId(), b.getId())
164 					.toComparison();
165 		}
166 
167 	}
168 
169 	protected static class CreateDateComparator implements
170 			Comparator<AbstractEntity>, Serializable {
171 		
172 		/**
173 		 * 
174 		 */
175 		private static final long serialVersionUID = 1L;
176 		
177 		public int compare(AbstractEntity a, AbstractEntity b) {
178 			return new CompareToBuilder().append(a.getCreateDate(),
179 					b.getCreateDate()).toComparison();
180 		}
181 
182 	}
183 
184 	/**
185 	 * Compares 2 instances by last modified date.
186 	 */
187 	protected static class LastModifiedDateComparator implements
188 			Comparator<AbstractEntity>, Serializable {
189 		
190 		/**
191 		 * 
192 		 */
193 		private static final long serialVersionUID = 1L;
194 		
195 		public int compare(AbstractEntity a, AbstractEntity b) {
196 			return new CompareToBuilder().append(a.getLastModifiedDate(),
197 					b.getLastModifiedDate()).toComparison();
198 		}
199 
200 	}
201 
202 	@Override
203 	public final boolean equals(Object obj) {
204 
205 		if (this == obj) {
206 			return true;
207 		}
208 		if (isNew() || null == obj) {
209 			return false;
210 		}
211 
212 		if (getClass().equals(obj.getClass())) {
213 			Entity o = (Entity) obj;
214 			return new EqualsBuilder()
215 					.append(getId(), o.getId()).isEquals();
216 
217 		}
218 
219 		return false;
220 
221 	}
222 
223 	public final int compareTo(Entity o) {
224 		if (this.equals(o)) {
225 			return 0;
226 		}
227 		return new CompareToBuilder().append(getClass(), o.getClass(), AbstractEntity.CLASS_COMPARATOR).append(
228 				getId(), o.getId()).toComparison();
229 	}
230 
231 	@Override
232 	public final int hashCode() {
233 		return new HashCodeBuilder().append(getClass()).append(getId()).toHashCode();
234 
235 	}
236 	
237 	private static final Comparator<Class<?>> CLASS_COMPARATOR = new Comparator<Class<?>>() {
238 		public int compare(Class<?> o1, Class<?> o2) {
239 			return new CompareToBuilder().append(o1.getSimpleName(), o2.getSimpleName()).append(o1.hashCode(), hashCode()).toComparison();
240 		}
241 	};
242 
243 }