File Source: WeblogCategory.java

         /* 
    P/P   *  Method: void readObject(ObjectInputStream)
          * 
          *  Preconditions:
          *    Param_1 != null
          * 
          *  Presumptions:
          *    init'ed(org.apache.openjpa.enhance.PersistenceCapable.DESERIALIZED)
          * 
          *  Postconditions:
          *    Param_0.pcDetachedState == org.apache.openjpa.enhance.PersistenceCapable.DESERIALIZED
          *    (soft) init'ed(Param_0.pcDetachedState)
          */
     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.pojos;
    20  
    21  import java.io.Serializable;
    22  import java.util.HashSet;
    23  import java.util.Iterator;
    24  import java.util.List;
    25  import java.util.Set;
    26  import org.apache.commons.lang.builder.EqualsBuilder;
    27  import org.apache.commons.lang.builder.HashCodeBuilder;
    28  import org.apache.commons.logging.Log;
    29  import org.apache.commons.logging.LogFactory;
    30  import org.apache.roller.weblogger.WebloggerException;
    31  import org.apache.roller.weblogger.business.WebloggerFactory;
    32  import org.apache.roller.weblogger.business.WeblogManager;
    33  import org.apache.roller.util.UUIDGenerator;
    34  
    35  
    36  /**
    37   * Weblog Category.
    38   * 
    39   * @hibernate.cache usage="read-write"
    40   * @hibernate.class lazy="true" table="weblogcategory"
    41   */
    42  public class WeblogCategory implements Serializable, Comparable {
    43      
    44      public static final long serialVersionUID = 1435782148712018954L;
    45      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.pojos.WeblogCategory__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              *    pcFieldFlags == &new byte[](WeblogCategory__static_init#3)
              *    pcFieldNames == &new String[](WeblogCategory__static_init#1)
              *    pcFieldTypes == &new Class[](WeblogCategory__static_init#2)
              *    new Class[](WeblogCategory__static_init#2) num objects == 1
              *    new String[](WeblogCategory__static_init#1) num objects == 1
              *    new byte[](WeblogCategory__static_init#3) num objects == 1
              *    pcFieldTypes.length == 8
              *    pcFieldNames.length == 8
              *    pcFieldFlags.length == 8
              *    ...
              */
    46      private static Log log = LogFactory.getLog(WeblogCategory.class);
    47      
    48      // attributes
    49      private String id = UUIDGenerator.generateUUID();
    50      private String name = null;
    51      private String description = null;
    52      private String image = null;
    53      private String path = null;
    54      
    55      // associations
    56      private Weblog website = null;
    57      private WeblogCategory parentCategory = null;
    58      private Set childCategories = new HashSet();
    59      
    60      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.pojos.WeblogCategory()
              * 
              *  Postconditions:
              *    this.childCategories == &new HashSet(WeblogCategory#1)
              *    this.description == null
              *    this.image == null
              *    this.name == null
              *    this.parentCategory == null
              *    this.path == null
              *    this.website == null
              *    init'ed(this.id)
              *    new HashSet(WeblogCategory#1) num objects == 1
              */
    61      public WeblogCategory() {
    62      }
    63      
    64      public WeblogCategory(
    65              Weblog website,
    66              WeblogCategory parent,
    67              String name,
    68              String description,
                     /* 
    P/P               *  Method: void org.apache.roller.weblogger.pojos.WeblogCategory(Weblog, WeblogCategory, String, String, String)
                      * 
                      *  Preconditions:
                      *    (soft) init'ed(parent.path)
                      *    (soft) init'ed(parent.pcStateManager)
                      *    (soft) pcInheritedFieldCount <= 232-6
                      * 
                      *  Postconditions:
                      *    init'ed(java.lang.StringBuilder:toString(...)._tainted)
                      *    this.childCategories == &new HashSet(WeblogCategory#1)
                      *    this.description == description
                      *    init'ed(this.description)
                      *    init'ed(this.id)
                      *    this.image == image
                      *    init'ed(this.image)
                      *    this.name == name
                      *    init'ed(this.name)
                      *    this.parentCategory == parent
                      *    ...
                      * 
                      *  Test Vectors:
                      *    parent: Inverse{null}, Addr_Set{null}
                      *    java.lang.String:equals(...)@81: {0}, {1}
                      */
    69              String image) {
    70          
    71          this.name = name;
    72          this.description = description;
    73          this.image = image;
    74          
    75          this.website = website;
    76          this.parentCategory = parent;
    77  
    78          // calculate path
    79          if(parent == null) {
    80              this.path = "/";
    81          } else if("/".equals(parent.getPath())) {
    82              this.path = "/"+name;
    83          } else {
    84              this.path = parent.getPath() + "/" + name;
    85          }
    86      }
    87      
    88      
    89      //------------------------------------------------------- Good citizenship
    90  
    91      public String toString() {
                 /* 
    P/P           *  Method: String toString()
                  * 
                  *  Preconditions:
                  *    init'ed(this.id)
                  *    init'ed(this.path)
                  * 
                  *  Postconditions:
                  *    java.lang.StringBuffer:toString(...)._tainted == this.id._tainted | this.path._tainted
                  *    init'ed(java.lang.StringBuffer:toString(...)._tainted)
                  *    return_value == &java.lang.StringBuffer:toString(...)
                  */
    92          StringBuffer buf = new StringBuffer();
    93          buf.append("{");
    94          buf.append(this.id);
    95          buf.append(", ").append(this.path);
    96          buf.append("}");
    97          return buf.toString();
    98      }
    99      
   100      public boolean equals(Object other) {
   101          
                 /* 
    P/P           *  Method: bool equals(Object)
                  * 
                  *  Preconditions:
                  *    (soft) init'ed(other.path)
                  *    (soft) init'ed(other.pcStateManager)
                  *    (soft) pcInheritedFieldCount <= 232-6
                  *    (soft) init'ed(this.path)
                  *    (soft) init'ed(this.pcStateManager)
                  * 
                  *  Presumptions:
                  *    org.apache.commons.lang.builder.EqualsBuilder:append(...)@106 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    other: Inverse{null}, Addr_Set{null}
                  */
   102          if (other == null) return false;
   103          
   104          if (other instanceof WeblogCategory) {
   105              WeblogCategory o = (WeblogCategory)other;
   106              return new EqualsBuilder()
   107                  .append(getPath(), o.getPath()) 
   108                  //.append(getWebsite(), o.getWebsite()) 
   109                  .isEquals();
   110          }        
   111          return false;
   112      }
   113          
   114      public int hashCode() {
                 /* 
    P/P           *  Method: int hashCode()
                  * 
                  *  Preconditions:
                  *    init'ed(this.path)
                  *    init'ed(this.pcStateManager)
                  *    (soft) pcInheritedFieldCount <= 232-6
                  * 
                  *  Presumptions:
                  *    org.apache.commons.lang.builder.HashCodeBuilder:append(...)@115 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   115          return new HashCodeBuilder()
   116              .append(getPath())
   117              //.append(getWebsite())
   118              .toHashCode();
   119      }
   120      
   121      /**
   122       * @see java.lang.Comparable#compareTo(java.lang.Object)
   123       */
   124      public int compareTo(Object o) {
                 /* 
    P/P           *  Method: int compareTo(Object)
                  * 
                  *  Preconditions:
                  *    o != null
                  *    init'ed(o.name)
                  *    init'ed(o.pcStateManager)
                  *    this.name != null
                  *    init'ed(this.pcStateManager)
                  *    (soft) pcInheritedFieldCount <= 232-4
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   125          WeblogCategory other = (WeblogCategory)o;
   126          return getName().compareTo(other.getName());
   127      }
   128      
   129      
   130      /**
   131       * Database surrogate key.
   132       *
   133       * @hibernate.id column="id" generator-class="assigned"  
   134       */
   135      public String getId() {
                 /* 
    P/P           *  Method: String pcgetId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.id)
                  * 
                  *  Postconditions:
                  *    return_value == this.id
                  *    init'ed(return_value)
                  */
   136          return this.id;
   137      }
   138      
   139      public void setId(String id) {
                 /* 
    P/P           *  Method: void pcsetId(String)
                  * 
                  *  Postconditions:
                  *    this.id == Param_1
                  *    init'ed(this.id)
                  */
   140          this.id = id;
   141      }
   142      
   143      
   144      /**
   145       * The display name for this category.
   146       *
   147       * @hibernate.property column="name" non-null="true" unique="false"
   148       */
   149      public String getName() {
                 /* 
    P/P           *  Method: String pcgetName()
                  * 
                  *  Preconditions:
                  *    init'ed(this.name)
                  * 
                  *  Postconditions:
                  *    return_value == this.name
                  *    init'ed(return_value)
                  */
   150          return this.name;
   151      }
   152      
   153      public void setName(String name) {
                 /* 
    P/P           *  Method: void pcsetName(String)
                  * 
                  *  Postconditions:
                  *    this.name == Param_1
                  *    init'ed(this.name)
                  */
   154          this.name = name;
   155      }
   156      
   157      
   158      /**
   159       * A full description for this category.
   160       *
   161       * @hibernate.property column="description" non-null="true" unique="false"
   162       */
   163      public String getDescription() {
                 /* 
    P/P           *  Method: String pcgetDescription()
                  * 
                  *  Preconditions:
                  *    init'ed(this.description)
                  * 
                  *  Postconditions:
                  *    return_value == this.description
                  *    init'ed(return_value)
                  */
   164          return this.description;
   165      }
   166      
   167      public void setDescription(String description) {
                 /* 
    P/P           *  Method: void pcsetDescription(String)
                  * 
                  *  Postconditions:
                  *    this.description == Param_1
                  *    init'ed(this.description)
                  */
   168          this.description = description;
   169      }
   170      
   171      
   172      /**
   173       * An image icon to represent this category.
   174       *
   175       * @hibernate.property column="image" non-null="true" unique="false"
   176       */
   177      public String getImage() {
                 /* 
    P/P           *  Method: String pcgetImage()
                  * 
                  *  Preconditions:
                  *    init'ed(this.image)
                  * 
                  *  Postconditions:
                  *    return_value == this.image
                  *    init'ed(return_value)
                  */
   178          return this.image;
   179      }
   180      
   181      public void setImage(String image) {
                 /* 
    P/P           *  Method: void pcsetImage(String)
                  * 
                  *  Postconditions:
                  *    this.image == Param_1
                  *    init'ed(this.image)
                  */
   182          this.image = image;
   183      }
   184      
   185      
   186      /**
   187       * The full path to this category in the hierarchy.
   188       *
   189       * @hibernate.property column="path" non-null="true" unique="false"
   190       */
   191      public String getPath() {
                 /* 
    P/P           *  Method: String pcgetPath()
                  * 
                  *  Preconditions:
                  *    init'ed(this.path)
                  * 
                  *  Postconditions:
                  *    return_value == this.path
                  *    init'ed(return_value)
                  */
   192          return this.path;
   193      }
   194      
   195      public void setPath(String path) {
                 /* 
    P/P           *  Method: void pcsetPath(String)
                  * 
                  *  Postconditions:
                  *    this.path == Param_1
                  *    init'ed(this.path)
                  */
   196          this.path = path;
   197      }
   198      
   199      
   200      /**
   201       * Get the weblog which owns this category.
   202       *
   203       * @hibernate.many-to-one column="websiteid" cascade="none" not-null="true"
   204       */
   205      public Weblog getWebsite() {
                 /* 
    P/P           *  Method: Weblog pcgetWebsite()
                  * 
                  *  Preconditions:
                  *    init'ed(this.website)
                  * 
                  *  Postconditions:
                  *    return_value == this.website
                  *    init'ed(return_value)
                  */
   206          return website;
   207      }
   208      
   209      public void setWebsite(Weblog website) {
                 /* 
    P/P           *  Method: void pcsetWebsite(Weblog)
                  * 
                  *  Postconditions:
                  *    this.website == Param_1
                  *    init'ed(this.website)
                  */
   210          this.website = website;
   211      }
   212      
   213      
   214      /**
   215       * Get parent category, or null if category is root of hierarchy.
   216       *
   217       * @hibernate.many-to-one column="parentid" cascade="none" not-null="false"
   218       */
   219      public WeblogCategory getParent() {
                 /* 
    P/P           *  Method: WeblogCategory pcgetParent()
                  * 
                  *  Preconditions:
                  *    init'ed(this.parentCategory)
                  * 
                  *  Postconditions:
                  *    return_value == this.parentCategory
                  *    init'ed(return_value)
                  */
   220          return this.parentCategory;
   221      }
   222      
   223      public void setParent(WeblogCategory parent) {
                 /* 
    P/P           *  Method: void pcsetParent(WeblogCategory)
                  * 
                  *  Postconditions:
                  *    this.parentCategory == Param_1
                  *    init'ed(this.parentCategory)
                  */
   224          this.parentCategory = parent;
   225      }
   226      
   227      
   228      /**
   229       * Get child categories of this category.
   230       * 
   231       * @hibernate.collection-key column="parentid"
   232       * @hibernate.collection-one-to-many class="org.apache.roller.weblogger.pojos.WeblogCategory"
   233       * @hibernate.set lazy="true" inverse="true" cascade="delete" order-by="name"
   234       */
   235      public Set getWeblogCategories() {
                 /* 
    P/P           *  Method: Set pcgetWeblogCategories()
                  * 
                  *  Preconditions:
                  *    init'ed(this.childCategories)
                  * 
                  *  Postconditions:
                  *    return_value == this.childCategories
                  *    init'ed(return_value)
                  */
   236          return this.childCategories;
   237      }
   238      
   239      private void setWeblogCategories(Set cats) {
                 /* 
    P/P           *  Method: void pcsetWeblogCategories(Set)
                  * 
                  *  Postconditions:
                  *    this.childCategories == Param_1
                  *    init'ed(this.childCategories)
                  */
   240          this.childCategories = cats;
   241      }
   242      
   243      
   244      /**
   245       * Retrieve all weblog entries in this category and, optionally, include
   246       * weblog entries all sub-categories.
   247       *
   248       * @param subcats True if entries from sub-categories are to be returned.
   249       * @return List of WeblogEntryData objects.
   250       * @throws WebloggerException
   251       */
   252      public List retrieveWeblogEntries(boolean subcats) throws WebloggerException {
                 /* 
    P/P           *  Method: List retrieveWeblogEntries(bool)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@253 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@253 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   253          WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   254          return wmgr.getWeblogEntries(this, subcats);
   255      }
   256      
   257      
   258      /**
   259       * Add a category as a child of this category.
   260       */
   261      public void addCategory(WeblogCategory category) {
   262          
   263          // make sure category is not null
                 /* 
    P/P           *  Method: void addCategory(WeblogCategory)
                  * 
                  *  Preconditions:
                  *    category != null
                  *    category.name != null
                  *    init'ed(category.pcStateManager)
                  *    this.childCategories != null
                  *    init'ed(this.pcStateManager)
                  *    (soft) init'ed(category.parentCategory)
                  *    (soft) pcInheritedFieldCount <= 232-7
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:hasNext(...)@290 == 0
                  * 
                  *  Postconditions:
                  *    category.parentCategory == One-of{this, old category.parentCategory}
                  *    (soft) init'ed(category.parentCategory)
                  */
   264          if(category == null || category.getName() == null) {
   265              throw new IllegalArgumentException("Category cannot be null and must have a valid name");
   266          }
   267          
   268          // make sure we don't already have a category with that name
   269          if(this.hasCategory(category.getName())) {
   270              throw new IllegalArgumentException("Duplicate category name '"+category.getName()+"'");
   271          }
   272          
   273          // set ourselves as the parent of the category
   274          category.setParent(this);
   275          
   276          // add it to our list of child categories
   277          getWeblogCategories().add(category);
   278      }
   279      
   280      
   281      /**
   282       * Does this category have a child category with the specified name?
   283       *
   284       * @param name The name of the category to check for.
   285       * @return boolean true if child category exists, false otherwise.
   286       */
   287      public boolean hasCategory(String name) {
                 /* 
    P/P           *  Method: bool hasCategory(String)
                  * 
                  *  Preconditions:
                  *    this.childCategories != null
                  *    init'ed(this.pcStateManager)
                  *    (soft) name != null
                  *    (soft) pcInheritedFieldCount <= 232-7
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@291 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@292: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@290: {0}, {1}
                  */
   288          Iterator cats = this.getWeblogCategories().iterator();
   289          WeblogCategory cat = null;
   290          while(cats.hasNext()) {
   291              cat = (WeblogCategory) cats.next();
   292              if(name.equals(cat.getName())) {
   293                  return true;
   294              }
   295          }
   296          return false;
   297      }
   298      
   299      
   300      /**
   301       * Is this category a descendent of the other category?
   302       */
   303      public boolean descendentOf(WeblogCategory ancestor) {
   304          
   305          // if this is a root node then we can't be a descendent
                 /* 
    P/P           *  Method: bool descendentOf(WeblogCategory)
                  * 
                  *  Preconditions:
                  *    init'ed(this.parentCategory)
                  *    init'ed(this.pcStateManager)
                  *    (soft) ancestor != null
                  *    (soft) init'ed(ancestor.path)
                  *    (soft) init'ed(ancestor.pcStateManager)
                  *    (soft) pcInheritedFieldCount <= 232-6
                  *    (soft) this.path != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    this.parentCategory: Inverse{null}, Addr_Set{null}
                  */
   306          if(getParent() == null) {
   307              return false;
   308          } else {
   309              // if our path starts with our parents path then we are a descendent
   310              return this.path.startsWith(ancestor.getPath());
   311          }
   312      }
   313      
   314      
   315      /**
   316       * Determine if category is in use. Returns true if any weblog entries
   317       * use this category or any of it's subcategories.
   318       */
   319      public boolean isInUse() {
   320          try {
                     /* 
    P/P               *  Method: bool isInUse()
                      * 
                      *  Presumptions:
                      *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@321 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@321 != null
                      * 
                      *  Postconditions:
                      *    init'ed(return_value)
                      */
   321              return WebloggerFactory.getWeblogger().getWeblogManager().isWeblogCategoryInUse(this);
   322          } catch (WebloggerException e) {
   323              throw new RuntimeException(e);
   324          }
   325      }
   326      
   327      
   328      // convenience method for updating the category name, which triggers a path tree rebuild
   329      public void updateName(String newName) throws WebloggerException {
   330          
   331          // update name
                 /* 
    P/P           *  Method: void updateName(String)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    this.childCategories != null
                  *    init'ed(this.parentCategory)
                  *    init'ed(this.pcStateManager)
                  *    (soft) init'ed(this.name)
                  *    (soft) init'ed(this.path)
                  *    (soft) pcInheritedFieldCount <= 232-7
                  *    (soft) init'ed(this...path)
                  *    (soft) init'ed(this...pcStateManager)
                  * 
                  *  Postconditions:
                  *    init'ed(java.lang.StringBuilder:toString(...)._tainted)
                  *    this.name == One-of{newName, old this.name}
                  *    (soft) init'ed(this.name)
                  *    this.path == One-of{&".", old this.path, &java.lang.StringBuilder:toString(...)}
                  *    init'ed(this.path)
                  * 
                  *  Test Vectors:
                  *    this.parentCategory: Inverse{null}, Addr_Set{null}
                  *    java.lang.String:equals(...)@337: {0}, {1}
                  */
   332          setName(newName);
   333          
   334          // calculate path
   335          if(getParent() == null) {
   336              setPath("/");
   337          } else if("/".equals(getParent().getPath())) {
   338              setPath("/"+getName());
   339          } else {
   340              setPath(getParent().getPath() + "/" + getName());
   341          }
   342          
   343          // update path tree for all children
   344          updatePathTree(this);
   345      }
   346      
   347      
   348      // updates the paths of all descendents of the given category
   349      public static void updatePathTree(WeblogCategory cat) 
   350              throws WebloggerException {
   351          
                 /* 
    P/P           *  Method: void updatePathTree(WeblogCategory)
                  * 
                  *  Preconditions:
                  *    cat != null
                  *    cat.childCategories != null
                  *    init'ed(cat.path)
                  *    init'ed(cat.pcStateManager)
                  *    log != null
                  *    (soft) pcInheritedFieldCount <= 232-7
                  * 
                  *  Presumptions:
                  *    childCat.childCategories@369 != null
                  *    java.util.Iterator:next(...)@357 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@367 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@367 != null
                  * 
                  *  Postconditions:
                  *    init'ed(java.lang.StringBuilder:toString(...)._tainted)
                  *    possibly_updated(java.lang.StringBuilder:toString(...)._tainted)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@362: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@356: {0}, {1}
                  */
   352          log.debug("Updating path tree for category "+cat.getPath());
   353          
   354          WeblogCategory childCat = null;
   355          Iterator childCats = cat.getWeblogCategories().iterator();
   356          while(childCats.hasNext()) {
   357              childCat = (WeblogCategory) childCats.next();
   358              
   359              log.debug("OLD child category path was "+childCat.getPath());
   360              
   361              // update path and save
   362              if("/".equals(cat.getPath())) {
   363                  childCat.setPath("/" + childCat.getName());
   364              } else {
   365                  childCat.setPath(cat.getPath() + "/" + childCat.getName());
   366              }
   367              WebloggerFactory.getWeblogger().getWeblogManager().saveWeblogCategory(childCat);
   368              
   369              log.debug("NEW child category path is "+ childCat.getPath());
   370              
   371              // then make recursive call to update this cats children
   372              updatePathTree(childCat);
   373          }
   374      }
   375      
   376  }
+  377  Other Messages








SofCheck Inspector Build Version : 2.18479
WeblogCategory.java 2009-Jan-02 14:25:18
WeblogCategory.class 2009-Sep-04 03:12:38