KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > pagers > WeblogEntriesMonthPager


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.ui.rendering.pagers;
20
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.TreeMap JavaDoc;
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.model.Roller;
33 import org.apache.roller.model.RollerFactory;
34 import org.apache.roller.model.WeblogManager;
35 import org.apache.roller.pojos.WeblogEntryData;
36 import org.apache.roller.pojos.WebsiteData;
37 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
38 import org.apache.roller.util.DateUtil;
39 import org.apache.roller.util.MessageUtilities;
40
41
42 /**
43  *
44  */

45 public class WeblogEntriesMonthPager extends AbstractWeblogEntriesPager {
46     
47     private static Log log = LogFactory.getLog(WeblogEntriesMonthPager.class);
48     
49     private SimpleDateFormat JavaDoc monthFormat = new SimpleDateFormat JavaDoc(
50             MessageUtilities.getString("weblogEntriesPager.month.dateFormat"));
51     
52     private Date JavaDoc month;
53     private Date JavaDoc nextMonth;
54     private Date JavaDoc prevMonth;
55     
56     // collection for the pager
57
private Map JavaDoc entries = null;
58     
59     // are there more pages?
60
private boolean more = false;
61     
62     
63     public WeblogEntriesMonthPager(
64             WebsiteData weblog,
65             String JavaDoc locale,
66             String JavaDoc pageLink,
67             String JavaDoc entryAnchor,
68             String JavaDoc dateString,
69             String JavaDoc catPath,
70             int page) {
71         
72         super(weblog, locale, pageLink, entryAnchor, dateString, catPath, page);
73         
74         getEntries();
75         
76         month = parseDate(dateString);
77         
78         Calendar JavaDoc cal = Calendar.getInstance();
79         
80         cal.setTime(month);
81         cal.add(Calendar.MONTH, 1);
82         nextMonth = cal.getTime();
83         if (nextMonth.after(getToday())) {
84             nextMonth = null;
85         }
86         
87         cal.setTime(month);
88         cal.add(Calendar.MONTH, -1);
89         prevMonth = cal.getTime();
90     }
91     
92     
93     public Map JavaDoc getEntries() {
94         Date JavaDoc date = parseDate(dateString);
95         Calendar JavaDoc cal = Calendar.getInstance(weblog.getTimeZoneInstance());
96         cal.setTime(date);
97         cal.add(Calendar.DATE, 1);
98         date = cal.getTime();
99         Date JavaDoc startDate = DateUtil.getStartOfMonth(date, cal);;
100         Date JavaDoc endDate = DateUtil.getEndOfMonth(date, cal);;
101         
102         if (entries == null) {
103             entries = new TreeMap JavaDoc(new ReverseComparator());
104             try {
105                 Roller roller = RollerFactory.getRoller();
106                 WeblogManager wmgr = roller.getWeblogManager();
107                 Map JavaDoc mmap = RollerFactory.getRoller().getWeblogManager().getWeblogEntryObjectMap(
108                         weblog,
109                         startDate,
110                         endDate,
111                         catPath,
112                         WeblogEntryData.PUBLISHED,
113                         locale,
114                         offset,
115                         length + 1);
116                               
117                 // need to wrap pojos
118
int count = 0;
119                 java.util.Date JavaDoc key = null;
120                 Iterator JavaDoc days = mmap.keySet().iterator();
121                 while(days.hasNext()) {
122                     key = (java.util.Date JavaDoc)days.next();
123
124                     // now we need to go through each entry in a day and wrap
125
List JavaDoc wrapped = new ArrayList JavaDoc();
126                     List JavaDoc unwrapped= (List JavaDoc) mmap.get(key);
127                     for(int i=0; i < unwrapped.size(); i++) {
128                         if (count++ < length) {
129                             wrapped.add(i,
130                             WeblogEntryDataWrapper.wrap((WeblogEntryData)unwrapped.get(i)));
131                         } else {
132                             more = true;
133                         }
134                     }
135                     
136                     // done with that day, put it in the map
137
if(wrapped.size() > 0) {
138                         entries.put(key, wrapped);
139                     }
140                 }
141             } catch (Exception JavaDoc e) {
142                 log.error("ERROR: getting entry month map", e);
143             }
144         }
145         return entries;
146     }
147     
148     
149     public String JavaDoc getHomeLink() {
150         return createURL(0, 0, weblog, locale, pageLink, null, null, catPath);
151     }
152     
153     
154     public String JavaDoc getHomeName() {
155         return MessageUtilities.getString("weblogEntriesPager.month.home");
156     }
157     
158     
159     public String JavaDoc getNextLink() {
160         if (more) {
161             return createURL(page, 1, weblog, locale, pageLink, null, dateString, catPath);
162         }
163         return null;
164     }
165     
166     
167     public String JavaDoc getNextName() {
168         if (getNextLink() != null) {
169             return MessageUtilities.getString("weblogEntriesPager.month.next", new Object JavaDoc[] {monthFormat.format(month)});
170         }
171         return null;
172     }
173     
174     
175     public String JavaDoc getPrevLink() {
176         if (offset > 0) {
177             return createURL(page, -1, weblog, locale, pageLink, null, dateString, catPath);
178         }
179         return null;
180     }
181     
182     
183     public String JavaDoc getPrevName() {
184         if (getPrevLink() != null) {
185             return MessageUtilities.getString("weblogEntriesPager.month.prev", new Object JavaDoc[] {monthFormat.format(month)});
186         }
187         return null;
188     }
189     
190     
191     public String JavaDoc getNextCollectionLink() {
192         if (nextMonth != null) {
193             String JavaDoc next = DateUtil.format6chars(nextMonth);
194             return createURL(0, 0, weblog, locale, pageLink, null, next, catPath);
195         }
196         return null;
197     }
198     
199     
200     public String JavaDoc getNextCollectionName() {
201         if (nextMonth != null) {
202             return MessageUtilities.getString("weblogEntriesPager.month.nextCollection", new Object JavaDoc[] {monthFormat.format(nextMonth)});
203         }
204         return null;
205     }
206     
207     
208     public String JavaDoc getPrevCollectionLink() {
209         if (prevMonth != null) {
210             String JavaDoc prev = DateUtil.format6chars(prevMonth);
211             return createURL(0, 0, weblog, locale, pageLink, null, prev, catPath);
212         }
213         return null;
214     }
215     
216     
217     public String JavaDoc getPrevCollectionName() {
218         if (prevMonth != null) {
219             return MessageUtilities.getString("weblogEntriesPager.month.prevCollection", new Object JavaDoc[] {monthFormat.format(prevMonth)});
220         }
221         return null;
222     }
223     
224 }
225
Popular Tags