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.Calendar;
19  import java.util.Date;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.seasar.framework.log.Logger;
28  import org.seasar.tuigwaa.cms.core.CmsRequest;
29  import org.seasar.tuigwaa.cms.core.CmsResponse;
30  import org.seasar.tuigwaa.database.DataRow;
31  import org.seasar.tuigwaa.database.DataTable;
32  import org.seasar.tuigwaa.database.function.DaoMethod;
33  import org.seasar.tuigwaa.database.function.criteria.CriteriaFunction;
34  import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
35  import org.seasar.tuigwaa.database.function.criteria.EqCriteriaFunction;
36  import org.seasar.tuigwaa.model.DAOService;
37  import org.seasar.tuigwaa.model.common.EntityDAO;
38  import org.seasar.tuigwaa.model.core.TgwEntity;
39  import org.seasar.tuigwaa.plugin.AbstractTgwPlugin;
40  import org.seasar.tuigwaa.plugin.PluginException;
41  import org.seasar.tuigwaa.plugin.PluginRequest;
42  import org.seasar.tuigwaa.system.TgwRuntimeException;
43  import org.seasar.tuigwaa.util.TgwContext;
44  
45  import com.isenshi.util.HtmlBuffer;
46  
47  /***
48   * Counter Plugin
49   * 
50   * @author nishioka
51   */
52  public class CounterPlugin extends AbstractTgwPlugin {
53  
54  	private final static Logger log = Logger.getLogger(CounterPlugin.class);
55  
56  	private Map accessCache = new HashMap();
57  
58  	private static final String OPTION_TOTAL = "total";
59  
60  	private static final String OPTION_TODAY = "today";
61  
62  	private static final String OPTION_YESTERDAY = "yesterday";
63  
64  	protected void initialize(CmsRequest request, CmsResponse response,
65  			PluginRequest prequest) throws PluginException {
66  		String siteName = request.getSiteName();
67  		requireEntity(siteName, Counter.class);
68  	}
69  
70  	public String doHTMLView(CmsRequest request, CmsResponse response,
71  			PluginRequest prequest) throws PluginException {
72  		// requireLogic();
73  
74  		String siteName = request.getSiteName();
75  		TgwEntity entity = requireEntity(siteName, Counter.class);
76  
77  		CounterDAO dao = new CounterDAO(entity);
78  
79  		String pagePath = request.getMainPagePath();
80  		if (pagePath == null) {
81  			throw new TgwRuntimeException("ITGW3001");
82  		}
83  
84  		increment(dao, siteName, pagePath);
85  
86  		String option = null;
87  		if (prequest.getArgs() != null && prequest.getArgs().length > 0) {
88  			option = prequest.getArgs()[0];
89  		}
90  
91  		return view(dao, pagePath, option);
92  	}
93  
94  	private String toInt(Counter counter) {
95  		if (counter == null) {
96  			return "0";
97  		}
98  		return "" + counter.getAccessCount();
99  	}
100 
101 	private String view(CounterDAO dao, String pagePath, String option) {
102 
103 		if (option == null) {
104 			Counter total = dao.getTotalCounter(pagePath);
105 			Counter today = dao.getTodayCounter(pagePath);
106 			Counter yesterday = dao.getYesterdayCounter(pagePath);
107 
108 			HtmlBuffer buf = new HtmlBuffer();
109 			buf.appendStartTag("h2");
110 			buf.appendBody(getMessage("counter.header"));
111 			buf.endTag();
112 
113 			buf.appendStartTag("ul");
114 
115 			buf.appendStartTag("li");
116 			buf.appendBody(getMessage("counter.total"));
117 			buf.appendBody(toInt(total));
118 			buf.endTag();
119 
120 			buf.appendStartTag("li");
121 			buf.appendBody(getMessage("counter.today"));
122 			buf.appendBody(toInt(today));
123 			buf.endTag();
124 
125 			buf.appendStartTag("li");
126 			buf.appendBody(getMessage("counter.yesterday"));
127 			buf.appendBody(toInt(yesterday));
128 			buf.endTag();
129 
130 			buf.endAllTags();
131 			return buf.toString();
132 		} else {
133 			Counter counter = null;
134 			if (OPTION_TOTAL.equals(option)) {
135 				counter = dao.getTotalCounter(pagePath);
136 			} else if (OPTION_YESTERDAY.equals(option)) {
137 				counter = dao.getYesterdayCounter(pagePath);
138 			} else if (OPTION_TODAY.equals(option)) {
139 				counter = dao.getTodayCounter(pagePath);
140 			}
141 			return toInt(counter);
142 		}
143 	}
144 
145 	private void increment(CounterDAO dao, String siteName, String pagePath)
146 			throws PluginException {
147 		String sessionId = TgwContext.getSessionId();
148 		siteName = TgwContext.getSiteName();
149 
150 		Set set = (Set) accessCache.get(sessionId);
151 		if (set != null && set.contains(siteName + "/" + pagePath)) {
152 			return;
153 		} else if (set == null) {
154 			set = new HashSet();
155 			accessCache.put(sessionId, set);
156 		}
157 		set.add(siteName + "/" + pagePath);
158 
159 		dao.increment(pagePath);
160 		log.info(pagePath);
161 	}
162 
163 	class CounterDAO {
164 
165 		static final String ACCESS_DATE = "accessDate";
166 
167 		static final String PAGE_NAME = "pageName";
168 
169 		DAOService daoService = (DAOService) getService(DAOService.class);
170 
171 		EntityDAO dao;
172 
173 		TgwEntity entity;
174 
175 		public CounterDAO(TgwEntity entity) {
176 			this.dao = daoService.getDAO(entity);
177 			this.entity = entity;
178 		}
179 
180 		// FIXME: want to use SumProjectionFunction
181 		public Counter getTotalCounter(String pagePath) {
182 			EqCriteriaFunction criteria = new EqCriteriaFunction(PAGE_NAME,
183 					pagePath);
184 			DataTable dataTable = getCounterDataTable(criteria);
185 
186 			Counter counter = (Counter) entity.newInstance();
187 			int total = 0;
188 			while (dataTable.hasNext()) {
189 				DataRow row = (DataRow) dataTable.next();
190 				Counter rowCounter = (Counter) row.getDataObject();
191 				int count = rowCounter.getAccessCount().intValue();
192 				total += count;
193 			}
194 			counter.setPageName(pagePath);
195 			counter.setAccessCount(new Integer(total));
196 			// counter.setAccessDate(new Date());
197 			return counter;
198 		}
199 
200 		public Counter getCounter(String pagePath) {
201 			EqCriteriaFunction func = new EqCriteriaFunction(PAGE_NAME,
202 					pagePath);
203 			return getCounter(func);
204 		}
205 
206 		public Counter getTodayCounter(String pagePath) {
207 			CriteriaListFunction criteria = new CriteriaListFunction();
208 			EqCriteriaFunction page = new EqCriteriaFunction(PAGE_NAME,
209 					pagePath);
210 			EqCriteriaFunction today = new EqCriteriaFunction(ACCESS_DATE,
211 					new Date());
212 
213 			criteria.addFunction(page);
214 			criteria.addFunction(today);
215 
216 			return getCounter(criteria);
217 		}
218 
219 		public Counter getYesterdayCounter(String pagePath) {
220 			CriteriaListFunction criteria = new CriteriaListFunction();
221 			EqCriteriaFunction func = new EqCriteriaFunction(PAGE_NAME,
222 					pagePath);
223 			Calendar yesterday = Calendar.getInstance();
224 			yesterday.roll(Calendar.DATE, -1);
225 			EqCriteriaFunction eqYesterday = new EqCriteriaFunction(
226 					ACCESS_DATE, yesterday.getTime());
227 
228 			criteria.addFunction(func);
229 			criteria.addFunction(eqYesterday);
230 
231 			return getCounter(criteria);
232 		}
233 
234 		public Counter getCounter(CriteriaFunction criteria) {
235 			DataTable dataTable = getCounterDataTable(criteria);
236 			if (dataTable.hasNext()) {
237 				DataRow dataRow = (DataRow) dataTable.next();
238 				return (Counter) dataRow.getDataObject();
239 			}
240 			return null;
241 		}
242 
243 		private DataTable getCounterDataTable(CriteriaFunction criteria) {
244 			DaoMethod method = dao.getMethod(EntityDAO.INJECT_CRITERIA);
245 			return (DataTable) method.evaluate(criteria);
246 		}
247 
248 		public void increment(String pagePath) {
249 			synchronized (dao) {
250 				Counter counter = getTodayCounter(pagePath);
251 				if (counter == null) {
252 					counter = (Counter) entity.newInstance();
253 					counter.setPageName(pagePath);
254 				}
255 				counter.setAccessDate(new Date());
256 				counter.increment();
257 				dao.saveOrUpdate(counter);
258 			}
259 		}
260 	}
261 }