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 WeblogEntriesDayPager extends AbstractWeblogEntriesPager {
045:
046: private static Log log = LogFactory
047: .getLog(WeblogEntriesDayPager.class);
048:
049: private SimpleDateFormat dayFormat = new SimpleDateFormat(
050: MessageUtilities
051: .getString("weblogEntriesPager.day.dateFormat"));
052:
053: private Date day;
054: private Date nextDay;
055: private Date prevDay;
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 WeblogEntriesDayPager(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: day = parseDate(dateString);
073:
074: Calendar cal = Calendar.getInstance();
075:
076: cal.setTime(day);
077: cal.add(Calendar.DAY_OF_MONTH, 1);
078: cal.set(Calendar.HOUR, 0);
079: cal.set(Calendar.MINUTE, 0);
080: cal.set(Calendar.SECOND, 0);
081: nextDay = cal.getTime();
082: if (nextDay.after(getToday())) {
083: nextDay = null;
084: }
085:
086: cal.setTime(day);
087: cal.add(Calendar.DAY_OF_MONTH, -1);
088: cal.set(Calendar.HOUR, 23);
089: cal.set(Calendar.MINUTE, 59);
090: cal.set(Calendar.SECOND, 59);
091: prevDay = cal.getTime();
092: }
093:
094: public Map getEntries() {
095: Date date = parseDate(dateString);
096: Calendar cal = Calendar.getInstance(weblog
097: .getTimeZoneInstance());
098: Date startDate = null;
099: Date endDate = date;
100: startDate = DateUtil.getStartOfDay(endDate, cal);
101: endDate = DateUtil.getEndOfDay(endDate, cal);
102:
103: if (entries == null) {
104: entries = new TreeMap(new ReverseComparator());
105: try {
106: Roller roller = RollerFactory.getRoller();
107: WeblogManager wmgr = roller.getWeblogManager();
108: Map mmap = RollerFactory.getRoller().getWeblogManager()
109: .getWeblogEntryObjectMap(weblog, startDate,
110: endDate, catPath, tags,
111: WeblogEntryData.PUBLISHED, locale,
112: offset, length + 1);
113:
114: // need to wrap pojos
115: int count = 0;
116: Date key = null;
117: Iterator days = mmap.keySet().iterator();
118: while (days.hasNext()) {
119: key = (Date) days.next();
120:
121: // now we need to go through each entry in a day and wrap
122: List wrapped = new ArrayList();
123: List unwrapped = (List) mmap.get(key);
124: for (int i = 0; i < unwrapped.size(); i++) {
125: if (count++ < length) {
126: wrapped.add(i, WeblogEntryDataWrapper
127: .wrap((WeblogEntryData) unwrapped
128: .get(i)));
129: } else {
130: more = true;
131: }
132: }
133:
134: // done with that day, put it in the map
135: if (wrapped.size() > 0) {
136: entries.put(key, wrapped);
137: }
138: }
139:
140: } catch (Exception e) {
141: log.error("ERROR: getting entry month map", e);
142: }
143: }
144: return entries;
145: }
146:
147: public String getHomeLink() {
148: return createURL(0, 0, weblog, locale, pageLink, null, null,
149: catPath, tags);
150: }
151:
152: public String getHomeName() {
153: return MessageUtilities
154: .getString("weblogEntriesPager.day.home");
155: }
156:
157: public String getNextLink() {
158: if (more) {
159: return createURL(page, 1, weblog, locale, pageLink, null,
160: dateString, catPath, tags);
161: }
162: return null;
163: }
164:
165: public String getNextName() {
166: if (getNextLink() != null) {
167: return MessageUtilities.getString(
168: "weblogEntriesPager.day.next",
169: new Object[] { dayFormat.format(day) });
170: }
171: return null;
172: }
173:
174: public String getPrevLink() {
175: if (page > 0) {
176: return createURL(page, -1, weblog, locale, pageLink, null,
177: dateString, catPath, tags);
178: }
179: return null;
180: }
181:
182: public String getPrevName() {
183: if (getPrevLink() != null) {
184: return MessageUtilities.getString(
185: "weblogEntriesPager.day.prev",
186: new Object[] { dayFormat.format(day) });
187: }
188: return null;
189: }
190:
191: public String getNextCollectionLink() {
192: if (nextDay != null) {
193: String next = DateUtil.format8chars(nextDay);
194: return createURL(0, 0, weblog, locale, pageLink, null,
195: next, catPath, tags);
196: }
197: return null;
198: }
199:
200: public String getNextCollectionName() {
201: if (nextDay != null) {
202: return MessageUtilities.getString(
203: "weblogEntriesPager.day.nextCollection",
204: new Object[] { dayFormat.format(nextDay) });
205: }
206: return null;
207: }
208:
209: public String getPrevCollectionLink() {
210: if (prevDay != null) {
211: String prev = DateUtil.format8chars(prevDay);
212: return createURL(0, 0, weblog, locale, pageLink, null,
213: prev, catPath, tags);
214: }
215: return null;
216: }
217:
218: public String getPrevCollectionName() {
219: if (prevDay != null) {
220: return MessageUtilities.getString(
221: "weblogEntriesPager.day.prevCollection",
222: new Object[] { dayFormat.format(prevDay) });
223: }
224: return null;
225: }
226:
227: }
|