KickJava   Java API By Example, From Geeks To Geeks.

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


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 WeblogEntriesDayPager extends AbstractWeblogEntriesPager {
46     
47     private static Log log = LogFactory.getLog(WeblogEntriesDayPager.class);
48     
49     private SimpleDateFormat JavaDoc dayFormat = new SimpleDateFormat JavaDoc(
50             MessageUtilities.getString("weblogEntriesPager.day.dateFormat"));
51     
52     private Date JavaDoc day;
53     private Date JavaDoc nextDay;
54     private Date JavaDoc prevDay;
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 WeblogEntriesDayPager(
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         day = parseDate(dateString);
77         
78         Calendar JavaDoc cal = Calendar.getInstance();
79         
80         cal.setTime(day);
81         cal.add(Calendar.DAY_OF_MONTH, 1);
82         cal.set(Calendar.HOUR, 0);
83         cal.set(Calendar.MINUTE, 0);
84         cal.set(Calendar.SECOND, 0);
85         nextDay = cal.getTime();
86         if (nextDay.after(getToday())) {
87             nextDay = null;
88         }
89         
90         cal.setTime(day);
91         cal.add(Calendar.DAY_OF_MONTH, -1);
92         cal.set(Calendar.HOUR, 23);
93         cal.set(Calendar.MINUTE, 59);
94         cal.set(Calendar.SECOND, 59);
95         prevDay = cal.getTime();
96     }
97     
98     
99     public Map JavaDoc getEntries() {
100         Date JavaDoc date = parseDate(dateString);
101         Calendar JavaDoc cal = Calendar.getInstance(weblog.getTimeZoneInstance());
102         Date JavaDoc startDate = null;
103         Date JavaDoc endDate = date;
104         startDate = DateUtil.getStartOfDay(endDate, cal);
105         endDate = DateUtil.getEndOfDay(endDate, cal);
106         
107         if (entries == null) {
108             entries = new TreeMap JavaDoc(new ReverseComparator());
109             try {
110                 Roller roller = RollerFactory.getRoller();
111                 WeblogManager wmgr = roller.getWeblogManager();
112                 Map JavaDoc mmap = RollerFactory.getRoller().getWeblogManager().getWeblogEntryObjectMap(
113                         weblog,
114                         startDate,
115                         endDate,
116                         catPath,
117                         WeblogEntryData.PUBLISHED,
118                         locale,
119                         offset,
120                         length + 1);
121                 
122                 // need to wrap pojos
123
int count = 0;
124                 Date JavaDoc key = null;
125                 Iterator JavaDoc days = mmap.keySet().iterator();
126                 while(days.hasNext()) {
127                     key = (Date JavaDoc) days.next();
128                     
129                     // now we need to go through each entry in a day and wrap
130
List JavaDoc wrapped = new ArrayList JavaDoc();
131                     List JavaDoc unwrapped = (List JavaDoc) mmap.get(key);
132                     for(int i=0; i < unwrapped.size(); i++) {
133                         if (count++ < length) {
134                             wrapped.add(i,
135                             WeblogEntryDataWrapper.wrap((WeblogEntryData)unwrapped.get(i)));
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                 
147                 
148             } catch (Exception JavaDoc e) {
149                 log.error("ERROR: getting entry month map", e);
150             }
151         }
152         return entries;
153     }
154     
155     
156     public String JavaDoc getHomeLink() {
157         return createURL(0, 0, weblog, locale, pageLink, null, null, catPath);
158     }
159     
160     
161     public String JavaDoc getHomeName() {
162         return MessageUtilities.getString("weblogEntriesPager.day.home");
163     }
164     
165     
166     public String JavaDoc getNextLink() {
167         if (more) {
168             return createURL(page, 1, weblog, locale, pageLink, null, dateString, catPath);
169         }
170         return null;
171     }
172     
173     
174     public String JavaDoc getNextName() {
175         if (getNextLink() != null) {
176             return MessageUtilities.getString("weblogEntriesPager.day.next", new Object JavaDoc[] {dayFormat.format(day)});
177         }
178         return null;
179     }
180     
181     
182     public String JavaDoc getPrevLink() {
183         if (page > 0) {
184             return createURL(page, -1, weblog, locale, pageLink, null, dateString, catPath);
185         }
186         return null;
187     }
188     
189     
190     public String JavaDoc getPrevName() {
191         if (getPrevLink() != null) {
192             return MessageUtilities.getString("weblogEntriesPager.day.prev", new Object JavaDoc[] {dayFormat.format(day)});
193         }
194         return null;
195     }
196     
197     
198     public String JavaDoc getNextCollectionLink() {
199         if (nextDay != null) {
200             String JavaDoc next = DateUtil.format8chars(nextDay);
201             return createURL(0, 0, weblog, locale, pageLink, null, next, catPath);
202         }
203         return null;
204     }
205     
206     
207     public String JavaDoc getNextCollectionName() {
208         if (nextDay != null) {
209             return MessageUtilities.getString("weblogEntriesPager.day.nextCollection", new Object JavaDoc[] {dayFormat.format(nextDay)});
210         }
211         return null;
212     }
213     
214     
215     public String JavaDoc getPrevCollectionLink() {
216         if (prevDay != null) {
217             String JavaDoc prev = DateUtil.format8chars(prevDay);
218             return createURL(0, 0, weblog, locale, pageLink, null, prev, catPath);
219         }
220         return null;
221     }
222     
223     
224     public String JavaDoc getPrevCollectionName() {
225         if (prevDay != null) {
226             return MessageUtilities.getString("weblogEntriesPager.day.prevCollection", new Object JavaDoc[] {dayFormat.format(prevDay)});
227         }
228         return null;
229     }
230     
231 }
232
Popular Tags