KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > tags > calendar > WeblogCalendarModel


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.core.tags.calendar;
20
21 import java.text.ParsePosition JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.RollerException;
30 import org.apache.roller.model.RollerFactory;
31 import org.apache.roller.model.WeblogManager;
32 import org.apache.roller.pojos.WeblogEntryData;
33 import org.apache.roller.pojos.WebsiteData;
34 import org.apache.roller.ui.rendering.util.WeblogPageRequest;
35 import org.apache.roller.util.DateUtil;
36 import org.apache.roller.util.URLUtilities;
37
38
39 /**
40  * Calendar model for calendar intended for use on view-weblog page.
41  */

42 public class WeblogCalendarModel implements CalendarModel {
43     
44     private static Log log = LogFactory.getLog(WeblogCalendarModel.class);
45     
46     protected Map JavaDoc monthMap;
47     protected Date JavaDoc day;
48     protected String JavaDoc cat = null;
49     protected String JavaDoc pageLink = null;
50     protected String JavaDoc locale = null;
51     protected Calendar JavaDoc calendar = null;
52     protected WebsiteData weblog = null;
53     
54     protected WeblogPageRequest pageRequest = null;
55     
56     
57     public WeblogCalendarModel(WeblogPageRequest pRequest, String JavaDoc catArgument) {
58         
59         this.pageRequest = pRequest;
60         try {
61             this.weblog = pageRequest.getWeblog();
62             
63             pageLink = pageRequest.getWeblogPageName();
64             
65             day = DateUtil.parseWeblogURLDateString(pageRequest.getWeblogDate(),
66                     weblog.getTimeZoneInstance(), weblog.getLocaleInstance());
67             initDay(day);
68             
69             locale = pageRequest.getLocale();
70             
71             if(weblog == null) {
72                 throw new RollerException("unable to lookup weblog: "+
73                         pageRequest.getWeblogHandle());
74             }
75             // Category method argument overrides category from URL
76
if (catArgument != null) {
77                 cat = catArgument;
78             } else if (pageRequest.getWeblogCategoryName() != null) {
79                 cat = pageRequest.getWeblogCategoryName();
80             }
81         } catch (Exception JavaDoc e) {
82             // some kind of error parsing the request or looking up weblog
83
log.debug("ERROR: initializing calendar", e);
84         }
85         
86     }
87     
88     
89     protected void initDay(Date JavaDoc month) {
90         this.day = day;
91         calendar = Calendar.getInstance(
92                 weblog.getTimeZoneInstance(),
93                 weblog.getLocaleInstance());
94         
95         Calendar JavaDoc cal = (Calendar JavaDoc)calendar.clone();
96         
97         // Compute first second of month
98
cal.setTime(month);
99         cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH));
100         cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
101         cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
102         cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
103         cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
104         Date JavaDoc startDate = cal.getTime();
105         
106         // Compute last second of month
107
cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH));
108         cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
109         cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
110         cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
111         cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
112         Date JavaDoc endDate = cal.getTime();
113         
114         loadWeblogEntries(startDate, endDate, cat);
115     }
116     
117     protected void loadWeblogEntries(Date JavaDoc startDate, Date JavaDoc endDate, String JavaDoc catName) {
118         try {
119             WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
120             monthMap = mgr.getWeblogEntryStringMap(
121                     weblog, // website
122
startDate, // startDate
123
endDate, // endDate
124
catName, // cat
125
WeblogEntryData.PUBLISHED, // status
126
locale,
127                     0, -1);
128         } catch (RollerException e) {
129             log.error(e);
130             monthMap = new HashMap JavaDoc();
131         }
132     }
133     
134     public void setDay(String JavaDoc month) throws Exception JavaDoc {
135         SimpleDateFormat JavaDoc fmt = DateUtil.get8charDateFormat();
136         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
137         initDay( fmt.parse( month, pos ) );
138     }
139     
140     public Date JavaDoc getDay() {
141         return (Date JavaDoc)day.clone();
142     }
143     
144     public String JavaDoc getParameterValue(Date JavaDoc day) {
145         return (String JavaDoc)monthMap.get( day );
146     }
147     
148     /**
149      * Create URL for use on view-weblog page
150      * @param day Day for URL or null if no entries on that day
151      * @param alwaysURL Always return a URL, never return null
152      * @return URL for day, or null if no weblog entry on that day
153      */

154     public String JavaDoc computeUrl(Date JavaDoc day, boolean monthURL, boolean alwaysURL) {
155         String JavaDoc url = null;
156         // get the 8 char YYYYMMDD datestring for day
157
String JavaDoc dateString = (String JavaDoc)monthMap.get(day);
158         if (dateString == null && !alwaysURL) return null;
159         else if (dateString == null && !monthURL) {
160             dateString = DateUtil.format8chars(day);
161         } else if (dateString == null && monthURL) {
162             dateString = DateUtil.format6chars(day);
163         }
164         try {
165             if (pageLink == null) { // create date URL
166
url = URLUtilities.getWeblogCollectionURL(weblog, locale, cat, dateString, -1, false);
167             } else { // create page URL
168
url = URLUtilities.getWeblogPageURL(weblog, locale, pageLink, null, cat, dateString, -1, false);
169             }
170         } catch (Exception JavaDoc e) {
171             log.error("ERROR: creating URL",e);
172         }
173         return url;
174     }
175     
176     public String JavaDoc getContent(Date JavaDoc day) {
177         return null;
178     }
179     
180     public Calendar JavaDoc getCalendar() {
181         return (Calendar JavaDoc)calendar.clone();
182     }
183     
184     public Date JavaDoc getNextMonth() {
185         Calendar JavaDoc nextCal = getCalendar();
186         nextCal.setTime( day );
187         nextCal.add( Calendar.MONTH, 1 );
188         return getFirstDayOfMonth(nextCal).getTime();
189     }
190     
191     protected Calendar JavaDoc getFirstDayOfMonth(Calendar JavaDoc cal) {
192         int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
193         cal.set(Calendar.DAY_OF_MONTH, firstDay);
194         return cal;
195     }
196     
197     protected Calendar JavaDoc getLastDayOfMonth(Calendar JavaDoc cal) {
198         int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
199         cal.set(Calendar.DAY_OF_MONTH, lastDay);
200         return cal;
201     }
202     
203     public String JavaDoc computeNextMonthUrl() {
204         // Create yyyyMMdd dates for next month, prev month and today
205
Calendar JavaDoc nextCal = getCalendar();
206         nextCal.setTime( day );
207         nextCal.add( Calendar.MONTH, 1 );
208         String JavaDoc nextMonth = computeUrl(nextCal.getTime(), true, true);
209         
210         // and strip off last two digits to get a month URL
211
return nextMonth;
212     }
213     
214     public String JavaDoc computePrevMonthUrl() {
215         // Create yyyyMMdd dates for prev month, prev month and today
216
Calendar JavaDoc prevCal = getCalendar();
217         prevCal.setTime(day);
218         prevCal.add(Calendar.MONTH, -1);
219         //getLastDayOfMonth( prevCal );
220
String JavaDoc prevMonth = computeUrl(prevCal.getTime(), true, true);
221         
222         // and strip off last two digits to get a month URL
223
return prevMonth;
224     }
225     
226     public String JavaDoc computeTodayMonthUrl() {
227         return URLUtilities.getWeblogCollectionURL(weblog, locale, cat, null, -1, false);
228     }
229     
230 }
231
Popular Tags