Coverage Report - net.sourceforge.pebble.domain.Response
 
Classes in this File Line Coverage Branch Coverage Complexity
Response
95%
46/48
90%
9/10
1.217
 
 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 java.util.Calendar;
 35  
 import java.util.Date;
 36  
 
 37  
 import net.sourceforge.pebble.util.StringUtils;
 38  
 
 39  
 /**
 40  
  * Represents a response to a blog entry - either a comment or a TrackBack.
 41  
  *
 42  
  * @author    Simon Brown
 43  
  */
 44  
 public abstract class Response extends Content {
 45  
 
 46  
   /** the title */
 47  
   protected String title;
 48  
 
 49  
   /** the ip address of the author */
 50  
   protected String ipAddress;
 51  
 
 52  
   /** the date that the trackback was received */
 53  
   protected Date date;
 54  
 
 55  
   /** the parent blog entry */
 56  
   protected BlogEntry blogEntry;
 57  
 
 58  
   /** a score used to help identify spam when repsonses are added */
 59  1208
   private int spamScore = 0;
 60  
 
 61  
   /**
 62  
    * Default, no args constructor.
 63  
    */
 64  0
   public Response() {
 65  0
   }
 66  
 
 67  
   /**
 68  
    * Creates a new instance with the specified properties.
 69  
    *
 70  
    * @param title       the title of the entry
 71  
    * @param ipAddress   the IP address of the author
 72  
    * @param date        the date that this comment was left
 73  
    * @param state       the state of the comment
 74  
    * @param blogEntry   the owning blog entry
 75  
    */
 76  1208
   Response(String title, String ipAddress, Date date, State state, BlogEntry blogEntry) {
 77  1208
     this.blogEntry = blogEntry;
 78  
 
 79  1208
     setTitle(title);
 80  1208
     setIpAddress(ipAddress);
 81  1208
     setDate(date);
 82  1208
     setState(state);
 83  1208
   }
 84  
 
 85  
   /**
 86  
    * Gets the id of this comment.
 87  
    *
 88  
    * @return    the id as a primitive long
 89  
    */
 90  
   public long getId() {
 91  7132
     return date.getTime();
 92  
   }
 93  
 
 94  
   /**
 95  
    * Gets the globally unique id of this response.
 96  
    *
 97  
    * @return  a String of the form type/blogEntryId/responseId
 98  
    */
 99  
   public String getGuid() {
 100  1120
     String s = "";
 101  1120
     if (this instanceof Comment) {
 102  956
       s = "c/";
 103  164
     } else if (this instanceof TrackBack) {
 104  164
       s = "t/";
 105  
     }
 106  
 
 107  1120
     s+= getBlogEntry().getId() + "/" + getId();
 108  
 
 109  1120
     return s;
 110  
   }
 111  
 
 112  
   /**
 113  
    * Gets the title.
 114  
    *
 115  
    * @return  the title as a String
 116  
    */
 117  
   public String getTitle() {
 118  836
     return this.title;
 119  
   }
 120  
 
 121  
   /**
 122  
    * Sets the title of the blog entry for this trackback.
 123  
    *
 124  
    * @param   title   the title as a String
 125  
    */
 126  
   public void setTitle(String title) {
 127  324
     this.title = StringUtils.transformHTML(title);
 128  324
   }
 129  
 
 130  
   /**
 131  
    * Gets the name of the source of this response.
 132  
    *
 133  
    * @return  a String
 134  
    */
 135  
   public abstract String getSourceName();
 136  
 
 137  
   /**
 138  
    * Gets the link to the source of this response.
 139  
    *
 140  
    * @return  a String
 141  
    */
 142  
   public abstract String getSourceLink();
 143  
 
 144  
   /**
 145  
    * Gets the IP address.
 146  
    *
 147  
    * @return  the IP address as a String
 148  
    */
 149  
   public String getIpAddress() {
 150  1524
     return ipAddress;
 151  
   }
 152  
 
 153  
   /**
 154  
    * Sets the IP address.
 155  
    *
 156  
    * @param ipAddress   the IP address of the responder
 157  
    */
 158  
   public void setIpAddress(String ipAddress) {
 159  1208
     if (ipAddress == null || ipAddress.length() == 0) {
 160  104
       this.ipAddress = null;
 161  
     } else {
 162  1104
       this.ipAddress = ipAddress;
 163  
     }
 164  1208
   }
 165  
 
 166  
   /**
 167  
    * Gets the date that this response was received.
 168  
    *
 169  
    * @return    the date as a java.util.Date instance.
 170  
    */
 171  
   public Date getDate() {
 172  3144
     return date;
 173  
   }
 174  
 
 175  
   /**
 176  
    * Sets the date that this response was received.
 177  
    *
 178  
    * @param   date    the date as a java.util.Date instance.
 179  
    */
 180  
   public void setDate(Date date) {
 181  1580
     if (date == null) {
 182  4
       date = new Date();
 183  
     }
 184  
 
 185  1580
     Calendar cal = blogEntry.getBlog().getCalendar();
 186  1580
     cal.setTime(date);
 187  1580
     this.date = cal.getTime();
 188  1580
   }
 189  
 
 190  
   /**
 191  
    * Gets the owning blog entry.
 192  
    *
 193  
    * @return    the owning BlogEntry instance
 194  
    */
 195  
   public BlogEntry getBlogEntry() {
 196  3076
     return blogEntry;
 197  
   }
 198  
 
 199  
   /**
 200  
    * Sets the owning blog entry.
 201  
    *
 202  
    * @param blogEntry   the owning BlogEntry instance
 203  
    */
 204  
   void setBlogEntry(BlogEntry blogEntry) {
 205  568
     this.blogEntry = blogEntry;
 206  568
   }
 207  
 
 208  
   /**
 209  
    * Gets the spam score.
 210  
    *
 211  
    * @return  an int
 212  
    */
 213  
   public int getSpamScore() {
 214  388
     return this.spamScore;
 215  
   }
 216  
 
 217  
   /**
 218  
    * Increments the spam score by 1.
 219  
    */
 220  
   public void incrementSpamScore() {
 221  148
     this.spamScore++;
 222  148
   }
 223  
 
 224  
   /**
 225  
    * Sets the state of this response to rejected.
 226  
    */
 227  
   public void setRejected() {
 228  28
     setState(State.REJECTED);
 229  28
   }
 230  
 
 231  
   /**
 232  
    * Determines whether this response is rejected.
 233  
    *
 234  
    * @return  true if the state is rejected, false otherwise
 235  
    */
 236  
   public boolean isRejected() {
 237  196
     return getState().equals(State.REJECTED);
 238  
   }
 239  
 
 240  
   /**
 241  
    * Sets the state of this response to approved.
 242  
    */
 243  
   public void setApproved() {
 244  124
     setState(State.APPROVED);
 245  124
   }
 246  
 
 247  
   /**
 248  
    * Determines whether this response is approved.
 249  
    *
 250  
    * @return  true if the state is approved, false otherwise
 251  
    */
 252  
   public boolean isApproved() {
 253  808
     return getState().equals(State.APPROVED);
 254  
   }
 255  
 
 256  
   /**
 257  
    * Sets the state of this response to pending.
 258  
    */
 259  
   public void setPending() {
 260  360
     setState(State.PENDING);
 261  360
   }
 262  
 
 263  
   /**
 264  
    * Determines whether this response is pending.
 265  
    *
 266  
    * @return  true if the state is pending, false otherwise
 267  
    */
 268  
   public boolean isPending() {
 269  292
     return getState().equals(State.PENDING);
 270  
   }
 271  
 
 272  
   /**
 273  
    * Whether this response has an email field
 274  
    */
 275  
   public abstract boolean isHasEmail();
 276  
 
 277  
 }