Source Code Cross Referenced for SiteStatManager.java in  » Portal » DLOG4J » dlog4j » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » DLOG4J » dlog4j 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  This program is free software; you can redistribute it and/or modify
003:         *  it under the terms of the GNU General Public License as published by
004:         *  the Free Software Foundation; either version 2 of the License, or
005:         *  (at your option) any later version.
006:         *
007:         *  This program is distributed in the hope that it will be useful,
008:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
009:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010:         *  GNU Library General Public License for more details.
011:         *
012:         *  You should have received a copy of the GNU General Public License
013:         *  along with this program; if not, write to the Free Software
014:         *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015:         */
016:        package dlog4j;
017:
018:        import java.sql.Connection;
019:        import java.sql.PreparedStatement;
020:        import java.sql.ResultSet;
021:        import java.sql.SQLException;
022:        import java.text.DateFormat;
023:        import java.text.SimpleDateFormat;
024:        import java.util.Calendar;
025:        import java.util.List;
026:
027:        import dlog4j.beans.RefererBean;
028:        import dlog4j.formbean.LogForm;
029:        import dlog4j.formbean.ReplyForm;
030:        import dlog4j.formbean.UserForm;
031:        import net.sf.hibernate.Criteria;
032:        import net.sf.hibernate.HibernateException;
033:        import net.sf.hibernate.Query;
034:        import net.sf.hibernate.Session;
035:        import net.sf.hibernate.expression.Order;
036:
037:        /**
038:         * 用于网站统计的工具类
039:         * @author Winter Lau
040:         */
041:        public class SiteStatManager extends ManagerBase {
042:
043:            public final static int SCOPE_ALL = -1;
044:
045:            /**
046:             * 统计日记数
047:             * 例如:
048:             * 统计所有日记数 int logCount = statLogs(0, SCOPE_ALL);
049:             * 统计昨天日记数 int logCount = statLogs(-1,Calendar.DATE);
050:             * 统计本月月日记数 int logCount = statLogs(0,Calendar.MONTH);
051:             * 
052:             * @param inc	增量值
053:             * @param scope	范围值
054:             * @return 日记数,-1表示统计失败
055:             * @throws SQLException
056:             * @throws HibernateException
057:             */
058:            public static int statLogs(int inc, int scope) throws SQLException,
059:                    HibernateException {
060:                Session ssn = null;
061:                int log_count = -1;
062:                try {
063:                    ssn = getSession();
064:                    String hql = "SELECT COUNT(*) FROM "
065:                            + LogForm.class.getName()
066:                            + " AS log WHERE log.status=0";
067:                    Calendar cal_begin = null;
068:                    Calendar cal_end = null;
069:                    if (scope != SCOPE_ALL) {
070:                        cal_begin = Calendar.getInstance();
071:                        cal_end = Calendar.getInstance();
072:                        if (inc != 0) {
073:                            cal_begin.add(scope, inc);
074:                            cal_end.add(scope, inc);
075:                        }
076:                        resetCalendar(cal_begin, scope);
077:                        resetCalendar(cal_end, scope);
078:                        cal_end.add(scope, 1);
079:                        hql += " AND log.logTime>=? AND log.logTime<?";
080:                    }
081:                    Query q2 = ssn.createQuery(hql);
082:                    if (scope != SCOPE_ALL) {
083:                        q2.setCalendar(0, cal_begin);
084:                        q2.setCalendar(1, cal_end);
085:                    }
086:                    List res = q2.list();
087:                    log_count = (res.size() > 0) ? ((Integer) res.get(0))
088:                            .intValue() : 0;
089:                } finally {
090:                    closeSession(ssn);
091:                }
092:                return log_count;
093:            }
094:
095:            /**
096:             * 统计评论数
097:             * 例如:
098:             * 统计所有评论数 int logCount = statLogs(0, SCOPE_ALL);
099:             * 统计昨天评论数 int logCount = statLogs(-1,Calendar.DATE);
100:             * 统计本月月评论数 int logCount = statLogs(0,Calendar.MONTH);
101:             * 
102:             * @param inc	增量值
103:             * @param scope	范围值
104:             * @return 评论数,-1表示统计失败
105:             * @throws SQLException
106:             * @throws HibernateException
107:             */
108:            public static int statReplies(int inc, int scope)
109:                    throws SQLException, HibernateException {
110:                Session ssn = null;
111:                int log_count = -1;
112:                try {
113:                    ssn = getSession();
114:                    String hql = "SELECT COUNT(reply.id) FROM "
115:                            + ReplyForm.class.getName()
116:                            + " AS reply WHERE reply.log.status<>"
117:                            + LogForm.STATUS_DELETED;
118:                    Calendar cal_begin = null;
119:                    Calendar cal_end = null;
120:                    if (scope != SCOPE_ALL) {
121:                        cal_begin = Calendar.getInstance();
122:                        cal_end = Calendar.getInstance();
123:                        if (inc != 0) {
124:                            cal_begin.add(scope, inc);
125:                            cal_end.add(scope, inc);
126:                        }
127:                        resetCalendar(cal_begin, scope);
128:                        resetCalendar(cal_end, scope);
129:                        cal_end.add(scope, 1);
130:                        hql += " AND reply.writeTime>=? AND reply.writeTime<?";
131:                    }
132:                    Query q2 = ssn.createQuery(hql);
133:                    if (scope != SCOPE_ALL) {
134:                        q2.setCalendar(0, cal_begin);
135:                        q2.setCalendar(1, cal_end);
136:                    }
137:                    List res = q2.list();
138:                    log_count = (res.size() > 0) ? ((Integer) res.get(0))
139:                            .intValue() : 0;
140:                } finally {
141:                    closeSession(ssn);
142:                }
143:                return log_count;
144:            }
145:
146:            /**
147:             * 获取第一篇日记
148:             * @return
149:             * @throws SQLException
150:             * @throws HibernateException
151:             */
152:            public static LogForm getFirstLog() throws SQLException,
153:                    HibernateException {
154:                Session ssn = null;
155:                try {
156:                    ssn = getSession();
157:                    Criteria crit = ssn.createCriteria(LogForm.class);
158:                    crit.addOrder(Order.asc("logTime"));
159:                    crit.setMaxResults(1);
160:                    List logs = crit.list();
161:                    if (logs.size() > 0)
162:                        return (LogForm) logs.get(0);
163:                } finally {
164:                    closeSession(ssn);
165:                }
166:                return null;
167:            }
168:
169:            /**
170:             * 获取第一个访问记录
171:             * @return
172:             * @throws SQLException
173:             * @throws HibernateException
174:             */
175:            public static RefererBean getFirstVisit() throws SQLException,
176:                    HibernateException {
177:                Session ssn = null;
178:                try {
179:                    ssn = getSession();
180:                    Criteria crit = ssn.createCriteria(RefererBean.class);
181:                    crit.addOrder(Order.asc("visitDate"));
182:                    crit.addOrder(Order.asc("visitTime"));
183:                    crit.setMaxResults(1);
184:                    List logs = crit.list();
185:                    if (logs.size() > 0)
186:                        return (RefererBean) logs.get(0);
187:                } finally {
188:                    closeSession(ssn);
189:                }
190:                return null;
191:            }
192:
193:            /**
194:             * 统计访问数
195:             * 例如:
196:             * 统计所有访问数 int logCount = statLogs(0, SCOPE_ALL);
197:             * 统计昨天访问数 int logCount = statLogs(-1,Calendar.DATE);
198:             * 统计本月月访问数 int logCount = statLogs(0,Calendar.MONTH);
199:             * 
200:             * @param inc	增量值
201:             * @param scope	范围值
202:             * @return 访问数,-1表示统计失败
203:             * @throws SQLException
204:             * @throws HibernateException
205:             */
206:            public static int statTotalVisit(int inc, int scope)
207:                    throws SQLException, HibernateException {
208:                Session ssn = null;
209:                int log_count = -1;
210:                try {
211:                    ssn = getSession();
212:                    String hql = "SELECT COUNT(*) FROM "
213:                            + RefererBean.class.getName() + " AS log";
214:                    if (scope != SCOPE_ALL)
215:                        hql += " WHERE log.visitDate LIKE ?";
216:                    Query q2 = ssn.createQuery(hql);
217:                    if (scope != SCOPE_ALL) {
218:                        Calendar cal = Calendar.getInstance();
219:                        cal.add(scope, inc);
220:                        DateFormat fmt = null;
221:                        String sDate = null;
222:                        switch (scope) {
223:                        case Calendar.DATE:
224:                            fmt = new SimpleDateFormat("yyyyMMdd");
225:                            sDate = fmt.format(cal.getTime());
226:                            break;
227:                        case Calendar.MONTH:
228:                            fmt = new SimpleDateFormat("yyyyMM");
229:                            sDate = fmt.format(cal.getTime()) + '%';
230:                            break;
231:                        case Calendar.YEAR:
232:                            fmt = new SimpleDateFormat("yyyy");
233:                            sDate = cal.get(Calendar.YEAR) + "%";
234:                            break;
235:                        default:
236:                            throw new IllegalArgumentException(
237:                                    "The value of scope is un-supported.");
238:                        }
239:                        q2.setString(0, sDate);
240:                    }
241:                    List res = q2.list();
242:                    log_count = (res.size() > 0) ? ((Integer) res.get(0))
243:                            .intValue() : 0;
244:                } finally {
245:                    closeSession(ssn);
246:                }
247:                return log_count;
248:            }
249:
250:            /**
251:             * 统计用户访问数
252:             * 例如:
253:             * 统计所有用户访问数 int logCount = statLogs(0, SCOPE_ALL);
254:             * 统计昨天用户访问数 int logCount = statLogs(-1,Calendar.DATE);
255:             * 统计本月月用户访问数 int logCount = statLogs(0,Calendar.MONTH);
256:             * 
257:             * @param inc	增量值
258:             * @param scope	范围值
259:             * @return 用户访问数,-1表示统计失败
260:             * @throws SQLException
261:             * @throws HibernateException
262:             */
263:            public static int statUserVisit(int inc, int scope)
264:                    throws SQLException {
265:                Connection conn = null;
266:                PreparedStatement ps = null;
267:                ResultSet rs = null;
268:                int log_count = -1;
269:                try {
270:                    conn = getConnection();
271:                    String sql = "SELECT distinct remoteAddr,userAgent FROM dlog_visit";
272:                    if (scope != SCOPE_ALL)
273:                        sql += " WHERE visitDate LIKE ?";
274:                    ps = conn.prepareStatement(sql,
275:                            ResultSet.TYPE_SCROLL_INSENSITIVE,
276:                            ResultSet.CONCUR_READ_ONLY);
277:                    if (scope != SCOPE_ALL) {
278:                        Calendar cal = Calendar.getInstance();
279:                        cal.add(scope, inc);
280:                        DateFormat fmt = null;
281:                        String sDate = null;
282:                        switch (scope) {
283:                        case Calendar.DATE:
284:                            fmt = new SimpleDateFormat("yyyyMMdd");
285:                            sDate = fmt.format(cal.getTime());
286:                            break;
287:                        case Calendar.MONTH:
288:                            fmt = new SimpleDateFormat("yyyyMM");
289:                            sDate = fmt.format(cal.getTime()) + '%';
290:                            break;
291:                        case Calendar.YEAR:
292:                            fmt = new SimpleDateFormat("yyyy");
293:                            sDate = cal.get(Calendar.YEAR) + "%";
294:                            break;
295:                        default:
296:                            throw new IllegalArgumentException(
297:                                    "The value of scope is un-supported.");
298:                        }
299:                        ps.setString(1, sDate);
300:                    }
301:                    rs = ps.executeQuery();
302:                    rs.last();
303:                    return rs.getRow();
304:                } finally {
305:                    close(rs, ps, conn);
306:                }
307:            }
308:
309:            /**
310:             * 计算最高流量的一天
311:             * @return
312:             * @throws SQLException
313:             */
314:            public static String[] maxVisit() throws SQLException {
315:                String[] values = { "", "" };
316:                Connection conn = null;
317:                PreparedStatement ps = null;
318:                ResultSet rs = null;
319:                int log_count = -1;
320:                try {
321:                    conn = getConnection();
322:                    String sql = "SELECT visitDate,COUNT(1) FROM dlog_visit GROUP BY visitDate ORDER BY 2 DESC";
323:                    ps = conn.prepareStatement(sql);
324:                    rs = ps.executeQuery();
325:                    if (rs.next()) {
326:                        values[0] = rs.getString(1);
327:                        values[1] = String.valueOf(rs.getInt(2));
328:                    }
329:                } finally {
330:                    close(rs, ps, conn);
331:                }
332:                return values;
333:            }
334:
335:            /**
336:             * 统计用户数
337:             * @return 数组第一个数为总用户数,第二个数为当日新增用户数
338:             * @throws SQLException
339:             * @throws HibernateException
340:             */
341:            public static int[] statUsers() throws SQLException,
342:                    HibernateException {
343:                Session ssn = null;
344:                int[] counts = { 0, 0 };
345:                try {
346:                    ssn = getSession();
347:                    String hql = "SELECT COUNT(*) FROM "
348:                            + UserForm.class.getName() + " AS u";
349:                    Query q2 = ssn.createQuery(hql);
350:                    List res = q2.list();
351:                    counts[0] = (res.size() > 0) ? ((Integer) res.get(0))
352:                            .intValue() : 0;
353:
354:                    hql = "SELECT COUNT(*) FROM " + UserForm.class.getName()
355:                            + " AS u WHERE u.regTime>=?";
356:                    q2 = ssn.createQuery(hql);
357:                    Calendar cal = Calendar.getInstance();
358:                    resetCalendar(cal, Calendar.DATE);
359:                    q2.setCalendar(0, cal);
360:                    res = q2.list();
361:                    counts[1] = (res.size() > 0) ? ((Integer) res.get(0))
362:                            .intValue() : 0;
363:                } finally {
364:                    closeSession(ssn);
365:                }
366:                return counts;
367:            }
368:
369:            /**
370:             * 重置日历的值
371:             * @param cal
372:             * @param scope
373:             */
374:            protected static void resetCalendar(Calendar cal, int scope) {
375:                cal.set(Calendar.MILLISECOND, 0);
376:                cal.set(Calendar.SECOND, 0);
377:                cal.set(Calendar.MINUTE, 0);
378:                cal.set(Calendar.HOUR_OF_DAY, 0);
379:                switch (scope) {
380:                case Calendar.DATE:
381:                    break;
382:                case Calendar.MONTH:
383:                    cal.set(Calendar.DATE, 1);
384:                    break;
385:                case Calendar.YEAR:
386:                    cal.set(Calendar.DATE, 1);
387:                    cal.set(Calendar.MONTH, 0);
388:                    break;
389:                default:
390:                    throw new IllegalArgumentException(
391:                            "The value of scope is un-supported.");
392:                }
393:            }
394:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.