1 16 package dlog4j; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Calendar ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import net.sf.hibernate.Criteria; 25 import net.sf.hibernate.HibernateException; 26 import net.sf.hibernate.Query; 27 import net.sf.hibernate.Session; 28 import net.sf.hibernate.expression.Expression; 29 import net.sf.hibernate.expression.Order; 30 31 import org.apache.commons.lang.StringUtils; 32 import org.apache.lucene.queryParser.ParseException; 33 34 import dlog4j.formbean.CategoryForm; 35 import dlog4j.formbean.LogForm; 36 import dlog4j.formbean.ReplyForm; 37 import dlog4j.formbean.SiteForm; 38 import dlog4j.formbean.UserForm; 39 import dlog4j.search.SearchProxy; 40 43 public class ReplyManager { 44 45 public final static String FIELD_WRITETIME = "writeTime"; 46 54 public static ReplyForm getLogForm(Session ssn, SiteForm site, UserForm loginUser, int log_id) 55 { 56 ReplyForm reply = null; 57 try{ 58 reply = (ReplyForm)ssn.load(ReplyForm.class, new Integer (log_id)); 59 if(reply!=null && reply.getSite().getId()!=site.getId()) 60 reply = null; 61 if(reply!=null) 62 reply = null; 63 }catch(HibernateException e){} 64 return reply; 65 } 66 67 76 public static int[] statReplies(Session ssn, SiteForm site, UserForm loginUser, int year, int month) throws HibernateException 77 { 78 Calendar cal = Calendar.getInstance(); 79 cal.set(Calendar.YEAR, year); 80 cal.set(Calendar.MONTH, month-1); 81 return statReplies(ssn,site,loginUser,cal); 82 } 83 84 public static int[] statReplies(Session ssn, SiteForm site, UserForm loginUser, Calendar month) throws HibernateException{ 85 86 Calendar firstDate = (Calendar )month.clone(); 87 firstDate.set(Calendar.DATE,1); 88 resetCalendar(firstDate); 89 Calendar nextMonthFirstDate = (Calendar )firstDate.clone(); 90 nextMonthFirstDate.add(Calendar.MONTH,1); 91 92 Calendar tempCal = (Calendar )nextMonthFirstDate.clone(); 94 tempCal.add(Calendar.DATE,-1); 95 int dateCount = tempCal.get(Calendar.DATE); 96 97 int[] replyCounts = new int[dateCount]; 98 99 101 Criteria crit = ssn.createCriteria(ReplyForm.class).add(Expression.eq("site.id", new Integer (site.getId()))); 102 crit = crit.addOrder(Order.asc(FIELD_WRITETIME)); 103 crit = crit.add(Expression.ge(FIELD_WRITETIME,firstDate.getTime())); 104 crit = crit.add(Expression.lt(FIELD_WRITETIME,nextMonthFirstDate.getTime())); 105 106 Iterator replyies = crit.list().iterator(); 107 while(replyies.hasNext()){ 108 ReplyForm reply = (ReplyForm)replyies.next(); 109 if(loginUser==null) 110 continue; 111 tempCal.setTime(reply.getWriteTime()); 112 int date = tempCal.get(Calendar.DATE) - 1; 113 replyCounts[date]++; 114 } 115 116 return replyCounts; 117 } 118 122 protected static void resetCalendar(Calendar cal){ 123 cal.set(Calendar.HOUR_OF_DAY,0); 124 cal.set(Calendar.MINUTE,0); 125 cal.set(Calendar.SECOND,0); 126 cal.set(Calendar.MILLISECOND,0); 127 } 128 136 protected static Calendar buildCalendar(int year,int month,int date,boolean clearTime){ 137 Calendar cal = Calendar.getInstance(); 138 if(clearTime) 139 resetCalendar(cal); 140 if(year!=-1) 141 cal.set(Calendar.YEAR,year); 142 if(month!=-1) 143 cal.set(Calendar.MONTH,month-1); 144 if(date!=-1) 145 cal.set(Calendar.DATE,date); 146 return cal; 147 } 148 160 public static List searchAllReplies(Session ssn, SiteForm site, UserForm loginUser, int cat_id, String search, String orderField) throws HibernateException, IOException , ParseException 161 { 162 List logs = new ArrayList (); 163 SearchProxy proxy = SearchProxy.getReplyQuery(); 164 List ids = proxy.searchFor(site.getId(),cat_id,search,0,-1); 165 if(ids.size()>0) { 166 Criteria crit = ssn.createCriteria(ReplyForm.class).add(Expression.eq("site.id", new Integer (site.getId()))); 167 crit = crit.add(Expression.in("id",ids)); 168 if(StringUtils.isEmpty(orderField)) 169 orderField = "writeTime"; 170 crit = crit.addOrder(Order.desc(orderField)); 171 logs = crit.list(); 172 Iterator ls = logs.iterator(); 174 while(ls.hasNext()) { 175 ReplyForm reply = (ReplyForm)ls.next(); 176 if(reply.getLog().getStatus()!=LogForm.STATUS_NORMAL) 177 ls.remove(); 178 if(loginUser==null||!loginUser.isAdmin()) { 179 if(reply.getLog().getCategory().getType()==CategoryForm.TYPE_OWNER) { 180 ls.remove(); 181 } 182 } 183 } 184 } 185 if(ids!=null) 186 ids.clear(); 187 return logs; 188 } 189 203 public static int getSearchReplyCount(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid, String search) throws IOException , ParseException, HibernateException { 204 SearchProxy proxy = SearchProxy.getReplyQuery(); 205 List ids = proxy.searchFor(site.getId(),cat_id,search,0,-1); 206 if(ids.size()==0) 207 return 0; 208 Criteria crit = ssn.createCriteria(ReplyForm.class).add(Expression.eq("site.id", new Integer (site.getId()))); 209 crit = crit.add(Expression.in("id",ids)); 210 List replies = crit.list(); 211 Iterator ls = replies.iterator(); 213 while(ls.hasNext()) { 214 ReplyForm reply = (ReplyForm)ls.next(); 215 if(loginUser==null||!loginUser.isAdmin()) { 216 if(reply.getLog().getCategory().getType()==CategoryForm.TYPE_OWNER) 217 ls.remove(); 218 } 219 } 220 int rc = replies.size(); 221 replies.clear(); 222 return rc; 223 } 224 241 public static List searchReplies(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid, String search, String orderField,int from,int count) throws IOException , ParseException, HibernateException { 242 SearchProxy proxy = SearchProxy.getReplyQuery(); 243 List ids = proxy.searchFor(site.getId(),cat_id,search,from,count); 244 if(ids.size()==0) 245 return new ArrayList (); 246 Criteria crit = ssn.createCriteria(ReplyForm.class).add(Expression.eq("site.id", new Integer (site.getId()))); 247 crit = crit.add(Expression.in("id",ids)); 248 if(StringUtils.isEmpty(orderField)) 249 orderField = FIELD_WRITETIME; 250 crit = crit.addOrder(Order.desc(orderField)); 251 257 List replies = crit.list(); 258 Iterator ls = replies.iterator(); 260 while(ls.hasNext()) { 261 ReplyForm reply = (ReplyForm)ls.next(); 262 if(loginUser==null||!loginUser.isAdmin()) { 263 if(reply.getLog().getCategory().getType()==CategoryForm.TYPE_OWNER) 264 ls.remove(); 265 } 266 } 267 return replies; 268 } 269 278 public static List listReplies(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid, int from, int count, String orderField, int year, int month, int date) 279 throws HibernateException { 280 String hql = "FROM " + ReplyForm.class.getName() + " AS reply WHERE reply.site.id=? "; 281 if(cat_id!=-1) 282 hql += " AND reply.log.category.id=?"; 283 else 284 hql += " AND reply.log.category.id<>?"; 285 if(userid>0) 286 hql += " AND reply.author.id="+userid; 287 if(loginUser==null||!loginUser.isAdmin()) 288 hql += " AND reply.log.category.type<>" + CategoryForm.TYPE_OWNER; 289 290 hql += " AND reply.log.status=?"; 291 292 Calendar begin = null; 293 Calendar end = null; 294 boolean hasTime = false; 295 if(year!=-1&&month!=-1&&date!=-1){ begin = buildCalendar(year,month,date,true); 297 end = (Calendar )begin.clone(); 298 end.add(Calendar.DATE,1); 299 hql += " AND reply.writeTime>=? AND reply.writeTime<?"; 300 hasTime = true; 301 } 302 else 303 if(year!=-1&&month!=-1){ begin = buildCalendar(year,month,1,true); 305 end = (Calendar )begin.clone(); 306 end.add(Calendar.MONTH,1); 307 hql += " AND reply.writeTime>=? AND reply.writeTime<?"; 308 hasTime = true; 309 } 310 else 311 if(year!=-1){ begin = buildCalendar(year,1,1,true); 313 end = (Calendar )begin.clone(); 314 end.add(Calendar.YEAR,1); 315 hql += " AND reply.writeTime>=? AND reply.writeTime<?"; 316 hasTime = true; 317 } 318 if(StringUtils.isEmpty(orderField)) 319 orderField = FIELD_WRITETIME; 320 hql += " ORDER BY reply." + orderField + " DESC"; 321 Query query = ssn.createQuery(hql); 322 query.setInteger(0,site.getId()); 323 query.setInteger(1,cat_id); 324 query.setInteger(2,LogForm.STATUS_NORMAL); 325 if(hasTime){ 326 query.setCalendar(3,begin); 327 query.setCalendar(4,end); 328 } 329 query.setFirstResult(from); 330 if ( count > 0 ) { 331 query.setMaxResults(count); 332 } 333 return query.list(); 334 335 } 336 337 338 348 public static List listReplies(Session ssn, SiteForm site, 349 UserForm loginUser, int count) throws HibernateException { 350 String hql = "FROM " + ReplyForm.class.getName() 351 + " AS r WHERE r.site.id=? AND r.log.status=?"; 352 if (loginUser == null || !loginUser.isAdmin()) 353 hql += " AND r.log.category.type<>?"; 354 hql += " ORDER BY r.writeTime DESC"; 355 Query query = ssn.createQuery(hql); 356 query.setInteger(0, site.getId()); 357 query.setInteger(1, LogForm.STATUS_NORMAL); 358 if (loginUser == null || !loginUser.isAdmin()) 359 query.setInteger(2, CategoryForm.TYPE_OWNER); 360 if (count > 0) query.setMaxResults(count); 361 return query.list(); 362 } 363 364 371 public static int getRepliesCount(Session ssn, SiteForm site, UserForm loginUser, int cat_id, int userid , int year, int month, int date) 372 throws HibernateException { 373 String hql = "SELECT COUNT(*) FROM " + ReplyForm.class.getName() + " AS reply WHERE reply.site.id=? "; 374 if(cat_id!=-1) 375 hql += " AND reply.log.category.id=?"; 376 else 377 hql += " AND reply.log.category.id<>?"; 378 if(userid>0) 379 hql += " AND reply.author.id="+userid; 380 if(loginUser==null||!loginUser.isAdmin()) 381 hql += " AND reply.log.category.type<>" + CategoryForm.TYPE_OWNER; 382 383 hql += " AND reply.log.status=?"; 384 385 Calendar begin = null; 386 Calendar end = null; 387 boolean hasTime = false; 388 if(year!=-1&&month!=-1&&date!=-1){ begin = buildCalendar(year,month,date,true); 390 end = (Calendar )begin.clone(); 391 end.add(Calendar.DATE,1); 392 hql += " AND reply.writeTime>=? AND reply.writeTime<?"; 393 hasTime = true; 394 } 395 else 396 if(year!=-1&&month!=-1){ begin = buildCalendar(year,month,1,true); 398 end = (Calendar )begin.clone(); 399 end.add(Calendar.MONTH,1); 400 hql += " AND reply.writeTime>=? AND reply.writeTime<?"; 401 hasTime = true; 402 } 403 else 404 if(year!=-1){ begin = buildCalendar(year,1,1,true); 406 end = (Calendar )begin.clone(); 407 end.add(Calendar.YEAR,1); 408 hql += " AND reply.writeTime>=? AND reply.writeTime<?"; 409 hasTime = true; 410 } 411 Query query = ssn.createQuery(hql); 412 query.setInteger(0,site.getId()); 413 query.setInteger(1,cat_id); 414 query.setInteger(2,LogForm.STATUS_NORMAL); 415 if(hasTime){ 416 query.setCalendar(3,begin); 417 query.setCalendar(4,end); 418 } 419 List res = query.list(); 420 int Replycount = (res.size() > 0) ? ((Integer ) res.get(0)).intValue() : 0; 421 return Replycount; 422 } 423 } 424 | Popular Tags |