File Source: EntryEdit.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.Arrays;
    23  import java.util.Collections;
    24  import java.util.Date;
    25  import java.util.Iterator;
    26  import java.util.List;
    27  import org.apache.commons.lang.StringUtils;
    28  import org.apache.commons.logging.Log;
    29  import org.apache.commons.logging.LogFactory;
    30  import org.apache.roller.weblogger.WebloggerException;
    31  import org.apache.roller.weblogger.business.WebloggerFactory;
    32  import org.apache.roller.weblogger.business.WeblogManager;
    33  import org.apache.roller.weblogger.pojos.WeblogCategory;
    34  import org.apache.roller.weblogger.pojos.WeblogPermission;
    35  import org.apache.roller.weblogger.pojos.WeblogEntry;
    36  import org.apache.roller.weblogger.util.cache.CacheManager;
    37  import org.apache.roller.weblogger.util.MailUtil;
    38  import org.apache.roller.weblogger.util.MediacastException;
    39  import org.apache.roller.weblogger.util.MediacastResource;
    40  import org.apache.roller.weblogger.util.MediacastUtil;
    41  import org.apache.roller.weblogger.util.RollerMessages;
    42  import org.apache.roller.weblogger.util.RollerMessages.RollerMessage;
    43  import org.apache.roller.weblogger.util.Trackback;
    44  import org.apache.roller.weblogger.util.TrackbackNotAllowedException;
    45  import org.apache.struts2.interceptor.validation.SkipValidation;
    46  
    47  
    48  /**
    49   * Edit an existing entry.
    50   */
    51  public final class EntryEdit extends EntryBase {
    52      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.EntryEdit__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    53      private static Log log = LogFactory.getLog(EntryEdit.class);
    54      
    55      // bean for managing form data
    56      private EntryBean bean = new EntryBean();
    57      
    58      // the entry we are editing
    59      private WeblogEntry entry = null;
    60      
    61      // url to send trackback to
    62      private String trackbackUrl = null;
    63      
    64      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.EntryEdit()
              * 
              *  Postconditions:
              *    this.actionName == &"entryEdit"
              *    this.bean == &new EntryBean(EntryEdit#1)
              *    this.desiredMenu == &"editor"
              *    this.entry == null
              *    this.trackbackUrl == null
              *    this.bean.categoryId == null
              *    this.bean.dateString == null
              *    this.bean.enclosureURL == null
              *    this.bean.id == null
              *    this.bean.locale == null
              *    ...
              */
    65      public EntryEdit() {
    66          this.actionName = "entryEdit";
    67          this.desiredMenu = "editor";
    68          this.pageTitle = "weblogEdit.title.editEntry";
    69      }
    70      
    71      
    72      @Override
    73      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)
                  */
    74          return WeblogPermission.LIMITED;
    75      }
    76      
    77      
    78      public void myPrepare() {
                 /* 
    P/P           *  Method: void myPrepare()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  *    init'ed(this.bean.id)
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@81 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@81 != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.entry)
                  * 
                  *  Test Vectors:
                  *    this.bean.id: Addr_Set{null}, Inverse{null}
                  */
    79          if(getBean().getId() != null) {
    80              try {
    81                  WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
    82                  setEntry(wmgr.getWeblogEntry(getBean().getId()));
    83              } catch (WebloggerException ex) {
    84                  log.error("Error looking up entry by id - "+getBean().getId(), ex);
    85              }
    86          }
    87      }
    88      
    89      
    90      /**
    91       * Show form for editing an existing entry.
    92       * 
    93       * @return String The result of the action.
    94       */
    95      @SkipValidation
    96      public String execute() {
    97          
    98          // make sure we have an entry to edit and it belongs to the action weblog
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    init'ed(this.entry)
                  *    (soft) org/apache/roller/weblogger/ui/struts2/editor/EntryBean.log != null
                  *    (soft) this.bean != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:getWebsite(...)@101 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"input",&"access-denied",&"error"}
                  *    possibly_updated(this.bean.allowComments)
                  *    possibly_updated(this.bean.categoryId)
                  *    possibly_updated(this.bean.commentCount)
                  *    possibly_updated(this.bean.commentDays)
                  *    possibly_updated(this.bean.dateString)
                  *    possibly_updated(this.bean.enclosureURL)
                  *    possibly_updated(this.bean.hours)
                  *    possibly_updated(this.bean.id)
                  *    possibly_updated(this.bean.locale)
                  *    ...
                  * 
                  *  Test Vectors:
                  *    this.entry: Inverse{null}, Addr_Set{null}
                  *    org.apache.roller.weblogger.pojos.Weblog:equals(...)@101: {1}, {0}
                  */
    99          if(getEntry() == null) {
   100              return ERROR;
   101          } else if(!getEntry().getWebsite().equals(getActionWeblog())) {
   102              return DENIED;
   103          }
   104          
   105          // load bean with pojo data
   106          getBean().copyFrom(getEntry(), getLocale());
   107          
   108          return INPUT;
   109      }
   110      
   111      
   112      /**
   113       * Save weblog entry.
   114       * 
   115       * @return String The result of the action.
   116       */
   117      public String save() {
   118          
   119          // make sure we have an entry to edit and it belongs to the action weblog
                 /* 
    P/P           *  Method: String save()
                  * 
                  *  Preconditions:
                  *    init'ed(this.entry)
                  *    (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)
                  *    ...
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getAutopingManager(...)@201 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@127 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@127 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@191 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@201 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"input",&"access-denied",&"error"}
                  * 
                  *  Test Vectors:
                  *    this.entry: Inverse{null}, Addr_Set{null}
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@156: {1}, {0}
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@182: {0}, {1}
                  *    org.apache.roller.weblogger.pojos.User:hasRole(...)@152: {0}, {1}
                  *    org.apache.roller.weblogger.pojos.Weblog:equals(...)@122: {1}, {0}
                  *    org.apache.roller.weblogger.pojos.Weblog:hasUserPermissions(...)@146: {1}, {0}
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:getPubTime(...)@140: Inverse{null}, Addr_Set{null}
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:isPending(...)@204: {0}, {1}
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:isPublished(...)@139: {0}, {1}
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:isPublished(...)@200: {0}, {1}
                  *    ...
                  */
   120          if(getEntry() == null) {
   121              return ERROR;
   122          } else if(!getEntry().getWebsite().equals(getActionWeblog())) {
   123              return DENIED;
   124          }
   125          
   126          if(!hasActionErrors()) try {
   127              WeblogManager weblogMgr = WebloggerFactory.getWeblogger().getWeblogManager();
   128              
   129              WeblogEntry entry = getEntry();
   130              
   131              // set updatetime & pubtime
   132              entry.setUpdateTime(new Timestamp(new Date().getTime()));
   133              entry.setPubTime(getBean().getPubTime(getLocale(), getActionWeblog().getTimeZoneInstance()));
   134              
   135              // copy data to pojo
   136              getBean().copyTo(entry);
   137              
   138              // handle pubtime auto set
   139              if(entry.isPublished()) {
   140                  if(entry.getPubTime() == null) {
   141                      // no time specified, use current time
   142                      entry.setPubTime(entry.getUpdateTime());
   143                  }
   144                  
   145                  // if user does not have author perms then force PENDING status
   146                  if(!getActionWeblog().hasUserPermissions(getAuthenticatedUser(),WeblogPermission.AUTHOR)) {
   147                      entry.setStatus(WeblogEntry.PENDING);
   148                  }
   149              }
   150              
   151              // if user is an admin then apply pinned to main value as well
   152              if(getAuthenticatedUser().hasRole("admin")) {
   153                  entry.setPinnedToMain(getBean().getPinnedToMain());
   154              }
   155              
   156              if(!StringUtils.isEmpty(getBean().getEnclosureURL())) {
   157                  try {
   158                      // Fetch MediaCast resource
   159                      log.debug("Checking MediaCast attributes");
   160                      MediacastResource mediacast = MediacastUtil.lookupResource(getBean().getEnclosureURL());
   161  
   162                      // set mediacast attributes
   163                      entry.putEntryAttribute("att_mediacast_url", mediacast.getUrl());
   164                      entry.putEntryAttribute("att_mediacast_type", mediacast.getContentType());
   165                      entry.putEntryAttribute("att_mediacast_length", ""+mediacast.getLength());
   166  
   167                  } catch (MediacastException ex) {
   168                      addMessage(getText(ex.getErrorKey()));
   169                  }
   170              } else {
   171                  try {
   172                      // if MediaCast string is empty, clean out MediaCast attributes
   173                      weblogMgr.removeWeblogEntryAttribute("att_mediacast_url", entry);
   174                      weblogMgr.removeWeblogEntryAttribute("att_mediacast_type", entry);
   175                      weblogMgr.removeWeblogEntryAttribute("att_mediacast_length", entry);
   176                      
   177                  } catch (WebloggerException e) {
   178                      addMessage(getText("weblogEdit.mediaCastErrorRemoving"));
   179                  }
   180              }
   181              
   182              if(log.isDebugEnabled()) {
   183                  log.debug("entry bean is ...\n"+getBean().toString());
   184                  log.debug("final status = "+entry.getStatus());
   185                  log.debug("updtime = "+entry.getUpdateTime());
   186                  log.debug("pubtime = "+entry.getPubTime());
   187              }
   188              
   189              log.debug("Saving entry");
   190              weblogMgr.saveWeblogEntry(entry);
   191              WebloggerFactory.getWeblogger().flush();
   192              
   193              // notify search of the new entry
   194              reindexEntry(entry);
   195              
   196              // notify caches
   197              CacheManager.invalidate(entry);
   198              
   199              // Queue applicable pings for this update.
   200              if(entry.isPublished()) {
   201                  WebloggerFactory.getWeblogger().getAutopingManager().queueApplicableAutoPings(entry);
   202              }
   203              
   204              if(entry.isPending()) {
   205                  // implies that entry just changed to pending
   206                  MailUtil.sendPendingEntryNotice(entry);
   207                  addMessage("weblogEdit.submittedForReview");
   208              } else {
   209                  addMessage("weblogEdit.changesSaved");
   210              }
   211              
   212              return INPUT;
   213              
   214          } catch (Exception e) {
   215              log.error("Error saving new entry", e);
   216              // TODO: i18n
   217              addError("Error saving new entry");
   218          }
   219  
   220  
   221          
   222          return INPUT;
   223      }
   224      
   225      
   226      /**
   227       * Send trackback to a specific url.
   228       */
   229      @SkipValidation
   230      public String trackback() {
   231          
   232          // make sure we have an entry to edit and it belongs to the action weblog
                 /* 
    P/P           *  Method: String trackback()
                  * 
                  *  Preconditions:
                  *    init'ed(this.entry)
                  *    (soft) log != null
                  *    (soft) init'ed(this.trackbackUrl)
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@254 != null
                  *    java.util.Iterator:next(...)@263 != null
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:getWebsite(...)@235 != null
                  *    org.apache.roller.weblogger.util.RollerMessages:getErrors(...)@262 != null
                  *    org.apache.roller.weblogger.util.RollerMessages:getMessages(...)@253 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"input",&"access-denied",&"error"}
                  *    this.trackbackUrl == One-of{old this.trackbackUrl, null}
                  *    (soft) init'ed(this.trackbackUrl)
                  * 
                  *  Test Vectors:
                  *    this.entry: Inverse{null}, Addr_Set{null}
                  *    java.util.Iterator:hasNext(...)@253: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@262: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@239: {1}, {0}
                  *    org.apache.roller.weblogger.pojos.Weblog:equals(...)@235: {1}, {0}
                  *    org.apache.roller.weblogger.util.RollerMessages_RollerMessage:getArgs(...)@255: Inverse{null}, Addr_Set{null}
                  *    org.apache.roller.weblogger.util.RollerMessages_RollerMessage:getArgs(...)@264: Inverse{null}, Addr_Set{null}
                  */
   233          if(getEntry() == null) {
   234              return ERROR;
   235          } else if(!getEntry().getWebsite().equals(getActionWeblog())) {
   236              return DENIED;
   237          }
   238          
   239          if(!StringUtils.isEmpty(getTrackbackUrl())) {
   240              RollerMessages results = null;
   241              try {
   242                  Trackback trackback = new Trackback(getEntry(), getTrackbackUrl());
   243                  results = trackback.send();
   244              } catch(TrackbackNotAllowedException ex) {
   245                  addError("error.trackbackNotAllowed");
   246              } catch(Throwable t) {
   247                  log.error("Error sending trackback", t);
   248                  // TODO: error handling
   249                  addError("error.general", t.getMessage());
   250              }
   251              
   252              if(results != null) {
   253                  for (Iterator mit = results.getMessages(); mit.hasNext();) {
   254                      RollerMessage msg = (RollerMessage) mit.next();
   255                      if(msg.getArgs() == null) {
   256                          addMessage(msg.getKey());
   257                      } else {
   258                          addMessage(msg.getKey(), Arrays.asList(msg.getArgs()));
   259                      }
   260                  }
   261                  
   262                  for (Iterator eit = results.getErrors(); eit.hasNext();) {
   263                      RollerMessage err = (RollerMessage) eit.next();
   264                      if(err.getArgs() == null) {
   265                          addError(err.getKey());
   266                      } else {
   267                          addError(err.getKey(), Arrays.asList(err.getArgs()));
   268                      }
   269                  }
   270              }
   271  
   272              // reset trackback url
   273              setTrackbackUrl(null);
   274              
   275          }
   276          
   277          return INPUT;
   278      }
   279      
   280      
   281      /**
   282       * Get the list of all categories for the action weblog, not including root.
   283       */
   284      public List<WeblogCategory> getCategories() {
   285          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(...)@286 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@286 != null
                      * 
                      *  Postconditions:
                      *    init'ed(return_value)
                      */
   286              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   287              return wmgr.getWeblogCategories(getActionWeblog(), false);
   288          } catch (WebloggerException ex) {
   289              log.error("Error getting category list for weblog - "+getWeblog(), ex);
   290              return Collections.EMPTY_LIST;
   291          }
   292      }
   293      
   294      public String getPreviewURL() {
                 /* 
    P/P           *  Method: String getPreviewURL()
                  * 
                  *  Preconditions:
                  *    this.entry != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.URLStrategy:getPreviewURLStrategy(...)@295 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@295 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@295 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   295          return WebloggerFactory.getWeblogger().getUrlStrategy().getPreviewURLStrategy(null).getWeblogEntryURL(getActionWeblog(), null, getEntry().getAnchor(), true);
   296      }
   297      
   298      
   299      public EntryBean getBean() {
                 /* 
    P/P           *  Method: EntryBean getBean()
                  * 
                  *  Preconditions:
                  *    init'ed(this.bean)
                  * 
                  *  Postconditions:
                  *    return_value == this.bean
                  *    init'ed(return_value)
                  */
   300          return bean;
   301      }
   302  
   303      public void setBean(EntryBean bean) {
                 /* 
    P/P           *  Method: void setBean(EntryBean)
                  * 
                  *  Postconditions:
                  *    this.bean == bean
                  *    init'ed(this.bean)
                  */
   304          this.bean = bean;
   305      }
   306  
   307      public WeblogEntry getEntry() {
                 /* 
    P/P           *  Method: WeblogEntry getEntry()
                  * 
                  *  Preconditions:
                  *    init'ed(this.entry)
                  * 
                  *  Postconditions:
                  *    return_value == this.entry
                  *    init'ed(return_value)
                  */
   308          return entry;
   309      }
   310  
   311      public void setEntry(WeblogEntry entry) {
                 /* 
    P/P           *  Method: void setEntry(WeblogEntry)
                  * 
                  *  Postconditions:
                  *    this.entry == entry
                  *    init'ed(this.entry)
                  */
   312          this.entry = entry;
   313      }
   314  
   315      public String getTrackbackUrl() {
                 /* 
    P/P           *  Method: String getTrackbackUrl()
                  * 
                  *  Preconditions:
                  *    init'ed(this.trackbackUrl)
                  * 
                  *  Postconditions:
                  *    return_value == this.trackbackUrl
                  *    init'ed(return_value)
                  */
   316          return trackbackUrl;
   317      }
   318  
   319      public void setTrackbackUrl(String trackbackUrl) {
                 /* 
    P/P           *  Method: void setTrackbackUrl(String)
                  * 
                  *  Postconditions:
                  *    this.trackbackUrl == trackbackUrl
                  *    init'ed(this.trackbackUrl)
                  */
   320          this.trackbackUrl = trackbackUrl;
   321      }
   322      
   323  }








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