File Source: CategoryEdit.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   * Edit an existing Category.
    36   */
    37  public class CategoryEdit extends UIAction {
    38      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.CategoryEdit__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    39      private static Log log = LogFactory.getLog(CategoryEdit.class);
    40      
    41      // the category we are editing
    42      private WeblogCategory category = null;
    43      
    44      // bean for managing form data
    45      private CategoryBean bean = new CategoryBean();
    46      
    47      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.CategoryEdit()
              * 
              *  Postconditions:
              *    this.actionName == &"categoryEdit"
              *    this.bean == &new CategoryBean(CategoryEdit#1)
              *    this.category == null
              *    this.bean.description == null
              *    this.bean.id == null
              *    this.bean.image == null
              *    this.bean.name == null
              *    this.desiredMenu == &"editor"
              *    this.pageTitle == &"categoryForm.edit.title"
              *    new CategoryBean(CategoryEdit#1) num objects == 1
              */
    48      public CategoryEdit() {
    49          this.actionName = "categoryEdit";
    50          this.desiredMenu = "editor";
    51          this.pageTitle = "categoryForm.edit.title";
    52      }
    53      
    54      
    55      // author perms required
    56      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)
                  */
    57          return WeblogPermission.AUTHOR;
    58      }
    59      
    60      
    61      public void myPrepare() {
    62          try {
                     /* 
    P/P               *  Method: void myPrepare()
                      * 
                      *  Preconditions:
                      *    (soft) log != null
                      *    (soft) this.bean != null
                      *    (soft) init'ed(this.bean.id)
                      * 
                      *  Presumptions:
                      *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@63 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@63 != null
                      * 
                      *  Postconditions:
                      *    possibly_updated(this.category)
                      * 
                      *  Test Vectors:
                      *    org.apache.commons.lang.StringUtils:isEmpty(...)@64: {1}, {0}
                      */
    63              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
    64              if(!StringUtils.isEmpty(getBean().getId())) {
    65                  setCategory(wmgr.getWeblogCategory(getBean().getId()));
    66              }
    67          } catch (WebloggerException ex) {
    68              log.error("Error looking up category", ex);
    69          }
    70      }
    71      
    72      
    73      /**
    74       * Show category form.
    75       */
    76      @SkipValidation
    77      public String execute() {
    78          
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    init'ed(this.category)
                  *    (soft) this.bean != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"input",&"error"}
                  *    possibly_updated(this.bean.description)
                  *    possibly_updated(this.bean.id)
                  *    possibly_updated(this.bean.image)
                  *    possibly_updated(this.bean.name)
                  * 
                  *  Test Vectors:
                  *    this.category: Inverse{null}, Addr_Set{null}
                  */
    79          if(getCategory() == null) {
    80              // TODO: i18n
    81              addError("Cannot edit null category");
    82              return ERROR;
    83          }
    84          
    85          // make sure bean is properly loaded from pojo data
    86          getBean().copyFrom(getCategory());
    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(...)@112 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@112 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@114 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"input",&"error"}
                  * 
                  *  Test Vectors:
                  *    this.category: Inverse{null}, Addr_Set{null}
                  *    org.apache.roller.weblogger.ui.struts2.editor.CategoryEdit:hasActionErrors(...)@106: {1}, {0}
                  */
    97          if(getCategory() == null) {
    98              // TODO: i18n
    99              addError("Cannot edit null category");
   100              return ERROR;
   101          }
   102          
   103          // validation
   104          myValidate();
   105          
   106          if(!hasActionErrors()) try {
   107              
   108              // copy updated attributes
   109              getBean().copyTo(getCategory());
   110              
   111              // save changes
   112              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   113              wmgr.saveWeblogCategory(getCategory());
   114              WebloggerFactory.getWeblogger().flush();
   115              
   116              // notify caches
   117              CacheManager.invalidate(getCategory());
   118              
   119              // TODO: i18n
   120              addMessage("category updated");
   121              
   122          } catch(Exception ex) {
   123              log.error("Error saving category", ex);
   124              // TODO: i18n
   125              addError("Error saving category");
   126          }
   127          
   128          return INPUT;
   129      }
   130  
   131      
   132      // TODO: validation
   133      public void myValidate() {
   134          
   135          // name is required, has max length, no html
   136          
   137          // make sure new name is not a duplicate of an existing category
                 /* 
    P/P           *  Method: void myValidate()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  *    init'ed(this.bean.name)
                  *    this.category != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.pojos.WeblogCategory:getName(...)@138 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@138: {1}, {0}
                  *    org.apache.roller.weblogger.pojos.WeblogCategory:getParent(...)@139: Addr_Set{null}, Inverse{null}
                  *    org.apache.roller.weblogger.pojos.WeblogCategory:hasCategory(...)@140: {0}, {1}
                  */
   138          if(!getCategory().getName().equals(getBean().getName())) {
   139              WeblogCategory parent = getCategory().getParent();
   140              if(parent != null && parent.hasCategory(getBean().getName())) {
   141                  addError("categoryForm.error.duplicateName", getBean().getName());
   142              }
   143          }
   144      }
   145  
   146  
   147      public WeblogCategory getCategory() {
                 /* 
    P/P           *  Method: WeblogCategory getCategory()
                  * 
                  *  Preconditions:
                  *    init'ed(this.category)
                  * 
                  *  Postconditions:
                  *    return_value == this.category
                  *    init'ed(return_value)
                  */
   148          return category;
   149      }
   150  
   151      public void setCategory(WeblogCategory category) {
                 /* 
    P/P           *  Method: void setCategory(WeblogCategory)
                  * 
                  *  Postconditions:
                  *    this.category == category
                  *    init'ed(this.category)
                  */
   152          this.category = category;
   153      }
   154  
   155      public CategoryBean getBean() {
                 /* 
    P/P           *  Method: CategoryBean getBean()
                  * 
                  *  Preconditions:
                  *    init'ed(this.bean)
                  * 
                  *  Postconditions:
                  *    return_value == this.bean
                  *    init'ed(return_value)
                  */
   156          return bean;
   157      }
   158  
   159      public void setBean(CategoryBean bean) {
                 /* 
    P/P           *  Method: void setBean(CategoryBean)
                  * 
                  *  Postconditions:
                  *    this.bean == bean
                  *    init'ed(this.bean)
                  */
   160          this.bean = bean;
   161      }
   162      
   163  }








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