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 */
18 package org.bejug.javacareers.jobs.model;
19
20 import java.util.Comparator;
21 import java.util.Set;
22 import java.util.TreeSet;
23
24 import org.apache.commons.lang.builder.EqualsBuilder;
25 import org.apache.commons.lang.builder.HashCodeBuilder;
26 import org.apache.commons.lang.builder.ToStringBuilder;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /***
31 * The comment which is linked to one Offer.
32 *
33 * @author Stephan Janssen (Last modified by $Author: shally $)
34 * @version $Revision: 1.6 $ - $Date: 2005/12/20 15:36:45 $
35 */
36 public class Comment extends AbstractPersistableObject {
37
38 /***
39 * The logger.
40 */
41 static final Log LOG = LogFactory.getLog(Comment.class);
42
43 /***
44 * A reference to the parent comment, if null then comment is parent (root).
45 */
46 private Comment parentComment;
47
48
49
50 /***
51 * Reference (one-to-many) to child comments.
52 */
53 private Set childComments = new TreeSet(new Comparator(){
54 public int compare(Object o1, Object o2) {
55 Comment c1 = (Comment)o1;
56 Comment c2 = (Comment)o2;
57 LOG.info("Debug: c1 = " + c1);
58 LOG.info("Debug: c2 = " + c2);
59 int value = 0;
60 if(c1.getCreationDate().before(c2.getCreationDate())){
61 LOG.info("Debug: c1 before c2");
62 value = 1;
63 }
64 if(c1.getCreationDate().after(c2.getCreationDate())){
65 LOG.info("Debug: c1 after c2");
66 value = -1;
67 }
68 LOG.info("Debug: value = " + value);
69 return value;
70 }
71 });
72
73 /***
74 * @return the subject
75 */
76 public String getSubject() {
77 return subject;
78 }
79
80 /***
81 * @param subject The subject to set
82 */
83 public void setSubject(String subject) {
84 this.subject = subject;
85 }
86
87 /***
88 * The subject of the comment
89 */
90 private String subject;
91
92 /***
93 * The actual content of the comment.
94 */
95 private String content;
96
97 /***
98 * A reference to the offer
99 */
100 private Item offer;
101
102 /***
103 * A reference to the user
104 */
105 private User user;
106
107
108 /***
109 * Default constructor needed for Hibernate.
110 */
111 public Comment() {
112 super();
113 }
114
115 /***
116 *
117 * @param offer The offer to set.
118 * @param user The user to set.
119 * @param subject The subject to set.
120 * @param content The content to set
121 */
122 public Comment(Item offer, User user, String subject, String content) {
123 this.offer = offer;
124 this.subject = subject;
125 this.content = content;
126 if (user!=null){
127 user.getComments().add(this);
128 this.user = user;
129 }
130 }
131
132 /***
133 *
134 * @param subject The subject to set.
135 * @param content The content to set
136 */
137 public Comment(String subject, String content) {
138 this.subject = subject;
139 this.content = content;
140 }
141
142 /***
143 * The offer to which the comment is related to.
144 *
145 * @param offer the offer.
146 */
147 public void setOffer(Item offer) {
148 this.offer = offer;
149 }
150
151
152
153 /***
154 * Returns the related offer.
155 *
156 * @return returns the related offer.
157 */
158 public Item getOffer() {
159 return offer;
160
161 }
162
163 /***
164 * Returns the parent comment.
165 *
166 * @return The parent comment.
167 */
168 public Comment getParentComment() {
169 return parentComment;
170 }
171
172 /***
173 * Set the parent comment, if null then this comment is the root comment.
174 *
175 * @param parentComment the parent comment.
176 */
177 public void setParentComment(Comment parentComment) {
178 this.parentComment = parentComment;
179 }
180
181 /***
182 * Returns the child comments.
183 *
184 * @return The child comments.
185 */
186 public Set getChildComments() {
187 return childComments;
188 }
189
190 /***
191 * Set the child comments.
192 *
193 * @param childComments the child comments.
194 */
195 public void setChildComments(Set childComments) {
196 this.childComments = childComments;
197 }
198
199 /***
200 * Returns the comment content.
201 *
202 * @return returns the comment content.
203 */
204 public String getContent() {
205 return content;
206 }
207
208 /***
209 * Set the comment content.
210 *
211 * @param content The comment content.
212 */
213 public void setContent(String content) {
214 this.content = content;
215 }
216
217 /***
218 * Convenience method to add a child comment.
219 *
220 * @param childComment The child comment.
221 */
222 public void addChildComment(Comment childComment) {
223 if (childComment == null) {
224 throw new IllegalArgumentException("Null child comment!");
225 }
226
227 if (childComment.getParentComment() != null) {
228 childComment.getParentComment().getChildComments().remove(childComment);
229 }
230 childComment.setParentComment(this);
231 childComments.add(childComment);
232 }
233
234 /***
235 * @return Returns the user.
236 */
237 public User getUser() {
238 return user;
239 }
240
241 /***
242 * @param user The user to set.
243 */
244 public void setUser(User user) {
245 this.user = user;
246
247
248
249
250
251
252 }
253
254 /***
255 * adds the user to the comment.
256 * @param newuser the user to add.
257 */
258 public void addUser(User newuser) {
259 if (newuser == null) {
260 throw new IllegalArgumentException("User is NULL");
261 }
262 newuser.getComments().add(this);
263 this.user = newuser;
264 }
265
266 /***
267 * method to implement for the comparator.
268 * @param comment the other comment to compare with.
269 * @return an int containing the result of the comparison.
270 */
271 public int compareTo(Object comment) {
272 Comment c = (Comment) comment;
273
274 if(this.getCreationDate().before(c.getCreationDate())){
275 return 1;
276 }
277 return -1;
278 }
279
280 /***
281 * {@inheritDoc}
282 */
283 public boolean equals(Object o) {
284 if (!(o instanceof Comment)) {
285 return false;
286 }
287 if (this == o) {
288 return true;
289 }
290 Comment comment = (Comment) o;
291 return new EqualsBuilder()
292 .appendSuper(super.equals(o))
293 .append(this.content, comment.getContent())
294 .append(this.offer, comment.getOffer())
295
296 .append(this.childComments, comment.getChildComments())
297 .append(this.subject, comment.getSubject())
298
299 .isEquals();
300 }
301
302
303 /***
304 * {@inheritDoc}
305 */
306 public int hashCode() {
307 return new HashCodeBuilder(113, 45)
308 .append(this.content)
309 .append(this.offer)
310
311 .append(this.childComments)
312 .append(this.subject)
313
314 .toHashCode();
315 }
316
317 /***
318 * {@inheritDoc}
319 */
320 public String toString() {
321 return new ToStringBuilder(this)
322 .appendSuper(super.toString())
323 .append("content", this.content)
324 .append("subject", this.subject)
325 .append("offer", this.offer)
326 .append("childComments", this.childComments)
327 .toString();
328 }
329
330
331 }
332
333 /***
334 * $Log: Comment.java,v $
335 * Revision 1.6 2005/12/20 15:36:45 shally
336 * CheckStyle and PMD changes.
337 *
338 * Revision 1.5 2005/12/09 10:46:55 shally
339 * Opkuis voor checkstyle en PMD
340 *
341 * Revision 1.4 2005/09/30 14:38:07 bavo_jcs
342 * Fixed URL
343 *
344 * Revision 1.3 2005/09/19 14:16:50 bavo_jcs
345 * Refactored comments to use Items
346 *
347 * Revision 1.2 2005/08/31 12:15:09 bme_jcs
348 * refactoring and introduction of apache builders
349 *
350 * Revision 1.1 2005/08/26 07:58:26 ge0ffrey
351 * split up the sources in service, serviceimpl and webclient
352 *
353 * Revision 1.10 2005/08/10 09:04:49 bavo_jcs
354 * Optimized imports according to checkstyle
355 *
356 * Revision 1.9 2005/08/09 12:59:55 bavo_jcs
357 * Optimized imports
358 *
359 * Revision 1.8 2005/08/08 09:38:21 bme_jcs
360 * resolved checkstyle errors
361 *
362 * Revision 1.7 2005/08/04 11:54:14 bme_jcs
363 * resolved checkstyle errors
364 *
365 * Revision 1.6 2005/07/12 16:10:16 psong09
366 * added compareTo method and changed childComments to TreeSet, added comparator
367 *
368 * Revision 1.5 2005/06/30 10:21:55 bme_jcs
369 * bugfix to be able to insert more than 1 comment on a job
370 *
371 * Revision 1.4 2005/06/17 11:42:46 schauwvliege
372 * CheckStyle/ PMD
373 *
374 * Revision 1.3 2005/06/16 14:44:12 psong09
375 * added subject to comments
376 *
377 * Revision 1.2 2005/06/09 08:18:52 bejug_cc
378 * Fix initial import
379 *
380 * Revision 1.6 2005/05/13 14:55:49 ssc
381 * fixed error relationship when adding offer to user/comment
382 *
383 * Revision 1.5 2005/05/12 08:23:55 ssc
384 * Checkstyle errors
385 *
386 * Revision 1.4 2005/05/07 15:44:39 ssc
387 * added child commend and tests
388 *
389 * Revision 1.3 2005/05/04 15:14:17 ssc
390 * removed Subsciber
391 *
392 * Revision 1.2 2005/05/04 11:42:57 ssc
393 * added commit mappings
394 *
395 * Revision 1.1 2005/05/01 21:31:25 sja
396 * First import.
397 *
398 *
399 */