File Source: ThemeEdit.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 org.apache.commons.lang.StringUtils;
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26  import org.apache.roller.weblogger.WebloggerException;
    27  import org.apache.roller.weblogger.business.WebloggerFactory;
    28  import org.apache.roller.weblogger.business.UserManager;
    29  import org.apache.roller.weblogger.business.themes.SharedTheme;
    30  import org.apache.roller.weblogger.business.themes.ThemeManager;
    31  import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
    32  import org.apache.roller.weblogger.pojos.WeblogPermission;
    33  import org.apache.roller.weblogger.pojos.Theme;
    34  import org.apache.roller.weblogger.pojos.WeblogTheme;
    35  import org.apache.roller.weblogger.pojos.Weblog;
    36  import org.apache.roller.weblogger.pojos.WeblogTemplate;
    37  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    38  import org.apache.roller.weblogger.util.cache.CacheManager;
    39  
    40  
    41  /**
    42   * Action for controlling theme selection.
    43   */
    44  public class ThemeEdit extends UIAction {
    45      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.ThemeEdit__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    46      private static Log log = LogFactory.getLog(Templates.class);
    47      
    48      // list of available themes
    49      private List themes = Collections.EMPTY_LIST;
    50      
    51      // type of theme desired, either 'shared' or 'custom'
    52      private String themeType = null;
    53      
    54      // the chosen shared theme id
    55      private String themeId = null;
    56      
    57      // import the selected theme to the action weblog
    58      private boolean importTheme = false;
    59      
    60      // the chosen import theme id
    61      private String importThemeId = null;
    62      
    63      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.ThemeEdit()
              * 
              *  Presumptions:
              *    init'ed(java.util.Collections.EMPTY_LIST)
              * 
              *  Postconditions:
              *    this.actionName == &"themeEdit"
              *    this.actionWeblog == null
              *    this.authenticatedUser == null
              *    this.importThemeId == null
              *    this.themeId == null
              *    this.themeType == null
              *    this.weblog == null
              *    this.desiredMenu == &"editor"
              *    this.importTheme == 0
              *    this.pageTitle == &"themeEditor.title"
              *    ...
              */
    64      public ThemeEdit() {
    65          this.actionName = "themeEdit";
    66          this.desiredMenu = "editor";
    67          this.pageTitle = "themeEditor.title";
    68      }
    69      
    70      
    71      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)
                  */
    72          return WeblogPermission.ADMIN;
    73      }
    74      
    75      
    76      public void myPrepare() {
                 /* 
    P/P           *  Method: void myPrepare()
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getThemeManager(...)@77 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@77 != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.themes)
                  */
    77          ThemeManager themeMgr = WebloggerFactory.getWeblogger().getThemeManager();
    78          setThemes(themeMgr.getEnabledThemesList());
    79      }
    80      
    81      
    82      public String execute() {
    83          
    84          // set theme to current value
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    this.actionWeblog != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.pojos.Weblog:getTheme(...)@88 != null
                  *    org.apache.roller.weblogger.pojos.Weblog:getTheme(...)@89 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"input",&"input-sharedonly"}
                  *    possibly_updated(this.importThemeId)
                  *    init'ed(this.themeId)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@85: {0}, {1}
                  *    org.apache.roller.weblogger.config.WebloggerRuntimeConfig:getBooleanProperty(...)@92: {1}, {0}
                  */
    85          if(WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme())) {
    86              setThemeId(null);
    87          } else {
    88              setThemeId(getActionWeblog().getTheme().getId());
    89              setImportThemeId(getActionWeblog().getTheme().getId());
    90          }
    91          
    92          if(!WebloggerRuntimeConfig.getBooleanProperty("themes.customtheme.allowed")) {
    93              return "input-sharedonly";
    94          } else {
    95              return INPUT;
    96          }
    97      }
    98      
    99  
   100      /**
   101       * Save new theme configuration.
   102       */
   103      public String save() {
   104          
                 /* 
    P/P           *  Method: String save()
                  * 
                  *  Preconditions:
                  *    init'ed(this.themeType)
                  *    (soft) log != null
                  *    (soft) init'ed(this.importTheme)
                  *    (soft) init'ed(this.importThemeId)
                  *    (soft) init'ed(this.themeId)
                  *    (soft) this.actionWeblog != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getThemeManager(...)@116 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getThemeManager(...)@167 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@130 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@187 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@116 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value == One-of{&"input-sharedonly", &"input"}
                  *    return_value in Addr_Set{&"input-sharedonly",&"input"}
                  *    (soft) init'ed(this.importTheme)
                  *    (soft) init'ed(this.importThemeId)
                  *    init'ed(this.themeId)
                  * 
                  *  Test Vectors:
                  *    this.importTheme: {0}, {1}
                  *    this.themeId: Inverse{null}, Addr_Set{null}
                  *    java.lang.String:equals(...)@108: {0}, {1}
                  *    java.lang.String:equals(...)@157: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@115: {1}, {0}
                  *    org.apache.roller.weblogger.config.WebloggerRuntimeConfig:getBooleanProperty(...)@111: {0}, {1}
                  *    org.apache.roller.weblogger.pojos.Theme:isEnabled(...)@170: {1}, {0}
                  *    org.apache.roller.weblogger.ui.struts2.editor.ThemeEdit:hasActionErrors(...)@125: {1}, {0}
                  *    org.apache.roller.weblogger.ui.struts2.editor.ThemeEdit:hasActionErrors(...)@182: {1}, {0}
                  */
   105          Weblog weblog = getActionWeblog();
   106          
   107          // we are dealing with a custom theme scenario
   108          if(WeblogTheme.CUSTOM.equals(getThemeType())) {
   109              
   110              // only continue if custom themes are allowed
   111              if(WebloggerRuntimeConfig.getBooleanProperty("themes.customtheme.allowed")) {
   112                  
   113                  // do theme import if necessary
   114                  SharedTheme importTheme = null;
   115                  if(isImportTheme() && !StringUtils.isEmpty(getImportThemeId())) try {
   116                      ThemeManager themeMgr = WebloggerFactory.getWeblogger().getThemeManager();
   117                      importTheme = themeMgr.getTheme(getImportThemeId());
   118                      themeMgr.importTheme(getActionWeblog(), importTheme);
   119                  } catch(WebloggerException re) {
   120                      log.error("Error customizing theme for weblog - "+getActionWeblog().getHandle(), re);
   121                      // TODO: i18n
   122                      addError("Error importing theme");
   123                  }
   124                  
   125                  if(!hasActionErrors()) try {
   126                      weblog.setEditorTheme(WeblogTheme.CUSTOM);
   127                      log.debug("Saving custom theme for weblog "+weblog.getHandle());
   128                      
   129                      // save updated weblog and flush
   130                      UserManager userMgr = WebloggerFactory.getWeblogger().getUserManager();
   131                      userMgr.saveWebsite(weblog);
   132                      WebloggerFactory.getWeblogger().flush();
   133                      
   134                      // make sure to flush the page cache so ppl can see the change
   135                      CacheManager.invalidate(weblog);
   136                      
   137                      // TODO: i18n
   138                      addMessage("Successfully set theme to - "+WeblogTheme.CUSTOM);
   139                      if(importTheme != null) {
   140                          addMessage("Successfully copied templates from theme - "+importTheme.getName());
   141                      }
   142                      
   143                      // reset import theme options
   144                      setImportTheme(false);
   145                      setImportThemeId(null);
   146                      
   147                  } catch(WebloggerException re) {
   148                      log.error("Error saving weblog - "+getActionWeblog().getHandle(), re);
   149                      addError("Error setting theme");
   150                  }
   151              } else {
   152                  // TODO: i18n
   153                  addError("Sorry, custom themes are not allowed");
   154              }
   155              
   156          // we are dealing with a shared theme scenario
   157          } else if("shared".equals(getThemeType())) {
   158              
   159              // make sure theme is valid and enabled
   160              Theme newTheme = null;
   161              if(getThemeId() == null) {
   162                  // TODO: i18n
   163                  addError("No theme specified");
   164                  
   165              } else {
   166                  try {
   167                      ThemeManager themeMgr = WebloggerFactory.getWeblogger().getThemeManager();
   168                      newTheme = themeMgr.getTheme(getThemeId());
   169                      
   170                      if(!newTheme.isEnabled()) {
   171                          // TODO: i18n
   172                          addError("Theme not enabled");
   173                      }
   174                      
   175                  } catch(Exception ex) {
   176                      log.warn(ex);
   177                      // TODO: i18n
   178                      addError("Theme not found");
   179                  }
   180              }
   181              
   182              if(!hasActionErrors()) try {
   183                  weblog.setEditorTheme(getThemeId());
   184                  log.debug("Saving theme "+getThemeId()+" for weblog "+weblog.getHandle());
   185                  
   186                  // save updated weblog and flush
   187                  UserManager userMgr = WebloggerFactory.getWeblogger().getUserManager();
   188                  userMgr.saveWebsite(weblog);
   189                  WebloggerFactory.getWeblogger().flush();
   190                  
   191                  // make sure to flush the page cache so ppl can see the change
   192                  CacheManager.invalidate(weblog);
   193                  
   194                  // TODO: i18n
   195                  addMessage("Successfully set theme to - "+newTheme.getName());
   196                  
   197              } catch(WebloggerException re) {
   198                  log.error("Error saving weblog - "+getActionWeblog().getHandle(), re);
   199                  addError("Error setting theme");
   200              }
   201              
   202          // unknown theme scenario, error
   203          } else {
   204              // invalid theme type
   205              // TODO: i18n
   206              addError("no valid theme type submitted");
   207          }
   208          
   209          return execute();
   210      }
   211      
   212      
   213      public boolean isCustomTheme() {
                 /* 
    P/P           *  Method: bool isCustomTheme()
                  * 
                  *  Preconditions:
                  *    this.actionWeblog != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   214          return (WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme()));
   215      }
   216      
   217      // has this weblog had a custom theme before?
   218      public boolean isFirstCustomization() {
   219          try {
                     /* 
    P/P               *  Method: bool isFirstCustomization()
                      * 
                      *  Preconditions:
                      *    (soft) log != null
                      *    (soft) init'ed(this.actionWeblog)
                      * 
                      *  Presumptions:
                      *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@220 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@220 != null
                      * 
                      *  Postconditions:
                      *    init'ed(return_value)
                      */
   220              UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
   221              return (umgr.getPageByAction(getActionWeblog(), WeblogTemplate.ACTION_WEBLOG) == null);
   222          } catch (WebloggerException ex) {
   223              log.error("Error looking up weblog template", ex);
   224          }
   225          return false;
   226      }
   227      
   228      
   229      public List getThemes() {
                 /* 
    P/P           *  Method: List getThemes()
                  * 
                  *  Preconditions:
                  *    init'ed(this.themes)
                  * 
                  *  Postconditions:
                  *    return_value == this.themes
                  *    init'ed(return_value)
                  */
   230          return themes;
   231      }
   232  
   233      public void setThemes(List themes) {
                 /* 
    P/P           *  Method: void setThemes(List)
                  * 
                  *  Postconditions:
                  *    this.themes == themes
                  *    init'ed(this.themes)
                  */
   234          this.themes = themes;
   235      }
   236  
   237      public String getThemeType() {
                 /* 
    P/P           *  Method: String getThemeType()
                  * 
                  *  Preconditions:
                  *    init'ed(this.themeType)
                  * 
                  *  Postconditions:
                  *    return_value == this.themeType
                  *    init'ed(return_value)
                  */
   238          return themeType;
   239      }
   240  
   241      public void setThemeType(String themeType) {
                 /* 
    P/P           *  Method: void setThemeType(String)
                  * 
                  *  Postconditions:
                  *    this.themeType == themeType
                  *    init'ed(this.themeType)
                  */
   242          this.themeType = themeType;
   243      }
   244      
   245      public String getThemeId() {
                 /* 
    P/P           *  Method: String getThemeId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.themeId)
                  * 
                  *  Postconditions:
                  *    return_value == this.themeId
                  *    init'ed(return_value)
                  */
   246          return themeId;
   247      }
   248  
   249      public void setThemeId(String theme) {
                 /* 
    P/P           *  Method: void setThemeId(String)
                  * 
                  *  Postconditions:
                  *    this.themeId == theme
                  *    init'ed(this.themeId)
                  */
   250          this.themeId = theme;
   251      }
   252  
   253      public boolean isImportTheme() {
                 /* 
    P/P           *  Method: bool isImportTheme()
                  * 
                  *  Preconditions:
                  *    init'ed(this.importTheme)
                  * 
                  *  Postconditions:
                  *    return_value == this.importTheme
                  *    init'ed(return_value)
                  */
   254          return importTheme;
   255      }
   256  
   257      public void setImportTheme(boolean importTheme) {
                 /* 
    P/P           *  Method: void setImportTheme(bool)
                  * 
                  *  Postconditions:
                  *    this.importTheme == importTheme
                  *    init'ed(this.importTheme)
                  */
   258          this.importTheme = importTheme;
   259      }
   260  
   261      public String getImportThemeId() {
                 /* 
    P/P           *  Method: String getImportThemeId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.importThemeId)
                  * 
                  *  Postconditions:
                  *    return_value == this.importThemeId
                  *    init'ed(return_value)
                  */
   262          return importThemeId;
   263      }
   264  
   265      public void setImportThemeId(String importThemeId) {
                 /* 
    P/P           *  Method: void setImportThemeId(String)
                  * 
                  *  Postconditions:
                  *    this.importThemeId == importThemeId
                  *    init'ed(this.importThemeId)
                  */
   266          this.importThemeId = importThemeId;
   267      }
   268      
   269  }








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