File Source: StylesheetEdit.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.Date;
    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.UserManager;
    27  import org.apache.roller.weblogger.business.themes.ThemeManager;
    28  import org.apache.roller.weblogger.pojos.Theme;
    29  import org.apache.roller.weblogger.pojos.ThemeTemplate;
    30  import org.apache.roller.weblogger.pojos.WeblogPermission;
    31  import org.apache.roller.weblogger.pojos.WeblogTemplate;
    32  import org.apache.roller.weblogger.pojos.WeblogTheme;
    33  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    34  import org.apache.roller.weblogger.util.cache.CacheManager;
    35  
    36  
    37  /**
    38   * Action which handles editing for a weblog stylesheet override template.
    39   */
    40  public class StylesheetEdit extends UIAction {
    41      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.StylesheetEdit__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    42      private static Log log = LogFactory.getLog(StylesheetEdit.class);
    43      
    44      // the template we are working on
    45      private WeblogTemplate template = null;
    46      
    47      // the contents of the stylesheet override
    48      private String contents = null;
    49      
    50      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.StylesheetEdit()
              * 
              *  Postconditions:
              *    this.actionName == &"stylesheetEdit"
              *    this.actionWeblog == null
              *    this.authenticatedUser == null
              *    this.contents == null
              *    this.template == null
              *    this.weblog == null
              *    this.desiredMenu == &"editor"
              *    this.pageTitle == &"stylesheetEdit.title"
              */
    51      public StylesheetEdit() {
    52          this.actionName = "stylesheetEdit";
    53          this.desiredMenu = "editor";
    54          this.pageTitle = "stylesheetEdit.title";
    55      }
    56      
    57      
    58      @Override
    59      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)
                  */
    60          return WeblogPermission.ADMIN;
    61      }
    62      
    63      
    64      @Override
    65      public void myPrepare() {
    66          
                 /* 
    P/P           *  Method: void myPrepare()
                  * 
                  *  Preconditions:
                  *    this.actionWeblog != null
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@77 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@77 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@97 != null
                  *    org.apache.roller.weblogger.pojos.Weblog:getTheme(...)@69 != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.template)
                  *    new WeblogTemplate(myPrepare#3) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.business.UserManager:getPageByLink(...)@78: Inverse{null}, Addr_Set{null}
                  */
    67          ThemeTemplate stylesheet = null;
    68          try {
    69              stylesheet = getActionWeblog().getTheme().getStylesheet();
    70          } catch (WebloggerException ex) {
    71              log.error("Error looking up stylesheet on weblog - "+getActionWeblog().getHandle(), ex);
    72          }
    73          
    74          if(stylesheet != null) {
    75              log.debug("custom stylesheet path is - "+stylesheet.getLink());
    76              try {
    77                  UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
    78                  setTemplate(mgr.getPageByLink(getActionWeblog(), stylesheet.getLink()));
    79                  
    80                  if(getTemplate() == null) {
    81                      log.debug("custom stylesheet not found, creating it");
    82                      
    83                      // template doesn't exist yet, so create it
    84                      WeblogTemplate stylesheetTmpl = new WeblogTemplate();
    85                      stylesheetTmpl.setWebsite(getActionWeblog());
    86                      stylesheetTmpl.setAction(stylesheet.ACTION_CUSTOM);
    87                      stylesheetTmpl.setName(stylesheet.getName());
    88                      stylesheetTmpl.setDescription(stylesheet.getDescription());
    89                      stylesheetTmpl.setLink(stylesheet.getLink());
    90                      stylesheetTmpl.setContents(stylesheet.getContents());
    91                      stylesheetTmpl.setHidden(false);
    92                      stylesheetTmpl.setNavbar(false);
    93                      stylesheetTmpl.setLastModified(new Date());
    94                      stylesheetTmpl.setTemplateLanguage(stylesheet.getTemplateLanguage());
    95                      
    96                      mgr.savePage(stylesheetTmpl);
    97                      WebloggerFactory.getWeblogger().flush();
    98                      
    99                      setTemplate(stylesheetTmpl);
   100                  }
   101              } catch (WebloggerException ex) {
   102                  log.error("Error finding/adding stylesheet tempalate from weblog - "+getActionWeblog().getHandle(), ex);
   103              }
   104          }
   105      }
   106      
   107      
   108      /**
   109       * Show stylesheet edit page.
   110       */
   111      public String execute() {
   112          
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    init'ed(this.template)
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"input",&"error"}
                  *    possibly_updated(this.contents)
                  * 
                  *  Test Vectors:
                  *    this.template: Inverse{null}, Addr_Set{null}
                  */
   113          if(getTemplate() == null) {
   114              return ERROR;
   115          }
   116          
   117          setContents(getTemplate().getContents());
   118          
   119          return INPUT;
   120      }
   121      
   122      
   123      /**
   124       * Save an existing stylesheet.
   125       */
   126      public String save() {
   127          
                 /* 
    P/P           *  Method: String save()
                  * 
                  *  Preconditions:
                  *    init'ed(this.template)
                  *    (soft) log != null
                  *    (soft) this.actionWeblog != null
                  *    (soft) init'ed(this.contents)
                  * 
                  *  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(...)@144 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"input",&"error"}
                  * 
                  *  Test Vectors:
                  *    this.template: Inverse{null}, Addr_Set{null}
                  *    org.apache.roller.weblogger.ui.struts2.editor.StylesheetEdit:hasActionErrors(...)@134: {1}, {0}
                  */
   128          if(getTemplate() == null) {
   129              // TODO: i18n
   130              addError("Unable to locate stylesheet template");
   131              return ERROR;
   132          }
   133          
   134          if(!hasActionErrors()) try {
   135              
   136              WeblogTemplate stylesheet = getTemplate();
   137              
   138              stylesheet.setLastModified(new Date());
   139              stylesheet.setContents(getContents());
   140              
   141              // save template and flush
   142              UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
   143              mgr.savePage(stylesheet);
   144              WebloggerFactory.getWeblogger().flush();
   145              
   146              // notify caches
   147              CacheManager.invalidate(stylesheet);
   148              
   149              // success message
   150              addMessage("stylesheetEdit.save.success", stylesheet.getName());
   151              
   152          } catch (WebloggerException ex) {
   153              log.error("Error updating stylesheet template for weblog - "+getActionWeblog().getHandle(), ex);
   154              // TODO: i18n
   155              addError("Error saving template");
   156          }
   157          
   158          return INPUT;
   159      }
   160      
   161      
   162      /**
   163       * Revert the stylesheet to its original state.
   164       */
   165      public String revert() {
   166          
                 /* 
    P/P           *  Method: String revert()
                  * 
                  *  Preconditions:
                  *    init'ed(this.template)
                  *    (soft) log != null
                  *    (soft) this.actionWeblog != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getThemeManager(...)@184 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@192 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@184 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@192 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@194 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"error",&"input"}
                  *    possibly_updated(this.contents)
                  * 
                  *  Test Vectors:
                  *    this.template: Inverse{null}, Addr_Set{null}
                  *    java.lang.String:equals(...)@174: {0}, {1}
                  *    org.apache.roller.weblogger.ui.struts2.editor.StylesheetEdit:hasActionErrors(...)@179: {1}, {0}
                  */
   167          if(getTemplate() == null) {
   168              // TODO: i18n
   169              addError("Unable to locate stylesheet template");
   170              return ERROR;
   171          }
   172          
   173          // make sure we are still using a shared theme so that reverting is possible
   174          if(WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme())) {
   175              // TODO: i18n
   176              addError("stylesheetEdit.error.customTheme");
   177          }
   178          
   179          if(!hasActionErrors()) try {
   180              
   181              WeblogTemplate stylesheet = getTemplate();
   182              
   183              // lookup the theme used by this weblog
   184              ThemeManager tmgr = WebloggerFactory.getWeblogger().getThemeManager();
   185              Theme theme = tmgr.getTheme(getActionWeblog().getEditorTheme());
   186              
   187              // lookup 
   188              stylesheet.setLastModified(new Date());
   189              stylesheet.setContents(theme.getStylesheet().getContents());
   190              
   191              // save template and flush
   192              UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
   193              mgr.savePage(stylesheet);
   194              WebloggerFactory.getWeblogger().flush();
   195              
   196              // notify caches
   197              CacheManager.invalidate(stylesheet);
   198              
   199              // success message
   200              addMessage("stylesheetEdit.revert.success", stylesheet.getName());
   201              
   202          } catch (WebloggerException ex) {
   203              log.error("Error updating stylesheet template for weblog - "+getActionWeblog().getHandle(), ex);
   204              // TODO: i18n
   205              addError("Error saving template");
   206          }
   207          
   208          return execute();
   209      }
   210      
   211      
   212      public boolean isCustomTheme() {
                 /* 
    P/P           *  Method: bool isCustomTheme()
                  * 
                  *  Preconditions:
                  *    this.actionWeblog != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   213          return (WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme()));
   214      }
   215      
   216      
   217      public WeblogTemplate getTemplate() {
                 /* 
    P/P           *  Method: WeblogTemplate getTemplate()
                  * 
                  *  Preconditions:
                  *    init'ed(this.template)
                  * 
                  *  Postconditions:
                  *    return_value == this.template
                  *    init'ed(return_value)
                  */
   218          return template;
   219      }
   220  
   221      public void setTemplate(WeblogTemplate template) {
                 /* 
    P/P           *  Method: void setTemplate(WeblogTemplate)
                  * 
                  *  Postconditions:
                  *    this.template == template
                  *    init'ed(this.template)
                  */
   222          this.template = template;
   223      }
   224  
   225      public String getContents() {
                 /* 
    P/P           *  Method: String getContents()
                  * 
                  *  Preconditions:
                  *    init'ed(this.contents)
                  * 
                  *  Postconditions:
                  *    return_value == this.contents
                  *    init'ed(return_value)
                  */
   226          return contents;
   227      }
   228  
   229      public void setContents(String contents) {
                 /* 
    P/P           *  Method: void setContents(String)
                  * 
                  *  Postconditions:
                  *    this.contents == contents
                  *    init'ed(this.contents)
                  */
   230          this.contents = contents;
   231      }
   232      
   233  }








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