001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.pagers;
020:
021: import java.text.ParsePosition;
022: import java.text.SimpleDateFormat;
023: import java.util.ArrayList;
024: import java.util.Calendar;
025: import java.util.Date;
026: import java.util.List;
027: import org.apache.commons.lang.StringUtils;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.roller.config.RollerRuntimeConfig;
031: import org.apache.roller.pojos.WebsiteData;
032: import org.apache.roller.util.DateUtil;
033: import org.apache.roller.util.MessageUtilities;
034: import org.apache.roller.util.URLUtilities;
035:
036: /**
037: * An abstract implementation of a WeblogEntriesPager.
038: *
039: * This implementation lays out the basic functionality of an entries pager so
040: * that subclasses can easily tweak only the few things necessary to handle
041: * paging their own way.
042: */
043: public abstract class AbstractWeblogEntriesPager implements
044: WeblogEntriesPager {
045:
046: private static Log log = LogFactory
047: .getLog(AbstractWeblogEntriesPager.class);
048:
049: WebsiteData weblog = null;
050: String locale = null;
051: String pageLink = null;
052: String entryAnchor = null;
053: String dateString = null;
054: String catPath = null;
055: List tags = new ArrayList();
056: int offset = 0;
057: int page = 0;
058: int length = 0;
059:
060: public AbstractWeblogEntriesPager(WebsiteData weblog,
061: String locale, String pageLink, String entryAnchor,
062: String dateString, String catPath, List tags, int page) {
063:
064: this .weblog = weblog;
065: this .locale = locale;
066: this .pageLink = pageLink;
067: this .entryAnchor = entryAnchor;
068: this .dateString = dateString;
069: this .catPath = catPath;
070:
071: if (tags != null)
072: this .tags = tags;
073:
074: // make sure offset, length, and page are valid
075: int maxLength = RollerRuntimeConfig
076: .getIntProperty("site.pages.maxEntries");
077: length = weblog.getEntryDisplayCount();
078: if (length > maxLength) {
079: length = maxLength;
080: }
081:
082: if (page > 0) {
083: this .page = page;
084: }
085: this .offset = length * page;
086: }
087:
088: public boolean hasMoreEntries() {
089: return false;
090: }
091:
092: public String getHomeLink() {
093: return createURL(0, 0, weblog, locale, pageLink, entryAnchor,
094: dateString, catPath, tags);
095: }
096:
097: public String getHomeName() {
098: return MessageUtilities
099: .getString("weblogEntriesPager.latest.home");
100: }
101:
102: public String getNextLink() {
103: if (hasMoreEntries()) {
104: return createURL(page, 1, weblog, locale, pageLink,
105: entryAnchor, dateString, catPath, tags);
106: }
107: return null;
108: }
109:
110: public String getNextName() {
111: if (hasMoreEntries()) {
112: return MessageUtilities
113: .getString("weblogEntriesPager.latest.next");
114: }
115: return null;
116: }
117:
118: public String getPrevLink() {
119: if (page > 0) {
120: return createURL(page, -1, weblog, locale, pageLink,
121: entryAnchor, dateString, catPath, tags);
122: }
123: return null;
124: }
125:
126: public String getPrevName() {
127: if (page > 0) {
128: return MessageUtilities
129: .getString("weblogEntriesPager.latest.prev");
130: }
131: return null;
132: }
133:
134: public String getNextCollectionLink() {
135: return null;
136: }
137:
138: public String getNextCollectionName() {
139: return null;
140: }
141:
142: public String getPrevCollectionLink() {
143: return null;
144: }
145:
146: public String getPrevCollectionName() {
147: return null;
148: }
149:
150: /**
151: * Parse data as either 6-char or 8-char format.
152: */
153: protected Date parseDate(String dateString) {
154: Date ret = null;
155: SimpleDateFormat char8DateFormat = DateUtil
156: .get8charDateFormat();
157: SimpleDateFormat char6DateFormat = DateUtil
158: .get6charDateFormat();
159: if (dateString != null && dateString.length() == 8
160: && StringUtils.isNumeric(dateString)) {
161: ParsePosition pos = new ParsePosition(0);
162: ret = char8DateFormat.parse(dateString, pos);
163:
164: // make sure the requested date is not in the future
165: Date today = getToday();
166: if (ret.after(today))
167: ret = today;
168: }
169: if (dateString != null && dateString.length() == 6
170: && StringUtils.isNumeric(dateString)) {
171: ParsePosition pos = new ParsePosition(0);
172: ret = char6DateFormat.parse(dateString, pos);
173:
174: // make sure the requested date is not in the future
175: Date today = getToday();
176: if (ret.after(today))
177: ret = today;
178: }
179: return ret;
180: }
181:
182: /**
183: * Return today based on current blog's timezone/locale.
184: */
185: protected Date getToday() {
186: Calendar todayCal = Calendar.getInstance();
187: todayCal = Calendar.getInstance(weblog.getTimeZoneInstance(),
188: weblog.getLocaleInstance());
189: todayCal.setTime(new Date());
190: return todayCal.getTime();
191: }
192:
193: /**
194: * Create URL that encodes pager state using most appropriate forms of URL.
195: * @param pageAdd To be added to page number, or 0 for no page number
196: */
197: protected String createURL(int page, int pageAdd,
198: WebsiteData website, String locale, String pageLink,
199: String entryAnchor, String dateString, String catPath,
200: List tags) {
201:
202: int pageNum = page + pageAdd;
203:
204: if (pageLink != null) {
205: return URLUtilities.getWeblogPageURL(website, locale,
206: pageLink, entryAnchor, catPath, dateString, tags,
207: pageNum, false);
208: } else if (entryAnchor != null) {
209: return URLUtilities.getWeblogEntryURL(website, locale,
210: entryAnchor, true);
211: }
212:
213: return URLUtilities.getWeblogCollectionURL(website, locale,
214: catPath, dateString, tags, pageNum, false);
215: }
216:
217: }
|