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.BlojsomConstants;
039:
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.util.Date;
043: import java.util.Map;
044:
045: /**
046: * CalendarFilterPlugin filters entries based on the calendar navigation parameters
047: * in the URL
048: *
049: * @author David Czarnecki
050: * @author Mark Lussier
051: * @since blojsom 3.0
052: * @version $Id: CalendarFilterPlugin.java,v 1.7 2007/01/17 02:35:08 czarneckid Exp $
053: */
054: public class CalendarFilterPlugin extends AbstractCalendarPlugin {
055:
056: private Log _logger = LogFactory.getLog(CalendarFilterPlugin.class);
057:
058: /**
059: * Process the blog entries
060: *
061: * @param httpServletRequest Request
062: * @param httpServletResponse Response
063: * @param blog {@link Blog} instance
064: * @param context Context
065: * @param entries Blog entries retrieved for the particular request
066: * @return Modified set of blog entries
067: * @throws PluginException If there is an error processing the blog entries
068: */
069: public Entry[] process(HttpServletRequest httpServletRequest,
070: HttpServletResponse httpServletResponse, Blog blog,
071: Map context, Entry[] entries) throws PluginException {
072: entries = super .process(httpServletRequest,
073: httpServletResponse, blog, context, entries);
074:
075: Date startDate = (Date) context.get(BLOJSOM_FILTER_START_DATE);
076: Date endDate = (Date) context.get(BLOJSOM_FILTER_END_DATE);
077: Date now = new Date();
078:
079: boolean isPermalinkRequest = context
080: .containsKey(BlojsomConstants.BLOJSOM_PERMALINK);
081:
082: if (startDate != null && endDate != null && !isPermalinkRequest) {
083: if (startDate.before(now)) {
084: if (endDate.after(now)) {
085: endDate = now;
086: }
087:
088: try {
089: if (_logger.isDebugEnabled()) {
090: _logger.debug("Filtering entries betweeen: "
091: + startDate.toString() + " and "
092: + endDate.toString());
093: }
094:
095: entries = _fetcher.findEntriesBetweenDates(blog,
096: startDate, endDate);
097: } catch (FetcherException e) {
098: if (_logger.isErrorEnabled()) {
099: _logger.error(e);
100: }
101: }
102: } else if (startDate.after(now)) {
103: entries = new Entry[0];
104: }
105: }
106:
107: return entries;
108: }
109: }
|