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.database;
17  
18  import java.util.Collection;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.seasar.tuigwaa.cms.core.CmsRequest;
23  import org.seasar.tuigwaa.cms.core.CmsResponse;
24  import org.seasar.tuigwaa.database.DataTable;
25  import org.seasar.tuigwaa.model.common.EntityUtils;
26  import org.seasar.tuigwaa.model.core.TgwEntity;
27  import org.seasar.tuigwaa.plugin.PluginException;
28  import org.seasar.tuigwaa.plugin.PluginRequest;
29  import org.seasar.tuigwaa.plugin.basic.VelocityPlugin;
30  import org.seasar.tuigwaa.util.TgwContext;
31  
32  /***
33   * @author someda
34   */
35  public class CommentView extends VelocityPlugin {
36  
37  	private Log log = LogFactory.getLog(getClass());
38  
39  	private static final int DEFAULT_DISPLAY_NUMBER = 10;
40  
41  	protected void initialize(CmsRequest request, CmsResponse response,
42  			PluginRequest prequest) throws PluginException {
43  		String siteName = request.getSiteName();
44  		requireEntity(siteName, Comment.class);
45  		putPath(prequest.getName(), getClass());
46  	}
47  
48  	public String doHTMLView(CmsRequest request, CmsResponse response,
49  			PluginRequest prequest) throws PluginException {
50  
51  		String siteName = TgwContext.getSiteName();
52  		String pagePath = request.getPage().getResource().getPath();
53  
54  		String[] args = prequest.getArgs();
55  		int maxSize = parseMaxSize(args);
56  
57  		String extraKey = getExtraKey(request, args);
58  
59  		CommentDao dao = (CommentDao) getDao();
60  		DataTable dataTable = dao.getComments(siteName, pagePath, maxSize,
61  				extraKey);
62  
63  		CommentInfo info = new CommentInfo(dataTable.getDataList(), extraKey);
64  
65  		return doHTMLViewBindingObject(request, response, prequest, info);
66  	}
67  
68  	private String getExtraKey(CmsRequest req, String[] args) {
69  		if (args == null || args.length <= 1) {
70  			return "";
71  		}
72  
73  		String siteName = req.getSiteName();
74  		String entityDisplayName = args[1];
75  		TgwEntity entity = getEntity(siteName, entityDisplayName);
76  		Object bean = getBean(req, entity);
77  
78  		if (bean != null) {
79  			return "" + EntityUtils.getId(bean);
80  		}
81  		return "";
82  
83  	}
84  
85  	private int parseMaxSize(String[] args) {
86  		int number = DEFAULT_DISPLAY_NUMBER;
87  		if (args != null) {
88  			try {
89  				if (args[0].length() == 0) {
90  					number = -1;
91  				} else {
92  					number = Integer.parseInt(args[0]);
93  				}
94  			} catch (NumberFormatException nfe) {
95  				log.error("argment invalid and ignore it");
96  				number = DEFAULT_DISPLAY_NUMBER;
97  			}
98  		}
99  		return number;
100 	} 
101 
102 	public static class CommentInfo {
103 
104 		private Collection dataList;
105 
106 		private String extraKey = "";
107 
108 		public CommentInfo(Collection table, String key) {
109 			dataList = table;
110 			extraKey = key;
111 		}
112 
113 		public Collection getDataList() {
114 			return dataList;
115 		}
116 
117 		public String getExtraKey() {
118 			return extraKey;
119 		}
120 	}
121 }