Coverage Report - net.sourceforge.pebble.domain.Comment
 
Classes in this File Line Coverage Branch Coverage Complexity
Comment
92%
83/90
82%
43/52
1.903
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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 URL of the avatar */
 64  
   private String avatar;
 65  
 
 66  
   /** the collection of nested comments */
 67  900
   private List comments = new ArrayList();
 68  
 
 69  
   /** a flag to indicate whether the user was authenticated when the comment was left */
 70  900
   private boolean authenticated = false;
 71  
 
 72  0
   public Comment() {
 73  0
   }
 74  
 
 75  
   /**
 76  
    * Creates a new comment with the specified properties.
 77  
    *
 78  
    * @param title        the comment title
 79  
    * @param body        the comment body
 80  
    * @param author      the name of the author
 81  
    * @param website     the author's website
 82  
    * @param ipAddress the IP address of the author
 83  
    * @param date        the date that this comment was left
 84  
    * @param state       the state of the comment
 85  
    * @param blogEntry   the owning blog entry
 86  
    */
 87  
   Comment(String title, String body, String author, String email, String website, String avatar, String ipAddress, Date date, State state, BlogEntry blogEntry) {
 88  900
     super(title, ipAddress, date, state, blogEntry);
 89  
 
 90  900
     setBody(body);
 91  900
     setAuthor(author);
 92  900
     setEmail(email);
 93  900
     setWebsite(website);
 94  900
     setAvatar(avatar);
 95  900
   }
 96  
 
 97  
   /**
 98  
    * Gets the body of this comment.
 99  
    *
 100  
    * @return    the body of this comment as a String
 101  
    */
 102  
   public String getBody() {
 103  692
     return body;
 104  
   }
 105  
 
 106  
   /**
 107  
    * Gets the content of this response.
 108  
    *
 109  
    * @return a String
 110  
    */
 111  
   public String getContent() {
 112  444
     return getBody();
 113  
   }
 114  
 
 115  
   /**
 116  
    * Gets the body of this comment, truncated and without HTML tags.
 117  
    *
 118  
    * @return    the body of this comment as a String
 119  
    */
 120  
   public String getTruncatedBody() {
 121  32
     return this.getTruncatedContent();
 122  
   }
 123  
 
 124  
   /**
 125  
    * Sets the title of this comment.
 126  
    *
 127  
    * @param   title    the title of this comment as a String
 128  
    */
 129  
   public void setTitle(String title) {
 130  940
     if (title == null || title.length() == 0) {
 131  96
       if (blogEntry != null) {
 132  96
         this.title = "Re: " + blogEntry.getTitle();
 133  
       } else {
 134  0
         this.title = "Comment";
 135  
       }
 136  
     } else {
 137  844
       this.title = title;
 138  
     }
 139  940
   }
 140  
 
 141  
   /**
 142  
    * Sets the body of this comment.
 143  
    *
 144  
    * @param   body    the body of this comment as a String
 145  
    */
 146  
   public void setBody(String body) {
 147  1144
     if (body == null || body.length() == 0) {
 148  56
       this.body = null;
 149  
     } else {
 150  1088
       this.body = body;
 151  
     }
 152  1144
   }
 153  
 
 154  
   /**
 155  
    * Gets the name of the author.
 156  
    *
 157  
    * @return    the name of the author as a String
 158  
    */
 159  
   public String getAuthor() {
 160  612
     return author;
 161  
   }
 162  
 
 163  
   /**
 164  
    * Gets the name of the source of this response.
 165  
    *
 166  
    * @return a String
 167  
    */
 168  
   public String getSourceName() {
 169  224
     return getAuthor();
 170  
   }
 171  
 
 172  
   /**
 173  
    * Sets the author of this blog comment. If an author isn't specified,
 174  
    * the author is set to be "Anonymous".
 175  
    *
 176  
    * @param author    the name of the author
 177  
    */
 178  
   public void setAuthor(String author) {
 179  952
     if (author == null || author.length() == 0) {
 180  36
       this.author = "Anonymous";
 181  
     } else {
 182  916
       this.author = author;
 183  
     }
 184  952
   }
 185  
 
 186  
   /**
 187  
    * Gets the author's e-mail address.
 188  
    *
 189  
    * @return    the author's e-mail address as a String
 190  
    */
 191  
   public String getEmail() {
 192  96
     return email;
 193  
   }
 194  
 
 195  
   /**
 196  
    * Sets the author's e-mail address.
 197  
    *
 198  
    * @param email   the e-mail address
 199  
    */
 200  
   public void setEmail(String email) {
 201  944
     if (email == null || email.length() == 0) {
 202  184
       this.email = null;
 203  
     } else {
 204  760
       this.email = StringUtils.transformHTML(email);
 205  
     }
 206  944
   }
 207  
 
 208  
   /**
 209  
    * Gets the author's website.
 210  
    *
 211  
    * @return    the author's website as a String
 212  
    */
 213  
   public String getWebsite() {
 214  348
     return website;
 215  
   }
 216  
 
 217  
   /**
 218  
    * Gets the link to the source of this response.
 219  
    *
 220  
    * @return a String
 221  
    */
 222  
   public String getSourceLink() {
 223  224
     return getWebsite();
 224  
   }
 225  
 
 226  
   /**
 227  
    * Sets the author's website.
 228  
    *
 229  
    * @param website   the website url
 230  
    */
 231  
   public void setWebsite(String website) {
 232  972
     this.website = StringUtils.validateUrl(StringUtils.filterHTML(website));
 233  972
   }
 234  
 
 235  
   /**
 236  
    * Gets the permalink for this comment.
 237  
    *
 238  
    * @return  a URL as a String
 239  
    */
 240  
   public String getPermalink() {
 241  4
     if (blogEntry != null) {
 242  4
       return blogEntry.getLocalPermalink() + "#comment" + getId();
 243  
     } else {
 244  0
       return "";
 245  
     }
 246  
   }
 247  
 
 248  
   /**
 249  
    * Gets the avatar URL
 250  
    *
 251  
    * @return a URL as a String
 252  
    */
 253  
   public String getAvatar() {
 254  80
     return avatar;
 255  
   }
 256  
 
 257  
   /**
 258  
    * Sets the avatar URL
 259  
    *
 260  
    * @param avatar a URL as a String
 261  
    */
 262  
   public void setAvatar(String avatar) {
 263  932
     this.avatar = StringUtils.validateUrl(StringUtils.filterHTML(avatar));
 264  932
   }
 265  
 
 266  
   /**
 267  
    * Gets the owning comment, if this comment is nested.
 268  
    *
 269  
    * @return    a Comment instance, or null if this comment isn't nested
 270  
    */
 271  
   public Comment getParent() {
 272  732
     return this.parent;
 273  
   }
 274  
 
 275  
   /**
 276  
    * Sets the owning comment.
 277  
    *
 278  
    * @param parent    the owning Comment instance
 279  
    */
 280  
   public void setParent(Comment parent) {
 281  424
     this.parent = parent;
 282  424
   }
 283  
 
 284  
   /**
 285  
    * Gets the number of parents that this comment has.
 286  
    *
 287  
    * @return  the number of parents as an int
 288  
    */
 289  
   public int getNumberOfParents() {
 290  8
     int count = 0;
 291  8
     Comment c = getParent();
 292  12
     while (c != null) {
 293  4
       count++;
 294  4
       c = c.getParent();
 295  
     }
 296  
 
 297  8
     return count;
 298  
   }
 299  
 
 300  
   /**
 301  
    * Adds a child comment.
 302  
    *
 303  
    * @param comment   the Comment to add
 304  
    */
 305  
   void addComment(Comment comment) {
 306  72
     if (comment != null && !comments.contains(comment)) {
 307  68
       comments.add(comment);
 308  68
       comment.setParent(this);
 309  
     }
 310  72
   }
 311  
 
 312  
   /**
 313  
    * Removes a child comment.
 314  
    *
 315  
    * @param comment   the Comment to be removed
 316  
    */
 317  
   void removeComment(Comment comment) {
 318  48
     if (comment != null && comments.contains(comment)) {
 319  
       // remove all children
 320  20
       for (Comment child : getComments()) {
 321  24
         comment.removeComment(child);
 322  
       }
 323  
 
 324  
       // then remove the comment itself
 325  20
       comments.remove(comment);
 326  20
       comment.setParent(null);
 327  
 
 328  20
       if (areEventsEnabled()) {
 329  16
         getBlogEntry().addEvent(new CommentEvent(comment, CommentEvent.COMMENT_REMOVED));
 330  
       }
 331  
     }
 332  48
   }
 333  
 
 334  
   /**
 335  
    * Gets a list of comments, in the order that they were left.
 336  
    *
 337  
    * @return  a List of Comment instances
 338  
    */
 339  
   public List<Comment> getComments() {
 340  7072
     return new ArrayList<Comment>(comments);
 341  
   }
 342  
 
 343  
   /**
 344  
    * Creates and returns a copy of this object.
 345  
    *
 346  
    * @return a clone of this instance.
 347  
    * @see Cloneable
 348  
    */
 349  
   public Object clone() {
 350  292
     Comment comment = new Comment(title, body, author, email, website, avatar, ipAddress, date, getState(), blogEntry);
 351  292
     comment.setParent(parent);
 352  292
     comment.setAuthenticated(authenticated);
 353  292
     return comment;
 354  
   }
 355  
 
 356  
   public boolean equals(Object o) {
 357  196
     if (this == o) {
 358  168
       return true;
 359  
     }
 360  
 
 361  28
     if (!(o instanceof Comment)) {
 362  0
       return false;
 363  
     }
 364  
 
 365  28
     Comment comment = (Comment)o;
 366  28
     return (getId() == comment.getId() && blogEntry.getId().equals(comment.getBlogEntry().getId()));
 367  
   }
 368  
 
 369  
   public int hashCode() {
 370  0
     return ("" + getId()).hashCode();
 371  
   }
 372  
 
 373  
   /**
 374  
    * Sets the state of this comment.
 375  
    */
 376  
   void setState(State s) {
 377  1276
     State previousState = getState();
 378  1276
     super.setState(s);
 379  
 
 380  1276
     if (areEventsEnabled()) {
 381  236
       if (isApproved() && previousState != State.APPROVED) {
 382  92
         getBlogEntry().addEvent(new CommentEvent(this, CommentEvent.COMMENT_APPROVED));
 383  144
       } else if (isRejected() && previousState != State.REJECTED) {
 384  8
         getBlogEntry().addEvent(new CommentEvent(this, CommentEvent.COMMENT_REJECTED));
 385  
       }
 386  
     }
 387  1276
   }
 388  
 
 389  
   public boolean isAuthenticated() {
 390  28
     return authenticated;
 391  
   }
 392  
 
 393  
   public void setAuthenticated(boolean authenticated) {
 394  324
     this.authenticated = authenticated;
 395  324
   }
 396  
 
 397  
   @Override
 398  
   public boolean isHasEmail() {
 399  0
     return true;
 400  
   }
 401  
 }