File Source: Categories.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 java.util.Collections;
    22  import java.util.List;
    23  import java.util.LinkedList;
    24  import java.util.Set;
    25  import java.util.TreeSet;
    26  import org.apache.commons.lang.StringUtils;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.roller.weblogger.WebloggerException;
    30  import org.apache.roller.weblogger.business.WebloggerFactory;
    31  import org.apache.roller.weblogger.business.WeblogManager;
    32  import org.apache.roller.weblogger.pojos.WeblogPermission;
    33  import org.apache.roller.weblogger.pojos.WeblogCategory;
    34  import org.apache.roller.weblogger.pojos.WeblogCategoryPathComparator;
    35  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    36  
    37  
    38  /**
    39   * Manage weblog categories.
    40   */
    41  public class Categories extends UIAction {
    42      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.Categories__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    43      private static Log log = LogFactory.getLog(Categories.class);
    44      
    45      // the id of the category we are viewing
    46      private String categoryId = null;
    47      
    48      // the category we are viewing
    49      private WeblogCategory category = null;
    50      
    51      // list of category ids to move
    52      private String[] selectedCategories = null;
    53      
    54      // category id of the category to move to
    55      private String targetCategoryId = null;
    56      
    57      // all categories from the action weblog
    58      private Set allCategories = Collections.EMPTY_SET;
    59      
    60      // path of categories representing selected categories hierarchy
    61      private List categoryPath = Collections.EMPTY_LIST;
    62      
    63      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.Categories()
              * 
              *  Presumptions:
              *    init'ed(java.util.Collections.EMPTY_LIST)
              *    init'ed(java.util.Collections.EMPTY_SET)
              * 
              *  Postconditions:
              *    this.actionName == &"categories"
              *    this.allCategories == java.util.Collections.EMPTY_SET
              *    (soft) init'ed(this.allCategories)
              *    this.category == null
              *    this.categoryId == null
              *    this.selectedCategories == null
              *    this.targetCategoryId == null
              *    this.categoryPath == java.util.Collections.EMPTY_LIST
              *    (soft) init'ed(this.categoryPath)
              *    this.desiredMenu == &"editor"
              *    ...
              */
    64      public Categories() {
    65          this.actionName = "categories";
    66          this.desiredMenu = "editor";
    67          this.pageTitle = "categoriesForm.rootTitle";
    68      }
    69      
    70      
    71      // author perms required
    72      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)
                  */
    73          return WeblogPermission.AUTHOR;
    74      }
    75      
    76      
    77      public void myPrepare() {
    78          try {
                     /* 
    P/P               *  Method: void myPrepare()
                      * 
                      *  Preconditions:
                      *    (soft) log != null
                      *    (soft) init'ed(this.categoryId)
                      * 
                      *  Presumptions:
                      *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@79 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@79 != null
                      * 
                      *  Postconditions:
                      *    possibly_updated(this.category)
                      * 
                      *  Test Vectors:
                      *    java.lang.String:equals(...)@80: {1}, {0}
                      *    org.apache.commons.lang.StringUtils:isEmpty(...)@80: {1}, {0}
                      */
    79              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
    80              if(!StringUtils.isEmpty(getCategoryId()) && 
    81                      !"/".equals(getCategoryId())) {
    82                  setCategory(wmgr.getWeblogCategory(getCategoryId()));
    83              } else {
    84                  setCategory(wmgr.getRootWeblogCategory(getActionWeblog()));
    85              }
    86          } catch (WebloggerException ex) {
    87              log.error("Error looking up category", ex);
    88          }
    89      }
    90      
    91      
    92      public String execute() {
    93          
    94          // build list of categories for display
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    (soft) log != null
                  *    (soft) this.category != null
                  *    (soft) init'ed(this.categoryId)
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@101 != null
                  *    org.apache.roller.weblogger.business.WeblogManager:getWeblogCategories(...)@100 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@99 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@99 != null
                  *    org.apache.roller.weblogger.pojos.WeblogCategory:getId(...)@102 != null
                  * 
                  *  Postconditions:
                  *    return_value == &"list"
                  *    this.allCategories == One-of{old this.allCategories, &new TreeSet(execute#1)}
                  *    this.categoryPath == One-of{old this.categoryPath, &new LinkedList(execute#3)}
                  *    new LinkedList(execute#3) num objects <= 1
                  *    new TreeSet(execute#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@102: {1}, {0}
                  *    java.util.Iterator:hasNext(...)@101: {0}, {1}
                  *    java.util.TreeSet:size(...)@124: {-231..0}, {1..232-1}
                  *    org.apache.roller.weblogger.pojos.WeblogCategory:getParent(...)@108: Addr_Set{null}, Inverse{null}
                  */
    95          TreeSet allCategories = new TreeSet(new WeblogCategoryPathComparator());
    96          
    97          try {
    98              // Build list of all categories, except for current one, sorted by path.
    99              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   100              List<WeblogCategory> cats = wmgr.getWeblogCategories(getActionWeblog(), true);
   101              for(WeblogCategory cat : cats) {
   102                  if (!cat.getId().equals(getCategoryId())) {
   103                      allCategories.add(cat);
   104                  }
   105              }
   106              
   107              // build category path
   108              WeblogCategory parent = getCategory().getParent();
   109              if(parent != null) {
   110                  List categoryPath = new LinkedList();
   111                  categoryPath.add(0, getCategory());
   112                  while (parent != null) {
   113                      categoryPath.add(0, parent);
   114                      parent = parent.getParent();
   115                  }
   116                  setCategoryPath(categoryPath);
   117              }
   118          } catch (WebloggerException ex) {
   119              log.error("Error building categories list", ex);
   120              // TODO: i18n
   121              addError("Error building categories list");
   122          }
   123          
   124          if (allCategories.size() > 0) {
   125              setAllCategories(allCategories);
   126          }
   127          
   128          return LIST;
   129      }
   130      
   131      
   132      public String move() {
   133          
   134          try {
                     /* 
    P/P               *  Method: String move()
                      * 
                      *  Preconditions:
                      *    log != null
                      *    (soft) this.category != null
                      *    (soft) init'ed(this.categoryId)
                      *    (soft) init'ed(this.selectedCategories)
                      *    (soft) this.selectedCategories.length <= 232-1
                      *    (soft) init'ed(this.selectedCategories[...])
                      *    (soft) init'ed(this.targetCategoryId)
                      * 
                      *  Presumptions:
                      *    org.apache.roller.weblogger.business.WeblogManager:getWeblogCategory(...)@141 != null
                      *    org.apache.roller.weblogger.business.WeblogManager:getWeblogCategory(...)@144 != null
                      *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@135 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@135 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@157 != null
                      *    ...
                      * 
                      *  Postconditions:
                      *    return_value == &"list"
                      *    this.allCategories == One-of{old this.allCategories, &new TreeSet(execute#1)}
                      *    this.categoryPath == One-of{old this.categoryPath, &new LinkedList(execute#3)}
                      *    new LinkedList(execute#3) num objects <= 1
                      *    new TreeSet(execute#1) num objects == 1
                      * 
                      *  Test Vectors:
                      *    this.selectedCategories: Addr_Set{null}, Inverse{null}
                      *    java.lang.String:equals(...)@148: {1}, {0}
                      *    org.apache.roller.weblogger.pojos.WeblogCategory:descendentOf(...)@148: {1}, {0}
                      */
   135              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   136              
   137              log.debug("Moving categories to category - "+getTargetCategoryId());
   138              
   139              // Move subCategories to new category.
   140              String[] cats = getSelectedCategories();
   141              WeblogCategory parent = wmgr.getWeblogCategory(getTargetCategoryId());
   142              if(cats != null) {
   143                  for (int i = 0; i < cats.length; i++) {
   144                      WeblogCategory cd =
   145                              wmgr.getWeblogCategory(cats[i]);
   146                      
   147                      // Don't move category into itself.
   148                      if (!cd.getId().equals(parent.getId()) && 
   149                              !parent.descendentOf(cd)) {
   150                          wmgr.moveWeblogCategory(cd, parent);
   151                      } else {
   152                          addMessage("categoriesForm.warn.notMoving", cd.getName());
   153                      }
   154                  }
   155                  
   156                  // flush changes
   157                  WebloggerFactory.getWeblogger().flush();
   158              }
   159              
   160          } catch (WebloggerException ex) {
   161              log.error("Error moving categories", ex);
   162              addError("categoriesForm.error.move");
   163          }
   164          
   165          return execute();
   166      }
   167      
   168  
   169      public String getCategoryId() {
                 /* 
    P/P           *  Method: String getCategoryId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.categoryId)
                  * 
                  *  Postconditions:
                  *    return_value == this.categoryId
                  *    init'ed(return_value)
                  */
   170          return categoryId;
   171      }
   172  
   173      public void setCategoryId(String categoryId) {
                 /* 
    P/P           *  Method: void setCategoryId(String)
                  * 
                  *  Postconditions:
                  *    this.categoryId == categoryId
                  *    init'ed(this.categoryId)
                  */
   174          this.categoryId = categoryId;
   175      }
   176  
   177      public WeblogCategory getCategory() {
                 /* 
    P/P           *  Method: WeblogCategory getCategory()
                  * 
                  *  Preconditions:
                  *    init'ed(this.category)
                  * 
                  *  Postconditions:
                  *    return_value == this.category
                  *    init'ed(return_value)
                  */
   178          return category;
   179      }
   180  
   181      public void setCategory(WeblogCategory category) {
                 /* 
    P/P           *  Method: void setCategory(WeblogCategory)
                  * 
                  *  Postconditions:
                  *    this.category == category
                  *    init'ed(this.category)
                  */
   182          this.category = category;
   183      }
   184  
   185      public String[] getSelectedCategories() {
                 /* 
    P/P           *  Method: String[] getSelectedCategories()
                  * 
                  *  Preconditions:
                  *    init'ed(this.selectedCategories)
                  * 
                  *  Postconditions:
                  *    return_value == this.selectedCategories
                  *    init'ed(return_value)
                  */
   186          return selectedCategories;
   187      }
   188  
   189      public void setSelectedCategories(String[] selectedCategories) {
                 /* 
    P/P           *  Method: void setSelectedCategories(String[])
                  * 
                  *  Postconditions:
                  *    this.selectedCategories == selectedCategories
                  *    init'ed(this.selectedCategories)
                  */
   190          this.selectedCategories = selectedCategories;
   191      }
   192  
   193      public String getTargetCategoryId() {
                 /* 
    P/P           *  Method: String getTargetCategoryId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.targetCategoryId)
                  * 
                  *  Postconditions:
                  *    return_value == this.targetCategoryId
                  *    init'ed(return_value)
                  */
   194          return targetCategoryId;
   195      }
   196  
   197      public void setTargetCategoryId(String targetCategoryId) {
                 /* 
    P/P           *  Method: void setTargetCategoryId(String)
                  * 
                  *  Postconditions:
                  *    this.targetCategoryId == targetCategoryId
                  *    init'ed(this.targetCategoryId)
                  */
   198          this.targetCategoryId = targetCategoryId;
   199      }
   200  
   201      public Set getAllCategories() {
                 /* 
    P/P           *  Method: Set getAllCategories()
                  * 
                  *  Preconditions:
                  *    init'ed(this.allCategories)
                  * 
                  *  Postconditions:
                  *    return_value == this.allCategories
                  *    init'ed(return_value)
                  */
   202          return allCategories;
   203      }
   204  
   205      public void setAllCategories(Set allCategories) {
                 /* 
    P/P           *  Method: void setAllCategories(Set)
                  * 
                  *  Postconditions:
                  *    this.allCategories == allCategories
                  *    init'ed(this.allCategories)
                  */
   206          this.allCategories = allCategories;
   207      }
   208  
   209      public List getCategoryPath() {
                 /* 
    P/P           *  Method: List getCategoryPath()
                  * 
                  *  Preconditions:
                  *    init'ed(this.categoryPath)
                  * 
                  *  Postconditions:
                  *    return_value == this.categoryPath
                  *    init'ed(return_value)
                  */
   210          return categoryPath;
   211      }
   212  
   213      public void setCategoryPath(List categoryPath) {
                 /* 
    P/P           *  Method: void setCategoryPath(List)
                  * 
                  *  Postconditions:
                  *    this.categoryPath == categoryPath
                  *    init'ed(this.categoryPath)
                  */
   214          this.categoryPath = categoryPath;
   215      }
   216      
   217  }








SofCheck Inspector Build Version : 2.18479
Categories.java 2009-Jan-02 14:25:08
Categories.class 2009-Sep-04 03:12:45