Coverage Report - net.sourceforge.pebble.domain.StaticPage
 
Classes in this File Line Coverage Branch Coverage Complexity
StaticPage
26%
12/45
0%
0/14
1.818
 
 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.util.StringUtils;
 35  
 import net.sourceforge.pebble.web.validation.ValidationContext;
 36  
 
 37  
 import java.util.ArrayList;
 38  
 import java.util.Collections;
 39  
 import java.util.List;
 40  
 
 41  
 /**
 42  
  * Represents a page.
 43  
  *
 44  
  * @author Simon Brown
 45  
  */
 46  
 public class StaticPage extends PageBasedContent {
 47  
 
 48  
         public static final String TEMPLATE_PROPERTY = "template";
 49  
         
 50  
   /** the name of the page */
 51  
   private String name;
 52  
   
 53  
   /** the name of the template **/
 54  60
   private String template = TEMPLATE_PROPERTY;
 55  
 
 56  
   /**
 57  
    * Creates a new blog entry.
 58  
    *
 59  
    * @param blog    the owning Blog
 60  
    */
 61  
   public StaticPage(Blog blog) {
 62  60
     super(blog);
 63  60
     setPublished(true);
 64  60
   }
 65  
 
 66  
   public String getName() {
 67  0
     return name;
 68  
   }
 69  
 
 70  
   public void setName(String name) {
 71  4
     this.name = StringUtils.transformHTML(name);
 72  4
   }
 73  
 
 74  
   /**
 75  
    * Gets a list of all tags.
 76  
    *
 77  
    * @return  a List of tags
 78  
    */
 79  
   public List<Tag> getAllTags() {
 80  4
     List<Tag> list = new ArrayList<Tag>(getTagsAsList());
 81  4
     Collections.sort(list);
 82  
 
 83  4
     return list;
 84  
   }
 85  
 
 86  
   /**
 87  
    * Gets a permalink for this blog entry that is local to the blog. In other
 88  
    * words, it doesn't take into account the original permalink for
 89  
    * aggregated content.
 90  
    *
 91  
    * @return an absolute URL as a String
 92  
    */
 93  
   public String getLocalPermalink() {
 94  0
     return getBlog().getUrl() + "pages/" + name + ".html";
 95  
   }
 96  
 
 97  
   public void validate(ValidationContext context) {
 98  
         // TODO: localize
 99  0
     if (name == null || name.length() == 0) {
 100  0
       context.addError("Name cannot be empty");
 101  0
     } else if (!name.matches("[\\w_/-]+")) {
 102  0
       context.addError("Name \"" + StringUtils.transformHTML(name) + "\" must contain only A-Za-z0-9_-/");
 103  
     }
 104  
 
 105  0
     String id = getBlog().getStaticPageIndex().getStaticPage(name);
 106  0
     if (id != null && !id.equals(getId())) {
 107  0
       context.addError("A page with the name \"" + name + "\" already exists");
 108  
     }
 109  0
   }
 110  
 
 111  
   public String getGuid() {
 112  0
     return "page/" + getBlog().getId() + "/" + getId();
 113  
   }
 114  
 
 115  
   public void setTemplate(String newTemplate) {
 116  4
     propertyChangeSupport.firePropertyChange(TEMPLATE_PROPERTY, template, newTemplate);
 117  4
     this.template = newTemplate;
 118  4
   }
 119  
   
 120  
   public String getTemplate() {
 121  0
     return template;
 122  
   }
 123  
   
 124  
   /**
 125  
    * Indicates whether some other object is "equal to" this one.
 126  
    *
 127  
    * @param o   the reference object with which to compare.
 128  
    * @return <code>true</code> if this object is the same as the obj
 129  
    *         argument; <code>false</code> otherwise.
 130  
    * @see #hashCode()
 131  
    * @see java.util.Hashtable
 132  
    */
 133  
   public boolean equals(Object o) {
 134  0
     if (this == o) {
 135  0
       return true;
 136  
     }
 137  
 
 138  0
     if (!(o instanceof StaticPage)) {
 139  0
       return false;
 140  
     }
 141  
 
 142  0
     StaticPage staticPage = (StaticPage)o;
 143  0
     return getGuid().equals(staticPage.getGuid());
 144  
   }
 145  
 
 146  
   /**
 147  
    * Creates and returns a copy of this object.
 148  
    *
 149  
    * @return a clone of this instance.
 150  
    * @see Cloneable
 151  
    */
 152  
   public Object clone() {
 153  0
     StaticPage page = new StaticPage(getBlog());
 154  0
     page.setEventsEnabled(false);
 155  0
     page.setPersistent(isPersistent());
 156  0
     page.setTitle(getTitle());
 157  0
     page.setSubtitle(getSubtitle());
 158  0
     page.setBody(getBody());
 159  0
     page.setDate(getDate());
 160  0
     page.setState(getState());
 161  0
     page.setAuthor(getAuthor());
 162  0
     page.setOriginalPermalink(getOriginalPermalink());
 163  0
     page.setName(getName());
 164  0
     page.setTags(getTags());
 165  0
     page.setLockedBy(getLockedBy());
 166  0
     page.setTemplate(getTemplate());
 167  
 
 168  0
     return page;
 169  
   }
 170  
 
 171  
 }