Source code: org/roller/presentation/weblog/tags/WeblogCalendarModel.java
1
2 package org.roller.presentation.weblog.tags;
3
4 import org.roller.presentation.RollerRequest;
5 import org.roller.presentation.tags.calendar.CalendarModel;
6
7 import java.text.ParsePosition;
8 import java.text.SimpleDateFormat;
9 import java.util.Calendar;
10 import java.util.Date;
11 import java.util.Map;
12
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 /**
17 * Calendar model for calendar intended for use on view-weblog page.
18 */
19 public class WeblogCalendarModel implements CalendarModel
20 {
21 protected HttpServletRequest mReq = null;
22 protected HttpServletResponse mRes = null;
23 protected Map mMonthMap;
24 protected String mSelfUrl;
25 protected Date mDay;
26 protected String mCatName = null;
27
28 public WeblogCalendarModel(
29 HttpServletRequest req, HttpServletResponse res, String url)
30 {
31 mReq = req;
32 mRes = res;
33 mSelfUrl = url;
34
35 // If category is specified in URL, then perpetuate it
36 String catKey = RollerRequest.WEBLOGCATEGORYNAME_KEY;
37 mCatName = mReq.getParameter(catKey);
38 if ( mCatName != null )
39 {
40 mCatName = "?"+catKey+"="+mCatName;
41 }
42 else
43 {
44 mCatName = "";
45 }
46 }
47
48 public static WeblogCalendarModel getInstance(
49 HttpServletRequest req, HttpServletResponse res, String url)
50 {
51 return new WeblogCalendarModel(req, res, url);
52 }
53
54 public void setDay(Date month) throws Exception
55 {
56 mDay = month;
57
58 // If category is specified in URL, then use it
59 String catName =
60 mReq.getParameter(RollerRequest.WEBLOGCATEGORYNAME_KEY);
61
62 // we want dayOnly and pubOnly
63 mMonthMap = RollerRequest.getRollerRequest(mReq)
64 .getWeblogEntryMonthMap( month, catName, true, true );
65 }
66
67 public void setDay(String month) throws Exception
68 {
69 SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
70 ParsePosition pos = new ParsePosition(0);
71 setDay( fmt.parse( month, pos ) );
72 }
73
74 public Date getDay()
75 {
76 return mDay;
77 }
78
79 public String getSelfUrl() throws Exception
80 {
81 return mRes.encodeURL(mSelfUrl);
82 }
83
84 public String getTargetUrl() throws Exception
85 {
86 return getSelfUrl();
87 }
88
89 public String getParameterName()
90 {
91 return RollerRequest.WEBLOGDAY_KEY;
92 }
93
94 public String getParameterValue( Date day )
95 {
96 return (String)mMonthMap.get( day );
97 }
98
99 /**
100 * Create URL for use on view-weblog page, ignores query-string.
101 * @param day Day for URL
102 * @param valid Always return a URL, never return null
103 * @return URL for day, or null if no weblog entry on that day
104 */
105 public String computeUrl(java.util.Date day, boolean valid)
106 {
107 String url = null;
108 RollerRequest rreq = RollerRequest.getRollerRequest(mReq);
109 try
110 {
111 if ( day == null )
112 {
113 url = mRes.encodeURL(mSelfUrl + mCatName);
114 }
115 else
116 {
117 // get the 8 char YYYYMMDD datestring for day, returns null
118 // if no weblog entry on that day
119 String dateString = (String)mMonthMap.get( day );
120 if ( dateString != null )
121 {
122 // append 8 char date string on end of selfurl
123 url = mRes.encodeURL(mSelfUrl+"/"+dateString+mCatName);
124 }
125 else if ( valid )
126 {
127 // Make the date yyyyMMdd and append it to URL
128 java.text.SimpleDateFormat format8chars =
129 new java.text.SimpleDateFormat("yyyyMMdd");
130 dateString = format8chars.format( day );
131 url = mRes.encodeURL( mSelfUrl+"/"+dateString+mCatName);
132 }
133 }
134 }
135 catch (Exception e)
136 {
137 rreq.getServletContext().log("ERROR: creating URL",e);
138 }
139 return url;
140 }
141
142 /**
143 * @see org.roller.presentation.tags.calendar.CalendarModel#getContent(Date, boolean)
144 */
145 public String getContent(Date day)
146 {
147 return null;
148 }
149
150 public String computeNextMonthUrl()
151 {
152 // Create yyyyMMdd dates for next month, prev month and today
153 Calendar nextCal = Calendar.getInstance();
154 nextCal.setTime( mDay );
155 nextCal.add( Calendar.MONTH, 1 );
156 String nextMonth = computeUrl(nextCal.getTime(),true);
157 return nextMonth;
158 }
159
160 public String computePrevMonthUrl()
161 {
162 Calendar prevCal = Calendar.getInstance();
163 prevCal.setTime( mDay );
164 prevCal.add( Calendar.MONTH, -1 );
165 String prevMonth = computeUrl(prevCal.getTime(),true);
166 return prevMonth;
167 }
168
169 public String computeTodayMonthUrl()
170 {
171 return computeUrl(null,true);
172 }
173 }
174