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.SimpleDateFormat;
022: import java.util.ArrayList;
023: import java.util.Calendar;
024: import java.util.Date;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.TreeMap;
029: import org.apache.commons.collections.comparators.ReverseComparator;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.roller.business.Roller;
033: import org.apache.roller.business.RollerFactory;
034: import org.apache.roller.business.WeblogManager;
035: import org.apache.roller.pojos.WeblogEntryData;
036: import org.apache.roller.pojos.WebsiteData;
037: import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
038: import org.apache.roller.util.DateUtil;
039: import org.apache.roller.util.MessageUtilities;
040:
041: /**
042: *
043: */
044: public class WeblogEntriesMonthPager extends AbstractWeblogEntriesPager {
045:
046: private static Log log = LogFactory
047: .getLog(WeblogEntriesMonthPager.class);
048:
049: private SimpleDateFormat monthFormat = new SimpleDateFormat(
050: MessageUtilities
051: .getString("weblogEntriesPager.month.dateFormat"));
052:
053: private Date month;
054: private Date nextMonth;
055: private Date prevMonth;
056:
057: // collection for the pager
058: private Map entries = null;
059:
060: // are there more pages?
061: private boolean more = false;
062:
063: public WeblogEntriesMonthPager(WebsiteData weblog, String locale,
064: String pageLink, String entryAnchor, String dateString,
065: String catPath, List tags, int page) {
066:
067: super (weblog, locale, pageLink, entryAnchor, dateString,
068: catPath, tags, page);
069:
070: getEntries();
071:
072: month = parseDate(dateString);
073:
074: Calendar cal = Calendar.getInstance();
075:
076: cal.setTime(month);
077: cal.add(Calendar.MONTH, 1);
078: nextMonth = cal.getTime();
079: if (nextMonth.after(getToday())) {
080: nextMonth = null;
081: }
082:
083: cal.setTime(month);
084: cal.add(Calendar.MONTH, -1);
085: prevMonth = cal.getTime();
086: }
087:
088: public Map getEntries() {
089: Date date = parseDate(dateString);
090: Calendar cal = Calendar.getInstance(weblog
091: .getTimeZoneInstance());
092: cal.setTime(date);
093: cal.add(Calendar.DATE, 1);
094: date = cal.getTime();
095: Date startDate = DateUtil.getStartOfMonth(date, cal);
096: ;
097: Date endDate = DateUtil.getEndOfMonth(date, cal);
098: ;
099:
100: if (entries == null) {
101: entries = new TreeMap(new ReverseComparator());
102: try {
103: Roller roller = RollerFactory.getRoller();
104: WeblogManager wmgr = roller.getWeblogManager();
105: Map mmap = RollerFactory.getRoller().getWeblogManager()
106: .getWeblogEntryObjectMap(weblog, startDate,
107: endDate, catPath, tags,
108: WeblogEntryData.PUBLISHED, locale,
109: offset, length + 1);
110:
111: // need to wrap pojos
112: int count = 0;
113: java.util.Date key = null;
114: Iterator days = mmap.keySet().iterator();
115: while (days.hasNext()) {
116: key = (java.util.Date) days.next();
117:
118: // now we need to go through each entry in a day and wrap
119: List wrapped = new ArrayList();
120: List unwrapped = (List) mmap.get(key);
121: for (int i = 0; i < unwrapped.size(); i++) {
122: if (count++ < length) {
123: wrapped.add(i, WeblogEntryDataWrapper
124: .wrap((WeblogEntryData) unwrapped
125: .get(i)));
126: } else {
127: more = true;
128: }
129: }
130:
131: // done with that day, put it in the map
132: if (wrapped.size() > 0) {
133: entries.put(key, wrapped);
134: }
135: }
136: } catch (Exception e) {
137: log.error("ERROR: getting entry month map", e);
138: }
139: }
140: return entries;
141: }
142:
143: public String getHomeLink() {
144: return createURL(0, 0, weblog, locale, pageLink, null, null,
145: catPath, tags);
146: }
147:
148: public String getHomeName() {
149: return MessageUtilities
150: .getString("weblogEntriesPager.month.home");
151: }
152:
153: public String getNextLink() {
154: if (more) {
155: return createURL(page, 1, weblog, locale, pageLink, null,
156: dateString, catPath, tags);
157: }
158: return null;
159: }
160:
161: public String getNextName() {
162: if (getNextLink() != null) {
163: return MessageUtilities.getString(
164: "weblogEntriesPager.month.next",
165: new Object[] { monthFormat.format(month) });
166: }
167: return null;
168: }
169:
170: public String getPrevLink() {
171: if (offset > 0) {
172: return createURL(page, -1, weblog, locale, pageLink, null,
173: dateString, catPath, tags);
174: }
175: return null;
176: }
177:
178: public String getPrevName() {
179: if (getPrevLink() != null) {
180: return MessageUtilities.getString(
181: "weblogEntriesPager.month.prev",
182: new Object[] { monthFormat.format(month) });
183: }
184: return null;
185: }
186:
187: public String getNextCollectionLink() {
188: if (nextMonth != null) {
189: String next = DateUtil.format6chars(nextMonth);
190: return createURL(0, 0, weblog, locale, pageLink, null,
191: next, catPath, tags);
192: }
193: return null;
194: }
195:
196: public String getNextCollectionName() {
197: if (nextMonth != null) {
198: return MessageUtilities.getString(
199: "weblogEntriesPager.month.nextCollection",
200: new Object[] { monthFormat.format(nextMonth) });
201: }
202: return null;
203: }
204:
205: public String getPrevCollectionLink() {
206: if (prevMonth != null) {
207: String prev = DateUtil.format6chars(prevMonth);
208: return createURL(0, 0, weblog, locale, pageLink, null,
209: prev, catPath, tags);
210: }
211: return null;
212: }
213:
214: public String getPrevCollectionName() {
215: if (prevMonth != null) {
216: return MessageUtilities.getString(
217: "weblogEntriesPager.month.prevCollection",
218: new Object[] { monthFormat.format(prevMonth) });
219: }
220: return null;
221: }
222:
223: }
|