File Source: WeblogEntriesMonthPager.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.ui.rendering.pagers;
20
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.TreeMap;
29 import org.apache.commons.collections.comparators.ReverseComparator;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.roller.weblogger.business.WebloggerFactory;
33 import org.apache.roller.weblogger.pojos.WeblogEntry;
34 import org.apache.roller.weblogger.pojos.Weblog;
35 import org.apache.roller.weblogger.pojos.wrapper.WeblogEntryWrapper;
36 import org.apache.roller.util.DateUtil;
37 import org.apache.roller.weblogger.business.URLStrategy;
38
39
40 /**
41 *
42 */
43 public class WeblogEntriesMonthPager extends AbstractWeblogEntriesPager {
44
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.pagers.WeblogEntriesMonthPager__static_init
*
* Postconditions:
* init'ed(log)
*/
45 private static Log log = LogFactory.getLog(WeblogEntriesMonthPager.class);
46
47 private SimpleDateFormat monthFormat = new SimpleDateFormat();
48
49 private Date month;
50 private Date nextMonth;
51 private Date prevMonth;
52
53 // collection for the pager
54 private Map entries = null;
55
56 // are there more pages?
57 private boolean more = false;
58
59
60 public WeblogEntriesMonthPager(
61 URLStrategy strat,
62 Weblog weblog,
63 String locale,
64 String pageLink,
65 String entryAnchor,
66 String dateString,
67 String catPath,
68 List tags,
69 int page) {
70
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.pagers.WeblogEntriesMonthPager(URLStrategy, Weblog, String, String, String, String, String, List, int)
*
* Preconditions:
* locale == null
* org/apache/roller/weblogger/util/I18nMessages.messagesMap != null
* weblog != null
* (soft) log != null
*
* Presumptions:
* java.util.Calendar:getInstance(...)@80 != null
* java.util.Calendar:getTime(...)@84 != null
* org.apache.roller.util.DateUtil:getEndOfMonth(...)@92 != null
* this.length in range
* this.messageUtils init'ed
* ...
*
* Postconditions:
* this.catPath == catPath
* init'ed(this.catPath)
* this.dateString == dateString
* init'ed(this.dateString)
* this.entries == &new TreeMap(getEntries#1)
* this.entryAnchor == entryAnchor
* init'ed(this.entryAnchor)
* (soft) this.length <= 232-2
* this.locale == null
* this.messageUtils != null
* ...
*
* Test Vectors:
* java.util.Date:after(...)@85: {0}, {1}
* java.util.Date:before(...)@94: {0}, {1}
* org.apache.roller.weblogger.pojos.Weblog:getDateCreated(...)@93: Addr_Set{null}, Inverse{null}
*/
71 super(strat, weblog, locale, pageLink, entryAnchor, dateString, catPath, tags, page);
72
73 monthFormat = new SimpleDateFormat(
74 messageUtils.getString("weblogEntriesPager.month.dateFormat"));
75
76 getEntries();
77
78 month = parseDate(dateString);
79
80 Calendar cal = Calendar.getInstance();
81
82 cal.setTime(month);
83 cal.add(Calendar.MONTH, 1);
84 nextMonth = cal.getTime();
85 if (nextMonth.after(getToday())) {
86 nextMonth = null;
87 }
88
89 cal.setTime(month);
90 cal.add(Calendar.MONTH, -1);
91 prevMonth = cal.getTime();
92 Date endOfPrevMonth = DateUtil.getEndOfMonth(prevMonth,cal) ;
93 Date weblogInitialDate = weblog.getDateCreated() != null ? weblog.getDateCreated() : new Date(0);
94 if (endOfPrevMonth.before(weblogInitialDate)) {
95 prevMonth = null;
96 }
97 }
98
99
100 public Map getEntries() {
/*
P/P * Method: Map getEntries()
*
* Preconditions:
* init'ed(this.entries)
* init'ed(this.dateString)
* this.weblog != null
* (soft) log != null
* (soft) init'ed(this.catPath)
* (soft) this.length <= 232-2
* (soft) init'ed(this.locale)
* (soft) init'ed(this.offset)
* (soft) init'ed(this.tags)
*
* Presumptions:
* java.util.Calendar:getInstance(...)@102 != null
* java.util.Map:get(...)@132 != null
* java.util.Map:keySet(...)@126 != null
* org.apache.roller.weblogger.business.WeblogManager:getWeblogEntryObjectMap(...)@112 != null
* org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@112 != null
* ...
*
* Postconditions:
* return_value == One-of{old this.entries, &new TreeMap(getEntries#1)}
* return_value != null
* this.entries == return_value
* possibly_updated(this.more)
* new TreeMap(getEntries#1) num objects <= 1
*
* Test Vectors:
* this.entries: Inverse{null}, Addr_Set{null}
* java.util.List:size(...)@142: {-231..0}, {1..232-1}
*/
101 Date date = parseDate(dateString);
102 Calendar cal = Calendar.getInstance(weblog.getTimeZoneInstance());
103 cal.setTime(date);
104 cal.add(Calendar.DATE, 1);
105 date = cal.getTime();
106 Date startDate = DateUtil.getStartOfMonth(date, cal);
107 Date endDate = DateUtil.getEndOfMonth(date, cal);
108
109 if (entries == null) {
110 entries = new TreeMap(new ReverseComparator());
111 try {
112 Map mmap = WebloggerFactory.getWeblogger().getWeblogManager().getWeblogEntryObjectMap(
113
114 weblog,
115 startDate,
116 endDate,
117 catPath,
118 tags,WeblogEntry.PUBLISHED,
119 locale,
120 offset,
121 length + 1);
122
123 // need to wrap pojos
124 int count = 0;
125 java.util.Date key = null;
126 Iterator days = mmap.keySet().iterator();
127 while(days.hasNext()) {
128 key = (java.util.Date)days.next();
129
130 // now we need to go through each entry in a day and wrap
131 List wrapped = new ArrayList();
132 List unwrapped= (List) mmap.get(key);
133 for(int i=0; i < unwrapped.size(); i++) {
+ 134 if (count++ < length) {
135 wrapped.add(i,WeblogEntryWrapper.wrap((WeblogEntry)unwrapped.get(i), urlStrategy));
136 } else {
137 more = true;
138 }
139 }
140
141 // done with that day, put it in the map
142 if(wrapped.size() > 0) {
143 entries.put(key, wrapped);
144 }
145 }
146 } catch (Exception e) {
147 log.error("ERROR: getting entry month map", e);
148 }
149 }
150 return entries;
151 }
152
153
154 public String getHomeLink() {
/*
P/P * Method: String getHomeLink()
*
* Preconditions:
* init'ed(this.catPath)
* init'ed(this.locale)
* init'ed(this.pageLink)
* init'ed(this.tags)
* this.urlStrategy != null
* init'ed(this.weblog)
*
* Postconditions:
* init'ed(return_value)
*/
155 return createURL(0, 0, weblog, locale, pageLink, null, null, catPath, tags);
156 }
157
158
159 public String getHomeName() {
/*
P/P * Method: String getHomeName()
*
* Preconditions:
* this.messageUtils != null
* (soft) this.messageUtils.bundle != null
*
* Postconditions:
* init'ed(return_value)
*/
160 return messageUtils.getString("weblogEntriesPager.month.home");
161 }
162
163
164 public String getNextLink() {
/*
P/P * Method: String getNextLink()
*
* Preconditions:
* init'ed(this.more)
* (soft) init'ed(this.catPath)
* (soft) init'ed(this.dateString)
* (soft) init'ed(this.locale)
* (soft) this.page <= 232-2
* (soft) init'ed(this.pageLink)
* (soft) init'ed(this.tags)
* (soft) this.urlStrategy != null
* (soft) init'ed(this.weblog)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.more: {0}, {1}
*/
165 if (more) {
166 return createURL(page, 1, weblog, locale, pageLink, null, dateString, catPath, tags);
167 }
168 return null;
169 }
170
171
172 public String getNextName() {
/*
P/P * Method: String getNextName()
*
* Preconditions:
* init'ed(this.more)
* (soft) init'ed(this.catPath)
* (soft) init'ed(this.dateString)
* (soft) init'ed(this.locale)
* (soft) this.messageUtils != null
* (soft) this.messageUtils.bundle != null
* (soft) init'ed(this.month)
* (soft) this.monthFormat != null
* (soft) this.page <= 232-2
* (soft) init'ed(this.pageLink)
* ...
*
* Postconditions:
* init'ed(return_value)
*/
173 if (getNextLink() != null) {
174 return messageUtils.getString("weblogEntriesPager.month.next", new Object[] {monthFormat.format(month)});
175 }
176 return null;
177 }
178
179
180 public String getPrevLink() {
/*
P/P * Method: String getPrevLink()
*
* Preconditions:
* init'ed(this.offset)
* (soft) init'ed(this.catPath)
* (soft) init'ed(this.dateString)
* (soft) init'ed(this.locale)
* (soft) this.page >= -231+1
* (soft) init'ed(this.pageLink)
* (soft) init'ed(this.tags)
* (soft) this.urlStrategy != null
* (soft) init'ed(this.weblog)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.offset: {-231..0}, {1..232-1}
*/
181 if (offset > 0) {
182 return createURL(page, -1, weblog, locale, pageLink, null, dateString, catPath, tags);
183 }
184 return null;
185 }
186
187
188 public String getPrevName() {
/*
P/P * Method: String getPrevName()
*
* Preconditions:
* init'ed(this.offset)
* (soft) init'ed(this.catPath)
* (soft) init'ed(this.dateString)
* (soft) init'ed(this.locale)
* (soft) this.messageUtils != null
* (soft) this.messageUtils.bundle != null
* (soft) init'ed(this.month)
* (soft) this.monthFormat != null
* (soft) this.page >= -231+1
* (soft) init'ed(this.pageLink)
* ...
*
* Postconditions:
* init'ed(return_value)
*/
189 if (getPrevLink() != null) {
190 return messageUtils.getString("weblogEntriesPager.month.prev", new Object[] {monthFormat.format(month)});
191 }
192 return null;
193 }
194
195
196 public String getNextCollectionLink() {
/*
P/P * Method: String getNextCollectionLink()
*
* Preconditions:
* init'ed(this.nextMonth)
* (soft) init'ed(this.catPath)
* (soft) init'ed(this.locale)
* (soft) init'ed(this.pageLink)
* (soft) init'ed(this.tags)
* (soft) this.urlStrategy != null
* (soft) init'ed(this.weblog)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.nextMonth: Addr_Set{null}, Inverse{null}
*/
197 if (nextMonth != null) {
198 String next = DateUtil.format6chars(nextMonth);
199 return createURL(0, 0, weblog, locale, pageLink, null, next, catPath, tags);
200 }
201 return null;
202 }
203
204
205 public String getNextCollectionName() {
/*
P/P * Method: String getNextCollectionName()
*
* Preconditions:
* init'ed(this.nextMonth)
* (soft) this.messageUtils != null
* (soft) this.messageUtils.bundle != null
* (soft) this.monthFormat != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.nextMonth: Addr_Set{null}, Inverse{null}
*/
206 if (nextMonth != null) {
207 return messageUtils.getString("weblogEntriesPager.month.nextCollection", new Object[] {monthFormat.format(nextMonth)});
208 }
209 return null;
210 }
211
212
213 public String getPrevCollectionLink() {
/*
P/P * Method: String getPrevCollectionLink()
*
* Preconditions:
* init'ed(this.prevMonth)
* (soft) init'ed(this.catPath)
* (soft) init'ed(this.locale)
* (soft) init'ed(this.pageLink)
* (soft) init'ed(this.tags)
* (soft) this.urlStrategy != null
* (soft) init'ed(this.weblog)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.prevMonth: Addr_Set{null}, Inverse{null}
*/
214 if (prevMonth != null) {
215 String prev = DateUtil.format6chars(prevMonth);
216 return createURL(0, 0, weblog, locale, pageLink, null, prev, catPath, tags);
217 }
218 return null;
219 }
220
221
222 public String getPrevCollectionName() {
/*
P/P * Method: String getPrevCollectionName()
*
* Preconditions:
* init'ed(this.prevMonth)
* (soft) this.messageUtils != null
* (soft) this.messageUtils.bundle != null
* (soft) this.monthFormat != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.prevMonth: Addr_Set{null}, Inverse{null}
*/
223 if (prevMonth != null) {
224 return messageUtils.getString("weblogEntriesPager.month.prevCollection", new Object[] {monthFormat.format(prevMonth)});
225 }
226 return null;
227 }
228
229 }
SofCheck Inspector Build Version : 2.18479
| WeblogEntriesMonthPager.java |
2009-Jan-02 14:25:08 |
| WeblogEntriesMonthPager.class |
2009-Sep-04 03:12:44 |