File Source: BigWeblogCalendarModel.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.SimpleDateFormat;
    22  import java.util.Date;
    23  import java.util.HashMap;
    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.WeblogEntry;
    31  import org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest;
    32  import org.apache.roller.util.DateUtil;
    33  
    34  
    35  /**
    36   * Model for big calendar that displays titles for each day.
    37   */
    38  public class BigWeblogCalendarModel extends WeblogCalendarModel {
    39      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.core.tags.calendar.BigWeblogCalendarModel__static_init
              * 
              *  Postconditions:
              *    init'ed(mLogger)
              *    mSingleDayFormat == &new SimpleDateFormat(BigWeblogCalendarModel__static_init#1)
              *    init'ed(mStarDateFormat)
              *    new SimpleDateFormat(BigWeblogCalendarModel__static_init#1) num objects == 1
              */
    40      private static Log mLogger = LogFactory.getLog(BigWeblogCalendarModel.class);
    41      
    42      protected static final SimpleDateFormat mStarDateFormat =
    43              DateUtil.get8charDateFormat();
    44      
    45      protected static final SimpleDateFormat mSingleDayFormat =
    46              new SimpleDateFormat("dd");
    47      
    48      
    49      public BigWeblogCalendarModel(WeblogPageRequest pRequest, String cat) {
                 /* 
    P/P           *  Method: void org.apache.roller.weblogger.ui.core.tags.calendar.BigWeblogCalendarModel(WeblogPageRequest, String)
                  * 
                  *  Preconditions:
                  *    (soft) org/apache/roller/weblogger/ui/core/tags/calendar/WeblogCalendarModel.log != null
                  *    (soft) pRequest != 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)
                  *    ...
                  */
    50          super(pRequest, cat);
    51      }
    52      
    53      
    54      protected void loadWeblogEntries(Date startDate, Date endDate, String catName) {
    55          try {
                     /* 
    P/P               *  Method: void loadWeblogEntries(Date, Date, String)
                      * 
                      *  Preconditions:
                      *    (soft) mLogger != null
                      *    (soft) init'ed(this.locale)
                      *    (soft) init'ed(this.weblog)
                      * 
                      *  Presumptions:
                      *    org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@56 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@56 != null
                      * 
                      *  Postconditions:
                      *    init'ed(this.monthMap)
                      *    new HashMap(loadWeblogEntries#1) num objects <= 1
                      */
    56              WeblogManager mgr = WebloggerFactory.getWeblogger().getWeblogManager();
    57              monthMap = mgr.getWeblogEntryObjectMap(
    58                      
    59                      weblog,                  // website
    60                      startDate,                 // startDate
    61                      endDate,                   // endDate
    62                      catName,                   // cat
    63                      null,WeblogEntry.PUBLISHED, // status
    64                      locale,
    65                      0, -1);
    66          } catch (WebloggerException e) {
    67              mLogger.error(e);
    68              monthMap = new HashMap();
    69          }
    70      }
    71      
    72      
    73      public String getContent(Date day) {
                 /* 
    P/P           *  Method: String getContent(Date)
                  * 
                  *  Preconditions:
                  *    (soft) mLogger != null
                  *    (soft) init'ed(this.cat)
                  *    (soft) init'ed(this.locale)
                  *    (soft) this.monthMap != null
                  *    (soft) init'ed(this.weblog)
                  * 
                  *  Presumptions:
                  *    java.util.List:get(...)@102 != null
                  *    java.util.List:get(...)@104 != null
                  *    java.util.List:get(...)@83 != null
                  *    java.util.List:get(...)@99 != null
                  *    org.apache.roller.util.DateUtil:get8charDateFormat(...)@42 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    init'ed(java.lang.StringBuffer:toString(...)._tainted)
                  *    return_value == One-of{&java.lang.StringBuffer:toString(...), null}
                  *    return_value in Addr_Set{null,&java.lang.StringBuffer:toString(...)}
                  * 
                  *  Test Vectors:
                  *    java.lang.String:length(...)@103: {1..232-1}, {0}
                  *    java.lang.String:length(...)@106: {0..20}, {21..232-1}
                  *    java.util.Map:get(...)@81: Addr_Set{null}, Inverse{null}
                  */
    74          String content = null;
    75          try {
    76              StringBuffer sb = new StringBuffer();
    77              
    78              // get the 8 char YYYYMMDD datestring for day, returns null
    79              // if no weblog entry on that day
    80              String dateString = null;
    81              List entries = (List)monthMap.get(day);
    82              if ( entries != null ) {
    83                  dateString = mStarDateFormat.format(
    84                          ((WeblogEntry)entries.get(0)).getPubTime());
    85                  
    86                  // append 8 char date string on end of selfurl
    87                  String dayUrl = WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogCollectionURL(weblog, locale, cat, dateString, null, -1, false);
    88                                
    89                  sb.append("<div class=\"hCalendarDayTitleBig\">");
    90                  sb.append("<a href=\"");
    91                  sb.append( dayUrl );
    92                  sb.append("\">");
    93                  sb.append( mSingleDayFormat.format( day ) );
    94                  sb.append("</a></div>");
    95                  
    96                  for ( int i=0; i<entries.size(); i++ ) {
    97                      sb.append("<div class=\"bCalendarDayContentBig\">");
    98                      sb.append("<a href=\"");
    99                      sb.append(((WeblogEntry)entries.get(i)).getPermalink());
   100                      sb.append("\">");
   101                      
   102                      String title = ((WeblogEntry)entries.get(i)).getTitle().trim();
   103                      if ( title.length()==0 ) {
   104                          title = ((WeblogEntry)entries.get(i)).getAnchor();
   105                      }
   106                      if ( title.length() > 20 ) {
   107                          title = title.substring(0,20)+"...";
   108                      }
   109                      
   110                      sb.append( title );
   111                      sb.append("</a></div>");
   112                  }
   113                  
   114              } else {
   115                  sb.append("<div class=\"hCalendarDayTitleBig\">");
   116                  sb.append( mSingleDayFormat.format( day ) );
   117                  sb.append("</div>");
   118                  sb.append("<div class=\"bCalendarDayContentBig\"/>");
   119              }
   120              content = sb.toString();
   121          } catch (Exception e) {
   122              mLogger.error("ERROR: creating URL", e);
   123          }
   124          return content;
   125      }
   126      
   127      /**
   128       * Create URL for use on view-weblog page
   129       * @param day              Day for URL or null if no entries on that day
   130       * @param nextPrevMonthURL True to create next/prev month URL
   131       * @param alwaysURL        Always return a URL, never return null
   132       * @return URL for day, or null if no weblog entry on that day
   133       */
   134      public String computeUrl(Date day, boolean nextPrevMonthURL, boolean alwaysURL) {
                 /* 
    P/P           *  Method: String computeUrl(Date, bool, bool)
                  * 
                  *  Preconditions:
                  *    this.monthMap != null
                  *    (soft) mLogger != null
                  *    (soft) init'ed(this.cat)
                  *    (soft) init'ed(this.locale)
                  *    (soft) init'ed(this.pageLink)
                  *    (soft) init'ed(this.weblog)
                  * 
                  *  Presumptions:
                  *    java.util.List:get(...)@141 != null
                  *    org.apache.roller.util.DateUtil:get8charDateFormat(...)@42 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@153 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@156 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@153 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    alwaysURL: {1}, {0}
                  *    day: Addr_Set{null}, Inverse{null}
                  *    nextPrevMonthURL: {1}, {0}
                  *    this.pageLink: Addr_Set{null}, Inverse{null}
                  *    java.util.Map:get(...)@139: Addr_Set{null}, Inverse{null}
                  */
   135          String url = null;
   136          // get the 8 char YYYYMMDD datestring for day, returns null
   137          // if no weblog entry on that day
   138          String dateString = null;
   139          List entries = (List)monthMap.get( day );
   140          if ( entries != null && day != null ) {
   141              WeblogEntry entry = (WeblogEntry)entries.get(0);
   142              dateString = mStarDateFormat.format(entry.getPubTime());
   143          }
   144          if (dateString == null && !alwaysURL) return null;
   145          else if (dateString == null && !nextPrevMonthURL) {
   146              dateString = DateUtil.format8chars(day);
   147          } else if (dateString == null && nextPrevMonthURL) {
   148              dateString = DateUtil.format6chars(day);
   149          }
   150          try {
   151              if (nextPrevMonthURL && pageLink != null) { 
   152                  // next/prev month URLs point to current page
   153                  url = WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogPageURL(weblog, locale, pageLink, null, cat, dateString, null, -1, false);
   154              } else { 
   155                  // all other URLs point back to main weblog page
   156                  url = WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogCollectionURL(weblog, locale, cat, dateString, null, -1, false);
   157              }
   158          } catch (Exception e) {
   159              mLogger.error("ERROR: creating URL",e);
   160          }
   161          return url;
   162      }
   163  }








SofCheck Inspector Build Version : 2.18479
BigWeblogCalendarModel.java 2009-Jan-02 14:24:46
BigWeblogCalendarModel.class 2009-Sep-04 03:12:46