View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not 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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.plugin.misc;
17  
18  import java.text.ParseException;
19  import java.text.SimpleDateFormat;
20  import java.util.Calendar;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.Date;
24  import java.util.GregorianCalendar;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.seasar.tuigwaa.cms.core.CmsConstants;
29  import org.seasar.tuigwaa.cms.core.CmsRequest;
30  import org.seasar.tuigwaa.cms.core.CmsResponse;
31  import org.seasar.tuigwaa.cms.core.Page;
32  import org.seasar.tuigwaa.cms.core.Resource;
33  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
34  import org.seasar.tuigwaa.plugin.AbstractTgwPlugin;
35  import org.seasar.tuigwaa.plugin.PluginException;
36  import org.seasar.tuigwaa.plugin.PluginRequest;
37  import org.seasar.tuigwaa.plugin.basic.CalendarPlugin;
38  import org.seasar.tuigwaa.system.TgwSecurityException;
39  
40  import com.isenshi.util.CharUtil;
41  import com.isenshi.util.HtmlBuffer;
42  
43  /***
44   * @author someda
45   */
46  public class CalendarviewerPlugin extends AbstractTgwPlugin {
47  
48  	private static final int DEFAULT_NUM = 10;
49  
50  	private static final int MODE_PAST = 1;
51  
52  	private static final int MODE_FUTURE = 2;
53  
54  	private static final int MODE_VIEW = 3;
55  
56  	private static final int DEFAULT_MODE = MODE_PAST;
57  	
58  	private static String FILENAME_FORMAT = getMessage("calendarviewer.datestyle");
59  	private static SimpleDateFormat FILENAME_DATEFORMAT = new SimpleDateFormat(FILENAME_FORMAT);
60  	private static String DISPLAY_FORMAT = getMessage("calendarviewer.displaystyle");
61  	private static SimpleDateFormat FILENAME_DISPLAYFORMAT = new SimpleDateFormat(DISPLAY_FORMAT);
62  
63  	private int mode = DEFAULT_MODE;
64  
65  	public String doHTMLView(CmsRequest request, CmsResponse response,
66  			PluginRequest prequest) throws PluginException {
67  
68  		String[] args = prequest.getArgs();
69  		String pagename = null;
70  
71  		if (args != null && args.length > 0)
72  			pagename = args[0];
73  
74  		if (pagename == null)
75  			pagename = request.getPage().getResource().getPath();
76  
77  		int num = DEFAULT_NUM;
78  		String month = null;
79  		if (args != null && args.length > 1) {
80  			try {
81  				num = Integer.parseInt(args[1]);
82  			} catch (NumberFormatException nfe) {
83  				if (args[1].indexOf("-") != -1)
84  					month = args[1];
85  			}
86  		}
87  
88  		mode = getMode(args);
89  
90  		Calendar cal = new GregorianCalendar();
91  		String today = CmsConstants.CALENDARFORMAT.format(cal.getTime());
92  
93  		WikiContext context = getConfiguration().getWikiContext();
94  
95  		List list;
96  		try {
97  			String path;
98  			int idx;
99  			if ((idx = pagename.lastIndexOf("/")) == -1) {
100 				path = "";
101 			} else {
102 				path = pagename.substring(0, idx);
103 			}
104 
105 			list = context.getResourceList(request, path);
106 		} catch (Exception e) {
107 			throw new PluginException(e);
108 		}
109 		return getHtml(list, context, request, pagename, num, month, today);
110 	}
111 
112 	private String getHtml(List list, WikiContext context, CmsRequest request,
113 			String pagename, int num, String month, String today)
114 			throws PluginException {
115 
116 		if (list == null || list.size() == 0) {
117 			return CharUtil.replace(getMessage("calendarviewer.notfound"),
118 					pagename);
119 		}
120 
121 		String fileprefix = pagename + CalendarPlugin.PAGENAME_APPENDER;
122 
123 
124 		
125 		HtmlBuffer buf = new HtmlBuffer();
126 		buf.appendHeading(2, CharUtil.replace(
127 				getMessage("calendarviewer.list"), pagename));
128 
129 		sortList(list);
130 		for (int i = 0; i < list.size(); i++) {
131 			Resource resource = (Resource) list.get(i);
132 
133 			if (filterByName(list, resource, fileprefix)) {
134 				i--;
135 				continue;
136 			}
137 
138 			String resourcename = resource.getPageName();
139 			int idx = resourcename.indexOf(CalendarPlugin.PAGENAME_APPENDER);
140 			String suffix = resourcename.substring(idx + 1);
141 
142 			if (filterByMonth(list, resource, suffix, month)) {
143 				i--;
144 				continue;
145 			}
146 
147 			if (filterByMode(list, resource, suffix, today, mode)) {
148 				i--;
149 				continue;
150 			}
151 		}
152 		
153 		if (month == null)
154 			filterByNum(list, num);
155 
156 		for (Iterator i = list.iterator(); i.hasNext();) {
157 			Resource res = (Resource) i.next();
158 			String pagepath = res.getPath();
159 
160 			assertIncludePage(request, pagepath);
161 
162 			request.pushScope();
163 			Page page = getPage(request, pagepath);
164 			request.popScope();
165 
166 			String pageDate = pagepath.replaceAll(fileprefix, "");
167 			try{
168 				Date date = FILENAME_DATEFORMAT.parse(pageDate);
169 				pageDate = FILENAME_DISPLAYFORMAT.format(date);
170 			}catch(ParseException pe){
171 				// do nothing
172 			}			
173 			
174 			try{
175 				String uri = context.getURLByName(pagepath, request).toString();
176 				
177 				buf.appendStartTag("h3");
178 				buf.appendAnchor(uri, pageDate);
179 				buf.endTag();
180 			}catch(TgwSecurityException tse){
181 				// do-nothing security link
182 			}
183 
184 			buf.appendBody(page.getContent().toString());
185 		}
186 		return buf.toString();
187 	}
188 
189 	// ----- [Start] private methods -----
190 
191 	private void sortList(List list) {
192 		// sort depending on view-mode
193 		Collections.sort(list, new Comparator() {
194 			public int compare(Object left, Object right) {
195 
196 				Resource res1 = (Resource) left;
197 				Resource res2 = (Resource) right;
198 
199 				if (res1.isFolder() && res2.isFile()) {
200 					return -1;
201 				} else if (res1.isFile() && res2.isFolder()) {
202 					return 1;
203 				} else {
204 					int weight = 1;
205 					if (mode == MODE_PAST || mode == MODE_VIEW)
206 						weight = -1;
207 					return weight * res1.getPath().compareTo(res2.getPath());
208 				}
209 			}
210 		});
211 	}
212 
213 	private boolean filterByName(List list, Resource resource, String name) {
214 		boolean filtered = false;
215 		if (resource.getPath().indexOf(name) == -1) {
216 			list.remove(resource);
217 			filtered = true;
218 		}
219 		return filtered;
220 	}
221 
222 	private boolean filterByMonth(List list, Resource resource, String suffix,
223 			String month) {
224 		if (month == null)
225 			return false;
226 
227 		boolean filtered = false;
228 		if (!suffix.startsWith(month)) {
229 			list.remove(resource);
230 			filtered = true;
231 		}
232 		return filtered;
233 	}
234 
235 	private boolean filterByMode(List list, Resource resource, String suffix,
236 			String today, int mode) {
237 		if (mode == MODE_VIEW)
238 			return false;
239 
240 		boolean filtered = false;
241 		if (mode == MODE_PAST) {
242 			if (suffix.compareTo(today) > 0)
243 				filtered = true;
244 		} else if (mode == MODE_FUTURE) {
245 			if (suffix.compareTo(today) < 0)
246 				filtered = true;
247 		}
248 		if (filtered)
249 			list.remove(resource);
250 		return filtered;
251 	}
252 
253 	private boolean filterByNum(List list, int num) {
254 		boolean filtered = false;
255 		while (list.size() > num) {
256 			filtered = true;
257 			list.remove(list.size() - 1);
258 		}
259 		return filtered;
260 	}
261 
262 	// ----- [End] private methods -----
263 
264 	// ----- [Start] static methods -----
265 	private static int getMode(String[] args) {
266 		int m = DEFAULT_MODE;
267 		if (args != null && args.length > 2) {
268 			if (args[2].equals("past")) {
269 				m = MODE_PAST;
270 			} else if (args[2].equals("future")) {
271 				m = MODE_FUTURE;
272 			} else if (args[2].equals("view")) {
273 				m = MODE_VIEW;
274 			}
275 		}
276 		return m;
277 	}
278 }