1 16 package dlog4j; 17 18 import java.io.IOException ; 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.sql.Timestamp ; 24 import java.util.ArrayList ; 25 import java.util.Calendar ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import net.sf.hibernate.Criteria; 30 import net.sf.hibernate.HibernateException; 31 import net.sf.hibernate.Query; 32 import net.sf.hibernate.Session; 33 import net.sf.hibernate.expression.Expression; 34 import net.sf.hibernate.expression.Order; 35 36 import org.apache.commons.lang.StringUtils; 37 import org.apache.lucene.queryParser.ParseException; 38 39 import dlog4j.formbean.CategoryForm; 40 import dlog4j.formbean.DraftForm; 41 import dlog4j.formbean.LogForm; 42 import dlog4j.formbean.SiteForm; 43 import dlog4j.formbean.UserForm; 44 import dlog4j.search.SearchProxy; 45 import dlog4j.security.DlogRole; 46 47 51 public class LogManager extends ManagerBase{ 52 53 66 public static List searchLogs(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String search, String orderField, int from, int count) throws HibernateException, IOException , ParseException 67 { 68 SearchProxy proxy = SearchProxy.getLogQuery(); 69 List ids = proxy.searchFor(site.getId(),cat_id,search,from,count); 70 if(ids.size()==0) 71 return new ArrayList (); 72 Criteria crit = ssn.createCriteria(LogForm.class).add(Expression.eq("site.id", new Integer (site.getId()))); 74 crit = crit.add(Expression.in("id",ids)); 75 76 if(StringUtils.isEmpty(orderField)) 77 orderField = "logTime"; 78 crit = crit.addOrder(Order.desc(orderField)); 79 List logs = crit.list(); 80 Iterator ls = logs.iterator(); 82 while(ls.hasNext()) { 83 LogForm log = (LogForm)ls.next(); 84 if(loginUser==null||!loginUser.isAdmin()) { 85 if(log.getCategory().getType()==CategoryForm.TYPE_OWNER) 86 ls.remove(); 87 } 88 } 89 return logs; 90 } 91 103 public static List searchAllLogs(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String search, String orderField) throws HibernateException, IOException , ParseException 104 { 105 List logs = new ArrayList (); 106 SearchProxy proxy = SearchProxy.getLogQuery(); 107 List ids = proxy.searchFor(site.getId(),cat_id,search,0,-1); 108 if(ids.size()>0) { 109 Criteria crit = ssn.createCriteria(LogForm.class).add(Expression.eq("site.id", new Integer (site.getId()))); 110 crit = crit.add(Expression.in("id",ids)); 111 if(StringUtils.isEmpty(orderField)) 112 orderField = "logTime"; 113 crit = crit.addOrder(Order.desc(orderField)); 114 logs = crit.list(); 115 Iterator ls = logs.iterator(); 117 while(ls.hasNext()) { 118 LogForm log = (LogForm)ls.next(); 119 if(loginUser==null||!loginUser.isAdmin()) { 120 if(log.getCategory().getType()==CategoryForm.TYPE_OWNER) { 121 ls.remove(); 122 } 123 } 124 } 125 } 126 if(ids!=null) 127 ids.clear(); 128 return logs; 129 } 130 143 public static int getSearchLogCount(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String search) throws HibernateException, IOException , ParseException 144 { 145 SearchProxy proxy = SearchProxy.getLogQuery(); 146 List ids = proxy.searchFor(site.getId(),cat_id,search,0,-1); 147 if(ids.size()==0) 148 return 0; 149 Criteria crit = ssn.createCriteria(LogForm.class).add(Expression.eq("site.id", new Integer (site.getId()))); 150 crit = crit.add(Expression.in("id",ids)); 151 List logs = crit.list(); 152 Iterator ls = logs.iterator(); 154 while(ls.hasNext()) { 155 LogForm log = (LogForm)ls.next(); 156 if(loginUser==null||!loginUser.isAdmin()) { 157 if(log.getCategory().getType()==CategoryForm.TYPE_OWNER) 158 ls.remove(); 159 } 160 } 161 int lc = logs.size(); 162 logs.clear(); 163 return lc; 164 } 165 173 public static LogForm getLogForm(Session ssn, SiteForm site, UserForm loginUser, int log_id) 174 { 175 LogForm log = null; 176 try{ 177 log = (LogForm)ssn.load(LogForm.class, new Integer (log_id)); 178 if(log.getStatus()==LogForm.STATUS_DELETED) 179 log = null; 180 else 181 if(log!=null && log.getSite().getId()!=site.getId()) 182 log = null; 183 else 184 if(log!=null&&(loginUser==null||!loginUser.isAdmin())&&log.getCategory().isOwnerOnly()) 185 log = null; 186 }catch(HibernateException e){} 187 return log; 188 } 189 190 200 public static int[] statLogs(Session ssn, SiteForm site, UserForm loginUser, int year, int month) throws HibernateException 201 { 202 Calendar cal = Calendar.getInstance(); 203 cal.set(Calendar.YEAR, year); 204 cal.set(Calendar.MONTH, month-1); 205 return statLogs(ssn,site,loginUser,cal); 206 } 207 208 218 public static int[] statLogs(Session ssn, SiteForm site, UserForm loginUser, Calendar month) throws HibernateException{ 219 220 Calendar firstDate = (Calendar )month.clone(); 221 firstDate.set(Calendar.DATE,1); 222 resetCalendar(firstDate); 223 Calendar nextMonthFirstDate = (Calendar )firstDate.clone(); 224 nextMonthFirstDate.add(Calendar.MONTH,1); 225 226 Calendar tempCal = (Calendar )nextMonthFirstDate.clone(); 228 tempCal.add(Calendar.DATE,-1); 229 int dateCount = tempCal.get(Calendar.DATE); 230 231 int[] logCounts = new int[dateCount]; 232 233 235 StringBuffer hql = new StringBuffer ("SELECT log.logTime FROM "); 236 hql.append(LogForm.class.getName()); 237 hql.append(" AS log WHERE log.logTime>=? AND log.logTime<? AND log.status=?"); 238 Query q = ssn.createQuery(hql.toString()); 239 q.setTimestamp(0, firstDate.getTime()); 240 q.setTimestamp(1, nextMonthFirstDate.getTime()); 241 q.setInteger(2, LogForm.STATUS_NORMAL); 242 243 Iterator logs = q.list().iterator(); 244 while(logs.hasNext()){ 245 tempCal.setTime((Timestamp )logs.next()); 246 int date = tempCal.get(Calendar.DATE) - 1; 247 logCounts[date]++; 248 } 249 250 return logCounts; 251 } 252 262 public static int[] statLogs(Connection conn, SiteForm site, UserForm loginUser, Calendar month) throws SQLException { 263 264 Calendar firstDate = (Calendar )month.clone(); 265 firstDate.set(Calendar.DATE,1); 266 resetCalendar(firstDate); 267 Calendar nextMonthFirstDate = (Calendar )firstDate.clone(); 268 nextMonthFirstDate.add(Calendar.MONTH,1); 269 270 Calendar tempCal = (Calendar )nextMonthFirstDate.clone(); 272 tempCal.add(Calendar.DATE,-1); 273 int dateCount = tempCal.get(Calendar.DATE); 274 275 int[] logCounts = new int[dateCount]; 276 277 279 String sql = "SELECT logTime FROM dlog_journal WHERE logTime>=? AND logTime<? AND status=?"; 280 281 PreparedStatement ps = null; 282 ResultSet rs = null; 283 284 try{ 285 ps = conn.prepareStatement(sql); 286 ps.setTimestamp(1, new Timestamp (firstDate.getTime().getTime())); 287 ps.setTimestamp(2, new Timestamp (nextMonthFirstDate.getTime().getTime())); 288 ps.setInt(3, LogForm.STATUS_NORMAL); 289 if(ps.execute()){ 290 rs = ps.getResultSet(); 291 while(rs.next()){ 292 tempCal.setTime(rs.getDate(1)); 293 int date = tempCal.get(Calendar.DATE) - 1; 294 logCounts[date]++; 295 } 296 } 297 }finally{ 298 close(rs,ps,null); 299 } 300 301 return logCounts; 302 } 303 307 protected static void resetCalendar(Calendar cal){ 308 cal.set(Calendar.HOUR_OF_DAY,0); 309 cal.set(Calendar.MINUTE,0); 310 cal.set(Calendar.SECOND,0); 311 cal.set(Calendar.MILLISECOND,0); 312 } 313 321 protected static Calendar buildCalendar(int year,int month,int date,boolean clearTime){ 322 Calendar cal = Calendar.getInstance(); 323 if(clearTime) 324 resetCalendar(cal); 325 if(year!=-1) 326 cal.set(Calendar.YEAR,year); 327 if(month!=-1) 328 cal.set(Calendar.MONTH,month-1); 329 if(date!=-1) 330 cal.set(Calendar.DATE,date); 331 return cal; 332 } 333 341 public static DraftForm getDraft(Session ssn, SiteForm site, int draft_id) throws HibernateException 342 { 343 Criteria crit = ssn.createCriteria(DraftForm.class); 344 crit = crit.add(Expression.eq("site.id", new Integer (site.getId()))); 345 crit = crit.add(Expression.eq("id", new Integer (draft_id))); 346 DraftForm draft = null; 347 List drafts = crit.list(); 348 if(drafts.size()>0) 349 draft = (DraftForm)drafts.get(0); 350 return draft; 351 352 } 353 360 public static List listDrafts(Session ssn, SiteForm site, UserForm loginUser) throws HibernateException{ 361 if(loginUser==null || (!loginUser.isAdmin()&&!loginUser.isFriend())) 362 return new ArrayList (); 363 Criteria crit = ssn.createCriteria(DraftForm.class); 364 crit = crit.add(Expression.eq("site.id", new Integer (site.getId()))); 365 crit = crit.add(Expression.eq("owner.id",new Integer (loginUser.getId()))); 367 crit = crit.addOrder(Order.desc("logTime")); 368 return crit.list(); 369 } 370 371 380 public static List listLogs(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid,int from, int count, String orderField, int year, int month, int date) 381 throws HibernateException { 382 String hql = "FROM " + LogForm.class.getName() + " AS log WHERE log.site.id=? AND log.status=?"; 383 if(cat_id!=-1) 384 hql += " AND log.category.id=?"; 385 else 386 hql += " AND log.category.id<>?"; 387 if(userid>0) 388 hql += " AND log.owner.id="+userid; 389 390 if(loginUser!=null && loginUser.getUserRole()==DlogRole.ROLE_BUDDY){ 391 int[] cats = loginUser.getOwnerCatids(); 392 if(cats.length==0) 393 hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER; 394 else{ 395 hql += " AND (log.category.type<>" + CategoryForm.TYPE_OWNER; 396 for(int i=0;i<cats.length;i++){ 397 hql += " OR log.category.id=" + cats[i]; 398 } 399 hql += ")"; 400 } 401 } 402 else 403 if(loginUser==null||!loginUser.isAdmin()) 404 hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER; 405 406 Calendar begin = null; 407 Calendar end = null; 408 boolean hasTime = false; 409 if(year!=-1&&month!=-1&&date!=-1){ begin = buildCalendar(year,month,date,true); 411 end = (Calendar )begin.clone(); 412 end.add(Calendar.DATE,1); 413 hql += " AND log.logTime>=? AND log.logTime<?"; 414 hasTime = true; 415 } 416 else 417 if(year!=-1&&month!=-1){ begin = buildCalendar(year,month,1,true); 419 end = (Calendar )begin.clone(); 420 end.add(Calendar.MONTH,1); 421 hql += " AND log.logTime>=? AND log.logTime<?"; 422 hasTime = true; 423 } 424 else 425 if(year!=-1){ begin = buildCalendar(year,1,1,true); 427 end = (Calendar )begin.clone(); 428 end.add(Calendar.YEAR,1); 429 hql += " AND log.logTime>=? AND log.logTime<?"; 430 hasTime = true; 431 } 432 if(StringUtils.isEmpty(orderField)) 433 orderField = "logTime"; 434 hql += " ORDER BY log." + orderField + " DESC"; 435 Query query = ssn.createQuery(hql); 436 query.setInteger(0,site.getId()); 437 query.setInteger(1,LogForm.STATUS_NORMAL); 438 query.setInteger(2,cat_id); 439 if(hasTime){ 440 query.setCalendar(3,begin); 441 query.setCalendar(4,end); 442 } 443 query.setFirstResult(from); 444 query.setMaxResults(count); 445 return query.list(); 446 447 } 448 449 456 public static int getLogCount(Session ssn, int cat_id) throws HibernateException{ 457 Query q2 = ssn.createQuery("SELECT COUNT(*) FROM "+LogForm.class.getName()+" AS log WHERE log.category.id=?"); 458 q2.setInteger(0, cat_id); 459 List res = q2.list(); 460 return (res.size()>0)?((Integer )res.get(0)).intValue():0; 461 } 462 463 470 public static int getLogCount(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid, int year, int month, int date) 471 throws HibernateException { 472 String hql = "SELECT COUNT(*) FROM " + LogForm.class.getName() + " AS log WHERE log.site.id=? AND log.status=?"; 473 if(cat_id!=-1) 474 hql += " AND log.category.id=?"; 475 else 476 hql += " AND log.category.id<>?"; 477 if(userid>0) 478 hql += " AND log.owner.id="+userid; 479 480 if(loginUser!=null && loginUser.getUserRole()==DlogRole.ROLE_BUDDY){ 481 int[] cats = loginUser.getOwnerCatids(); 482 if(cats.length==0) 483 hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER; 484 else{ 485 hql += " AND (log.category.type<>" + CategoryForm.TYPE_OWNER; 486 for(int i=0;i<cats.length;i++){ 487 hql += " OR log.category.id=" + cats[i]; 488 } 489 hql += ")"; 490 } 491 } 492 else 493 if(loginUser==null||!loginUser.isAdmin()) 494 hql += " AND log.category.type<>" + CategoryForm.TYPE_OWNER; 495 496 497 Calendar begin = null; 498 Calendar end = null; 499 boolean hasTime = false; 500 if(year!=-1&&month!=-1&&date!=-1){ begin = buildCalendar(year,month,date,true); 502 end = (Calendar )begin.clone(); 503 end.add(Calendar.DATE,1); 504 hql += " AND log.logTime>=? AND log.logTime<?"; 505 hasTime = true; 506 } 507 else 508 if(year!=-1&&month!=-1){ begin = buildCalendar(year,month,1,true); 510 end = (Calendar )begin.clone(); 511 end.add(Calendar.MONTH,1); 512 hql += " AND log.logTime>=? AND log.logTime<?"; 513 hasTime = true; 514 } 515 else 516 if(year!=-1){ begin = buildCalendar(year,1,1,true); 518 end = (Calendar )begin.clone(); 519 end.add(Calendar.YEAR,1); 520 hql += " AND log.logTime>=? AND log.logTime<?"; 521 hasTime = true; 522 } 523 Query query = ssn.createQuery(hql); 524 query.setInteger(0,site.getId()); 525 query.setInteger(1,LogForm.STATUS_NORMAL); 526 query.setInteger(2,cat_id); 527 if(hasTime){ 528 query.setCalendar(3,begin); 529 query.setCalendar(4,end); 530 } 531 List res = query.list(); 532 int logcount = (res.size() > 0) ? ((Integer ) res.get(0)).intValue() : 0; 533 return logcount; 534 } 535 } 536 | Popular Tags |