KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > calendar > AbstractCalendarPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
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 notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

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 JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.util.Calendar JavaDoc;
43 import java.util.Locale JavaDoc;
44 import java.util.Map JavaDoc;
45 import java.util.Date JavaDoc;
46
47 /**
48  * AbstractCalendarPlugin is a base plugin that is used by the various calendar plugins
49  * to filter content.
50  *
51  * @author David Czarnecki
52  * @author Mark Lussier
53  * @since blojsom 3.0
54  * @version $Id: AbstractCalendarPlugin.java,v 1.4 2006/05/05 17:40:33 czarneckid Exp $
55  */

56 public abstract class AbstractCalendarPlugin implements Plugin {
57
58     protected static final String JavaDoc BLOJSOM_CALENDAR_LOCALE = "BLOJSOM_CALENDAR_LOCALE";
59
60     /**
61      * Request parameter for the "year"
62      */

63     protected static final String JavaDoc YEAR_PARAM = "year";
64
65     /**
66      * Request parameter for the "month"
67      */

68     protected static final String JavaDoc MONTH_PARAM = "month";
69
70     /**
71      * Request parameter for the "day"
72      */

73     protected static final String JavaDoc DAY_PARAM = "day";
74
75     /**
76      * Format String for Calendar Month
77      * (Example: March 2003)
78      */

79     protected static final String JavaDoc BLOJSOM_CALENDAR_FORMAT = "MMMMM yyyy";
80
81     /**
82      * Short Format String for Previous/Next Calendar Month(s)
83      * (Example: Mar)
84      */

85     protected static final String JavaDoc BLOJSOM_CALENDAR_SHORTFORMAT = "MMM";
86
87     protected static final String JavaDoc BLOJSOM_FILTER_START_DATE = "BLOJSOM_FILTER_START_DATE";
88     protected static final String JavaDoc BLOJSOM_FILTER_END_DATE = "BLOJSOM_FILTER_END_DATE";
89
90     /**
91      * Key under which the blog calendar will be placed
92      * (example: on the request for the JSPDispatcher)
93      */

94     public static final String JavaDoc BLOJSOM_CALENDAR = "BLOJSOM_CALENDAR";
95
96     /**
97      * Key under which the blog calendar vtl helper will be placed
98      * (example: on the request for the JSPDispatcher)
99      */

100     public static final String JavaDoc BLOJSOM_CALENDAR_VTLHELPER = "BLOJSOM_CALENDAR_VTLHELPER";
101
102     protected Fetcher _fetcher;
103
104     /**
105      * Set the {@link Fetcher}
106      *
107      * @param fetcher {@link Fetcher}
108      */

109     public void setFetcher(Fetcher fetcher) {
110         _fetcher = fetcher;
111     }
112
113     /**
114      * Initialize this plugin. This method only called when the plugin is instantiated.
115      *
116      * @throws PluginException If there is an error initializing the plugin
117      */

118     public void init() throws PluginException {
119     }
120
121     /**
122      * Process the blog entries
123      *
124      * @param httpServletRequest Request
125      * @param httpServletResponse Response
126      * @param blog {@link Blog} instance
127      * @param context Context
128      * @param entries Blog entries retrieved for the particular request
129      * @return Modified set of blog entries
130      * @throws PluginException If there is an error processing the blog entries
131      */

132     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
133         Locale JavaDoc locale = Locale.getDefault();
134
135         // If blog-language is set in blojsom.properties, use it instead
136
String JavaDoc localeLanguage = blog.getBlogLanguage();
137
138         // If no locale is configured, use the system default
139
if (localeLanguage != null) {
140             locale = new Locale JavaDoc(localeLanguage);
141         }
142         context.put(BLOJSOM_CALENDAR_LOCALE, locale);
143
144         String JavaDoc requestedDateKey;
145         String JavaDoc calendarUrl = blog.getBlogURL();
146         calendarUrl = BlojsomUtils.addTrailingSlash(calendarUrl);
147
148         // Default to the Current Month and Year
149
Calendar JavaDoc 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         // Determine a calendar-based request
157
String JavaDoc year = null;
158         String JavaDoc month = null;
159         String JavaDoc day = null;
160
161         year = httpServletRequest.getParameter(YEAR_PARAM);
162         if (year != null) {
163
164             // Must be a 4 digit year
165
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 JavaDoc startDate = BlojsomUtils.getFirstDateOfYear(locale, currentYear);
173                     Date JavaDoc endDate = BlojsomUtils.getLastDateOfYear(locale, currentYear);
174                     context.put(BLOJSOM_FILTER_START_DATE, startDate);
175                     context.put(BLOJSOM_FILTER_END_DATE, endDate);
176                 } catch (NumberFormatException JavaDoc 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; // Damm Sun!
191
calendar.set(Calendar.MONTH, currentMonth);
192
193                         Date JavaDoc startDate = BlojsomUtils.getFirstDateOfYearMonth(locale, currentYear, currentMonth);
194                         Date JavaDoc 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 JavaDoc 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 JavaDoc startDate = BlojsomUtils.getFirstDateOfYearMonthDay(locale, currentYear, currentMonth, currentDay);
219                         Date JavaDoc 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 JavaDoc 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     /**
242      * Perform any cleanup for the plugin. Called after {@link #process}.
243      *
244      * @throws PluginException If there is an error performing cleanup for this plugin
245      */

246     public void cleanup() throws PluginException {
247     }
248
249     /**
250      * Called when BlojsomServlet is taken out of service
251      *
252      * @throws PluginException If there is an error in finalizing this plugin
253      */

254     public void destroy() throws PluginException {
255     }
256
257 }
258
Popular Tags