abstractlogaction.java

### File Source: abstractlogaction.java

             /*
        P/P   *  Method: net.sourceforge.pebble.web.action.AbstractLogAction__static_init
              */
         1  /*
         2   * Copyright (c) 2003-2006, Simon Brown
         3   * All rights reserved.
         4   *
         5   * Redistribution and use in source and binary forms, with or without
         6   * modification, are permitted provided that the following conditions are met:
         7   *
         8   *   - Redistributions of source code must retain the above copyright
         9   *     notice, this list of conditions and the following disclaimer.
        10   *
        11   *   - Redistributions in binary form must reproduce the above copyright
        12   *     notice, this list of conditions and the following disclaimer in
        13   *     the documentation and/or other materials provided with the
        14   *     distribution.
        15   *
        16   *   - Neither the name of Pebble nor the names of its contributors may
        17   *     be used to endorse or promote products derived from this software
        18   *     without specific prior written permission.
        19   *
        20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
        24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        30   * POSSIBILITY OF SUCH DAMAGE.
        31   */
        32  package net.sourceforge.pebble.web.action;
        33
        34  import net.sourceforge.pebble.Constants;
        35  import net.sourceforge.pebble.domain.Blog;
        36  import net.sourceforge.pebble.domain.Month;
        37  import net.sourceforge.pebble.domain.Day;
        38  import net.sourceforge.pebble.logging.Log;
        39
             /*
        P/P   *  Method: void net.sourceforge.pebble.web.action.AbstractLogAction()
              */
        40  import javax.servlet.ServletException;
        41  import javax.servlet.http.HttpServletRequest;
        42  import javax.servlet.http.HttpServletResponse;
        43  import java.text.SimpleDateFormat;
        44  import java.util.Calendar;
        45
        46  /**
        47   * Superclass for all log related actions.
        48   *
        49   * @author    Simon Brown
        50   */
        51  public abstract class AbstractLogAction extends SecureAction {
        52
        53    protected Log getLog(HttpServletRequest request, HttpServletResponse response) throws ServletException {
                 /*
        P/P       *  Method: Log getLog(HttpServletRequest, HttpServletResponse)
                  *
                  *  Preconditions:
                  *    request != null
                  *    this.model != null
                  *    this.model.data != null
                  *
                  *  Presumptions:
                  *    java.lang.Integer:parseInt(...)@68 >= -231+1
                  *    java.lang.Integer:parseInt(...)@81 >= -231+1
                  *    java.util.Calendar:get(...)@73 <= 232-2
                  *    java.util.Calendar:get(...)@84 <= 232-2
                  *    java.util.HashMap:get(...)@63 != null
                  *    ...
                  *
                  *  Postconditions:
                  *    return_value != null
                  *    init'ed(new ArrayList(Log#1) num objects)
                  *    new ArrayList(getLog#1*) num objects <= 1
                  *    new ArrayList(getLog#2*) num objects <= 1
                  *    new HashSet(getLog#1*) num objects <= 1
                  *    new Log(getLog#1*) num objects <= 1
                  *    init'ed(new Log(getLog#1*).blog)
                  *    new Log(getLog#1*).logEntries != null
                  *    new Log(getLog#2*) num objects <= 1
                  *    (soft) new Log(getLog#2*).blog != null
                  *    ...
                  *
                  *  Test Vectors:
                  *    java.lang.String:length(...)@64: {0}, {1..232-1}
                  *    java.lang.String:length(...)@65: {0}, {1..232-1}
                  *    java.lang.String:length(...)@66: {0}, {1..232-1}
                  *    java.lang.String:length(...)@78: {0}, {1..232-1}
                  *    java.lang.String:length(...)@79: {0}, {1..232-1}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@56: Addr_Set{null}, Inverse{null}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@57: Addr_Set{null}, Inverse{null}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@58: Addr_Set{null}, Inverse{null}
                  */
        54      Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
        55
        56      String yearAsString = request.getParameter("year");
        57      String monthAsString = request.getParameter("month");
        58      String dayAsString = request.getParameter("day");
        59
        60      Calendar cal = blog.getCalendar();
        61      Log log = null;
    +   62      String logPeriod = "";
        63
        64      if (yearAsString != null && yearAsString.length() > 0 &&
        65          monthAsString != null && monthAsString.length() > 0 &&
        66          dayAsString != null && dayAsString.length() > 0) {
        67        int year = Integer.parseInt(yearAsString);
        68        int month = Integer.parseInt(monthAsString);
        69        int day = Integer.parseInt(dayAsString);
        70        cal.set(Calendar.YEAR, year);
        71        cal.set(Calendar.MONTH, month-1);
        72        cal.set(Calendar.DAY_OF_MONTH, day);
        73        log = blog.getLogger().getLog(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH));
        74        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", blog.getLocale());
        75        dateFormat.setTimeZone(blog.getTimeZone());
        76        registerObjectsForNavigation(blog, blog.getBlogForDay(year, month, day));
        77        logPeriod = dateFormat.format(cal.getTime());
        78      } else if (yearAsString != null && yearAsString.length() > 0 &&
        79            monthAsString != null && monthAsString.length() > 0) {
        80        int year = Integer.parseInt(yearAsString);
        81        int month = Integer.parseInt(monthAsString);
        82        cal.set(Calendar.YEAR, year);
        83        cal.set(Calendar.MONTH, month-1);
        84        log = blog.getLogger().getLog(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
        85        SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", blog.getLocale());
        86        dateFormat.setTimeZone(blog.getTimeZone());
        87        registerObjectsForNavigation(blog, blog.getBlogForMonth(year, month));
        88        logPeriod = dateFormat.format(cal.getTime());
        89      } else {
        90        // get the log for today
        91        log = blog.getLogger().getLog();
        92        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", blog.getLocale());
        93        dateFormat.setTimeZone(blog.getTimeZone());
        94        registerObjectsForNavigation(blog, blog.getBlogForToday());
        95        logPeriod = dateFormat.format(cal.getTime());
        96      }
        97
        98      getModel().put("logPeriod", logPeriod);
        99
       100      return log;
       101    }
       102
       103    protected String getLogFile(HttpServletRequest request, HttpServletResponse response) throws ServletException {
                 /*
        P/P       *  Method: String getLogFile(HttpServletRequest, HttpServletResponse)
                  *
                  *  Preconditions:
                  *    request != null
                  *    this.model != null
                  *    this.model.data != null
                  *
                  *  Presumptions:
                  *    java.lang.Integer:parseInt(...)@118 >= -231+1
                  *    java.lang.Integer:parseInt(...)@131 >= -231+1
                  *    java.util.Calendar:get(...)@123 <= 232-2
                  *    java.util.Calendar:get(...)@134 <= 232-2
                  *    java.util.HashMap:get(...)@63 != null
                  *    ...
                  *
                  *  Postconditions:
                  *    return_value != null
                  *
                  *  Test Vectors:
                  *    java.lang.String:length(...)@114: {0}, {1..232-1}
                  *    java.lang.String:length(...)@115: {0}, {1..232-1}
                  *    java.lang.String:length(...)@116: {0}, {1..232-1}
                  *    java.lang.String:length(...)@128: {0}, {1..232-1}
                  *    java.lang.String:length(...)@129: {0}, {1..232-1}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@106: Addr_Set{null}, Inverse{null}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@107: Addr_Set{null}, Inverse{null}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@108: Addr_Set{null}, Inverse{null}
                  */
       104      Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
       105
       106      String yearAsString = request.getParameter("year");
       107      String monthAsString = request.getParameter("month");
       108      String dayAsString = request.getParameter("day");
       109
       110      Calendar cal = blog.getCalendar();
       111      String log = null;
    +  112      String logPeriod = "";
       113
       114      if (yearAsString != null && yearAsString.length() > 0 &&
       115          monthAsString != null && monthAsString.length() > 0 &&
       116          dayAsString != null && dayAsString.length() > 0) {
       117        int year = Integer.parseInt(yearAsString);
       118        int month = Integer.parseInt(monthAsString);
       119        int day = Integer.parseInt(dayAsString);
       120        cal.set(Calendar.YEAR, year);
       121        cal.set(Calendar.MONTH, month-1);
       122        cal.set(Calendar.DAY_OF_MONTH, day);
       123        log = blog.getLogger().getLogFile(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH));
       124        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", blog.getLocale());
       125        dateFormat.setTimeZone(blog.getTimeZone());
       126        registerObjectsForNavigation(blog, blog.getBlogForDay(year, month, day));
       127        logPeriod = dateFormat.format(cal.getTime());
       128      } else if (yearAsString != null && yearAsString.length() > 0 &&
       129            monthAsString != null && monthAsString.length() > 0) {
       130        int year = Integer.parseInt(yearAsString);
       131        int month = Integer.parseInt(monthAsString);
       132        cal.set(Calendar.YEAR, year);
       133        cal.set(Calendar.MONTH, month-1);
       134        log = blog.getLogger().getLogFile(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
       135        SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", blog.getLocale());
       136        dateFormat.setTimeZone(blog.getTimeZone());
       137        registerObjectsForNavigation(blog, blog.getBlogForMonth(year, month));
       138        logPeriod = dateFormat.format(cal.getTime());
       139      } else {
       140        // get the log for today
       141        log = blog.getLogger().getLogFile();
       142        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", blog.getLocale());
       143        dateFormat.setTimeZone(blog.getTimeZone());
       144        registerObjectsForNavigation(blog, blog.getBlogForToday());
       145        logPeriod = dateFormat.format(cal.getTime());
       146      }
       147
       148      getModel().put("logPeriod", logPeriod);
       149
       150      return log;
       151    }
       152
               /*
        P/P     *  Method: void registerObjectsForNavigation(Blog, Month)
                *
                *  Preconditions:
                *    blog != null
                *    month != null
                *    this.model != null
                *    this.model.data != null
                *
                *  Presumptions:
                *    net.sourceforge.pebble.domain.Blog:getCalendar(...)@162 != null
                *    net.sourceforge.pebble.domain.Month:getDate(...)@162 != null
                *    net.sourceforge.pebble.domain.Month:getNextMonth(...)@156 != null
                *    net.sourceforge.pebble.domain.Month:getPreviousMonth(...)@155 != null
                *
                *  Test Vectors:
                *    java.util.Date:after(...)@162: {0}, {1}
                *    net.sourceforge.pebble.domain.Month:before(...)@158: {1}, {0}
                *    net.sourceforge.pebble.domain.Month:before(...)@162: {0}, {1}
                */
       153    private void registerObjectsForNavigation(Blog blog, Month month) {
       154      Month firstMonth = blog.getBlogForFirstMonth();
       155      Month previousMonth = month.getPreviousMonth();
       156      Month nextMonth = month.getNextMonth();
       157
       158      if (!previousMonth.before(firstMonth)) {
       159        getModel().put("previousMonth", previousMonth);
       160      }
       161
       162      if (!nextMonth.getDate().after(blog.getCalendar().getTime()) || nextMonth.before(firstMonth)) {
       163        getModel().put("nextMonth", nextMonth);
       164      }
       165      getModel().put("displayMode", "logSummaryForMonth");
       166    }
       167
               /*
        P/P     *  Method: void registerObjectsForNavigation(Blog, Day)
                *
                *  Preconditions:
                *    blog != null
                *    day != null
                *    this.model != null
                *    this.model.data != null
                *
                *  Presumptions:
                *    net.sourceforge.pebble.domain.Blog:getBlogForFirstMonth(...)@169 != null
                *    net.sourceforge.pebble.domain.Blog:getCalendar(...)@177 != null
                *    net.sourceforge.pebble.domain.Day:getDate(...)@177 != null
                *    net.sourceforge.pebble.domain.Day:getNextDay(...)@171 != null
                *    net.sourceforge.pebble.domain.Day:getPreviousDay(...)@170 != null
                *
                *  Test Vectors:
                *    java.util.Date:after(...)@177: {0}, {1}
                *    net.sourceforge.pebble.domain.Day:before(...)@173: {1}, {0}
                *    net.sourceforge.pebble.domain.Day:before(...)@177: {0}, {1}
                */
       168    private void registerObjectsForNavigation(Blog blog, Day day) {
       169      Day firstDay = blog.getBlogForFirstMonth().getBlogForFirstDay();
       170      Day previousDay = day.getPreviousDay();
       171      Day nextDay = day.getNextDay();
       172
       173      if (!previousDay.before(firstDay)) {
       174        getModel().put("previousDay", previousDay);
       175      }
       176
       177      if (!nextDay.getDate().after(blog.getCalendar().getTime()) || nextDay.before(firstDay)) {
       178        getModel().put("nextDay", nextDay);
       179      }
       180      getModel().put("displayMode", "logSummaryForDay");
       181    }
       182
       183    /**
       184     * Gets a list of all roles that are allowed to access this action.
       185     *
       186     * @return  an array of Strings representing role names
       187     * @param request
       188     */
       189    public String[] getRoles(HttpServletRequest request) {
                 /*
        P/P       *  Method: String[] getRoles(HttpServletRequest)
                  *
                  *  Presumptions:
                  *    init'ed(net.sourceforge.pebble.Constants.BLOG_ADMIN_ROLE)
                  *    init'ed(net.sourceforge.pebble.Constants.BLOG_CONTRIBUTOR_ROLE)
                  *    init'ed(net.sourceforge.pebble.Constants.BLOG_OWNER_ROLE)
                  *    init'ed(net.sourceforge.pebble.Constants.BLOG_PUBLISHER_ROLE)
                  *
                  *  Postconditions:
                  *    return_value == &new String[](getRoles#1)
                  *    new String[](getRoles#1) num objects == 1
                  *    return_value.length == 4
                  *    return_value[0] == net.sourceforge.pebble.Constants.BLOG_ADMIN_ROLE
                  *    (soft) init'ed(return_value[0])
                  *    return_value[1] == net.sourceforge.pebble.Constants.BLOG_OWNER_ROLE
                  *    (soft) init'ed(return_value[1])
                  *    return_value[2] == net.sourceforge.pebble.Constants.BLOG_PUBLISHER_ROLE
                  *    (soft) init'ed(return_value[2])
                  *    return_value[3] == net.sourceforge.pebble.Constants.BLOG_CONTRIBUTOR_ROLE
                  *    ...
                  */
       190      return new String[]{Constants.BLOG_ADMIN_ROLE, Constants.BLOG_OWNER_ROLE, Constants.BLOG_PUBLISHER_ROLE, Constants.BLOG_CONTRIBUTOR_ROLE};
       191    }
       192
       193  }

SofCheck Inspector Build Version : 2.22510

abstractlogaction.java | 2010-Jun-25 19:40:34
abstractlogaction.class | 2010-Jul-19 20:23:38