File Source: CategoryAdd.java

     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.ui.struts2.editor;
    20  
    21  import org.apache.commons.lang.StringUtils;
    22  import org.apache.commons.logging.Log;
    23  import org.apache.commons.logging.LogFactory;
    24  import org.apache.roller.weblogger.WebloggerException;
    25  import org.apache.roller.weblogger.business.WebloggerFactory;
    26  import org.apache.roller.weblogger.business.WeblogManager;
    27  import org.apache.roller.weblogger.pojos.WeblogCategory;
    28  import org.apache.roller.weblogger.pojos.WeblogPermission;
    29  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    30  import org.apache.roller.weblogger.util.cache.CacheManager;
    31  import org.apache.struts2.interceptor.validation.SkipValidation;
    32  
    33  
    34  /**
    35   * Add a new subCategory to an existing Category.
    36   */
    37  public class CategoryAdd extends UIAction {
    38      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.CategoryAdd__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    39      private static Log log = LogFactory.getLog(CategoryAdd.class);
    40      
    41      // the id of the Category we are adding the new subCategory into
    42      private String categoryId = null;
    43      
    44      // the category we are adding the new subcategory into
    45      private WeblogCategory category = null;
    46      
    47      // bean for managing form data
    48      private CategoryBean bean = new CategoryBean();
    49      
    50      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.CategoryAdd()
              * 
              *  Postconditions:
              *    this.actionName == &"categoryAdd"
              *    this.bean == &new CategoryBean(CategoryAdd#1)
              *    this.category == null
              *    this.categoryId == null
              *    this.bean.description == null
              *    this.bean.id == null
              *    this.bean.image == null
              *    this.bean.name == null
              *    this.desiredMenu == &"editor"
              *    this.pageTitle == &"categoryForm.add.title"
              *    ...
              */
    51      public CategoryAdd() {
    52          this.actionName = "categoryAdd";
    53          this.desiredMenu = "editor";
    54          this.pageTitle = "categoryForm.add.title";
    55      }
    56      
    57      
    58      // author perms required
    59      public short requiredWeblogPermissions() {
                 /* 
    P/P           *  Method: short requiredWeblogPermissions()
                  * 
                  *  Presumptions:
                  *    init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR)
                  * 
                  *  Postconditions:
                  *    return_value == org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR
                  *    (soft) init'ed(return_value)
                  */
    60          return WeblogPermission.AUTHOR;
    61      }
    62      
    63      
    64      public void myPrepare() {
    65          try {
                     /* 
    P/P               *  Method: void myPrepare()
                      * 
                      *  Preconditions:
                      *    (soft) log != null
                      *    (soft) init'ed(this.categoryId)
                      * 
                      *  Presumptions:
                      *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@66 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@66 != null
                      * 
                      *  Postconditions:
                      *    possibly_updated(this.category)
                      * 
                      *  Test Vectors:
                      *    org.apache.commons.lang.StringUtils:isEmpty(...)@67: {1}, {0}
                      */
    66              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
    67              if(!StringUtils.isEmpty(getCategoryId())) {
    68                  setCategory(wmgr.getWeblogCategory(getCategoryId()));
    69              }
    70          } catch (WebloggerException ex) {
    71              log.error("Error looking up category", ex);
    72          }
    73      }
    74      
    75      
    76      /**
    77       * Show category form.
    78       */
    79      @SkipValidation
    80      public String execute() {
    81          
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    init'ed(this.category)
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"input",&"error"}
                  * 
                  *  Test Vectors:
                  *    this.category: Inverse{null}, Addr_Set{null}
                  */
    82          if(getCategory() == null) {
    83              // TODO: i18n
    84              addError("Cannot add category to null parent category");
    85              return ERROR;
    86          }
    87          
    88          return INPUT;
    89      }
    90  
    91      
    92      /**
    93       * Save new category.
    94       */
    95      public String save() {
    96          
                 /* 
    P/P           *  Method: String save()
                  * 
                  *  Preconditions:
                  *    init'ed(this.category)
                  *    (soft) log != null
                  *    (soft) this.bean != null
                  *    (soft) init'ed(this.bean.description)
                  *    (soft) init'ed(this.bean.image)
                  *    (soft) init'ed(this.bean.name)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@119 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@119 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@121 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"success",&"input",&"error"}
                  * 
                  *  Test Vectors:
                  *    this.category: Inverse{null}, Addr_Set{null}
                  *    org.apache.roller.weblogger.ui.struts2.editor.CategoryAdd:hasActionErrors(...)@106: {1}, {0}
                  */
    97          if(getCategory() == null) {
    98              // TODO: i18n
    99              addError("Cannot add category to null parent category");
   100              return ERROR;
   101          }
   102          
   103          // validation
   104          myValidate();
   105          
   106          if(!hasActionErrors()) try {
   107              
   108              WeblogCategory newCategory = new WeblogCategory(
   109                      getActionWeblog(),
   110                      getCategory(),
   111                      getBean().getName(),
   112                      getBean().getDescription(),
   113                      getBean().getImage());
   114              
   115              // add new folder to parent
   116              getCategory().addCategory(newCategory);
   117              
   118              // save changes
   119              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   120              wmgr.saveWeblogCategory(newCategory);
   121              WebloggerFactory.getWeblogger().flush();
   122              
   123              // notify caches
   124              CacheManager.invalidate(newCategory);
   125              
   126              // TODO: i18n
   127              addMessage("category added");
   128              
   129              return SUCCESS;
   130              
   131          } catch(Exception ex) {
   132              log.error("Error saving new category", ex);
   133              // TODO: i18n
   134              addError("Error saving new category");
   135          }
   136          
   137          return INPUT;
   138      }
   139  
   140      
   141      // TODO: validation
   142      public void myValidate() {
   143          
   144          // name is required, has max length, no html
   145          
   146          // make sure new name is not a duplicate of an existing folder
                 /* 
    P/P           *  Method: void myValidate()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  *    init'ed(this.bean.name)
                  *    this.category != null
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.pojos.WeblogCategory:hasCategory(...)@147: {0}, {1}
                  */
   147          if(getCategory().hasCategory(getBean().getName())) {
   148              addError("categoryForm.error.duplicateName", getBean().getName());
   149          }
   150      }
   151  
   152      
   153      public String getCategoryId() {
                 /* 
    P/P           *  Method: String getCategoryId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.categoryId)
                  * 
                  *  Postconditions:
                  *    return_value == this.categoryId
                  *    init'ed(return_value)
                  */
   154          return categoryId;
   155      }
   156  
   157      public void setCategoryId(String categoryId) {
                 /* 
    P/P           *  Method: void setCategoryId(String)
                  * 
                  *  Postconditions:
                  *    this.categoryId == categoryId
                  *    init'ed(this.categoryId)
                  */
   158          this.categoryId = categoryId;
   159      }
   160  
   161      public WeblogCategory getCategory() {
                 /* 
    P/P           *  Method: WeblogCategory getCategory()
                  * 
                  *  Preconditions:
                  *    init'ed(this.category)
                  * 
                  *  Postconditions:
                  *    return_value == this.category
                  *    init'ed(return_value)
                  */
   162          return category;
   163      }
   164  
   165      public void setCategory(WeblogCategory category) {
                 /* 
    P/P           *  Method: void setCategory(WeblogCategory)
                  * 
                  *  Postconditions:
                  *    this.category == category
                  *    init'ed(this.category)
                  */
   166          this.category = category;
   167      }
   168  
   169      public CategoryBean getBean() {
                 /* 
    P/P           *  Method: CategoryBean getBean()
                  * 
                  *  Preconditions:
                  *    init'ed(this.bean)
                  * 
                  *  Postconditions:
                  *    return_value == this.bean
                  *    init'ed(return_value)
                  */
   170          return bean;
   171      }
   172  
   173      public void setBean(CategoryBean bean) {
                 /* 
    P/P           *  Method: void setBean(CategoryBean)
                  * 
                  *  Postconditions:
                  *    this.bean == bean
                  *    init'ed(this.bean)
                  */
   174          this.bean = bean;
   175      }
   176      
   177  }








SofCheck Inspector Build Version : 2.18479
CategoryAdd.java 2009-Jan-02 14:24:48
CategoryAdd.class 2009-Sep-04 03:12:45