KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > tags > WeblogCalendarModel


1
2 package org.roller.presentation.weblog.tags;
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.roller.RollerException;
7 import org.roller.model.WeblogManager;
8 import org.roller.presentation.RollerRequest;
9 import org.roller.presentation.tags.calendar.CalendarModel;
10 import org.roller.util.DateUtil;
11
12 import java.text.ParsePosition JavaDoc;
13 import java.text.SimpleDateFormat JavaDoc;
14 import java.util.Calendar JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 /**
23  * Calendar model for calendar intended for use on view-weblog page.
24  */

25 public class WeblogCalendarModel implements CalendarModel
26 {
27     private static Log mLogger =
28         LogFactory.getFactory().getInstance(WeblogCalendarModel.class);
29     
30     protected RollerRequest mRollerReq = null;
31     protected HttpServletRequest JavaDoc mReq = null;
32     protected HttpServletResponse JavaDoc mRes = null;
33     protected Map JavaDoc mMonthMap;
34     protected String JavaDoc mSelfUrl;
35     protected Date JavaDoc mDay;
36     protected String JavaDoc mCatName = null;
37     protected Calendar JavaDoc mCalendar = null;
38
39     public WeblogCalendarModel(
40         RollerRequest rreq, HttpServletResponse JavaDoc res, String JavaDoc url, String JavaDoc cat)
41     {
42         mRollerReq = rreq;
43         mReq = rreq.getRequest();
44         mRes = res;
45         mSelfUrl = url;
46         
47         setDay( mRollerReq.getDate(true) );
48         
49         // If category is specified in URL, then perpetuate it
50
String JavaDoc catKey = RollerRequest.WEBLOGCATEGORYNAME_KEY;
51         String JavaDoc catToUse = mReq.getParameter(catKey);
52         if (mReq.getParameter(catKey) == null)
53         {
54             // If no cat in URL, then use method argument
55
catToUse = cat;
56         }
57         if ( catToUse != null )
58         {
59             mCatName = "?"+catKey+"="+catToUse;
60         }
61         else
62         {
63             mCatName = "";
64         }
65     }
66  /*
67     public static WeblogCalendarModel getInstance(
68         HttpServletRequest req, HttpServletResponse res, String url, String cat)
69     {
70         return new WeblogCalendarModel(RollerRequest.getRollerRequest(req), res, url, cat);
71     }
72 */

73     private void setDay(Date JavaDoc month)
74     {
75         mDay = month;
76         
77         // If category is specified in URL, then use it
78
String JavaDoc catName =
79             mReq.getParameter(RollerRequest.WEBLOGCATEGORYNAME_KEY);
80
81         mCalendar = Calendar.getInstance(
82                                 mRollerReq.getWebsite().getTimeZoneInstance(),
83                                 mRollerReq.getWebsite().getLocaleInstance()
84         );
85         
86         Calendar JavaDoc cal = (Calendar JavaDoc)mCalendar.clone();
87         
88         // Compute first second of month
89
cal.setTime(month);
90         cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH));
91         cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
92         cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
93         cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
94         cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
95         Date JavaDoc startDate = cal.getTime();
96         
97         // Compute last second of month
98
cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH));
99         cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
100         cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
101         cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
102         cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
103         Date JavaDoc endDate = cal.getTime();
104
105         loadWeblogEntries(startDate, endDate, catName);
106     }
107     
108     /**
109      * @param startDate
110      * @param endDate
111      */

112     protected void loadWeblogEntries(Date JavaDoc startDate, Date JavaDoc endDate, String JavaDoc catName)
113     {
114         try
115         {
116             WeblogManager mgr = mRollerReq.getRoller().getWeblogManager();
117             mMonthMap = mgr.getWeblogEntryStringMap(
118                             mRollerReq.getWebsite(), // userName
119
startDate, // startDate
120
endDate, // endDate
121
catName, // catName
122
WeblogManager.PUB_ONLY, // status
123
null
124             );
125         }
126         catch (RollerException e)
127         {
128             mLogger.error(e);
129             mMonthMap = new HashMap JavaDoc();
130         }
131     }
132     
133     public void setDay(String JavaDoc month) throws Exception JavaDoc
134     {
135         SimpleDateFormat JavaDoc fmt = DateUtil.get8charDateFormat();
136         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
137         setDay( fmt.parse( month, pos ) );
138     }
139     
140     public Date JavaDoc getDay()
141     {
142         return (Date JavaDoc)mDay.clone();
143     }
144     
145     public String JavaDoc getSelfUrl() throws Exception JavaDoc
146     {
147         return mRes.encodeURL(mSelfUrl);
148     }
149
150     public String JavaDoc getTargetUrl() throws Exception JavaDoc
151     {
152         return getSelfUrl();
153     }
154
155     public String JavaDoc getParameterName()
156     {
157         return RollerRequest.WEBLOGDAY_KEY;
158     }
159
160     public String JavaDoc getParameterValue( Date JavaDoc day )
161     {
162         return (String JavaDoc)mMonthMap.get( day );
163     }
164
165     /**
166      * Create URL for use on view-weblog page, ignores query-string.
167      * @param day Day for URL
168      * @param valid Always return a URL, never return null
169      * @return URL for day, or null if no weblog entry on that day
170      */

171     public String JavaDoc computeUrl(java.util.Date JavaDoc day, boolean valid)
172     {
173         String JavaDoc url = null;
174         try
175         {
176             if ( day == null )
177             {
178                 url = mRes.encodeURL(mSelfUrl + mCatName);
179             }
180             else
181             {
182                 // get the 8 char YYYYMMDD datestring for day, returns null
183
// if no weblog entry on that day
184
String JavaDoc dateString = (String JavaDoc)mMonthMap.get( day );
185                 if ( dateString != null )
186                 {
187                     // append 8 char date string on end of selfurl
188
url = mRes.encodeURL(mSelfUrl+"/"+dateString+mCatName);
189                 }
190                 else if ( valid )
191                 {
192                     // Make the date yyyyMMdd and append it to URL
193
dateString = DateUtil.format8chars( day );
194                     url = mRes.encodeURL( mSelfUrl+"/"+dateString+mCatName);
195                 }
196             }
197         }
198         catch (Exception JavaDoc e)
199         {
200            mRollerReq.getServletContext().log("ERROR: creating URL",e);
201         }
202         return url;
203     }
204     
205     /**
206      * @see org.roller.presentation.tags.calendar.CalendarModel#getContent(Date, boolean)
207      */

208     public String JavaDoc getContent(Date JavaDoc day)
209     {
210         return null;
211     }
212     
213     public Calendar JavaDoc getCalendar()
214     {
215         return (Calendar JavaDoc)mCalendar.clone();
216     }
217     
218     public Date JavaDoc getNextMonth()
219     {
220         Calendar JavaDoc nextCal = getCalendar();
221         nextCal.setTime( mDay );
222         nextCal.add( Calendar.MONTH, 1 );
223         return getFirstDayOfMonth(nextCal).getTime();
224     }
225     
226     protected Calendar JavaDoc getFirstDayOfMonth(Calendar JavaDoc cal)
227     {
228         int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
229         cal.set(Calendar.DAY_OF_MONTH, firstDay);
230         return cal;
231     }
232     
233     protected Calendar JavaDoc getLastDayOfMonth(Calendar JavaDoc cal)
234     {
235         int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
236         cal.set(Calendar.DAY_OF_MONTH, lastDay);
237         return cal;
238     }
239     
240     public String JavaDoc computeNextMonthUrl()
241     {
242         // Create yyyyMMdd dates for next month, prev month and today
243
Calendar JavaDoc nextCal = getCalendar();
244         nextCal.setTime( mDay );
245         nextCal.add( Calendar.MONTH, 1 );
246         Date JavaDoc nextMonth = getLastDayOfMonth(nextCal).getTime();
247         String JavaDoc nextMonthUrl = computeUrl(nextMonth, true);
248         return nextMonthUrl;
249     }
250
251     public String JavaDoc computePrevMonthUrl()
252     {
253         Calendar JavaDoc prevCal = getCalendar();
254         prevCal.setTime( mDay );
255         prevCal.add( Calendar.MONTH, -1 );
256         getLastDayOfMonth( prevCal );
257         String JavaDoc prevMonth = computeUrl(prevCal.getTime(),true);
258         return prevMonth;
259     }
260
261     public String JavaDoc computeTodayMonthUrl()
262     {
263         return computeUrl(null,true);
264     }
265 }
266
267
Popular Tags