1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.plugin.basic;
17
18 import java.net.URL;
19 import java.util.ArrayList;
20 import java.util.Calendar;
21 import java.util.GregorianCalendar;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.seasar.tuigwaa.cms.core.CmsConstants;
27 import org.seasar.tuigwaa.cms.core.CmsRequest;
28 import org.seasar.tuigwaa.cms.core.CmsResponse;
29 import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
30 import org.seasar.tuigwaa.plugin.AbstractPlugin;
31 import org.seasar.tuigwaa.plugin.PluginException;
32 import org.seasar.tuigwaa.plugin.PluginRequest;
33 import org.seasar.tuigwaa.system.Constants;
34 import org.seasar.tuigwaa.system.TgwSecurityException;
35
36 import com.isenshi.util.CharUtil;
37 import com.isenshi.util.HtmlBuffer;
38
39 /***
40 * @author someda
41 */
42 public class CalendarPlugin extends AbstractPlugin {
43
44 public static final String PAGENAME_APPENDER = "_";
45
46 private static final String CALENDAR_MONTH = getParameterName("calendar.month");
47
48 private static final String CALENDAR_DATE = getParameterName("calendar.date");
49
50 private static final String CALENDAR_PAGENAME = getParameterName("calendar.pagename");
51
52 private static final String PREVIOUS_MONTH = "←";
53
54 private static final String NEXT_MONTH = "→";
55
56 private static List weeklist = new ArrayList();
57
58 private static Map headermap = new HashMap();
59
60 private static Map titlemap = new HashMap();
61
62 private static Map pagenamemap = new HashMap();
63
64 static {
65 String s = getMessage("calendar.weeklist");
66 String[] week = s.split(",");
67
68 for (int i = 0; i < week.length; i++) {
69 if (i == 0) {
70 weeklist.add("<span style=\"color:red;\">" + week[i]
71 + "</span>");
72 } else if (i == week.length - 1) {
73 weeklist.add("<span style=\"color:blue;\">" + week[i]
74 + "</span>");
75 } else {
76 weeklist.add(week[i]);
77 }
78 }
79 headermap.put("colspan", "2");
80 titlemap.put("colspan", "3");
81 pagenamemap.put("colspan", "7");
82 pagenamemap.put("class", "caltitle");
83 }
84
85 public String doHTMLView(CmsRequest request, CmsResponse response,
86 PluginRequest prequest) throws PluginException {
87
88 String month = request.getParameter(CALENDAR_MONTH);
89 String date = request.getParameter(CALENDAR_DATE);
90 String reqpagename = request.getParameter(CALENDAR_PAGENAME);
91
92 String[] args = prequest.getArgs();
93 String pagename = null;
94
95 WikiContext context = getConfiguration().getWikiContext();
96
97 if(existArg(args,0)){
98 pagename = args[0];
99 }else{
100 pagename = request.getMainPagePath();
101 }
102
103 Calendar today = new GregorianCalendar();
104 Calendar cal = new GregorianCalendar();
105
106 if (month != null && reqpagename != null
107 && pagename.equals(reqpagename)) {
108 String year = month.substring(0, 4);
109 String mon = month.substring(5);
110 cal.set(Calendar.YEAR, Integer.parseInt(year));
111 cal.set(Calendar.MONTH, Integer.parseInt(mon) - 1);
112 }
113 cal.set(Calendar.DATE, 1);
114
115
116 URL monthpath = null;
117 try {
118 monthpath = context
119 .getURLByName(request.getMainPagePath(), request);
120 } catch (TgwSecurityException tse) {
121
122 return "";
123 }
124
125 HtmlBuffer buf = new HtmlBuffer();
126 appendTableHeader(buf, pagename, cal, monthpath);
127
128 boolean firstweek = true;
129
130
131 while (true) {
132 buf.appendStartTag(HtmlBuffer.TAG_TR);
133 for (int i = 1; i <= 7; i++) {
134 if ((firstweek && i < cal.get(Calendar.DAY_OF_WEEK))
135 || (!firstweek && cal.get(Calendar.DATE) == 1)) {
136 buf.appendTd(" ");
137 } else {
138 String datepath = pagename + PAGENAME_APPENDER
139 + CmsConstants.CALENDARFORMAT.format(cal.getTime());
140 String day = String.valueOf(cal.get(Calendar.DATE));
141
142 buf.appendStartTag(HtmlBuffer.TAG_TD);
143 appendWeekAttribute(buf, i, cal, today);
144
145 if (date != null
146 && Integer.parseInt(date) == cal.get(Calendar.DATE)
147 && (reqpagename == null || pagename
148 .equals(reqpagename))) {
149 buf.appendStartTag("span");
150 buf.appendAttribute("class", "current");
151 buf.appendBody(day);
152 buf.endTag();
153 } else {
154 URL url = null;
155 try {
156 if (context.isPageExist(datepath, request)) {
157 buf.appendStartTag("span");
158 buf.appendAttribute("class", "calexist");
159 url = context.getURLByName(datepath, request);
160 appendDayAnchor(buf, url, day, pagename, false,
161 cal);
162 buf.endTag();
163 } else {
164 url = context.getCreatePageURL(datepath,
165 request);
166 appendDayAnchor(buf, url, day, pagename, true,
167 cal);
168 }
169 } catch (TgwSecurityException tse) {
170 buf.appendBody(day);
171 }
172 }
173 buf.endTag();
174 cal.add(Calendar.DATE, 1);
175 }
176 }
177 firstweek = false;
178 buf.endTag();
179 if (cal.get(Calendar.DATE) == 1)
180 break;
181 }
182 buf.endAllTags();
183 return buf.toString();
184 }
185
186 private void appendTableHeader(HtmlBuffer buf, String pagename,
187 Calendar cal, URL monthpath) {
188
189 cal.add(Calendar.MONTH, -1);
190 String prevmonth = getArrowURLString(monthpath, cal, pagename);
191 cal.add(Calendar.MONTH, 1);
192 String cur = getCalendarMonth(cal);
193 cal.add(Calendar.MONTH, 1);
194 String nextmonth = getArrowURLString(monthpath, cal, pagename);
195
196 cal.add(Calendar.MONTH, -1);
197
198
199
200 cal.add(Calendar.YEAR, 1);
201 String nextyear = getArrowURLString(monthpath, cal, pagename);
202 cal.add(Calendar.YEAR, -2);
203 String prevyear = getArrowURLString(monthpath, cal, pagename);
204
205 cal.add(Calendar.YEAR, 1);
206
207
208 HtmlBuffer prevanchor = new HtmlBuffer();
209 prevanchor.appendAnchor(prevmonth, PREVIOUS_MONTH);
210 HtmlBuffer nextanchor = new HtmlBuffer();
211 nextanchor.appendAnchor(nextmonth, NEXT_MONTH);
212
213
214 HtmlBuffer prevyearanchor = new HtmlBuffer();
215 prevyearanchor.appendAnchor(prevyear, "↓");
216 HtmlBuffer nextyearanchor = new HtmlBuffer();
217 nextyearanchor.appendAnchor(nextyear, "↑");
218
219 buf.appendStartTag("table");
220 buf.appendAttribute("class", "calendar");
221
222 buf.appendStartTag(HtmlBuffer.TAG_TR);
223 buf.appendTd(pagename, pagenamemap, false);
224 buf.endTag();
225
226 buf.appendStartTag(HtmlBuffer.TAG_TR);
227 buf.appendTd(prevanchor.toString() + prevyearanchor.toString(),
228 headermap, false);
229 buf.appendTd(cur, titlemap, false);
230 buf.appendTd(nextyearanchor.toString() + nextanchor.toString(),
231 headermap, false);
232 buf.endTag();
233
234 buf.appendTr(weeklist.iterator(), true);
235 }
236
237
238 private void appendWeekAttribute(HtmlBuffer buf, int idx, Calendar cal,
239 Calendar today) {
240 if (idx == 1) {
241 buf.appendAttribute("class", "calsunday");
242 } else if (idx == 7) {
243 buf.appendAttribute("class", "calsaturday");
244 } else if (isSameDate(cal, today)) {
245 buf.appendAttribute("class", "today");
246 }
247 }
248
249 private void appendDayAnchor(HtmlBuffer buf, URL url, String day,
250 String pagename, boolean createPage, Calendar cal) {
251 StringBuffer href = new StringBuffer();
252 href.append(url.toString());
253
254 if(createPage){
255 href.append("&");
256 }else{
257 href.append("?");
258 }
259 href.append(CALENDAR_DATE + "=" + day);
260
261 if (pagename != null)
262 href.append("&" + CALENDAR_PAGENAME + "="
263 + CharUtil.urlEncode(pagename));
264
265 if (createPage) {
266 href.append("&" + Constants.PARAM_PREFIX_PAGESETTINGS
267 + CmsConstants.PROPERTY_HIDDEN
268 + Constants.PARAM_SUFFIX_PAGESETTINGS + "=true");
269 } else {
270 href.append("&" + CALENDAR_MONTH + "="
271 + CharUtil.urlEncode(getCalendarMonth(cal)));
272 }
273 buf.appendAnchor(href.toString(), day);
274 }
275
276 private boolean isSameDate(Calendar cal1, Calendar cal2) {
277
278 return (cal1.get(Calendar.DATE) == cal2.get(Calendar.DATE)
279 && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1
280 .get(Calendar.YEAR) == cal2.get(Calendar.YEAR));
281 }
282
283 private String getCalendarMonth(Calendar cal) {
284 return cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1);
285 }
286
287 private String getArrowURLString(URL url, Calendar cal, String pagename) {
288 return url.toString() + "?" + CALENDAR_MONTH + "="
289 + CharUtil.urlEncode(getCalendarMonth(cal)) + "&"
290 + CALENDAR_PAGENAME + "=" + CharUtil.urlEncode(pagename);
291 }
292 }