File Source: CommentsPager.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.rendering.pagers;
    20  
    21  import java.sql.Timestamp;
    22  import java.util.ArrayList;
    23  import java.util.Calendar;
    24  import java.util.Date;
    25  import java.util.Iterator;
    26  import java.util.List;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.roller.weblogger.business.URLStrategy;
    30  import org.apache.roller.weblogger.business.Weblogger;
    31  import org.apache.roller.weblogger.business.WebloggerFactory;
    32  import org.apache.roller.weblogger.business.WeblogManager;
    33  import org.apache.roller.weblogger.pojos.WeblogEntryComment;
    34  import org.apache.roller.weblogger.pojos.Weblog;
    35  import org.apache.roller.weblogger.pojos.wrapper.WeblogEntryCommentWrapper;
    36  
    37  
    38  /**
    39   * Paging through a collection of comments.
    40   */
    41  public class CommentsPager extends AbstractPager {
    42      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.rendering.pagers.CommentsPager__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    43      private static Log log = LogFactory.getLog(CommentsPager.class);
    44      
    45      private Weblog weblog = null;
    46      private String locale = null;
    47      private int sinceDays = -1;
    48      private int length = 0;
    49      
    50      // the collection for the pager
    51      private List comments = null;
    52      
    53      // are there more items?
    54      private boolean more = false;
    55      
    56      // most recent update time of current set of entries
    57      private Date lastUpdated = null;        
    58      
    59      public CommentsPager(
    60              URLStrategy    strat,
    61              String         baseUrl,
    62              Weblog         weblog,
    63              String         locale,
    64              int            sinceDays,
    65              int            page,
    66              int            length) {
    67          
                 /* 
    P/P           *  Method: void org.apache.roller.weblogger.ui.rendering.pagers.CommentsPager(URLStrategy, String, Weblog, String, int, int, int)
                  * 
                  *  Preconditions:
                  *    (soft) length <= 232-2
                  *    (soft) log != null
                  *    (soft) sinceDays <= 231
                  * 
                  *  Postconditions:
                  *    this.comments == &new ArrayList(getItems#1)
                  *    init'ed(this.lastUpdated)
                  *    this.length == length
                  *    (soft) this.length <= 232-2
                  *    this.locale == locale
                  *    init'ed(this.locale)
                  *    init'ed(this.more)
                  *    this.page == One-of{0, page}
                  *    this.page >= 0
                  *    this.sinceDays == sinceDays
                  *    ...
                  */
    68          super(strat, baseUrl, page);
    69          
    70          this.weblog = weblog;
    71          this.locale = locale;
    72          this.sinceDays = sinceDays;
    73          this.length = length;
    74          
    75          // initialize the collection
    76          getItems();
    77      }
    78      
    79      
    80      public List getItems() {
    81          
                 /* 
    P/P           *  Method: List getItems()
                  * 
                  *  Preconditions:
                  *    init'ed(this.comments)
                  *    (soft) log != null
                  *    (soft) this.length <= 232-2
                  *    (soft) this.length*this.page in -231..232-1
                  *    (soft) init'ed(this.page)
                  *    (soft) this.sinceDays <= 231
                  *    (soft) init'ed(this.weblog)
                  * 
                  *  Presumptions:
                  *    java.util.Calendar:getInstance(...)@90 != null
                  *    org.apache.roller.weblogger.business.WeblogManager:getComments(...)@99 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@98 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@97 != null
                  * 
                  *  Postconditions:
                  *    return_value == One-of{old this.comments, &new ArrayList(getItems#1)}
                  *    return_value != null
                  *    this.comments == return_value
                  *    possibly_updated(this.more)
                  *    new ArrayList(getItems#1) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    this.comments: Inverse{null}, Addr_Set{null}
                  *    this.sinceDays: {-231..0}, {1..231}
                  */
    82          if (comments == null) {
    83              // calculate offset
    84              int offset = getPage() * length;
    85              
    86              List results = new ArrayList();
    87              
    88              Date startDate = null;
    89              if(sinceDays > 0) {
    90                  Calendar cal = Calendar.getInstance();
    91                  cal.setTime(new Date());
    92                  cal.add(Calendar.DATE, -1 * sinceDays);
    93                  startDate = cal.getTime();
    94              }
    95              
    96              try {
    97                  Weblogger roller = WebloggerFactory.getWeblogger();
    98                  WeblogManager wmgr = roller.getWeblogManager();
    99                  List entries = wmgr.getComments(
   100                          weblog, null, null, startDate, null, WeblogEntryComment.APPROVED, true, offset, length + 1);
   101                  
   102                  // wrap the results
   103                  int count = 0;
   104                  for (Iterator it = entries.iterator(); it.hasNext();) {
   105                      WeblogEntryComment comment = (WeblogEntryComment) it.next();
+  106                      if (count++ < length) {
   107                          results.add(WeblogEntryCommentWrapper.wrap(comment, urlStrategy));
   108                      } else {
   109                          more = true;
   110                      }
   111                  }
   112                  
   113              } catch (Exception e) {
   114                  log.error("ERROR: fetching comment list", e);
   115              }
   116              
   117              comments = results;
   118          }
   119          
   120          return comments;
   121      }
   122      
   123      
   124      public boolean hasMoreItems() {
                 /* 
    P/P           *  Method: bool hasMoreItems()
                  * 
                  *  Preconditions:
                  *    init'ed(this.more)
                  * 
                  *  Postconditions:
                  *    return_value == this.more
                  *    init'ed(return_value)
                  */
   125          return more;
   126      }
   127      
   128      /** Get last updated time from items in pager */
   129      public Date getLastUpdated() {
                 /* 
    P/P           *  Method: Date getLastUpdated()
                  * 
                  *  Preconditions:
                  *    init'ed(this.lastUpdated)
                  *    (soft) log != null
                  *    (soft) init'ed(this.comments)
                  *    (soft) this.length <= 232-2
                  *    (soft) this.length*this.page in -231..232-1
                  *    (soft) init'ed(this.page)
                  *    (soft) this.sinceDays <= 231
                  *    (soft) init'ed(this.weblog)
                  * 
                  *  Presumptions:
                  *    c.pojo@135 != null
                  *    java.util.Iterator:next(...)@135 != null
                  *    java.util.List:get(...).pojo@134 != null
                  *    java.util.List:get(...)@134 != null
                  *    org.apache.roller.weblogger.pojos.WeblogEntryComment:getPostTime(...)@128 != null
                  * 
                  *  Postconditions:
                  *    return_value == One-of{old this.lastUpdated, &new Date(getLastUpdated#1), &new Date(getLastUpdated#2)}
                  *    return_value != null
                  *    this.lastUpdated == return_value
                  *    this.comments == One-of{old this.comments, &new ArrayList(getItems#1)}
                  *    (soft) init'ed(this.comments)
                  *    possibly_updated(this.more)
                  *    new ArrayList(getItems#1) num objects <= 1
                  *    new Date(getLastUpdated#1) num objects <= 1
                  *    new Date(getLastUpdated#2) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    this.lastUpdated: Inverse{null}, Addr_Set{null}
                  *    java.sql.Timestamp:after(...)@136: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@135: {0}, {1}
                  *    java.util.List:size(...)@133: {-231..0}, {1..232-1}
                  */
   130          if (lastUpdated == null) {
   131              // feeds are sorted by pubtime, so first might not be last updated
   132              List<WeblogEntryCommentWrapper> items = (List<WeblogEntryCommentWrapper>)getItems();
+  133              if (getItems() != null && getItems().size() > 0) {
   134                  Timestamp newest = ((WeblogEntryCommentWrapper)getItems().get(0)).getPostTime();
   135                  for (WeblogEntryCommentWrapper c : items) {
   136                      if (c.getPostTime().after(newest)) {
   137                          newest = c.getPostTime();
   138                      }
   139                  }
   140                  lastUpdated = new Date(newest.getTime());
   141              } else {
   142                  // no update so we assume it's brand new
   143                  lastUpdated = new Date();
   144              }
   145          }
   146          return lastUpdated;
   147      }
   148  }
   149  








SofCheck Inspector Build Version : 2.18479
CommentsPager.java 2009-Jan-02 14:25:30
CommentsPager.class 2009-Sep-04 03:12:44