File Source: WeblogCalendarModel.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.core.tags.calendar;
    20  
    21  import java.text.ParsePosition;
    22  import java.text.SimpleDateFormat;
    23  import java.util.Calendar;
    24  import java.util.Date;
    25  import java.util.HashMap;
    26  import java.util.List;
    27  import java.util.Locale;
    28  import java.util.Map;
    29  import java.util.TimeZone;
    30  
    31  import org.apache.commons.lang.StringUtils;
    32  import org.apache.commons.logging.Log;
    33  import org.apache.commons.logging.LogFactory;
    34  import org.apache.roller.weblogger.WebloggerException;
    35  import org.apache.roller.weblogger.business.WebloggerFactory;
    36  import org.apache.roller.weblogger.business.WeblogManager;
    37  import org.apache.roller.weblogger.pojos.WeblogEntry;
    38  import org.apache.roller.weblogger.pojos.Weblog;
    39  import org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest;
    40  import org.apache.roller.util.DateUtil;
    41  
    42  
    43  /**
    44   * Calendar model for calendar intended for use on view-weblog page.
    45   */
    46  public class WeblogCalendarModel implements CalendarModel {
    47      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.core.tags.calendar.WeblogCalendarModel__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    48      private static Log log = LogFactory.getLog(WeblogCalendarModel.class);
    49      
    50      protected Map               monthMap;
    51      protected Date              day;
    52      protected String            cat = null;
    53      protected String            pageLink = null;
    54      protected String            locale = null;
    55      protected Calendar          calendar = null;
    56      protected Weblog       weblog = null;
    57      protected Date              prevMonth = null; // prev month or null if none
    58      protected Date              nextMonth = null; // next month or null if none    
    59      protected WeblogPageRequest pageRequest = null;
    60      
    61      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.core.tags.calendar.WeblogCalendarModel(WeblogPageRequest, String)
              * 
              *  Preconditions:
              *    (soft) log != null
              *    (soft) pRequest != null
              * 
              *  Presumptions:
              *    org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getWeblog(...)@66 != null
              * 
              *  Postconditions:
              *    init'ed(this.calendar)
              *    init'ed(this.cat)
              *    possibly_updated(this.day)
              *    init'ed(this.locale)
              *    possibly_updated(this.monthMap)
              *    init'ed(this.nextMonth)
              *    init'ed(this.pageLink)
              *    this.pageRequest == pRequest
              *    (soft) this.pageRequest != null
              *    init'ed(this.prevMonth)
              *    ...
              * 
              *  Test Vectors:
              *    catArgument: Addr_Set{null}, Inverse{null}
              *    org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:getWeblogCategoryName(...)@83: Addr_Set{null}, Inverse{null}
              */
    62      public WeblogCalendarModel(WeblogPageRequest pRequest, String catArgument) {
    63          
    64          this.pageRequest = pRequest;
    65          try {
    66              this.weblog = pageRequest.getWeblog();            
    67              if(weblog == null) {
    68                  throw new WebloggerException("unable to lookup weblog: "+
    69                          pageRequest.getWeblogHandle());
    70              }
    71              pageLink = pageRequest.getWeblogPageName();            
    72  //            day = DateUtil.parseWeblogURLDateString(pageRequest.getWeblogDate(),
    73  //                    weblog.getTimeZoneInstance(), weblog.getLocaleInstance());
    74              day = parseWeblogURLDateString(pageRequest.getWeblogDate(),
    75                    weblog.getTimeZoneInstance(), weblog.getLocaleInstance());
    76              locale = pageRequest.getLocale();
    77              
    78              initDay(day);  
    79              
    80              // Category method argument overrides category from URL
    81              if (catArgument != null) {
    82                  cat = catArgument;
    83              } else if (pageRequest.getWeblogCategoryName() != null) {
    84                  cat = pageRequest.getWeblogCategoryName();
    85              }
    86              
    87          } catch (Exception e) {
    88              // some kind of error parsing the request or looking up weblog
    89              log.debug("ERROR: initializing calendar", e);
    90          }
    91          
    92      }
    93      
    94      
    95      protected void initDay(Date month) {
                 /* 
    P/P           *  Method: void initDay(Date)
                  * 
                  *  Preconditions:
                  *    init'ed(this.cat)
                  *    this.weblog != null
                  *    (soft) log != null
                  *    (soft) init'ed(this.locale)
                  * 
                  *  Presumptions:
                  *    java.util.Calendar:getInstance(...)@96 != null
                  *    java.util.Date:getTime(...)@109 >= -263+1
                  *    java.util.Date:getTime(...)@136 <= 264-2
                  *    java.util.List:get(...)@124 != null
                  *    java.util.List:get(...)@151 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    (soft) this.calendar != null
                  *    possibly_updated(this.monthMap)
                  *    possibly_updated(this.nextMonth)
                  *    possibly_updated(this.prevMonth)
                  *    new HashMap(loadWeblogEntries#1) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    java.util.Date:after(...)@160: {0}, {1}
                  *    java.util.List:size(...)@123: {-231..0}, {1..232-1}
                  *    java.util.List:size(...)@150: {-231..0}, {1..232-1}
                  */
    96          calendar = Calendar.getInstance(
    97                  weblog.getTimeZoneInstance(),
    98                  weblog.getLocaleInstance());
    99          
   100          Calendar cal = (Calendar)calendar.clone();
   101          Date startDate = DateUtil.getStartOfMonth(month,cal);
   102          Date endDate = DateUtil.getEndOfMonth(month,cal);
   103          
   104          // Determine previous non-empty month
   105          // Get entries before startDate, using category restriction limit 1
   106          // Use entry's date as previous month
   107          try {
   108              WeblogManager mgr = WebloggerFactory.getWeblogger().getWeblogManager();
   109              List prevEntries = mgr.getWeblogEntries(
   110                      
   111                      weblog,                    // website
   112                      null,                      // user
   113                      null,                      // startDate
   114                      // since we need an entry.pubTime<startDate, but the method use <=
   115                      new Date(startDate.getTime()-1),                 // endDate 
   116                      cat,                       // cat
   117                      null,WeblogEntry.PUBLISHED, // status
   118                      null,                      // text
   119                      null,                      // sortby (null means pubTime)
   120                      WeblogManager.DESCENDING,  // sortorder, null means DESCENDING
   121                      locale,                    // locale
   122                      0, 1);                     // offset, range
   123              if (prevEntries.size() > 0) {
   124                  WeblogEntry prevEntry = (WeblogEntry)prevEntries.get(0);
   125                  prevMonth = DateUtil.getStartOfMonth(new Date(prevEntry.getPubTime().getTime()),getCalendar());
   126              }
   127          } catch (WebloggerException e) {
   128              log.error("ERROR determining previous non-empty month");
   129          }
   130          
   131          // Determine next non-empty month
   132          // Get entries after endDate, using category restriction limit 1
   133          // Use entry's date as next month
   134          try {
   135              WeblogManager mgr = WebloggerFactory.getWeblogger().getWeblogManager();
   136              List nextEntries = mgr.getWeblogEntries(
   137                      
   138                      weblog,                    // website
   139                      null,                      // user
   140                      // since we need an entry.pubTime>endDate, but the method use >=
   141                      new Date(endDate.getTime()+1),                   // startDate
   142                      null,                      // endDate 
   143                      cat,                       // cat
   144                      null,WeblogEntry.PUBLISHED, // status
   145                      null,                      // text
   146                      null,                      // sortby (null means pubTime)
   147                      WeblogManager.ASCENDING,   // sortorder
   148                      locale,                    // locale
   149                      0, 1);                     // offset, range
   150              if (nextEntries.size() > 0) {
   151                  WeblogEntry nextEntry = (WeblogEntry)nextEntries.get(0);
   152                  nextMonth = DateUtil.getStartOfMonth(new Date(nextEntry.getPubTime().getTime()),getCalendar());
   153              }
   154          } catch (WebloggerException e) {
   155              log.error("ERROR determining next non-empty month");
   156          }  
   157          
   158          // Fix for ROL-840 Don't include future entries
   159          Date now = new Date();
   160          if (endDate.after(now)) {
   161          	endDate = now;
   162          	nextMonth = null;
   163          }
   164          
   165          loadWeblogEntries(startDate, endDate, cat);
   166      }
   167      
   168      protected void loadWeblogEntries(Date startDate, Date endDate, String catName) {
   169          try {
                     /* 
    P/P               *  Method: void loadWeblogEntries(Date, Date, String)
                      * 
                      *  Preconditions:
                      *    (soft) log != null
                      *    (soft) init'ed(this.locale)
                      *    (soft) init'ed(this.weblog)
                      * 
                      *  Presumptions:
                      *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@170 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@170 != null
                      * 
                      *  Postconditions:
                      *    init'ed(this.monthMap)
                      *    new HashMap(loadWeblogEntries#1) num objects <= 1
                      */
   170              WeblogManager mgr = WebloggerFactory.getWeblogger().getWeblogManager();
   171              monthMap = mgr.getWeblogEntryStringMap(
   172                      
   173                      weblog,                  // website
   174                      startDate,                 // startDate
   175                      endDate,                   // endDate
   176                      catName,                   // cat
   177                      null,WeblogEntry.PUBLISHED, // status
   178                      locale,
   179                      0, -1);
   180          } catch (WebloggerException e) {
   181              log.error(e);
   182              monthMap = new HashMap();
   183          }
   184      }
   185      
   186      public void setDay(String month) throws Exception {
                 /* 
    P/P           *  Method: void setDay(String)
                  * 
                  *  Preconditions:
                  *    this.calendar != null
                  *    init'ed(this.cat)
                  *    this.weblog != null
                  *    (soft) log != null
                  *    (soft) init'ed(this.locale)
                  * 
                  *  Presumptions:
                  *    java.util.Calendar:getInstance(...)@96 != null
                  *    org.apache.roller.util.DateUtil:get8charDateFormat(...)@187 != null
                  * 
                  *  Postconditions:
                  *    this.calendar != null
                  *    possibly_updated(this.monthMap)
                  *    possibly_updated(this.nextMonth)
                  *    possibly_updated(this.prevMonth)
                  *    new HashMap(loadWeblogEntries#1) num objects <= 1
                  */
   187          SimpleDateFormat fmt = DateUtil.get8charDateFormat();
   188          fmt.setCalendar(getCalendar());
   189          ParsePosition pos = new ParsePosition(0);
   190          initDay( fmt.parse( month, pos ) );
   191      }
   192      
   193      public Date getDay() {
                 /* 
    P/P           *  Method: Date getDay()
                  * 
                  *  Preconditions:
                  *    this.day != null
                  * 
                  *  Postconditions:
                  *    return_value != null
                  */
   194          return (Date)day.clone();
   195      }
   196      
   197      public String getParameterValue(Date day) {
                 /* 
    P/P           *  Method: String getParameterValue(Date)
                  * 
                  *  Preconditions:
                  *    this.monthMap != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   198          return (String)monthMap.get( day );
   199      }
   200  
   201      // convenience method returns 8 char day stamp YYYYMMDD
   202      public static String format8chars(Date date, Calendar cal) {
            	 /* 
    P/P     	  *  Method: String format8chars(Date, Calendar)
            	  * 
            	  *  Presumptions:
            	  *    org.apache.roller.util.DateUtil:get8charDateFormat(...)@203 != null
            	  * 
            	  *  Postconditions:
            	  *    init'ed(return_value)
            	  */
   203      	SimpleDateFormat format = DateUtil.get8charDateFormat();
   204      	format.setCalendar(cal);
   205      	return DateUtil.format(date,format);
   206      }
   207      
   208      
   209      // convenience method returns 6 char month stamp YYYYMM
   210      public static String format6chars(Date date, Calendar cal) {
            	 /* 
    P/P     	  *  Method: String format6chars(Date, Calendar)
            	  * 
            	  *  Presumptions:
            	  *    org.apache.roller.util.DateUtil:get6charDateFormat(...)@211 != null
            	  * 
            	  *  Postconditions:
            	  *    init'ed(return_value)
            	  */
   211      	SimpleDateFormat format = DateUtil.get6charDateFormat();
   212      	format.setCalendar(cal);
   213      	return DateUtil.format(date,format);
   214      }
   215  
   216      /**
   217       * Parse data as either 6-char or 8-char format.
   218       */
   219      public static Date parseWeblogURLDateString(String dateString, TimeZone tz, Locale locale) {
   220          
                 /* 
    P/P           *  Method: Date parseWeblogURLDateString(String, TimeZone, Locale)
                  * 
                  *  Presumptions:
                  *    java.text.SimpleDateFormat:parse(...)@230 != null
                  *    java.text.SimpleDateFormat:parse(...)@250 != null
                  *    org.apache.roller.util.DateUtil:get6charDateFormat(...)@247 != null
                  *    org.apache.roller.util.DateUtil:get8charDateFormat(...)@227 != null
                  * 
                  *  Postconditions:
                  *    (soft) return_value != null
                  *    new Date(parseWeblogURLDateString#1) num objects == 1
                  *    new Date(parseWeblogURLDateString#3) num objects <= 1
                  *    new Date(parseWeblogURLDateString#5) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    dateString: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@224: {0..7, 9..232-1}, {8}
                  *    java.lang.String:length(...)@244: {0..5, 7..232-1}, {6}
                  *    java.util.Date:after(...)@240: {0}, {1}
                  *    java.util.Date:after(...)@258: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isNumeric(...)@224: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isNumeric(...)@244: {0}, {1}
                  */
   221          Date ret = new Date();
   222          Calendar cal = Calendar.getInstance(tz,locale);
   223          
   224          if (dateString != null
   225                  && dateString.length()==8
   226                  && StringUtils.isNumeric(dateString) ) {
   227              SimpleDateFormat char8DateFormat = DateUtil.get8charDateFormat();
   228              char8DateFormat.setCalendar(cal);
   229              ParsePosition pos = new ParsePosition(0);
   230              ret = char8DateFormat.parse(dateString, pos);
   231              
   232              // make sure the requested date is not in the future
   233  //            Date today = null;
   234  //            Calendar todayCal = Calendar.getInstance();
   235  //            todayCal = Calendar.getInstance(tz, locale);
   236  //            todayCal.setTime(new Date());
   237  //            today = todayCal.getTime();
   238              // Date is always ms offset from epoch in UTC, by no means of timezone.
   239              Date today = new Date();
   240              if(ret.after(today)) {
   241                  ret = today;
   242              }
   243              
   244          } else if(dateString != null
   245                  && dateString.length()==6
   246                  && StringUtils.isNumeric(dateString)) {
   247              SimpleDateFormat char6DateFormat = DateUtil.get6charDateFormat();
   248              char6DateFormat.setCalendar(cal);
   249              ParsePosition pos = new ParsePosition(0);
   250              ret = char6DateFormat.parse(dateString, pos);
   251              
   252              // make sure the requested date is not in the future
   253  //            Calendar todayCal = Calendar.getInstance();
   254  //            todayCal = Calendar.getInstance(tz, locale);
   255  //            todayCal.setTime(new Date());
   256  //            Date today = todayCal.getTime();
   257              Date today = new Date();
   258              if(ret.after(today)) {
   259                  ret = today;
   260              }
   261          }
   262          
   263          return ret;
   264      }
   265      
   266      /**
   267       * Create URL for use on view-weblog page
   268       * @param day       Day for URL or null if no entries on that day
   269       * @param alwaysURL Always return a URL, never return null
   270       * @return          URL for day, or null if no weblog entry on that day
   271       */
   272      public String computeUrl(Date day, boolean monthURL, boolean alwaysURL) {
                 /* 
    P/P           *  Method: String computeUrl(Date, bool, bool)
                  * 
                  *  Preconditions:
                  *    this.monthMap != null
                  *    (soft) log != null
                  *    (soft) this.calendar != null
                  *    (soft) init'ed(this.cat)
                  *    (soft) init'ed(this.locale)
                  *    (soft) init'ed(this.pageLink)
                  *    (soft) init'ed(this.weblog)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@286 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@288 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@286 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@288 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    alwaysURL: {1}, {0}
                  *    monthURL: {1}, {0}
                  *    this.pageLink: Inverse{null}, Addr_Set{null}
                  *    java.util.Map:get(...)@275: Inverse{null}, Addr_Set{null}
                  */
   273          String url = null;
   274          // get the 8 char YYYYMMDD datestring for day
   275          String dateString = (String)monthMap.get(day);
   276          if (dateString == null && !alwaysURL) return null;
   277          else if (dateString == null && !monthURL) {
+  278              dateString = DateUtil.format8chars(day);
   279          	dateString = format8chars(day,getCalendar());
   280          } else if (dateString == null && monthURL) {
   281  //            dateString = DateUtil.format6chars(day);
   282              dateString = format6chars(day,getCalendar());
   283          }
   284          try {
   285              if (pageLink == null) { // create date URL
   286                  url = WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogCollectionURL(weblog, locale, cat, dateString, null, -1, false);
   287              } else { // create page URL
   288                  url = WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogPageURL(weblog, locale, pageLink, null, cat, dateString, null, -1, false);
   289              }
   290          } catch (Exception e) {
   291              log.error("ERROR: creating URL",e);
   292          }
   293          return url;
   294      }
   295      
   296      public String getContent(Date day) {
                 /* 
    P/P           *  Method: String getContent(Date)
                  * 
                  *  Postconditions:
                  *    return_value == null
                  */
   297          return null;
   298      }
   299      
   300      public Calendar getCalendar() {
                 /* 
    P/P           *  Method: Calendar getCalendar()
                  * 
                  *  Preconditions:
                  *    this.calendar != null
                  * 
                  *  Postconditions:
                  *    return_value != null
                  */
   301          return (Calendar)calendar.clone();
   302      }
   303      
   304      public Date getNextMonth() {
                 /* 
    P/P           *  Method: Date getNextMonth()
                  * 
                  *  Preconditions:
                  *    init'ed(this.nextMonth)
                  * 
                  *  Postconditions:
                  *    return_value == this.nextMonth
                  *    init'ed(return_value)
                  */
   305          return nextMonth;
   306      }
   307  
   308      public Date getPrevMonth() {
                 /* 
    P/P           *  Method: Date getPrevMonth()
                  * 
                  *  Preconditions:
                  *    init'ed(this.prevMonth)
                  * 
                  *  Postconditions:
                  *    return_value == this.prevMonth
                  *    init'ed(return_value)
                  */
   309          return prevMonth;
   310      }
   311         
   312      public String computeNextMonthUrl() {
                 /* 
    P/P           *  Method: String computeNextMonthUrl()
                  * 
                  *  Preconditions:
                  *    this.monthMap != null
                  *    init'ed(this.nextMonth)
                  *    (soft) init'ed(this.cat)
                  *    (soft) init'ed(this.locale)
                  *    (soft) init'ed(this.pageLink)
                  *    (soft) init'ed(this.weblog)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   313          return computeUrl(nextMonth, true, true);
   314      }
   315  
   316      public String computePrevMonthUrl() {
                 /* 
    P/P           *  Method: String computePrevMonthUrl()
                  * 
                  *  Preconditions:
                  *    this.monthMap != null
                  *    init'ed(this.prevMonth)
                  *    (soft) init'ed(this.cat)
                  *    (soft) init'ed(this.locale)
                  *    (soft) init'ed(this.pageLink)
                  *    (soft) init'ed(this.weblog)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   317          return computeUrl(prevMonth, true, true);
   318      }
   319      
   320      public String computeTodayMonthUrl() {
   321      	String url;
                 /* 
    P/P           *  Method: String computeTodayMonthUrl()
                  * 
                  *  Preconditions:
                  *    init'ed(this.cat)
                  *    init'ed(this.locale)
                  *    init'ed(this.pageLink)
                  *    init'ed(this.weblog)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@323 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@325 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@323 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@325 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    this.pageLink: Inverse{null}, Addr_Set{null}
                  */
   322          if (pageLink == null) { // create default URL
   323              url = WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogCollectionURL(weblog, locale, cat, null, null, -1, false);
   324          } else { // create page URL
   325              url = WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogPageURL(weblog, locale, pageLink, null, cat, null, null, -1, false);
   326          }
   327      	return url;
   328      }
   329      
   330  }








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