File Source: EntryAdd.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.sql.Timestamp;
    22  import java.util.Collections;
    23  import java.util.Date;
    24  import java.util.List;
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27  import org.apache.roller.weblogger.WebloggerException;
    28  import org.apache.roller.weblogger.business.WebloggerFactory;
    29  import org.apache.roller.weblogger.business.WeblogManager;
    30  import org.apache.roller.weblogger.pojos.WeblogPermission;
    31  import org.apache.roller.weblogger.pojos.WeblogEntry;
    32  import org.apache.roller.weblogger.util.MediacastException;
    33  import org.apache.roller.weblogger.util.cache.CacheManager;
    34  import org.apache.roller.weblogger.util.MailUtil;
    35  import org.apache.commons.lang.StringUtils;
    36  import org.apache.roller.weblogger.pojos.WeblogCategory;
    37  import org.apache.roller.weblogger.util.MediacastResource;
    38  import org.apache.roller.weblogger.util.MediacastUtil;
    39  import org.apache.struts2.interceptor.validation.SkipValidation;
    40  
    41  
    42  /**
    43   * Add a new weblog entry.
    44   */
    45  public final class EntryAdd extends EntryBase {
    46      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.EntryAdd__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    47      private static Log log = LogFactory.getLog(EntryAdd.class);
    48      
    49      // bean for managing form data
    50      private EntryBean bean = new EntryBean();
    51      
    52      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.EntryAdd()
              * 
              *  Postconditions:
              *    this.actionName == &"entryAdd"
              *    this.bean == &new EntryBean(EntryAdd#1)
              *    this.desiredMenu == &"editor"
              *    this.pageTitle == &"weblogEdit.title.newEntry"
              *    new EntryBean(EntryAdd#1) num objects == 1
              *    this.bean.allowComments == 1
              *    new Integer(EntryBean#1) num objects == 1
              *    this.bean.categoryId == null
              *    this.bean.dateString == null
              *    this.bean.enclosureURL == null
              *    ...
              */
    53      public EntryAdd() {
    54          this.actionName = "entryAdd";
    55          this.desiredMenu = "editor";
    56          this.pageTitle = "weblogEdit.title.newEntry";
    57      }
    58      
    59      
    60      @Override
    61      public short requiredWeblogPermissions() {
                 /* 
    P/P           *  Method: short requiredWeblogPermissions()
                  * 
                  *  Presumptions:
                  *    init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.LIMITED)
                  * 
                  *  Postconditions:
                  *    return_value == org.apache.roller.weblogger.pojos.WeblogPermission.LIMITED
                  *    (soft) init'ed(return_value)
                  */
    62          return WeblogPermission.LIMITED;
    63      }
    64      
    65      
    66      /**
    67       * Show form for adding a new weblog entry.
    68       * 
    69       * @return String The result of the action.
    70       */
    71      @SkipValidation
    72      public String execute() {
    73          
    74          // if user is an author then post status defaults to PUBLISHED, otherwise PENDING
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.pojos.Weblog:getDefaultAllowComments(...)@85 != null
                  *    init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR)
                  *    org.apache.roller.weblogger.ui.struts2.editor.EntryAdd:getActionWeblog(...)@75 != null
                  *    org.apache.roller.weblogger.ui.struts2.editor.EntryAdd:getActionWeblog(...)@82 != null
                  *    org.apache.roller.weblogger.ui.struts2.editor.EntryAdd:getActionWeblog(...)@85 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value == &"input"
                  *    init'ed(this.bean.allowComments)
                  *    this.bean.commentDays == &new Integer(execute#1)
                  *    init'ed(this.bean.locale)
                  *    possibly_updated(this.bean.plugins)
                  *    this.bean.status in Addr_Set{&"PENDING",&"PUBLISHED"}
                  *    new Integer(execute#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.pojos.Weblog:getDefaultPlugins(...)@89: Addr_Set{null}, Inverse{null}
                  *    org.apache.roller.weblogger.pojos.Weblog:hasUserPermissions(...)@75: {0}, {1}
                  */
    75          if(getActionWeblog().hasUserPermissions(getAuthenticatedUser(),WeblogPermission.AUTHOR)) {
    76              getBean().setStatus(WeblogEntry.PUBLISHED);
    77          } else {
    78              getBean().setStatus(WeblogEntry.PENDING);
    79          }
    80          
    81          // set entry locale based on weblog locale
    82          getBean().setLocale(getActionWeblog().getLocale());
    83          
    84          // set comment defaults
    85          getBean().setAllowComments(getActionWeblog().getDefaultAllowComments());
    86          getBean().setCommentDays(new Integer(getActionWeblog().getDefaultCommentDays()));
    87          
    88          // apply weblog default plugins
    89          if(getActionWeblog().getDefaultPlugins() != null) {
    90              getBean().setPlugins(StringUtils.split(getActionWeblog().getDefaultPlugins(), ","));
    91          }
    92          
    93          return INPUT;
    94      }
    95      
    96      
    97      /**
    98       * Save a new weblog entry.
    99       * 
   100       * @return String The result of the action.
   101       */
   102      public String save() {
   103          
                 /* 
    P/P           *  Method: String save()
                  * 
                  *  Preconditions:
                  *    (soft) log != null
                  *    (soft) org/apache/roller/weblogger/ui/struts2/editor/EntryBase.log != null
                  *    (soft) org/apache/roller/weblogger/ui/struts2/editor/EntryBean.log != null
                  *    (soft) this.bean != null
                  *    (soft) init'ed(this.bean.allowComments)
                  *    (soft) this.bean.categoryId != null
                  *    (soft) init'ed(this.bean.commentDays)
                  *    (soft) init'ed(this.bean.dateString)
                  *    (soft) init'ed(this.bean.enclosureURL)
                  *    (soft) init'ed(this.bean.hours)
                  *    ...
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getAutopingManager(...)@169 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@105 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@105 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@159 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@169 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"success",&"input"}
                  *    possibly_updated(this.bean.id)
                  * 
                  *  Test Vectors:
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@136: {1}, {0}
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@150: {0}, {1}
                  *    org.apache.roller.weblogger.pojos.User:hasRole(...)@132: {0}, {1}
                  *    org.apache.roller.weblogger.pojos.Weblog:hasUserPermissions(...)@126: {1}, {0}
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:getPubTime(...)@120: Inverse{null}, Addr_Set{null}
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:isPending(...)@172: {0}, {1}
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:isPublished(...)@119: {0}, {1}
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:isPublished(...)@168: {0}, {1}
                  *    org.apache.roller.weblogger.ui.struts2.editor.EntryAdd:hasActionErrors(...)@104: {1}, {0}
                  */
   104          if(!hasActionErrors()) try {
   105              WeblogManager weblogMgr = WebloggerFactory.getWeblogger().getWeblogManager();
   106              
   107              WeblogEntry entry = new WeblogEntry();
   108              entry.setCreator(getAuthenticatedUser());
   109              entry.setWebsite(getActionWeblog());
   110              
   111              // set updatetime & pubtime if it was specified
   112              entry.setUpdateTime(new Timestamp(new Date().getTime()));
   113              entry.setPubTime(getBean().getPubTime(getLocale(), getActionWeblog().getTimeZoneInstance()));
   114              
   115              // copy data to new entry pojo
   116              getBean().copyTo(entry);
   117              
   118              // handle pubtime auto set
   119              if(entry.isPublished()) {
   120                  if(entry.getPubTime() == null) {
   121                      // no time specified, use current time
   122                      entry.setPubTime(entry.getUpdateTime());
   123                  }
   124                  
   125                  // if user does not have author perms then force PENDING status
   126                  if(!getActionWeblog().hasUserPermissions(getAuthenticatedUser(),WeblogPermission.AUTHOR)) {
   127                      entry.setStatus(WeblogEntry.PENDING);
   128                  }
   129              }
   130              
   131              // if user is an admin then apply pinned to main value as well
   132              if(getAuthenticatedUser().hasRole("admin")) {
   133                  entry.setPinnedToMain(getBean().getPinnedToMain());
   134              }
   135              
   136              if(!StringUtils.isEmpty(getBean().getEnclosureURL())) try {
   137                  // Fetch MediaCast resource
   138                  log.debug("Checking MediaCast attributes");
   139                  MediacastResource mediacast = MediacastUtil.lookupResource(getBean().getEnclosureURL());
   140                  
   141                  // set mediacast attributes
   142                  entry.putEntryAttribute("att_mediacast_url", mediacast.getUrl());
   143                  entry.putEntryAttribute("att_mediacast_type", mediacast.getContentType());
   144                  entry.putEntryAttribute("att_mediacast_length", ""+mediacast.getLength());
   145                  
   146              } catch (MediacastException ex) {
   147                  addMessage(getText(ex.getErrorKey()));
   148              }
   149              
   150              if(log.isDebugEnabled()) {
   151                  log.debug("entry bean is ...\n"+getBean().toString());
   152                  log.debug("final status = "+entry.getStatus());
   153                  log.debug("updtime = "+entry.getUpdateTime());
   154                  log.debug("pubtime = "+entry.getPubTime());
   155              }
   156              
   157              log.debug("Saving entry");
   158              weblogMgr.saveWeblogEntry(entry);
   159              WebloggerFactory.getWeblogger().flush();
   160              
   161              // notify search of the new entry
   162              reindexEntry(entry);
   163              
   164              // notify caches
   165              CacheManager.invalidate(entry);
   166              
   167              // Queue applicable pings for this update.
   168              if(entry.isPublished()) {
   169                  WebloggerFactory.getWeblogger().getAutopingManager().queueApplicableAutoPings(entry);
   170              }
   171              
   172              if(entry.isPending()) {
   173                  // implies that entry just changed to pending
   174                  MailUtil.sendPendingEntryNotice(entry);
   175                  addMessage("weblogEdit.submittedForReview");
   176              } else {
   177                  addMessage("weblogEdit.changesSaved");
   178              }
   179              
   180              // now that entry is saved we want to store the id in the bean
   181              // so that our success action can use it properly
   182              getBean().setId(entry.getId());
   183              
   184              return SUCCESS;
   185              
   186          } catch (Exception e) {
   187              log.error("Error saving new entry", e);
   188              // TODO: i18n
   189              addError("Error saving new entry");
   190          }
   191  
   192  
   193          
   194          return INPUT;
   195      }
   196      
   197      
   198      /**
   199       * Get the list of all categories for the action weblog, not including root.
   200       */
   201      public List<WeblogCategory> getCategories() {
   202          try {
                     /* 
    P/P               *  Method: List getCategories()
                      * 
                      *  Preconditions:
                      *    (soft) log != null
                      * 
                      *  Presumptions:
                      *    init'ed(java.util.Collections.EMPTY_LIST)
                      *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@203 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@203 != null
                      * 
                      *  Postconditions:
                      *    init'ed(return_value)
                      */
   203              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   204              return wmgr.getWeblogCategories(getActionWeblog(), false);
   205          } catch (WebloggerException ex) {
   206              log.error("Error getting category list for weblog - "+getWeblog(), ex);
   207              return Collections.EMPTY_LIST;
   208          }
   209      }
   210      
   211      
   212      public EntryBean getBean() {
                 /* 
    P/P           *  Method: EntryBean getBean()
                  * 
                  *  Preconditions:
                  *    init'ed(this.bean)
                  * 
                  *  Postconditions:
                  *    return_value == this.bean
                  *    init'ed(return_value)
                  */
   213          return bean;
   214      }
   215  
   216      public void setBean(EntryBean bean) {
                 /* 
    P/P           *  Method: void setBean(EntryBean)
                  * 
                  *  Postconditions:
                  *    this.bean == bean
                  *    init'ed(this.bean)
                  */
   217          this.bean = bean;
   218      }
   219      
   220  }








SofCheck Inspector Build Version : 2.18479
EntryAdd.java 2009-Jan-02 14:25:18
EntryAdd.class 2009-Sep-04 03:12:46