001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.calendar;
031:
032: import org.blojsom.blog.Blog;
033: import org.blojsom.blog.Entry;
034: import org.blojsom.plugin.Plugin;
035: import org.blojsom.plugin.PluginException;
036: import org.blojsom.util.BlojsomUtils;
037: import org.blojsom.fetcher.Fetcher;
038:
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041: import java.util.Calendar;
042: import java.util.Locale;
043: import java.util.Map;
044: import java.util.Date;
045:
046: /**
047: * AbstractCalendarPlugin is a base plugin that is used by the various calendar plugins
048: * to filter content.
049: *
050: * @author David Czarnecki
051: * @author Mark Lussier
052: * @since blojsom 3.0
053: * @version $Id: AbstractCalendarPlugin.java,v 1.5 2007/01/17 02:35:08 czarneckid Exp $
054: */
055: public abstract class AbstractCalendarPlugin implements Plugin {
056:
057: protected static final String BLOJSOM_CALENDAR_LOCALE = "BLOJSOM_CALENDAR_LOCALE";
058:
059: /**
060: * Request parameter for the "year"
061: */
062: protected static final String YEAR_PARAM = "year";
063:
064: /**
065: * Request parameter for the "month"
066: */
067: protected static final String MONTH_PARAM = "month";
068:
069: /**
070: * Request parameter for the "day"
071: */
072: protected static final String DAY_PARAM = "day";
073:
074: /**
075: * Format String for Calendar Month
076: * (Example: March 2003)
077: */
078: protected static final String BLOJSOM_CALENDAR_FORMAT = "MMMMM yyyy";
079:
080: /**
081: * Short Format String for Previous/Next Calendar Month(s)
082: * (Example: Mar)
083: */
084: protected static final String BLOJSOM_CALENDAR_SHORTFORMAT = "MMM";
085:
086: protected static final String BLOJSOM_FILTER_START_DATE = "BLOJSOM_FILTER_START_DATE";
087: protected static final String BLOJSOM_FILTER_END_DATE = "BLOJSOM_FILTER_END_DATE";
088:
089: /**
090: * Key under which the blog calendar will be placed
091: * (example: on the request for the JSPDispatcher)
092: */
093: public static final String BLOJSOM_CALENDAR = "BLOJSOM_CALENDAR";
094:
095: /**
096: * Key under which the blog calendar vtl helper will be placed
097: * (example: on the request for the JSPDispatcher)
098: */
099: public static final String BLOJSOM_CALENDAR_VTLHELPER = "BLOJSOM_CALENDAR_VTLHELPER";
100:
101: protected Fetcher _fetcher;
102:
103: /**
104: * Set the {@link Fetcher}
105: *
106: * @param fetcher {@link Fetcher}
107: */
108: public void setFetcher(Fetcher fetcher) {
109: _fetcher = fetcher;
110: }
111:
112: /**
113: * Initialize this plugin. This method only called when the plugin is instantiated.
114: *
115: * @throws PluginException If there is an error initializing the plugin
116: */
117: public void init() throws PluginException {
118: }
119:
120: /**
121: * Process the blog entries
122: *
123: * @param httpServletRequest Request
124: * @param httpServletResponse Response
125: * @param blog {@link Blog} instance
126: * @param context Context
127: * @param entries Blog entries retrieved for the particular request
128: * @return Modified set of blog entries
129: * @throws PluginException If there is an error processing the blog entries
130: */
131: public Entry[] process(HttpServletRequest httpServletRequest,
132: HttpServletResponse httpServletResponse, Blog blog,
133: Map context, Entry[] entries) throws PluginException {
134: Locale locale = Locale.getDefault();
135:
136: // If blog-language is set in blojsom.properties, use it instead
137: String localeLanguage = blog.getBlogLanguage();
138:
139: // If no locale is configured, use the system default
140: if (localeLanguage != null) {
141: locale = new Locale(localeLanguage);
142: }
143: context.put(BLOJSOM_CALENDAR_LOCALE, locale);
144:
145: String requestedDateKey;
146: String calendarUrl = blog.getBlogURL();
147: calendarUrl = BlojsomUtils.addTrailingSlash(calendarUrl);
148:
149: // Default to the Current Month and Year
150: Calendar calendar = Calendar.getInstance(locale);
151: calendar.set(Calendar.DAY_OF_MONTH, 1);
152:
153: int currentMonth = calendar.get(Calendar.MONTH);
154: int currentYear = calendar.get(Calendar.YEAR);
155: int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
156:
157: // Determine a calendar-based request
158: String year = null;
159: String month = null;
160: String day = null;
161:
162: year = httpServletRequest.getParameter(YEAR_PARAM);
163: if (year != null) {
164:
165: // Must be a 4 digit year
166: if (year.length() != 4) {
167: year = null;
168: } else {
169: try {
170: currentYear = Integer.parseInt(year);
171: calendar.set(Calendar.YEAR, currentYear);
172:
173: Date startDate = BlojsomUtils.getFirstDateOfYear(
174: locale, currentYear);
175: Date endDate = BlojsomUtils.getLastDateOfYear(
176: locale, currentYear);
177: context.put(BLOJSOM_FILTER_START_DATE, startDate);
178: context.put(BLOJSOM_FILTER_END_DATE, endDate);
179: } catch (NumberFormatException e) {
180: year = "";
181: }
182:
183: month = httpServletRequest.getParameter(MONTH_PARAM);
184:
185: if (month == null) {
186: month = "";
187: } else if (month.length() < 2) {
188: month = "0" + month;
189: }
190:
191: if (!month.equals("")) {
192: try {
193: currentMonth = Integer.parseInt(month) - 1; // Damm Sun!
194: calendar.set(Calendar.MONTH, currentMonth);
195:
196: Date startDate = BlojsomUtils
197: .getFirstDateOfYearMonth(locale,
198: currentYear, currentMonth);
199: Date endDate = BlojsomUtils
200: .getLastDateOfYearMonth(locale,
201: currentYear, currentMonth);
202: context.put(BLOJSOM_FILTER_START_DATE,
203: startDate);
204: context.put(BLOJSOM_FILTER_END_DATE, endDate);
205: } catch (NumberFormatException e) {
206: month = "";
207: }
208: }
209:
210: day = httpServletRequest.getParameter(DAY_PARAM);
211: if (day == null) {
212: day = "";
213: } else if (day.length() < 2) {
214: day = "0" + day;
215: }
216:
217: if (!day.equals("")) {
218: try {
219: currentDay = Integer.parseInt(day);
220: if (currentDay > calendar
221: .getActualMaximum(Calendar.DAY_OF_MONTH)) {
222: currentDay = calendar
223: .getActualMaximum(Calendar.DAY_OF_MONTH);
224: }
225:
226: calendar.set(Calendar.DAY_OF_MONTH, currentDay);
227:
228: Date startDate = BlojsomUtils
229: .getFirstDateOfYearMonthDay(locale,
230: currentYear, currentMonth,
231: currentDay);
232: Date endDate = BlojsomUtils
233: .getLastDateOfYearMonthDay(locale,
234: currentYear, currentMonth,
235: currentDay);
236: context.put(BLOJSOM_FILTER_START_DATE,
237: startDate);
238: context.put(BLOJSOM_FILTER_END_DATE, endDate);
239: } catch (NumberFormatException e) {
240: }
241: }
242: }
243:
244: requestedDateKey = year + month + day;
245:
246: } else {
247: requestedDateKey = null;
248: }
249:
250: BlogCalendar blogCalendar = new BlogCalendar(calendar,
251: calendarUrl, locale);
252: blogCalendar.setRequestedDateKey(requestedDateKey);
253:
254: context.put(BLOJSOM_CALENDAR, blogCalendar);
255:
256: return entries;
257: }
258:
259: /**
260: * Perform any cleanup for the plugin. Called after {@link #process}.
261: *
262: * @throws PluginException If there is an error performing cleanup for this plugin
263: */
264: public void cleanup() throws PluginException {
265: }
266:
267: /**
268: * Called when BlojsomServlet is taken out of service
269: *
270: * @throws PluginException If there is an error in finalizing this plugin
271: */
272: public void destroy() throws PluginException {
273: }
274:
275: }
|