1 /***
2 Copyright (C) 2005 The Java Community
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 2 of the License, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 Place, Suite 330, Boston, MA 02111-1307 USA.
16 */
17 package org.bejug.javacareers.jobs.model;
18
19 import java.io.Serializable;
20 import java.sql.Timestamp;
21
22 import org.apache.commons.lang.builder.CompareToBuilder;
23 import org.apache.commons.lang.builder.EqualsBuilder;
24 import org.apache.commons.lang.builder.HashCodeBuilder;
25 import org.apache.commons.lang.builder.ToStringBuilder;
26
27 /***
28 * The abstract persistable object contains the id, version, creation and
29 * modification fields for all persistable objects.
30 *
31 * @author bme (Last modified by $Author$)
32 * @version $Revision$ - $Date$
33 */
34 public abstract class AbstractPersistableObject
35 implements Serializable, Comparable {
36
37 /***
38 * The unique identifier for each persistable object.
39 */
40 private Integer id = null;
41
42 /***
43 * The version 'timestamp' for optimistic locking.
44 */
45 private Integer version = null;
46
47 /***
48 * The creation date of the object.
49 */
50 private Timestamp creationDate = new Timestamp(System.currentTimeMillis());
51
52 /***
53 * The last modification date.
54 */
55 private Timestamp modificationDate = null;
56
57 /***
58 * transient field
59 * flags this object as editable
60 */
61 private boolean editable;
62
63 /***
64 * default constructor.
65 */
66 public AbstractPersistableObject() {
67 }
68
69 /***
70 * {@inheritDoc}
71 */
72 public boolean equals(Object o) {
73 if (!(o instanceof AbstractPersistableObject)) {
74 return false;
75 }
76 if (this == o) {
77 return true;
78 }
79 AbstractPersistableObject apo = (AbstractPersistableObject) o;
80 return new EqualsBuilder()
81 .append(this.id, apo.getId())
82 .append(this.creationDate, apo.getCreationDate())
83 .append(this.modificationDate, apo.getModificationDate())
84 .append(this.version, apo.getVersion())
85 .append(this.editable, apo.isEditable())
86 .isEquals();
87 }
88
89
90 /***
91 * {@inheritDoc}
92 */
93 public int hashCode() {
94 return new HashCodeBuilder(69, 15)
95 .append(this.id)
96 .append(this.version)
97 .append(this.creationDate)
98 .append(this.modificationDate)
99 .toHashCode();
100 }
101
102
103 /***
104 * {@inheritDoc}
105 */
106 public int compareTo(Object o) {
107 AbstractPersistableObject myClass = (AbstractPersistableObject) o;
108 return new CompareToBuilder()
109 .append(this.id, myClass.getId())
110 .append(this.version, myClass.getVersion())
111 .append(this.creationDate, myClass.getCreationDate())
112 .append(this.modificationDate, myClass.getModificationDate())
113 .toComparison();
114 }
115
116
117 /***
118 * {@inheritDoc}
119 */
120 public String toString() {
121 return new ToStringBuilder(this)
122 .append("id", this.id)
123 .append("version", this.version)
124 .append("creationDate", this.creationDate)
125 .append("modificationDate", this.modificationDate)
126 .toString();
127 }
128
129 /***
130 * Indicates wether this is a transient object.
131 *
132 * @return true if this object has not been persisted yet; false otherwise
133 */
134 public boolean isNew() {
135 return (this.id == null);
136 }
137
138 /***
139 * Gets the id.
140 *
141 * @return the id
142 */
143 public Integer getId() {
144 return id;
145 }
146
147 /***
148 * Sets the id.
149 *
150 * @param id
151 * the id
152 */
153 public void setId(Integer id) {
154 this.id = id;
155 }
156
157 /***
158 * Gets the version.
159 *
160 * @return the version
161 */
162 public Integer getVersion() {
163 return version;
164 }
165
166 /***
167 * Sets the version.
168 *
169 * @param version
170 * the version
171 */
172 public void setVersion(Integer version) {
173 this.version = version;
174 }
175
176 /***
177 * If we have a new object, then we have to create the creationDate and
178 * modificationDate.
179 *
180 * @return Returns the creation date.
181 */
182 public Timestamp getCreationDate() {
183 return creationDate;
184 }
185
186 /***
187 *
188 * @param creationDate Timestamp
189 */
190 public void setCreationDate (Timestamp creationDate) {
191 this.creationDate = creationDate;
192 }
193
194 /***
195 *
196 * @return modificationDate Timestamp
197 */
198 public Timestamp getModificationDate() {
199 return modificationDate;
200 }
201
202 /***
203 *
204 * @param modificationDate Timestamp
205 */
206 public void setModificationDate(Timestamp modificationDate) {
207 this.modificationDate = modificationDate;
208 }
209
210 /***
211 *
212 * @return editable boolean
213 */
214 public boolean isEditable() {
215 return editable;
216 }
217
218 /***
219 * convenience method to flag this object as editable
220 * @param newValue the bolean value to set
221 */
222 public void setEditable(boolean newValue) {
223 editable = newValue;
224 }
225
226 }
227 /***
228 * $Log$
229 * Revision 1.3 2005/12/20 15:36:45 shally
230 * CheckStyle and PMD changes.
231 *
232 * Revision 1.2 2005/12/08 14:53:44 shally
233 * Opkuis voor checkstyle.
234 *
235 * Revision 1.1 2005/08/31 12:15:09 bme_jcs
236 * refactoring and introduction of apache builders
237 *
238 * Revision 1.1 2005/08/26 07:58:25 ge0ffrey
239 * split up the sources in service, serviceimpl and webclient
240 *
241 * Revision 1.3 2005/06/20 15:18:31 psong09
242 * extended update functionality, added boolean editable
243 *
244 * Revision 1.2 2005/06/09 08:18:43 bejug_cc
245 * Fix initial import
246 *
247 * Revision 1.7 2005/05/09 14:54:45 ssc
248 * checkstyle
249 *
250 * Revision 1.6 2005/05/04 09:47:15 bme
251 * modified for introduction AOP
252 *
253 * Revision 1.5 2005/05/01 12:10:32 sja
254 * Corrected hashCode method.
255 *
256 * Revision 1.4 2005/04/29 07:10:03 PSONG09
257 * update
258 * Revision 1.3 2005/04/23 14:32:15 sja
259 * Corrected reation and modifcation date
260 *
261 * Revision 1.2 2005/04/22 13:30:05 bme Modified for usage of timestamp etc
262 *
263 * Revision 1.1 2005/04/19 11:51:22 PSONG09 First release
264 *
265 * Revision 1.3 2005/04/18 18:46:23 sja Added javadoc.
266 *
267 * Revision 1.2 2005/04/15 20:55:23 sja cleanup
268 *
269 * Revision 1.1 2005/04/15 14:22:22 sja First release
270 *
271 */
272