Coverage Report - net.sourceforge.pebble.web.action.AbstractLogAction
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractLogAction
0%
0/94
0%
0/52
6.2
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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  
 
 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  0
 public abstract class AbstractLogAction extends SecureAction {
 52  
 
 53  
   protected Log getLog(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 54  0
     Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
 55  
 
 56  0
     String yearAsString = request.getParameter("year");
 57  0
     String monthAsString = request.getParameter("month");
 58  0
     String dayAsString = request.getParameter("day");
 59  
 
 60  0
     Calendar cal = blog.getCalendar();
 61  0
     Log log = null;
 62  0
     String logPeriod = "";
 63  
 
 64  0
     if (yearAsString != null && yearAsString.length() > 0 &&
 65  
         monthAsString != null && monthAsString.length() > 0 &&
 66  
         dayAsString != null && dayAsString.length() > 0) {
 67  0
       int year = Integer.parseInt(yearAsString);
 68  0
       int month = Integer.parseInt(monthAsString);
 69  0
       int day = Integer.parseInt(dayAsString);
 70  0
       cal.set(Calendar.YEAR, year);
 71  0
       cal.set(Calendar.MONTH, month-1);
 72  0
       cal.set(Calendar.DAY_OF_MONTH, day);
 73  0
       log = blog.getLogger().getLog(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH));
 74  0
       SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", blog.getLocale());
 75  0
       dateFormat.setTimeZone(blog.getTimeZone());
 76  0
       registerObjectsForNavigation(blog, blog.getBlogForDay(year, month, day));
 77  0
       logPeriod = dateFormat.format(cal.getTime());
 78  0
     } else if (yearAsString != null && yearAsString.length() > 0 &&
 79  
           monthAsString != null && monthAsString.length() > 0) {
 80  0
       int year = Integer.parseInt(yearAsString);
 81  0
       int month = Integer.parseInt(monthAsString);
 82  0
       cal.set(Calendar.YEAR, year);
 83  0
       cal.set(Calendar.MONTH, month-1);
 84  0
       log = blog.getLogger().getLog(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
 85  0
       SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", blog.getLocale());
 86  0
       dateFormat.setTimeZone(blog.getTimeZone());
 87  0
       registerObjectsForNavigation(blog, blog.getBlogForMonth(year, month));
 88  0
       logPeriod = dateFormat.format(cal.getTime());
 89  0
     } else {
 90  
       // get the log for today
 91  0
       log = blog.getLogger().getLog();
 92  0
       SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", blog.getLocale());
 93  0
       dateFormat.setTimeZone(blog.getTimeZone());
 94  0
       registerObjectsForNavigation(blog, blog.getBlogForToday());
 95  0
       logPeriod = dateFormat.format(cal.getTime());
 96  
     }
 97  
 
 98  0
     getModel().put("logPeriod", logPeriod);
 99  
 
 100  0
     return log;
 101  
   }
 102  
 
 103  
   protected String getLogFile(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 104  0
     Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
 105  
 
 106  0
     String yearAsString = request.getParameter("year");
 107  0
     String monthAsString = request.getParameter("month");
 108  0
     String dayAsString = request.getParameter("day");
 109  
 
 110  0
     Calendar cal = blog.getCalendar();
 111  0
     String log = null;
 112  0
     String logPeriod = "";
 113  
 
 114  0
     if (yearAsString != null && yearAsString.length() > 0 &&
 115  
         monthAsString != null && monthAsString.length() > 0 &&
 116  
         dayAsString != null && dayAsString.length() > 0) {
 117  0
       int year = Integer.parseInt(yearAsString);
 118  0
       int month = Integer.parseInt(monthAsString);
 119  0
       int day = Integer.parseInt(dayAsString);
 120  0
       cal.set(Calendar.YEAR, year);
 121  0
       cal.set(Calendar.MONTH, month-1);
 122  0
       cal.set(Calendar.DAY_OF_MONTH, day);
 123  0
       log = blog.getLogger().getLogFile(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH));
 124  0
       SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", blog.getLocale());
 125  0
       dateFormat.setTimeZone(blog.getTimeZone());
 126  0
       registerObjectsForNavigation(blog, blog.getBlogForDay(year, month, day));
 127  0
       logPeriod = dateFormat.format(cal.getTime());
 128  0
     } else if (yearAsString != null && yearAsString.length() > 0 &&
 129  
           monthAsString != null && monthAsString.length() > 0) {
 130  0
       int year = Integer.parseInt(yearAsString);
 131  0
       int month = Integer.parseInt(monthAsString);
 132  0
       cal.set(Calendar.YEAR, year);
 133  0
       cal.set(Calendar.MONTH, month-1);
 134  0
       log = blog.getLogger().getLogFile(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
 135  0
       SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", blog.getLocale());
 136  0
       dateFormat.setTimeZone(blog.getTimeZone());
 137  0
       registerObjectsForNavigation(blog, blog.getBlogForMonth(year, month));
 138  0
       logPeriod = dateFormat.format(cal.getTime());
 139  0
     } else {
 140  
       // get the log for today
 141  0
       log = blog.getLogger().getLogFile();
 142  0
       SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", blog.getLocale());
 143  0
       dateFormat.setTimeZone(blog.getTimeZone());
 144  0
       registerObjectsForNavigation(blog, blog.getBlogForToday());
 145  0
       logPeriod = dateFormat.format(cal.getTime());
 146  
     }
 147  
 
 148  0
     getModel().put("logPeriod", logPeriod);
 149  
 
 150  0
     return log;
 151  
   }
 152  
 
 153  
   private void registerObjectsForNavigation(Blog blog, Month month) {
 154  0
     Month firstMonth = blog.getBlogForFirstMonth();
 155  0
     Month previousMonth = month.getPreviousMonth();
 156  0
     Month nextMonth = month.getNextMonth();
 157  
 
 158  0
     if (!previousMonth.before(firstMonth)) {
 159  0
       getModel().put("previousMonth", previousMonth);
 160  
     }
 161  
 
 162  0
     if (!nextMonth.getDate().after(blog.getCalendar().getTime()) || nextMonth.before(firstMonth)) {
 163  0
       getModel().put("nextMonth", nextMonth);
 164  
     }
 165  0
     getModel().put("displayMode", "logSummaryForMonth");
 166  0
   }
 167  
 
 168  
   private void registerObjectsForNavigation(Blog blog, Day day) {
 169  0
     Day firstDay = blog.getBlogForFirstMonth().getBlogForFirstDay();
 170  0
     Day previousDay = day.getPreviousDay();
 171  0
     Day nextDay = day.getNextDay();
 172  
 
 173  0
     if (!previousDay.before(firstDay)) {
 174  0
       getModel().put("previousDay", previousDay);
 175  
     }
 176  
 
 177  0
     if (!nextDay.getDate().after(blog.getCalendar().getTime()) || nextDay.before(firstDay)) {
 178  0
       getModel().put("nextDay", nextDay);
 179  
     }
 180  0
     getModel().put("displayMode", "logSummaryForDay");
 181  0
   }
 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) {
 190  0
     return new String[]{Constants.BLOG_ADMIN_ROLE, Constants.BLOG_OWNER_ROLE, Constants.BLOG_PUBLISHER_ROLE, Constants.BLOG_CONTRIBUTOR_ROLE};
 191  
   }
 192  
 
 193  
 }