Coverage Report - net.sourceforge.pebble.domain.PageBasedContent
 
Classes in this File Line Coverage Branch Coverage Complexity
PageBasedContent
90%
73/81
87%
14/16
1.265
 
 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  
 
 33  
 package net.sourceforge.pebble.domain;
 34  
 
 35  
 import net.sourceforge.pebble.PebbleContext;
 36  
 import net.sourceforge.pebble.security.PebbleUserDetails;
 37  
 import net.sourceforge.pebble.security.SecurityRealm;
 38  
 import net.sourceforge.pebble.security.SecurityRealmException;
 39  
 import net.sourceforge.pebble.util.StringUtils;
 40  
 
 41  
 import org.apache.commons.logging.Log;
 42  
 import org.apache.commons.logging.LogFactory;
 43  
 
 44  
 import java.util.*;
 45  
 
 46  
 /**
 47  
  * The superclass for blog entries and pages.
 48  
  *
 49  
  * @author Simon Brown
 50  
  */
 51  
 public abstract class PageBasedContent extends Content {
 52  
 
 53  4
   private static final Log log = LogFactory.getLog(PageBasedContent.class);
 54  
 
 55  
   public static final String TITLE_PROPERTY = "title";
 56  
   public static final String SUBTITLE_PROPERTY = "subtitle";
 57  
   public static final String BODY_PROPERTY = "body";
 58  
   public static final String AUTHOR_PROPERTY = "author";
 59  
   public static final String DATE_PROPERTY = "date";
 60  
   public static final String ORIGINAL_PERMALINK_PROPERTY = "originalPermalink";
 61  
   public static final String TAGS_PROPERTY = "tags";
 62  
 
 63  
   /**
 64  
    * the id
 65  
    */
 66  
   private String id;
 67  
 
 68  
   /**
 69  
    * the title
 70  
    */
 71  3136
   private String title = "";
 72  
 
 73  
   /**
 74  
    * the subtitle
 75  
    */
 76  3136
   private String subtitle = "";
 77  
 
 78  
   /**
 79  
    * the body
 80  
    */
 81  3136
   private String body = "";
 82  
 
 83  
   /**
 84  
    * the date that the content was created
 85  
    */
 86  
   private Date date;
 87  
 
 88  
   /**
 89  
    * the author of the blog entry
 90  
    */
 91  3136
   private String author = "";
 92  
 
 93  
   /** the enriched user details */
 94  
   private PebbleUserDetails user;
 95  
 
 96  
   /**
 97  
    * the alternative permalink for this blog entry
 98  
    */
 99  
   private String originalPermalink;
 100  
 
 101  
   /** the list of tags for this blog entry */
 102  3136
   private String tags = "";
 103  
 
 104  
   /** the List of tags for this blog entry */
 105  3136
   private List<Tag> tagsAsList = new LinkedList<Tag>();
 106  
 
 107  
   /** the tags, comma separated */
 108  3136
   private String tagsAsCommaSeparated = "";
 109  
 
 110  
   /** the owning blog */
 111  
   private Blog blog;
 112  
 
 113  3136
   private boolean persistent = false;
 114  3136
   private String lockedBy = null;
 115  
 
 116  
   /**
 117  
    * Creates a new blog entry.
 118  
    *
 119  
    * @param blog    the owning Blog
 120  
    */
 121  3136
   public PageBasedContent(Blog blog) {
 122  3136
     this.blog = blog;
 123  3136
     setDate(new Date());
 124  3136
   }
 125  
 
 126  
   /**
 127  
    * Gets the unique id of this blog entry.
 128  
    *
 129  
    * @return the id as a String
 130  
    */
 131  
   public String getId() {
 132  10692
     return id;
 133  
   }
 134  
 
 135  
   /**
 136  
    * Gets the title of this blog entry.
 137  
    *
 138  
    * @return the title as a String
 139  
    */
 140  
   public String getTitle() {
 141  4824
     return title;
 142  
   }
 143  
 
 144  
   /**
 145  
    * Sets the title of this blog entry.
 146  
    *
 147  
    * @param newTitle  the title as a String
 148  
    */
 149  
   public void setTitle(String newTitle) {
 150  2524
         newTitle = newTitle;
 151  2524
     propertyChangeSupport.firePropertyChange(TITLE_PROPERTY, title, newTitle);
 152  2524
     this.title = newTitle;
 153  2524
   }
 154  
 
 155  
   /**
 156  
    * Gets the subtitle of this blog entry.
 157  
    *
 158  
    * @return the subtitle as a String
 159  
    */
 160  
   public String getSubtitle() {
 161  1676
     return subtitle;
 162  
   }
 163  
 
 164  
   /**
 165  
    * Sets the subtitle of this blog entry.
 166  
    *
 167  
    * @param newSubtitle  the subtitle as a String
 168  
    */
 169  
   public void setSubtitle(String newSubtitle) {
 170  
         //newSubtitle = StringUtils.transformHTML(newSubtitle);
 171  1644
     propertyChangeSupport.firePropertyChange(SUBTITLE_PROPERTY, subtitle, newSubtitle);
 172  1644
     this.subtitle = newSubtitle;
 173  1644
   }
 174  
 
 175  
   /**
 176  
    * Gets the body of this blog entry.
 177  
    *
 178  
    * @return the body as a String
 179  
    */
 180  
   public String getBody() {
 181  2632
     return body;
 182  
   }
 183  
 
 184  
   /**
 185  
    * Gets the content of this response.
 186  
    *
 187  
    * @return a String
 188  
    */
 189  
   public String getContent() {
 190  0
     return body;
 191  
   }
 192  
 
 193  
   /**
 194  
    * Sets the body of this blog entry.
 195  
    *
 196  
    * @param newBody the body as a String
 197  
    */
 198  
   public void setBody(String newBody) {
 199  2680
     propertyChangeSupport.firePropertyChange(BODY_PROPERTY, body, newBody);
 200  2680
     this.body = newBody;
 201  2680
   }
 202  
 
 203  
   /**
 204  
    * Gets the date that this blog entry was created.
 205  
    *
 206  
    * @return a java.util.Date instance
 207  
    */
 208  
   public Date getDate() {
 209  3596
     return date;
 210  
   }
 211  
 
 212  
   /**
 213  
    * Gets the date that this blog entry was last updated.
 214  
    *
 215  
    * @return  a Date instance representing the time of the last comment/TrackBack
 216  
    */
 217  
   public Date getLastModified() {
 218  0
     return date;
 219  
   }
 220  
 
 221  
   /**
 222  
    * Sets the date that this blog entry was created.
 223  
    *
 224  
    * @param newDate a java.util.Date instance
 225  
    */
 226  
   public void setDate(Date newDate) {
 227  5472
     propertyChangeSupport.firePropertyChange(DATE_PROPERTY, date, newDate);
 228  5472
     this.date = newDate;
 229  5472
     this.id = "" + this.date.getTime();
 230  5472
   }
 231  
 
 232  
   /**
 233  
    * Gets the author of this blog entry.
 234  
    *
 235  
    * @return the author as a String
 236  
    */
 237  
   public String getAuthor() {
 238  2484
     return author;
 239  
   }
 240  
 
 241  
   /**
 242  
    * Gets full user details about the author including name, email-address, etc.
 243  
    *
 244  
    * @return  a PebbleUserDetails instance
 245  
    */
 246  
   public PebbleUserDetails getUser() {
 247  36
     if (this.user == null) {
 248  36
       SecurityRealm realm = PebbleContext.getInstance().getConfiguration().getSecurityRealm();
 249  
       try {
 250  36
         this.user = realm.getUser(getAuthor());
 251  0
       } catch (SecurityRealmException e) {
 252  0
         log.error("Exception encountered", e);
 253  36
       }
 254  
     }
 255  
 
 256  36
     return this.user;
 257  
   }
 258  
 
 259  
   /**
 260  
    * Sets the author of this blog entry.
 261  
    *
 262  
    * @param newAuthor the author as a String
 263  
    */
 264  
   public void setAuthor(String newAuthor) {
 265  2048
     this.author = StringUtils.transformHTML(newAuthor);
 266  2048
   }
 267  
 
 268  
   /**
 269  
    * Gets the tags associated with this category.
 270  
    *
 271  
    * @return  a list of tags
 272  
    */
 273  
   public String getTags() {
 274  1812
     return this.tags;
 275  
   }
 276  
 
 277  
   /**
 278  
    * Gets the tags associated with this category, as a List.
 279  
    *
 280  
    * @return  a List of tags
 281  
    */
 282  
   public List<Tag> getTagsAsList() {
 283  2236
     return this.tagsAsList;
 284  
   }
 285  
 
 286  
   /**
 287  
    * Gets a list of all tags, as a comma separated string.
 288  
    *
 289  
    * @return    a comma separated String of tags
 290  
    */
 291  
   public String getTagsAsCommaSeparated() {
 292  0
     return this.tagsAsCommaSeparated;
 293  
   }
 294  
 
 295  
   /**
 296  
    * Gets a list of all tags.
 297  
    *
 298  
    * @return  a List of tags
 299  
    */
 300  
   public abstract List<Tag> getAllTags();
 301  
 
 302  
   /**
 303  
    * Sets the set of tags associated with this category.
 304  
    *
 305  
    * @param newTags    a set of tags
 306  
    */
 307  
   public void setTags(String newTags) {
 308  1780
     if (newTags != null && newTags.indexOf(",") > -1) {
 309  
       // if the tags have been comma separated, convert them to
 310  
       // whitespace separated by
 311  
       // - remove whitespace
 312  
       // - convert commas to whitespace
 313  60
       newTags = newTags.replaceAll(" ", "").replaceAll(",", " ");
 314  
     }
 315  1780
     propertyChangeSupport.firePropertyChange(TAGS_PROPERTY, tags, newTags);
 316  1780
     this.tags = newTags;
 317  1780
     this.tagsAsList = Tag.parse(getBlog(), tags);
 318  1780
     this.tagsAsCommaSeparated = Tag.format(getAllTags());
 319  1780
   }
 320  
 
 321  
   /**
 322  
    * Determines whether this blog entry has been aggregated from another
 323  
    * source. An aggregated blog entry will have a specified permalink.
 324  
    *
 325  
    * @return true if this blog entry has been aggegrated, false otherwise
 326  
    */
 327  
   public boolean isAggregated() {
 328  368
     return (originalPermalink != null);
 329  
   }
 330  
 
 331  
   /**
 332  
    * Gets the alternative permalink for this blog entry.
 333  
    *
 334  
    * @return an absolute URL as a String
 335  
    */
 336  
   public String getOriginalPermalink() {
 337  1536
     return this.originalPermalink;
 338  
   }
 339  
 
 340  
   /**
 341  
    * Sets the alternative permalink for this blog entry.
 342  
    *
 343  
    * @param newPermalink an absolute URL as a String
 344  
    */
 345  
   public void setOriginalPermalink(String newPermalink) {
 346  1560
         newPermalink = StringUtils.transformHTML(newPermalink);
 347  1560
     if (newPermalink == null || newPermalink.length() == 0) {
 348  1508
       propertyChangeSupport.firePropertyChange(ORIGINAL_PERMALINK_PROPERTY, originalPermalink, null);
 349  1508
       this.originalPermalink = null;
 350  
     } else {
 351  52
       propertyChangeSupport.firePropertyChange(ORIGINAL_PERMALINK_PROPERTY, originalPermalink, newPermalink);
 352  52
       this.originalPermalink = newPermalink;
 353  
     }
 354  1560
   }
 355  
 
 356  
   /**
 357  
    * Gets a permalink for this blog entry.
 358  
    *
 359  
    * @return an absolute URL as a String
 360  
    */
 361  
   public String getPermalink() {
 362  192
     if (isAggregated()) {
 363  0
       return getOriginalPermalink();
 364  
     } else {
 365  192
       return getLocalPermalink();
 366  
     }
 367  
   }
 368  
 
 369  
   /**
 370  
    * Gets a permalink for this blog entry that is local to the blog. In other
 371  
    * words, it doesn't take into account the original permalink for
 372  
    * aggregated content.
 373  
    *
 374  
    * @return an absolute URL as a String
 375  
    */
 376  
   public abstract String getLocalPermalink();
 377  
 
 378  
   /**
 379  
    * Helper method to get the owning Blog instance.
 380  
    *
 381  
    * @return the overall owning Blog instance
 382  
    */
 383  
   public Blog getBlog() {
 384  18440
     return this.blog;
 385  
   }
 386  
 
 387  
   /**
 388  
    * Gets a string representation of this object.
 389  
    *
 390  
    * @return  a String
 391  
    */
 392  
   public String toString() {
 393  0
     return getBlog().getId() + "/" + getTitle();
 394  
   }
 395  
 
 396  
   public boolean isPersistent() {
 397  3312
     return persistent;
 398  
   }
 399  
 
 400  
   public void setPersistent(boolean persistent) {
 401  4052
     this.persistent = persistent;
 402  4052
   }
 403  
 
 404  
   /**
 405  
    * Determines whether this content is published.
 406  
    *
 407  
    * @return  true if the state is published, false otherwise
 408  
    */
 409  
   public boolean isPublished() {
 410  5952
     return getState().equals(State.PUBLISHED);
 411  
   }
 412  
 
 413  
   /**
 414  
    * Determines whether this content is unpublished.
 415  
    *
 416  
    * @return  true if the state is unpublished, false otherwise
 417  
    */
 418  
   public boolean isUnpublished() {
 419  52
     return getState().equals(State.UNPUBLISHED);
 420  
   }
 421  
 
 422  
   /**
 423  
    * Sets the state of this content.
 424  
    *
 425  
    * @param published   true if this content is published, false if unpublished
 426  
    */
 427  
   public void setPublished(boolean published) {
 428  5072
     if (published) {
 429  728
       setState(State.PUBLISHED);
 430  
     } else {
 431  4344
       setState(State.UNPUBLISHED);
 432  
     }
 433  5072
   }
 434  
 
 435  
   public String getLockedBy() {
 436  0
     return lockedBy;
 437  
   }
 438  
 
 439  
   public void setLockedBy(String lockedBy) {
 440  4
     this.lockedBy = lockedBy;
 441  4
   }
 442  
 
 443  
 }