Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
75   386   57   2,68
34   172   0,76   28
28     2,04  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  Comment       Line # 46 75 0% 57 9 93,4% 0.93430656
 
  (158)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.domain;
33   
34    import net.sourceforge.pebble.api.event.comment.CommentEvent;
35    import net.sourceforge.pebble.util.StringUtils;
36   
37    import java.util.ArrayList;
38    import java.util.Date;
39    import java.util.List;
40   
41    /**
42    * Represents a blog comment.
43    *
44    * @author Simon Brown
45    */
 
46    public class Comment extends Response {
47   
48    /** the body of the comment */
49    private String body;
50   
51    /** the name of the author */
52    private String author;
53   
54    /** the author's e-mail address */
55    private String email;
56   
57    /** the author's website */
58    private String website;
59   
60    /** the parent comment, if applicable */
61    private Comment parent;
62   
63    /** the collection of nested comments */
64    private List comments = new ArrayList();
65   
66    /** a flag to indicate whether the user was authenticated when the comment was left */
67    private boolean authenticated = false;
68   
 
69  0 toggle public Comment() {
70    }
71   
72    /**
73    * Creates a new comment with the specified properties.
74    *
75    * @param title the comment title
76    * @param body the comment body
77    * @param author the name of the author
78    * @param website the author's website
79    * @param ipAddress the IP address of the author
80    * @param date the date that this comment was left
81    * @param state the state of the comment
82    * @param blogEntry the owning blog entry
83    */
 
84  444 toggle Comment(String title, String body, String author, String email, String website, String ipAddress, Date date, State state, BlogEntry blogEntry) {
85  444 super(title, ipAddress, date, state, blogEntry);
86   
87  444 setBody(body);
88  444 setAuthor(author);
89  444 setEmail(email);
90  444 setWebsite(website);
91    }
92   
93    /**
94    * Gets the body of this comment.
95    *
96    * @return the body of this comment as a String
97    */
 
98  344 toggle public String getBody() {
99  344 return body;
100    }
101   
102    /**
103    * Gets the content of this response.
104    *
105    * @return a String
106    */
 
107  222 toggle public String getContent() {
108  222 return getBody();
109    }
110   
111    /**
112    * Gets the body of this comment, truncated and without HTML tags.
113    *
114    * @return the body of this comment as a String
115    */
 
116  16 toggle public String getTruncatedBody() {
117  16 return this.getTruncatedContent();
118    }
119   
120    /**
121    * Sets the title of this comment.
122    *
123    * @param title the title of this comment as a String
124    */
 
125  464 toggle public void setTitle(String title) {
126  464 if (title == null || title.length() == 0) {
127  48 if (blogEntry != null) {
128  48 this.title = "Re: " + blogEntry.getTitle();
129    } else {
130  0 this.title = "Comment";
131    }
132    } else {
133  416 this.title = title;
134    }
135    }
136   
137    /**
138    * Sets the body of this comment.
139    *
140    * @param body the body of this comment as a String
141    */
 
142  566 toggle public void setBody(String body) {
143  566 if (body == null || body.length() == 0) {
144  28 this.body = null;
145    } else {
146  538 this.body = body;
147    }
148    }
149   
150    /**
151    * Gets the name of the author.
152    *
153    * @return the name of the author as a String
154    */
 
155  304 toggle public String getAuthor() {
156  304 return author;
157    }
158   
159    /**
160    * Gets the name of the source of this response.
161    *
162    * @return a String
163    */
 
164  112 toggle public String getSourceName() {
165  112 return getAuthor();
166    }
167   
168    /**
169    * Sets the author of this blog comment. If an author isn't specified,
170    * the author is set to be "Anonymous".
171    *
172    * @param author the name of the author
173    */
 
174  470 toggle public void setAuthor(String author) {
175  470 if (author == null || author.length() == 0) {
176  18 this.author = "Anonymous";
177    } else {
178  452 this.author = author;
179    }
180    }
181   
182    /**
183    * Gets the author's e-mail address.
184    *
185    * @return the author's e-mail address as a String
186    */
 
187  46 toggle public String getEmail() {
188  46 return email;
189    }
190   
191    /**
192    * Sets the author's e-mail address.
193    *
194    * @param email the e-mail address
195    */
 
196  466 toggle public void setEmail(String email) {
197  466 if (email == null || email.length() == 0) {
198  92 this.email = null;
199    } else {
200  374 this.email = StringUtils.transformHTML(email);
201    }
202    }
203   
204    /**
205    * Gets the author's website.
206    *
207    * @return the author's website as a String
208    */
 
209  172 toggle public String getWebsite() {
210  172 return website;
211    }
212   
213    /**
214    * Gets the link to the source of this response.
215    *
216    * @return a String
217    */
 
218  112 toggle public String getSourceLink() {
219  112 return getWebsite();
220    }
221   
222    /**
223    * Sets the author's website.
224    *
225    * @param website the website url
226    */
 
227  480 toggle public void setWebsite(String website) {
228  480 website = StringUtils.filterHTML(website);
229  480 if (website == null || website.length() == 0) {
230  54 this.website = null;
231  426 } else if (
232  426 !website.startsWith("http://") &&
233    !website.startsWith("https://") &&
234    !website.startsWith("ftp://") &&
235    !website.startsWith("mailto:")) {
236  78 this.website = "http://" + website;
237    } else {
238  348 this.website = website;
239    }
240    }
241   
242    /**
243    * Gets the permalink for this comment.
244    *
245    * @return a URL as a String
246    */
 
247  2 toggle public String getPermalink() {
248  2 if (blogEntry != null) {
249  2 return blogEntry.getLocalPermalink() + "#comment" + getId();
250    } else {
251  0 return "";
252    }
253    }
254   
255    /**
256    * Gets the owning comment, if this comment is nested.
257    *
258    * @return a Comment instance, or null if this comment isn't nested
259    */
 
260  360 toggle public Comment getParent() {
261  360 return this.parent;
262    }
263   
264    /**
265    * Sets the owning comment.
266    *
267    * @param parent the owning Comment instance
268    */
 
269  212 toggle public void setParent(Comment parent) {
270  212 this.parent = parent;
271    }
272   
273    /**
274    * Gets the number of parents that this comment has.
275    *
276    * @return the number of parents as an int
277    */
 
278  4 toggle public int getNumberOfParents() {
279  4 int count = 0;
280  4 Comment c = getParent();
281  6 while (c != null) {
282  2 count++;
283  2 c = c.getParent();
284    }
285   
286  4 return count;
287    }
288   
289    /**
290    * Adds a child comment.
291    *
292    * @param comment the Comment to add
293    */
 
294  36 toggle void addComment(Comment comment) {
295  36 if (comment != null && !comments.contains(comment)) {
296  34 comments.add(comment);
297  34 comment.setParent(this);
298    }
299    }
300   
301    /**
302    * Removes a child comment.
303    *
304    * @param comment the Comment to be removed
305    */
 
306  24 toggle void removeComment(Comment comment) {
307  24 if (comment != null && comments.contains(comment)) {
308    // remove all children
309  10 for (Comment child : getComments()) {
310  12 comment.removeComment(child);
311    }
312   
313    // then remove the comment itself
314  10 comments.remove(comment);
315  10 comment.setParent(null);
316   
317  10 if (areEventsEnabled()) {
318  8 getBlogEntry().addEvent(new CommentEvent(comment, CommentEvent.COMMENT_REMOVED));
319    }
320    }
321    }
322   
323    /**
324    * Gets a list of comments, in the order that they were left.
325    *
326    * @return a List of Comment instances
327    */
 
328  3295 toggle public List<Comment> getComments() {
329  3295 return new ArrayList<Comment>(comments);
330    }
331   
332    /**
333    * Creates and returns a copy of this object.
334    *
335    * @return a clone of this instance.
336    * @see Cloneable
337    */
 
338  146 toggle public Object clone() {
339  146 Comment comment = new Comment(title, body, author, email, website, ipAddress, date, getState(), blogEntry);
340  146 comment.setParent(parent);
341  146 comment.setAuthenticated(authenticated);
342  146 return comment;
343    }
344   
 
345  98 toggle public boolean equals(Object o) {
346  98 if (this == o) {
347  84 return true;
348    }
349   
350  14 if (!(o instanceof Comment)) {
351  0 return false;
352    }
353   
354  14 Comment comment = (Comment)o;
355  14 return (getId() == comment.getId() && blogEntry.getId().equals(comment.getBlogEntry().getId()));
356    }
357   
 
358  0 toggle public int hashCode() {
359  0 return ("" + getId()).hashCode();
360    }
361   
362    /**
363    * Sets the state of this comment.
364    */
 
365  632 toggle void setState(State s) {
366  632 State previousState = getState();
367  632 super.setState(s);
368   
369  632 if (areEventsEnabled()) {
370  118 if (isApproved() && previousState != State.APPROVED) {
371  46 getBlogEntry().addEvent(new CommentEvent(this, CommentEvent.COMMENT_APPROVED));
372  72 } else if (isRejected() && previousState != State.REJECTED) {
373  4 getBlogEntry().addEvent(new CommentEvent(this, CommentEvent.COMMENT_REJECTED));
374    }
375    }
376    }
377   
 
378  12 toggle public boolean isAuthenticated() {
379  12 return authenticated;
380    }
381   
 
382  160 toggle public void setAuthenticated(boolean authenticated) {
383  160 this.authenticated = authenticated;
384    }
385   
386    }