1 16 package dlog4j; 17 18 import java.sql.Connection ; 19 import java.sql.PreparedStatement ; 20 import java.sql.ResultSet ; 21 import java.sql.SQLException ; 22 import java.text.DateFormat ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Calendar ; 25 import java.util.List ; 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 41 public class SiteStatManager extends ManagerBase { 42 43 public final static int SCOPE_ALL = -1; 44 45 58 public static int statLogs(int inc, int scope) throws SQLException , HibernateException{ 59 Session ssn = null; 60 int log_count = -1; 61 try{ 62 ssn = getSession(); 63 String hql = "SELECT COUNT(*) FROM "+LogForm.class.getName()+" AS log WHERE log.status=0"; 64 Calendar cal_begin = null; 65 Calendar 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 res = q2.list(); 84 log_count = (res.size()>0)?((Integer )res.get(0)).intValue():0; 85 }finally{ 86 closeSession(ssn); 87 } 88 return log_count; 89 } 90 91 104 public static int statReplies(int inc, int scope) throws SQLException , HibernateException{ 105 Session ssn = null; 106 int log_count = -1; 107 try{ 108 ssn = getSession(); 109 String hql = "SELECT COUNT(reply.id) FROM "+ReplyForm.class.getName()+" AS reply WHERE reply.log.status<>" + LogForm.STATUS_DELETED; 110 Calendar cal_begin = null; 111 Calendar 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 res = q2.list(); 130 log_count = (res.size()>0)?((Integer )res.get(0)).intValue():0; 131 }finally{ 132 closeSession(ssn); 133 } 134 return log_count; 135 } 136 137 143 public static LogForm getFirstLog() throws SQLException , 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 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 165 public static RefererBean getFirstVisit() throws SQLException , 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 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 195 public static int statTotalVisit(int inc, int scope) throws SQLException , HibernateException{ 196 Session ssn = null; 197 int log_count = -1; 198 try{ 199 ssn = getSession(); 200 String 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 cal = Calendar.getInstance(); 206 cal.add(scope,inc); 207 DateFormat fmt = null; 208 String sDate = null; 209 switch(scope){ 210 case Calendar.DATE: 211 fmt = new SimpleDateFormat ("yyyyMMdd"); 212 sDate = fmt.format(cal.getTime()); 213 break; 214 case Calendar.MONTH: 215 fmt = new SimpleDateFormat ("yyyyMM"); 216 sDate = fmt.format(cal.getTime())+'%'; 217 break; 218 case Calendar.YEAR: 219 fmt = new SimpleDateFormat ("yyyy"); 220 sDate = cal.get(Calendar.YEAR) + "%"; 221 break; 222 default: 223 throw new IllegalArgumentException ("The value of scope is un-supported."); 224 } 225 q2.setString(0, sDate); 226 } 227 List res = q2.list(); 228 log_count = (res.size()>0)?((Integer )res.get(0)).intValue():0; 229 }finally{ 230 closeSession(ssn); 231 } 232 return log_count; 233 } 234 235 248 public static int statUserVisit(int inc, int scope) throws SQLException { 249 Connection conn = null; 250 PreparedStatement ps = null; 251 ResultSet rs = null; 252 int log_count = -1; 253 try{ 254 conn = getConnection(); 255 String 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 cal = Calendar.getInstance(); 262 cal.add(scope,inc); 263 DateFormat fmt = null; 264 String sDate = null; 265 switch(scope){ 266 case Calendar.DATE: 267 fmt = new SimpleDateFormat ("yyyyMMdd"); 268 sDate = fmt.format(cal.getTime()); 269 break; 270 case Calendar.MONTH: 271 fmt = new SimpleDateFormat ("yyyyMM"); 272 sDate = fmt.format(cal.getTime())+'%'; 273 break; 274 case Calendar.YEAR: 275 fmt = new SimpleDateFormat ("yyyy"); 276 sDate = cal.get(Calendar.YEAR) + "%"; 277 break; 278 default: 279 throw new IllegalArgumentException ("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 296 public static String [] maxVisit() throws SQLException { 297 String [] values = {"",""}; 298 Connection conn = null; 299 PreparedStatement ps = null; 300 ResultSet rs = null; 301 int log_count = -1; 302 try{ 303 conn = getConnection(); 304 String 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 323 public static int[] statUsers() throws SQLException , HibernateException{ 324 Session ssn = null; 325 int[] counts = {0,0}; 326 try{ 327 ssn = getSession(); 328 String hql = "SELECT COUNT(*) FROM "+UserForm.class.getName()+" AS u"; 329 Query q2 = ssn.createQuery(hql); 330 List res = q2.list(); 331 counts[0] = (res.size()>0)?((Integer )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 cal = Calendar.getInstance(); 336 resetCalendar(cal, Calendar.DATE); 337 q2.setCalendar(0, cal); 338 res = q2.list(); 339 counts[1] = (res.size()>0)?((Integer )res.get(0)).intValue():0; 340 }finally{ 341 closeSession(ssn); 342 } 343 return counts; 344 } 345 346 351 protected static void resetCalendar(Calendar 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 ("The value of scope is un-supported."); 368 } 369 } 370 } 371 | Popular Tags |