1 31 package org.blojsom.plugin.calendar; 32 33 import org.blojsom.blog.Blog; 34 import org.blojsom.blog.Entry; 35 import org.blojsom.plugin.Plugin; 36 import org.blojsom.plugin.PluginException; 37 import org.blojsom.util.BlojsomUtils; 38 import org.blojsom.fetcher.Fetcher; 39 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import java.util.Calendar ; 43 import java.util.Locale ; 44 import java.util.Map ; 45 import java.util.Date ; 46 47 56 public abstract class AbstractCalendarPlugin implements Plugin { 57 58 protected static final String BLOJSOM_CALENDAR_LOCALE = "BLOJSOM_CALENDAR_LOCALE"; 59 60 63 protected static final String YEAR_PARAM = "year"; 64 65 68 protected static final String MONTH_PARAM = "month"; 69 70 73 protected static final String DAY_PARAM = "day"; 74 75 79 protected static final String BLOJSOM_CALENDAR_FORMAT = "MMMMM yyyy"; 80 81 85 protected static final String BLOJSOM_CALENDAR_SHORTFORMAT = "MMM"; 86 87 protected static final String BLOJSOM_FILTER_START_DATE = "BLOJSOM_FILTER_START_DATE"; 88 protected static final String BLOJSOM_FILTER_END_DATE = "BLOJSOM_FILTER_END_DATE"; 89 90 94 public static final String BLOJSOM_CALENDAR = "BLOJSOM_CALENDAR"; 95 96 100 public static final String BLOJSOM_CALENDAR_VTLHELPER = "BLOJSOM_CALENDAR_VTLHELPER"; 101 102 protected Fetcher _fetcher; 103 104 109 public void setFetcher(Fetcher fetcher) { 110 _fetcher = fetcher; 111 } 112 113 118 public void init() throws PluginException { 119 } 120 121 132 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 133 Locale locale = Locale.getDefault(); 134 135 String localeLanguage = blog.getBlogLanguage(); 137 138 if (localeLanguage != null) { 140 locale = new Locale (localeLanguage); 141 } 142 context.put(BLOJSOM_CALENDAR_LOCALE, locale); 143 144 String requestedDateKey; 145 String calendarUrl = blog.getBlogURL(); 146 calendarUrl = BlojsomUtils.addTrailingSlash(calendarUrl); 147 148 Calendar calendar = Calendar.getInstance(locale); 150 calendar.set(Calendar.DAY_OF_MONTH, 1); 151 152 int currentMonth = calendar.get(Calendar.MONTH); 153 int currentYear = calendar.get(Calendar.YEAR); 154 int currentDay = calendar.get(Calendar.DAY_OF_MONTH); 155 156 String year = null; 158 String month = null; 159 String day = null; 160 161 year = httpServletRequest.getParameter(YEAR_PARAM); 162 if (year != null) { 163 164 if (year.length() != 4) { 166 year = null; 167 } else { 168 try { 169 currentYear = Integer.parseInt(year); 170 calendar.set(Calendar.YEAR, currentYear); 171 172 Date startDate = BlojsomUtils.getFirstDateOfYear(locale, currentYear); 173 Date endDate = BlojsomUtils.getLastDateOfYear(locale, currentYear); 174 context.put(BLOJSOM_FILTER_START_DATE, startDate); 175 context.put(BLOJSOM_FILTER_END_DATE, endDate); 176 } catch (NumberFormatException e) { 177 year = ""; 178 } 179 180 month = httpServletRequest.getParameter(MONTH_PARAM); 181 182 if (month == null) { 183 month = ""; 184 } else if (month.length() < 2) { 185 month = "0" + month; 186 } 187 188 if (!month.equals("")) { 189 try { 190 currentMonth = Integer.parseInt(month) - 1; calendar.set(Calendar.MONTH, currentMonth); 192 193 Date startDate = BlojsomUtils.getFirstDateOfYearMonth(locale, currentYear, currentMonth); 194 Date endDate = BlojsomUtils.getLastDateOfYearMonth(locale, currentYear, currentMonth); 195 context.put(BLOJSOM_FILTER_START_DATE, startDate); 196 context.put(BLOJSOM_FILTER_END_DATE, endDate); 197 } catch (NumberFormatException e) { 198 month = ""; 199 } 200 } 201 202 day = httpServletRequest.getParameter(DAY_PARAM); 203 if (day == null) { 204 day = ""; 205 } else if (day.length() < 2) { 206 day = "0" + day; 207 } 208 209 if (!day.equals("")) { 210 try { 211 currentDay = Integer.parseInt(day); 212 if (currentDay > calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) { 213 currentDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 214 } 215 216 calendar.set(Calendar.DAY_OF_MONTH, currentDay); 217 218 Date startDate = BlojsomUtils.getFirstDateOfYearMonthDay(locale, currentYear, currentMonth, currentDay); 219 Date endDate = BlojsomUtils.getLastDateOfYearMonthDay(locale, currentYear, currentMonth, currentDay); 220 context.put(BLOJSOM_FILTER_START_DATE, startDate); 221 context.put(BLOJSOM_FILTER_END_DATE, endDate); 222 } catch (NumberFormatException e) { 223 } 224 } 225 } 226 227 requestedDateKey = year + month + day; 228 229 } else { 230 requestedDateKey = null; 231 } 232 233 BlogCalendar blogCalendar = new BlogCalendar(calendar, calendarUrl, locale); 234 blogCalendar.setRequestedDateKey(requestedDateKey); 235 236 context.put(BLOJSOM_CALENDAR, blogCalendar); 237 238 return entries; 239 } 240 241 246 public void cleanup() throws PluginException { 247 } 248 249 254 public void destroy() throws PluginException { 255 } 256 257 } 258 | Popular Tags |