KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > SiteStatManager


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.List JavaDoc;
26
27 import dlog4j.beans.RefererBean;
28 import dlog4j.formbean.LogForm;
29 import dlog4j.formbean.ReplyForm;
30 import dlog4j.formbean.UserForm;
31 import net.sf.hibernate.Criteria;
32 import net.sf.hibernate.HibernateException;
33 import net.sf.hibernate.Query;
34 import net.sf.hibernate.Session;
35 import net.sf.hibernate.expression.Order;
36
37 /**
38  * 用于网站统计的工具类
39  * @author Winter Lau
40  */

41 public class SiteStatManager extends ManagerBase {
42     
43     public final static int SCOPE_ALL = -1;
44     
45     /**
46      * 统计日记数
47      * 例如:
48      * 统计所有日记数 int logCount = statLogs(0, SCOPE_ALL);
49      * 统计昨天日记数 int logCount = statLogs(-1,Calendar.DATE);
50      * 统计本月月日记数 int logCount = statLogs(0,Calendar.MONTH);
51      *
52      * @param inc 增量值
53      * @param scope 范围值
54      * @return 日记数,-1表示统计失败
55      * @throws SQLException
56      * @throws HibernateException
57      */

58     public static int statLogs(int inc, int scope) throws SQLException JavaDoc, HibernateException{
59         Session ssn = null;
60         int log_count = -1;
61         try{
62             ssn = getSession();
63             String JavaDoc hql = "SELECT COUNT(*) FROM "+LogForm.class.getName()+" AS log WHERE log.status=0";
64             Calendar JavaDoc cal_begin = null;
65             Calendar JavaDoc cal_end = null;
66             if(scope != SCOPE_ALL){
67                 cal_begin = Calendar.getInstance();
68                 cal_end = Calendar.getInstance();
69                 if(inc != 0){
70                     cal_begin.add(scope, inc);
71                     cal_end.add(scope, inc);
72                 }
73                 resetCalendar(cal_begin, scope);
74                 resetCalendar(cal_end, scope);
75                 cal_end.add(scope, 1);
76                 hql += " AND log.logTime>=? AND log.logTime<?";
77             }
78             Query q2 = ssn.createQuery(hql);
79             if(scope != SCOPE_ALL){
80                 q2.setCalendar(0, cal_begin);
81                 q2.setCalendar(1, cal_end);
82             }
83             List JavaDoc res = q2.list();
84             log_count = (res.size()>0)?((Integer JavaDoc)res.get(0)).intValue():0;
85         }finally{
86             closeSession(ssn);
87         }
88         return log_count;
89     }
90
91     /**
92      * 统计评论数
93      * 例如:
94      * 统计所有评论数 int logCount = statLogs(0, SCOPE_ALL);
95      * 统计昨天评论数 int logCount = statLogs(-1,Calendar.DATE);
96      * 统计本月月评论数 int logCount = statLogs(0,Calendar.MONTH);
97      *
98      * @param inc 增量值
99      * @param scope 范围值
100      * @return 评论数,-1表示统计失败
101      * @throws SQLException
102      * @throws HibernateException
103      */

104     public static int statReplies(int inc, int scope) throws SQLException JavaDoc, HibernateException{
105         Session ssn = null;
106         int log_count = -1;
107         try{
108             ssn = getSession();
109             String JavaDoc hql = "SELECT COUNT(reply.id) FROM "+ReplyForm.class.getName()+" AS reply WHERE reply.log.status<>" + LogForm.STATUS_DELETED;
110             Calendar JavaDoc cal_begin = null;
111             Calendar JavaDoc cal_end = null;
112             if(scope != SCOPE_ALL){
113                 cal_begin = Calendar.getInstance();
114                 cal_end = Calendar.getInstance();
115                 if(inc != 0){
116                     cal_begin.add(scope, inc);
117                     cal_end.add(scope, inc);
118                 }
119                 resetCalendar(cal_begin, scope);
120                 resetCalendar(cal_end, scope);
121                 cal_end.add(scope, 1);
122                 hql += " AND reply.writeTime>=? AND reply.writeTime<?";
123             }
124             Query q2 = ssn.createQuery(hql);
125             if(scope != SCOPE_ALL){
126                 q2.setCalendar(0, cal_begin);
127                 q2.setCalendar(1, cal_end);
128             }
129             List JavaDoc res = q2.list();
130             log_count = (res.size()>0)?((Integer JavaDoc)res.get(0)).intValue():0;
131         }finally{
132             closeSession(ssn);
133         }
134         return log_count;
135     }
136     
137     /**
138      * 获取第一篇日记
139      * @return
140      * @throws SQLException
141      * @throws HibernateException
142      */

143     public static LogForm getFirstLog() throws SQLException JavaDoc, HibernateException{
144         Session ssn = null;
145         try{
146             ssn = getSession();
147             Criteria crit = ssn.createCriteria(LogForm.class);
148             crit.addOrder(Order.asc("logTime"));
149             crit.setMaxResults(1);
150             List JavaDoc logs = crit.list();
151             if(logs.size()>0)
152                 return (LogForm)logs.get(0);
153         }finally{
154             closeSession(ssn);
155         }
156         return null;
157     }
158
159     /**
160      * 获取第一个访问记录
161      * @return
162      * @throws SQLException
163      * @throws HibernateException
164      */

165     public static RefererBean getFirstVisit() throws SQLException JavaDoc, HibernateException{
166         Session ssn = null;
167         try{
168             ssn = getSession();
169             Criteria crit = ssn.createCriteria(RefererBean.class);
170             crit.addOrder(Order.asc("visitDate"));
171             crit.addOrder(Order.asc("visitTime"));
172             crit.setMaxResults(1);
173             List JavaDoc logs = crit.list();
174             if(logs.size()>0)
175                 return (RefererBean)logs.get(0);
176         }finally{
177             closeSession(ssn);
178         }
179         return null;
180     }
181
182     /**
183      * 统计访问数
184      * 例如:
185      * 统计所有访问数 int logCount = statLogs(0, SCOPE_ALL);
186      * 统计昨天访问数 int logCount = statLogs(-1,Calendar.DATE);
187      * 统计本月月访问数 int logCount = statLogs(0,Calendar.MONTH);
188      *
189      * @param inc 增量值
190      * @param scope 范围值
191      * @return 访问数,-1表示统计失败
192      * @throws SQLException
193      * @throws HibernateException
194      */

195     public static int statTotalVisit(int inc, int scope) throws SQLException JavaDoc, HibernateException{
196         Session ssn = null;
197         int log_count = -1;
198         try{
199             ssn = getSession();
200             String JavaDoc hql = "SELECT COUNT(*) FROM "+RefererBean.class.getName()+" AS log";
201             if(scope != SCOPE_ALL)
202                 hql += " WHERE log.visitDate LIKE ?";
203             Query q2 = ssn.createQuery(hql);
204             if(scope != SCOPE_ALL){
205                 Calendar JavaDoc cal = Calendar.getInstance();
206                 cal.add(scope,inc);
207                 DateFormat JavaDoc fmt = null;
208                 String JavaDoc sDate = null;
209                 switch(scope){
210                 case Calendar.DATE:
211                     fmt = new SimpleDateFormat JavaDoc("yyyyMMdd");
212                     sDate = fmt.format(cal.getTime());
213                     break;
214                 case Calendar.MONTH:
215                     fmt = new SimpleDateFormat JavaDoc("yyyyMM");
216                     sDate = fmt.format(cal.getTime())+'%';
217                     break;
218                 case Calendar.YEAR:
219                     fmt = new SimpleDateFormat JavaDoc("yyyy");
220                     sDate = cal.get(Calendar.YEAR) + "%";
221                     break;
222                 default:
223                     throw new IllegalArgumentException JavaDoc("The value of scope is un-supported.");
224                 }
225                 q2.setString(0, sDate);
226             }
227             List JavaDoc res = q2.list();
228             log_count = (res.size()>0)?((Integer JavaDoc)res.get(0)).intValue():0;
229         }finally{
230             closeSession(ssn);
231         }
232         return log_count;
233     }
234
235     /**
236      * 统计用户访问数
237      * 例如:
238      * 统计所有用户访问数 int logCount = statLogs(0, SCOPE_ALL);
239      * 统计昨天用户访问数 int logCount = statLogs(-1,Calendar.DATE);
240      * 统计本月月用户访问数 int logCount = statLogs(0,Calendar.MONTH);
241      *
242      * @param inc 增量值
243      * @param scope 范围值
244      * @return 用户访问数,-1表示统计失败
245      * @throws SQLException
246      * @throws HibernateException
247      */

248     public static int statUserVisit(int inc, int scope) throws SQLException JavaDoc{
249         Connection JavaDoc conn = null;
250         PreparedStatement JavaDoc ps = null;
251         ResultSet JavaDoc rs = null;
252         int log_count = -1;
253         try{
254             conn = getConnection();
255             String JavaDoc sql = "SELECT distinct remoteAddr,userAgent FROM dlog_visit";
256             if(scope != SCOPE_ALL)
257                 sql += " WHERE visitDate LIKE ?";
258             ps = conn.prepareStatement(sql,
259                     ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
260             if(scope != SCOPE_ALL){
261                 Calendar JavaDoc cal = Calendar.getInstance();
262                 cal.add(scope,inc);
263                 DateFormat JavaDoc fmt = null;
264                 String JavaDoc sDate = null;
265                 switch(scope){
266                 case Calendar.DATE:
267                     fmt = new SimpleDateFormat JavaDoc("yyyyMMdd");
268                     sDate = fmt.format(cal.getTime());
269                     break;
270                 case Calendar.MONTH:
271                     fmt = new SimpleDateFormat JavaDoc("yyyyMM");
272                     sDate = fmt.format(cal.getTime())+'%';
273                     break;
274                 case Calendar.YEAR:
275                     fmt = new SimpleDateFormat JavaDoc("yyyy");
276                     sDate = cal.get(Calendar.YEAR) + "%";
277                     break;
278                 default:
279                     throw new IllegalArgumentException JavaDoc("The value of scope is un-supported.");
280                 }
281                 ps.setString(1, sDate);
282             }
283             rs = ps.executeQuery();
284             rs.last();
285             return rs.getRow();
286         }finally{
287             close(rs,ps,conn);
288         }
289     }
290
291     /**
292      * 计算最高流量的一天
293      * @return
294      * @throws SQLException
295      */

296     public static String JavaDoc[] maxVisit() throws SQLException JavaDoc{
297         String JavaDoc[] values = {"",""};
298         Connection JavaDoc conn = null;
299         PreparedStatement JavaDoc ps = null;
300         ResultSet JavaDoc rs = null;
301         int log_count = -1;
302         try{
303             conn = getConnection();
304             String JavaDoc sql = "SELECT visitDate,COUNT(1) FROM dlog_visit GROUP BY visitDate ORDER BY 2 DESC";
305             ps = conn.prepareStatement(sql);
306             rs = ps.executeQuery();
307             if(rs.next()){
308                 values[0] = rs.getString(1);
309                 values[1] = String.valueOf(rs.getInt(2));
310             }
311         }finally{
312             close(rs,ps,conn);
313         }
314         return values;
315     }
316     
317     /**
318      * 统计用户数
319      * @return 数组第一个数为总用户数,第二个数为当日新增用户数
320      * @throws SQLException
321      * @throws HibernateException
322      */

323     public static int[] statUsers() throws SQLException JavaDoc, HibernateException{
324         Session ssn = null;
325         int[] counts = {0,0};
326         try{
327             ssn = getSession();
328             String JavaDoc hql = "SELECT COUNT(*) FROM "+UserForm.class.getName()+" AS u";
329             Query q2 = ssn.createQuery(hql);
330             List JavaDoc res = q2.list();
331             counts[0] = (res.size()>0)?((Integer JavaDoc)res.get(0)).intValue():0;
332             
333             hql = "SELECT COUNT(*) FROM "+UserForm.class.getName()+" AS u WHERE u.regTime>=?";
334             q2 = ssn.createQuery(hql);
335             Calendar JavaDoc cal = Calendar.getInstance();
336             resetCalendar(cal, Calendar.DATE);
337             q2.setCalendar(0, cal);
338             res = q2.list();
339             counts[1] = (res.size()>0)?((Integer JavaDoc)res.get(0)).intValue():0;
340         }finally{
341             closeSession(ssn);
342         }
343         return counts;
344     }
345     
346     /**
347      * 重置日历的值
348      * @param cal
349      * @param scope
350      */

351     protected static void resetCalendar(Calendar JavaDoc cal, int scope){
352         cal.set(Calendar.MILLISECOND, 0);
353         cal.set(Calendar.SECOND, 0);
354         cal.set(Calendar.MINUTE, 0);
355         cal.set(Calendar.HOUR_OF_DAY, 0);
356         switch(scope){
357         case Calendar.DATE:
358             break;
359         case Calendar.MONTH:
360             cal.set(Calendar.DATE, 1);
361             break;
362         case Calendar.YEAR:
363             cal.set(Calendar.DATE, 1);
364             cal.set(Calendar.MONTH, 0);
365             break;
366         default:
367             throw new IllegalArgumentException JavaDoc("The value of scope is un-supported.");
368         }
369     }
370 }
371
Popular Tags