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.skip;
031:
032: import org.blojsom.plugin.Plugin;
033: import org.blojsom.plugin.PluginException;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.fetcher.Fetcher;
037: import org.blojsom.fetcher.FetcherException;
038: import org.blojsom.util.BlojsomUtils;
039: import org.blojsom.util.BlojsomConstants;
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045: import java.util.Map;
046:
047: /**
048: * Skip Filter Helper plugin
049: *
050: * @author David Czarnecki
051: * @since blojsom 3.1
052: * @version $Id: SkipFilterHelperPlugin.java,v 1.2 2007/01/17 02:35:13 czarneckid Exp $
053: */
054: public class SkipFilterHelperPlugin implements Plugin {
055:
056: private Log _logger = LogFactory
057: .getLog(SkipFilterHelperPlugin.class);
058:
059: private static final String BLOJSOM_CURRENT_PAGE = "BLOJSOM_CURRENT_PAGE";
060: private static final String BLOJSOM_TOTAL_PAGES = "BLOJSOM_TOTAL_PAGES";
061:
062: private Fetcher _fetcher;
063:
064: /**
065: * Construct a new instance of the Skip Filter Helper plugin
066: */
067: public SkipFilterHelperPlugin() {
068: }
069:
070: /**
071: * Set the {@link Fetcher}
072: *
073: * @param fetcher {@link Fetcher}
074: */
075: public void setFetcher(Fetcher fetcher) {
076: _fetcher = fetcher;
077: }
078:
079: /**
080: * Initialize this plugin. This method only called when the plugin is instantiated.
081: *
082: * @throws PluginException If there is an error initializing the plugin
083: */
084: public void init() throws PluginException {
085: }
086:
087: /**
088: * Process the blog entries
089: *
090: * @param httpServletRequest Request
091: * @param httpServletResponse Response
092: * @param blog {@link Blog} instance
093: * @param context Context
094: * @param entries Blog entries retrieved for the particular request
095: * @return Modified set of blog entries
096: * @throws PluginException If there is an error processing the blog entries
097: */
098: public Entry[] process(HttpServletRequest httpServletRequest,
099: HttpServletResponse httpServletResponse, Blog blog,
100: Map context, Entry[] entries) throws PluginException {
101: try {
102: Integer totalEntries = _fetcher.countEntries(blog);
103:
104: String pgNum = BlojsomUtils.getRequestValue(
105: BlojsomConstants.PAGE_NUMBER_PARAM,
106: httpServletRequest);
107: int page;
108: try {
109: page = Integer.parseInt(pgNum);
110: page -= 1;
111: if (page < 0) {
112: page = 0;
113: }
114: } catch (NumberFormatException e) {
115: page = 0;
116: }
117:
118: int totalPages = totalEntries.intValue()
119: / blog.getBlogDisplayEntries();
120:
121: context.put(BLOJSOM_CURRENT_PAGE, new Integer(page + 1));
122: context.put(BLOJSOM_TOTAL_PAGES, new Integer(totalPages));
123: } catch (FetcherException e) {
124: if (_logger.isErrorEnabled()) {
125: _logger.error(e);
126: }
127: }
128:
129: return entries;
130: }
131:
132: /**
133: * Perform any cleanup for the plugin. Called after {@link #process}.
134: *
135: * @throws PluginException If there is an error performing cleanup for this plugin
136: */
137: public void cleanup() throws PluginException {
138: }
139:
140: /**
141: * Called when BlojsomServlet is taken out of service
142: *
143: * @throws PluginException If there is an error in finalizing this plugin
144: */
145: public void destroy() throws PluginException {
146: }
147: }
|