File Source: Templates.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.ArrayList;
    22  import java.util.Collections;
    23  import java.util.Date;
    24  import java.util.List;
    25  import org.apache.commons.lang.StringUtils;
    26  import org.apache.commons.logging.Log;
    27  import org.apache.commons.logging.LogFactory;
    28  import org.apache.roller.weblogger.WebloggerException;
    29  import org.apache.roller.weblogger.business.WebloggerFactory;
    30  import org.apache.roller.weblogger.business.UserManager;
    31  import org.apache.roller.weblogger.pojos.WeblogPermission;
    32  import org.apache.roller.weblogger.pojos.WeblogTemplate;
    33  import org.apache.roller.weblogger.pojos.WeblogTheme;
    34  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    35  
    36  
    37  /**
    38   * Templates listing page.
    39   */
    40  public class Templates extends UIAction {
    41      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.Templates__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    42      private static Log log = LogFactory.getLog(Templates.class);
    43      
    44      // list of templates to display
    45      private List<WeblogTemplate> templates = Collections.EMPTY_LIST;
    46      
    47      // list of template action types user is allowed to create
    48      private List availableActions = Collections.EMPTY_LIST;
    49      
    50      // name and action of new template if we are adding a template
    51      private String newTmplName = null;
    52      private String newTmplAction = null;
    53      
    54      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.Templates()
              * 
              *  Presumptions:
              *    init'ed(java.util.Collections.EMPTY_LIST)
              * 
              *  Postconditions:
              *    this.actionName == &"templates"
              *    this.actionWeblog == null
              *    this.authenticatedUser == null
              *    this.newTmplAction == null
              *    this.newTmplName == null
              *    this.weblog == null
              *    this.availableActions == java.util.Collections.EMPTY_LIST
              *    (soft) init'ed(this.availableActions)
              *    this.templates == this.availableActions
              *    this.desiredMenu == &"editor"
              *    ...
              */
    55      public Templates() {
    56          this.actionName = "templates";
    57          this.desiredMenu = "editor";
    58          this.pageTitle = "pagesForm.title";
    59      }
    60      
    61      
    62      public short requiredWeblogPermissions() {
                 /* 
    P/P           *  Method: short requiredWeblogPermissions()
                  * 
                  *  Presumptions:
                  *    init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN)
                  * 
                  *  Postconditions:
                  *    return_value == org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN
                  *    (soft) init'ed(return_value)
                  */
    63          return WeblogPermission.ADMIN;
    64      }
    65      
    66      
    67      public String execute() {
    68          
    69          // query for templates list
    70          try {
                     /* 
    P/P               *  Method: String execute()
                      * 
                      *  Preconditions:
                      *    this.actionWeblog != null
                      *    (soft) log != null
                      * 
                      *  Presumptions:
                      *    java.util.Iterator:next(...)@95 != null
                      *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@71 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@71 != null
                      *    org.apache.roller.weblogger.pojos.Weblog:getTheme(...)@77 != null
                      *    org.apache.roller.weblogger.pojos.Weblog:getTheme(...)@78 != null
                      *    ...
                      * 
                      *  Postconditions:
                      *    return_value == &"list"
                      *    this.availableActions == One-of{&new ArrayList(execute#2), old this.availableActions}
                      *    this.templates == One-of{&new ArrayList(execute#1), old this.templates}
                      *    new ArrayList(execute#1) num objects <= 1
                      *    new ArrayList(execute#2) num objects <= 1
                      * 
                      *  Test Vectors:
                      *    java.lang.String:equals(...)@87: {0}, {1}
                      *    java.lang.String:equals(...)@96: {1}, {0}
                      *    java.util.Iterator:hasNext(...)@95: {0}, {1}
                      *    org.apache.roller.weblogger.pojos.WeblogTheme:getStylesheet(...)@77: Addr_Set{null}, Inverse{null}
                      */
    71              UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
    72              
    73              // get current list of templates, minus custom stylesheet
    74              List<WeblogTemplate> raw = mgr.getPages(getActionWeblog()); 
    75              List<WeblogTemplate> pages = new ArrayList<WeblogTemplate>();
    76              pages.addAll(raw);
    77              if(getActionWeblog().getTheme().getStylesheet() != null) {
    78                  pages.remove(mgr.getPageByLink(getActionWeblog(), 
    79                          getActionWeblog().getTheme().getStylesheet().getLink()));
    80              }
    81              setTemplates(pages);
    82              
    83              // build list of action types that may be added
    84              List availableActions = new ArrayList();
    85              availableActions.add(WeblogTemplate.ACTION_CUSTOM);
    86              
    87              if(WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme())) {
    88                  // if the weblog is using a custom theme then determine which
    89                  // action templates are still available to be created
    90                  availableActions.add(WeblogTemplate.ACTION_PERMALINK);
    91                  availableActions.add(WeblogTemplate.ACTION_SEARCH);
    92                  availableActions.add(WeblogTemplate.ACTION_WEBLOG);
    93                  availableActions.add(WeblogTemplate.ACTION_TAGSINDEX);
    94                  
    95                  for(WeblogTemplate tmpPage : getTemplates()) {
    96                      if(!WeblogTemplate.ACTION_CUSTOM.equals(tmpPage.getAction())) {
    97                          availableActions.remove(tmpPage.getAction());
    98                      }
    99                  }
   100              }
   101              setAvailableActions(availableActions);
   102  
   103          } catch (WebloggerException ex) {
   104              log.error("Error getting templates for weblog - "+getActionWeblog().getHandle(), ex);
   105              // TODO: i18n
   106              addError("Error getting template list");
   107          }
   108          
   109          return LIST;
   110      }
   111      
   112      
   113      /**
   114       * Save a new template.
   115       */
   116      public String add() {
   117          
   118          // validation
                 /* 
    P/P           *  Method: String add()
                  * 
                  *  Preconditions:
                  *    init'ed(this.newTmplAction)
                  *    (soft) log != null
                  *    (soft) this.newTmplName != null
                  *    (soft) this.actionWeblog != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@142 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@142 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@153 != null
                  * 
                  *  Postconditions:
                  *    return_value == &"list"
                  *    this.availableActions == One-of{&new ArrayList(execute#2), old this.availableActions}
                  *    this.newTmplAction == One-of{old this.newTmplAction, null}
                  *    init'ed(this.newTmplAction)
                  *    this.newTmplName == One-of{old this.newTmplName, null}
                  *    (soft) init'ed(this.newTmplName)
                  *    this.templates == One-of{&new ArrayList(execute#1), old this.templates}
                  *    new ArrayList(execute#1) num objects <= 1
                  *    new ArrayList(execute#2) num objects == new ArrayList(execute#1) num objects
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@137: {1}, {0}
                  *    java.lang.String:equals(...)@147: {0}, {1}
                  *    org.apache.roller.weblogger.ui.struts2.editor.Templates:hasActionErrors(...)@121: {1}, {0}
                  */
   119          myValidate();
   120          
   121          if(!hasActionErrors()) try {
   122              
   123              WeblogTemplate newTemplate = new WeblogTemplate();
   124              newTemplate.setWebsite(getActionWeblog());
   125              newTemplate.setAction(getNewTmplAction());
   126              newTemplate.setName(getNewTmplName());
   127              newTemplate.setDescription(newTemplate.getName());
   128              newTemplate.setContents(getText("pageForm.newTemplateContent"));
   129              newTemplate.setHidden(false);
   130              newTemplate.setNavbar(false);
   131              newTemplate.setLastModified( new Date() );
   132              
   133              // all templates start out as velocity templates
   134              newTemplate.setTemplateLanguage("velocity");
   135              
   136              // for now, all templates just use _decorator
   137              if(!"_decorator".equals(newTemplate.getName())) {
   138                  newTemplate.setDecoratorName("_decorator");
   139              }
   140              
   141              // save the new Template
   142              UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
   143              mgr.savePage( newTemplate );
   144              
   145              // if this person happened to create a Weblog template from
   146              // scratch then make sure and set the defaultPageId
   147              if(WeblogTemplate.DEFAULT_PAGE.equals(newTemplate.getName())) {
   148                  getActionWeblog().setDefaultPageId(newTemplate.getId());
   149                  mgr.saveWebsite(getActionWeblog());
   150              }
   151              
   152              // flush results to db
   153              WebloggerFactory.getWeblogger().flush();
   154              
   155              // reset form fields
   156              setNewTmplName(null);
   157              setNewTmplAction(null);
   158              
   159          } catch (WebloggerException ex) {
   160              log.error("Error adding new template for weblog - "+getActionWeblog().getHandle(), ex);
   161              // TODO: i18n
   162              addError("Error adding new template");
   163          }
   164          
   165          return execute();
   166      }
   167      
   168      
   169      // validation when adding a new template
   170      private void myValidate() {
   171          
   172          // make sure name is non-null and within proper size
                 /* 
    P/P           *  Method: void myValidate()
                  * 
                  *  Preconditions:
                  *    init'ed(this.newTmplAction)
                  *    (soft) log != null
                  *    (soft) init'ed(this.actionWeblog)
                  *    (soft) this.newTmplName != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@186 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@186 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:length(...)@175: {0..255}, {256..232-1}
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@173: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@180: {0}, {1}
                  *    org.apache.roller.weblogger.business.UserManager:getPageByName(...)@187: Addr_Set{null}, Inverse{null}
                  */
   173          if(StringUtils.isEmpty(getNewTmplName())) {
   174              addError("TemplateEdit.error.nameNull");
   175          } else if(getNewTmplName().length() > 255) {
   176              addError("TemplateEdit.error.nameSize");
   177          }
   178          
   179          // make sure action is a valid
   180          if(StringUtils.isEmpty(getNewTmplAction())) {
   181              addError("TemplateEdit.error.actionNull");
   182          }
   183          
   184          // check if template by that name already exists
   185          try {
   186              UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
   187              WeblogTemplate existingPage = umgr.getPageByName(getActionWeblog(), getNewTmplName());
   188              if(existingPage != null) {
   189                  addError("pagesForm.error.alreadyExists", getNewTmplName());
   190              }
   191          } catch (WebloggerException ex) {
   192              log.error("Error checking for existing template", ex);
   193          }
   194      }
   195      
   196      
   197      public List<WeblogTemplate> getTemplates() {
                 /* 
    P/P           *  Method: List getTemplates()
                  * 
                  *  Preconditions:
                  *    init'ed(this.templates)
                  * 
                  *  Postconditions:
                  *    return_value == this.templates
                  *    init'ed(return_value)
                  */
   198          return templates;
   199      }
   200  
   201      public void setTemplates(List<WeblogTemplate> templates) {
                 /* 
    P/P           *  Method: void setTemplates(List)
                  * 
                  *  Postconditions:
                  *    this.templates == templates
                  *    init'ed(this.templates)
                  */
   202          this.templates = templates;
   203      }
   204  
   205      public List getAvailableActions() {
                 /* 
    P/P           *  Method: List getAvailableActions()
                  * 
                  *  Preconditions:
                  *    init'ed(this.availableActions)
                  * 
                  *  Postconditions:
                  *    return_value == this.availableActions
                  *    init'ed(return_value)
                  */
   206          return availableActions;
   207      }
   208  
   209      public void setAvailableActions(List availableActions) {
                 /* 
    P/P           *  Method: void setAvailableActions(List)
                  * 
                  *  Postconditions:
                  *    this.availableActions == availableActions
                  *    init'ed(this.availableActions)
                  */
   210          this.availableActions = availableActions;
   211      }
   212      
   213      public String getNewTmplName() {
                 /* 
    P/P           *  Method: String getNewTmplName()
                  * 
                  *  Preconditions:
                  *    init'ed(this.newTmplName)
                  * 
                  *  Postconditions:
                  *    return_value == this.newTmplName
                  *    init'ed(return_value)
                  */
   214          return newTmplName;
   215      }
   216  
   217      public void setNewTmplName(String newTmplName) {
                 /* 
    P/P           *  Method: void setNewTmplName(String)
                  * 
                  *  Postconditions:
                  *    this.newTmplName == newTmplName
                  *    init'ed(this.newTmplName)
                  */
   218          this.newTmplName = newTmplName;
   219      }
   220  
   221      public String getNewTmplAction() {
                 /* 
    P/P           *  Method: String getNewTmplAction()
                  * 
                  *  Preconditions:
                  *    init'ed(this.newTmplAction)
                  * 
                  *  Postconditions:
                  *    return_value == this.newTmplAction
                  *    init'ed(return_value)
                  */
   222          return newTmplAction;
   223      }
   224  
   225      public void setNewTmplAction(String newTmplAction) {
                 /* 
    P/P           *  Method: void setNewTmplAction(String)
                  * 
                  *  Postconditions:
                  *    this.newTmplAction == newTmplAction
                  *    init'ed(this.newTmplAction)
                  */
   226          this.newTmplAction = newTmplAction;
   227      }
   228      
   229  }








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