File Source: AbstractWeblogEntriesPager.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.text.ParsePosition;
    22  import java.text.SimpleDateFormat;
    23  import java.util.ArrayList;
    24  import java.util.Calendar;
    25  import java.util.Date;
    26  import java.util.List;
    27  import java.util.Locale;
    28  import org.apache.commons.lang.StringUtils;
    29  import org.apache.commons.logging.Log;
    30  import org.apache.commons.logging.LogFactory;
    31  import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
    32  import org.apache.roller.weblogger.pojos.Weblog;
    33  import org.apache.roller.util.DateUtil;
    34  import org.apache.roller.weblogger.business.URLStrategy;
    35  import org.apache.roller.weblogger.business.WebloggerFactory;
    36  import org.apache.roller.weblogger.util.I18nMessages;
    37  
    38  /**
    39   * An abstract implementation of a WeblogEntriesPager.
    40   *
    41   * This implementation lays out the basic functionality of an entries pager so
    42   * that subclasses can easily tweak only the few things necessary to handle
    43   * paging their own way.
    44   */
    45  public abstract class AbstractWeblogEntriesPager implements WeblogEntriesPager {
    46      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.rendering.pagers.AbstractWeblogEntriesPager__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    47      private static Log log = LogFactory.getLog(AbstractWeblogEntriesPager.class);
    48      
    49      // message utils for doing i18n messages
    50      I18nMessages messageUtils = null;
    51      
    52      // url strategy for building urls
    53      URLStrategy urlStrategy = null;
    54      
    55      Weblog weblog = null;
    56      String locale = null;
    57      String pageLink = null;
    58      String entryAnchor = null;
    59      String dateString = null;
    60      String catPath = null;
    61      List tags = new ArrayList();
    62      int offset = 0;
    63      int page = 0;
    64      int length = 0;
    65      
    66      
    67      public AbstractWeblogEntriesPager(
    68              URLStrategy        strat,
    69              Weblog             weblog,
    70              String             locale,
    71              String             pageLink,
    72              String             entryAnchor,
    73              String             dateString,
    74              String             catPath,
    75              List               tags,
                     /* 
    P/P               *  Method: void org.apache.roller.weblogger.ui.rendering.pagers.AbstractWeblogEntriesPager(URLStrategy, Weblog, String, String, String, String, String, List, int)
                      * 
                      *  Preconditions:
                      *    locale == null
                      *    org/apache/roller/weblogger/util/I18nMessages.messagesMap != null
                      *    weblog != null
                      * 
                      *  Postconditions:
                      *    this.catPath == catPath
                      *    init'ed(this.catPath)
                      *    this.dateString == dateString
                      *    init'ed(this.dateString)
                      *    this.entryAnchor == entryAnchor
                      *    init'ed(this.entryAnchor)
                      *    init'ed(this.length)
                      *    this.locale == null
                      *    this.messageUtils != null
                      *    init'ed(this.offset)
                      *    ...
                      * 
                      *  Test Vectors:
                      *    page: {-231..0}, {1..232-1}
                      *    tags: Addr_Set{null}, Inverse{null}
                      */
    76              int                page) {
    77          
    78          this.urlStrategy = strat;
    79          
    80          this.weblog = weblog;
    81          this.locale = locale;
    82          this.pageLink = pageLink;
    83          this.entryAnchor = entryAnchor;
    84          this.dateString = dateString;
    85          this.catPath = catPath;
    86          
    87          if(tags != null)
    88            this.tags = tags;
    89          
    90          // make sure offset, length, and page are valid
    91          int maxLength = WebloggerRuntimeConfig.getIntProperty("site.pages.maxEntries");
    92          length = weblog.getEntryDisplayCount();
    93          if(length > maxLength) {
    94              length = maxLength;
    95          }
    96          
    97          if(page > 0) {
    98              this.page = page;
    99          }
   100          this.offset = length * page;
   101          
   102          // get a message utils instance to handle i18n of messages
   103          Locale viewLocale = null;
+  104          if(locale != null) {
+  105              String[] langCountry = locale.split("_");
   106              if(langCountry.length == 1) {
   107                  viewLocale = new Locale(langCountry[0]);
   108              } else if(langCountry.length == 2) {
   109                  viewLocale = new Locale(langCountry[0], langCountry[1]);
   110              }
   111          } else {
   112              viewLocale = weblog.getLocaleInstance();
   113          }
+  114          this.messageUtils = I18nMessages.getMessages(viewLocale);
   115      }
   116      
   117      
   118      public boolean hasMoreEntries() {
                 /* 
    P/P           *  Method: bool hasMoreEntries()
                  * 
                  *  Postconditions:
                  *    return_value == 0
                  */
   119          return false;
   120      }
   121      
   122      
   123      public String getHomeLink() {
                 /* 
    P/P           *  Method: String getHomeLink()
                  * 
                  *  Preconditions:
                  *    init'ed(this.catPath)
                  *    init'ed(this.dateString)
                  *    init'ed(this.entryAnchor)
                  *    init'ed(this.locale)
                  *    init'ed(this.pageLink)
                  *    init'ed(this.tags)
                  *    this.urlStrategy != null
                  *    init'ed(this.weblog)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   124          return createURL(0, 0, weblog, locale, pageLink, entryAnchor, dateString, catPath, tags);
   125      }
   126      
   127      
   128      public String getHomeName() {
                 /* 
    P/P           *  Method: String getHomeName()
                  * 
                  *  Preconditions:
                  *    this.messageUtils != null
                  *    (soft) this.messageUtils.bundle != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   129          return messageUtils.getString("weblogEntriesPager.latest.home");
   130      }
   131      
   132      
   133      public String getNextLink() {
                 /* 
    P/P           *  Method: String getNextLink()
                  * 
                  *  Preconditions:
                  *    (soft) init'ed(this.catPath)
                  *    (soft) init'ed(this.dateString)
                  *    (soft) init'ed(this.entryAnchor)
                  *    (soft) init'ed(this.locale)
                  *    (soft) init'ed(this.more)
                  *    (soft) this.page <= 232-2
                  *    (soft) init'ed(this.pageLink)
                  *    (soft) init'ed(this.tags)
                  *    (soft) this.urlStrategy != null
                  *    (soft) init'ed(this.weblog)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   134          if (hasMoreEntries()) {
   135              return createURL(page, 1, weblog, locale, pageLink, entryAnchor, dateString, catPath, tags);
   136          }
   137          return null;
   138      }
   139      
   140      
   141      public String getNextName() {
                 /* 
    P/P           *  Method: String getNextName()
                  * 
                  *  Preconditions:
                  *    (soft) this.messageUtils != null
                  *    (soft) this.messageUtils.bundle != null
                  *    (soft) init'ed(this.more)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   142          if (hasMoreEntries()) {
   143              return messageUtils.getString("weblogEntriesPager.latest.next");
   144          }
   145          return null;
   146      }
   147      
   148      
   149      public String getPrevLink() {
                 /* 
    P/P           *  Method: String getPrevLink()
                  * 
                  *  Preconditions:
                  *    init'ed(this.page)
                  *    (soft) init'ed(this.catPath)
                  *    (soft) init'ed(this.dateString)
                  *    (soft) init'ed(this.entryAnchor)
                  *    (soft) init'ed(this.locale)
                  *    (soft) init'ed(this.pageLink)
                  *    (soft) init'ed(this.tags)
                  *    (soft) this.urlStrategy != null
                  *    (soft) init'ed(this.weblog)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    this.page: {-231..0}, {1..232-1}
                  */
   150          if (page > 0) {
   151              return createURL(page, -1, weblog, locale, pageLink, entryAnchor, dateString, catPath, tags);
   152          }
   153          return null;
   154      }
   155      
   156      
   157      public String getPrevName() {
                 /* 
    P/P           *  Method: String getPrevName()
                  * 
                  *  Preconditions:
                  *    init'ed(this.page)
                  *    (soft) this.messageUtils != null
                  *    (soft) this.messageUtils.bundle != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    this.page: {-231..0}, {1..232-1}
                  */
   158          if (page > 0) {
   159              return messageUtils.getString("weblogEntriesPager.latest.prev");
   160          }
   161          return null;
   162      }
   163      
   164      
   165      public String getNextCollectionLink() {
                 /* 
    P/P           *  Method: String getNextCollectionLink()
                  * 
                  *  Postconditions:
                  *    return_value == null
                  */
   166          return null;
   167      }
   168      
   169      
   170      public String getNextCollectionName() {
                 /* 
    P/P           *  Method: String getNextCollectionName()
                  * 
                  *  Postconditions:
                  *    return_value == null
                  */
   171          return null;
   172      }
   173      
   174      
   175      public String getPrevCollectionLink() {
                 /* 
    P/P           *  Method: String getPrevCollectionLink()
                  * 
                  *  Postconditions:
                  *    return_value == null
                  */
   176          return null;
   177      }
   178      
   179      
   180      public String getPrevCollectionName() {
                 /* 
    P/P           *  Method: String getPrevCollectionName()
                  * 
                  *  Postconditions:
                  *    return_value == null
                  */
   181          return null;
   182      }
   183      
   184      
   185      /**
   186       * Parse data as either 6-char or 8-char format.
   187       */
   188      protected Date parseDate(String dateString) {
                 /* 
    P/P           *  Method: Date parseDate(String)
                  * 
                  *  Preconditions:
                  *    this.weblog != null
                  * 
                  *  Presumptions:
                  *    java.text.SimpleDateFormat:parse(...)@199 != null
                  *    java.text.SimpleDateFormat:parse(...)@210 != null
                  *    org.apache.roller.util.DateUtil:get6charDateFormat(...)@191 != null
                  *    org.apache.roller.util.DateUtil:get8charDateFormat(...)@190 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    dateString: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@194: {0..7, 9..232-1}, {8}
                  *    java.lang.String:length(...)@205: {0..5, 7..232-1}, {6}
                  *    java.util.Date:after(...)@203: {0}, {1}
                  *    java.util.Date:after(...)@214: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isNumeric(...)@194: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isNumeric(...)@205: {0}, {1}
                  */
   189          Date ret = null;
   190          SimpleDateFormat char8DateFormat = DateUtil.get8charDateFormat();
   191          SimpleDateFormat char6DateFormat = DateUtil.get6charDateFormat();
   192          Calendar cal = Calendar.getInstance(
   193                  weblog.getTimeZoneInstance(), weblog.getLocaleInstance());
   194          if (   dateString!=null
   195                  && dateString.length()==8
   196                  && StringUtils.isNumeric(dateString) ) {
   197          	char8DateFormat.setCalendar(cal);
   198              ParsePosition pos = new ParsePosition(0);
   199              ret = char8DateFormat.parse( dateString, pos );
   200              
   201              // make sure the requested date is not in the future
   202              Date today = getToday();
   203              if (ret.after(today)) ret = today;
   204          }
   205          if (   dateString!=null
   206                  && dateString.length()==6
   207                  && StringUtils.isNumeric(dateString) ) {
   208          	char6DateFormat.setCalendar(cal);
   209              ParsePosition pos = new ParsePosition(0);
   210              ret = char6DateFormat.parse( dateString, pos );
   211              
   212              // make sure the requested date is not in the future
   213              Date today = getToday();
   214              if (ret.after(today)) ret = today;
   215          }
   216          return ret;
   217      }
   218      
   219      
   220      /**
   221       * Return today based on current blog's timezone/locale.
   222       */
   223      protected Date getToday() {
                 /* 
    P/P           *  Method: Date getToday()
                  * 
                  *  Preconditions:
                  *    this.weblog != null
                  * 
                  *  Presumptions:
                  *    java.util.Calendar:getInstance(...)@225 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
+  224          Calendar todayCal = Calendar.getInstance();
   225          todayCal = Calendar.getInstance(
   226                  weblog.getTimeZoneInstance(), weblog.getLocaleInstance());
   227          todayCal.setTime(new Date());
   228          return todayCal.getTime();
   229      }
   230      
   231      
   232      /**
   233       * Create URL that encodes pager state using most appropriate forms of URL.
   234       * @param pageAdd To be added to page number, or 0 for no page number
   235       */
   236      protected String createURL(
   237              int                page,
   238              int                pageAdd,
   239              Weblog        website,
   240              String             locale,
   241              String             pageLink,
   242              String             entryAnchor,
   243              String             dateString,
   244              String             catPath,
   245              List               tags) {
   246          
                 /* 
    P/P           *  Method: String createURL(int, int, Weblog, String, String, String, String, String, List)
                  * 
                  *  Preconditions:
                  *    page + pageAdd in -231..232-1
                  *    this.urlStrategy != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    entryAnchor: Addr_Set{null}, Inverse{null}
                  *    pageLink: Addr_Set{null}, Inverse{null}
                  */
   247          int pageNum = page + pageAdd;
   248          
   249          if (pageLink != null) {
   250              return urlStrategy.getWeblogPageURL(website, locale, pageLink, entryAnchor, catPath, dateString, tags, pageNum, false);
   251          } else if (entryAnchor != null) {
   252              return urlStrategy.getWeblogEntryURL(website, locale, entryAnchor, true);
   253          }
   254          
   255          return urlStrategy.getWeblogCollectionURL(website, locale, catPath, dateString, tags, pageNum, false);
   256      }
   257      
   258  }








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