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.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.fetcher.FetcherException;
037: import org.blojsom.plugin.PluginException;
038: import org.blojsom.util.BlojsomUtils;
039:
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.util.Calendar;
043: import java.util.Date;
044: import java.util.Locale;
045: import java.util.Map;
046:
047: /**
048: * AbstractVisualCalendarPlugin
049: *
050: * @author David Czarnecki
051: * @author Mark Lussier
052: * @version $Id: AbstractVisualCalendarPlugin.java,v 1.5 2007/01/17 02:35:08 czarneckid Exp $
053: * @since blojsom 3.0
054: */
055: public abstract class AbstractVisualCalendarPlugin extends
056: AbstractCalendarPlugin {
057:
058: private Log _logger = LogFactory
059: .getLog(AbstractVisualCalendarPlugin.class);
060:
061: protected BlogCalendar _blogCalendar;
062:
063: /**
064: * Process the blog entries
065: *
066: * @param httpServletRequest Request
067: * @param httpServletResponse Response
068: * @param user {@link BlogUser} instance
069: * @param context Context
070: * @param entries Blog entries retrieved for the particular request
071: * @return Modified set of blog entries
072: * @throws PluginException If there is an error processing the blog entries
073: */
074: public Entry[] process(HttpServletRequest httpServletRequest,
075: HttpServletResponse httpServletResponse, Blog blog,
076: Map context, Entry[] entries) throws PluginException {
077: entries = super .process(httpServletRequest,
078: httpServletResponse, blog, context, entries);
079:
080: Locale locale = (Locale) context.get(BLOJSOM_CALENDAR_LOCALE);
081: BlogCalendar blogCalendar = (BlogCalendar) context
082: .get(BLOJSOM_CALENDAR);
083: Calendar entrycalendar = Calendar.getInstance(locale);
084:
085: Date startDate = BlojsomUtils.getFirstDateOfYearMonth(locale,
086: blogCalendar.getCurrentYear(), blogCalendar
087: .getCurrentMonth());
088: Date endDate = BlojsomUtils.getLastDateOfYearMonth(locale,
089: blogCalendar.getCurrentYear(), blogCalendar
090: .getCurrentMonth());
091: Date now = new Date();
092:
093: if (startDate.before(now)) {
094: if (endDate.after(now)) {
095: endDate = now;
096: }
097:
098: try {
099: Entry[] entriesForMonth = _fetcher
100: .findEntriesBetweenDates(blog, startDate,
101: endDate);
102: for (int i = 0; i < entriesForMonth.length; i++) {
103: Entry entry = entriesForMonth[i];
104: entrycalendar.setTime(entry.getDate());
105: int entrymonth = entrycalendar.get(Calendar.MONTH);
106: int entryyear = entrycalendar.get(Calendar.YEAR);
107:
108: // If the entry is is the same month and the same year, then flag that date as having a entry
109: if ((entrymonth == blogCalendar.getCurrentMonth())
110: && (entryyear == blogCalendar
111: .getCurrentYear())) {
112: blogCalendar.setEntryForDOM(entrycalendar
113: .get(Calendar.DAY_OF_MONTH));
114: }
115: }
116: } catch (FetcherException e) {
117: if (_logger.isErrorEnabled()) {
118: _logger.error(e);
119: }
120: }
121: }
122:
123: context.put(BLOJSOM_CALENDAR, blogCalendar);
124:
125: return entries;
126: }
127: }
|