| 1 25 26 package com.j2biz.blogunity.dao; 27 28 import java.io.Serializable ; 29 import java.util.Calendar ; 30 import java.util.Collections ; 31 import java.util.Date ; 32 import java.util.List ; 33 import java.util.Set ; 34 35 import net.sf.hibernate.Criteria; 36 import net.sf.hibernate.HibernateException; 37 import net.sf.hibernate.Query; 38 import net.sf.hibernate.Session; 39 import net.sf.hibernate.expression.Expression; 40 import net.sf.hibernate.expression.Order; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 import com.j2biz.blogunity.exception.BlogunityException; 46 import com.j2biz.blogunity.i18n.I18N; 47 import com.j2biz.blogunity.i18n.I18NStatusFactory; 48 import com.j2biz.blogunity.pojo.Blog; 49 import com.j2biz.blogunity.pojo.CalendarEntry; 50 import com.j2biz.blogunity.pojo.Entry; 51 import com.j2biz.blogunity.pojo.User; 52 import com.j2biz.blogunity.pojo.Userpic; 53 import com.j2biz.blogunity.util.HibernateUtil; 54 55 62 public class EntryDAO extends AbstractDAO { 63 66 private static final Log log = LogFactory.getLog(EntryDAO.class); 67 68 71 public EntryDAO() { 72 super(); 73 } 74 75 public Entry getEntryByID(long id) throws BlogunityException { 76 return getEntryByID(new Long (id)); 77 } 78 79 public Entry getEntryByID(Long id) throws BlogunityException { 80 81 Session session = HibernateUtil.getSession(); 82 Entry entry = null; 83 try { 84 entry = (Entry) session.load(Entry.class, id); 85 } catch (HibernateException ex) { 86 87 log.error("getEntryByID(Long)", ex); 88 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID, 89 "Entry", ex)); 90 } 91 92 return entry; 93 } 94 95 public List getEntiresByUserpic(Userpic pic) throws BlogunityException { 96 Session session = HibernateUtil.getSession(); 97 try { 98 99 Criteria criteria = session.createCriteria(Entry.class); 100 criteria.add(Expression.eq("userpic", pic)); 101 return criteria.list(); 102 103 } catch (HibernateException e) { 104 log.error("getPaginatedEntriesByCategory(String, Long, int, int)", e); 105 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_BY_USERPIC, 106 "Entry", e)); 107 } 108 } 109 110 public List getPaginatedEntriesByCategory(Long blogId, Long categoryId, int firstResult, 111 int maxResults) throws BlogunityException { 112 113 Session session = HibernateUtil.getSession(); 114 115 try { 116 117 Criteria criteria = session.createCriteria(Entry.class); 118 criteria.createCriteria("categories").add(Expression.eq("id", categoryId)); 119 criteria.createCriteria("blog").add(Expression.eq("id", blogId)); 120 criteria.setFirstResult(firstResult); 121 criteria.setMaxResults(maxResults); 122 criteria.setCacheable(true); 123 return criteria.list(); 124 125 } catch (HibernateException e) { 126 log.error("getPaginatedEntriesByCategory(String, Long, int, int)", e); 127 throw new BlogunityException(I18NStatusFactory.create( 128 I18N.ERRORS.FETCH_PAGINATED_LIST_BY_CATEGORY, e)); 129 } 130 } 131 132 public List getPaginatedEntriesByCategory(String blogUrlname, Long categoryId, int firstResult, 133 int maxResults) throws BlogunityException { 134 135 Session session = HibernateUtil.getSession(); 136 137 try { 138 139 Criteria criteria = session.createCriteria(Entry.class); 140 criteria.createCriteria("categories").add(Expression.eq("id", categoryId)); 141 criteria.createCriteria("blog").add(Expression.eq("urlName", blogUrlname)); 142 criteria.setFirstResult(firstResult); 143 criteria.setMaxResults(maxResults); 144 criteria.setCacheable(true); 145 return criteria.list(); 146 147 } catch (HibernateException e) { 148 log.error("getPaginatedEntriesByCategory(String, Long, int, int)", e); 149 throw new BlogunityException(I18NStatusFactory.create( 150 I18N.ERRORS.FETCH_PAGINATED_LIST_BY_CATEGORY, e)); 151 } 152 } 153 154 public List getPaginatedEntries(String urlname, int firstResult, int maxResults) 155 throws BlogunityException { 156 157 Session session = HibernateUtil.getSession(); 158 159 try { 160 161 Query q = session 162 .createQuery("from Entry entry where entry.blog.urlName = :blogName order by entry.createTime desc"); 163 q.setString("blogName", urlname); 164 q.setFirstResult(firstResult); 165 q.setMaxResults(maxResults); 166 167 return q.list(); 168 169 } catch (HibernateException e) { 170 log.error("getPaginatedEntries(String, int, int)", e); 171 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST, 172 "Entries", e)); 173 } 174 } 175 176 185 public List getRecentBlogEntries(Blog b) throws BlogunityException { 186 return getPaginatedEntries(b.getId(), 0, 10); 187 } 188 189 196 public List getPaginatedEntries(Long blogId, int firstResult, int maxResults) 197 throws BlogunityException { 198 199 Session session = HibernateUtil.getSession(); 200 201 try { 202 203 Query q = session 204 .createQuery("from Entry entry where entry.blog.id = :blogId order by entry.createTime desc"); 205 q.setLong("blogId", blogId.longValue()); 206 q.setFirstResult(firstResult); 207 q.setMaxResults(maxResults); 208 return q.list(); 209 210 } catch (HibernateException e) { 211 log.error("getPaginatedEntries(Long, int, int)", e); 212 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FETCH_PAGINATED_LIST, 213 "Entries", e)); 214 } 215 } 216 217 public Entry getEntryByIDorAliasname(String param, String blogname) throws BlogunityException { 218 Session session = HibernateUtil.getSession(); 219 220 try { 221 222 Query q = session 223 .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.aliasname=:aliasParam or entry.id=:idParam"); 224 q.setString("blogName", blogname); 225 q.setString("aliasParam", param); 226 227 long id; 228 try { 229 id = Long.parseLong(param); 230 } catch (NumberFormatException e) { 231 id = -1; 232 } 233 234 q.setLong("idParam", id); 235 236 return (Entry) q.uniqueResult(); 237 238 } catch (HibernateException e) { 239 log.error("getPaginatedEntries(Long, int, int)", e); 240 throw new BlogunityException(I18NStatusFactory.create( 241 I18N.ERRORS.FETCH_ENTRY_BY_ALIASNAME, "Entry", e)); 242 } 243 } 244 245 public Entry getEntryByAliasname(String alias, String blogname) throws BlogunityException { 246 Session session = HibernateUtil.getSession(); 247 248 try { 249 250 Query q = session 251 .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.aliasname=:aliasName"); 252 q.setString("blogName", blogname); 253 q.setString("aliasName", alias); 254 return (Entry) q.uniqueResult(); 255 256 } catch (HibernateException e) { 257 log.error("getPaginatedEntries(Long, int, int)", e); 258 throw new BlogunityException(I18NStatusFactory.create( 259 I18N.ERRORS.FETCH_ENTRY_BY_ALIASNAME, "Entries", e)); 260 } 261 } 262 263 public List getPaginatedFriendsTape(User user, int firstResult, int maxResults) 264 throws BlogunityException { 265 266 Session session = HibernateUtil.getSession(); 267 268 try { 269 270 Criteria c = session.createCriteria(Entry.class); 271 272 Set friends = user.getFriends(); 273 274 if (friends.size() == 0) return Collections.EMPTY_LIST; 275 276 c.add(Expression.in("author", friends)); 277 c.addOrder(Order.desc("createTime")); 278 c.setFirstResult(firstResult); 279 c.setMaxResults(maxResults); 280 c.setCacheable(true); 281 return c.list(); 282 283 } catch (HibernateException e) { 284 log.error("getPaginatedFriendsTape(User, int, int)", e); 285 throw new BlogunityException(I18NStatusFactory.create( 286 I18N.ERRORS.FETCH_PAGINATED_FRIENDS_TAPE, e)); 287 288 } 289 } 290 291 public List getPaginatedFavoriteBlogsTape(User user, int firstResult, int maxResults) 292 throws BlogunityException { 293 294 Session session = HibernateUtil.getSession(); 295 296 try { 297 298 Criteria c = session.createCriteria(Entry.class); 299 300 Set blogs = user.getFavoriteBlogs(); 301 302 if (blogs.size() == 0) return Collections.EMPTY_LIST; 303 304 c.add(Expression.in("blog", blogs)); 305 c.addOrder(Order.desc("createTime")); 306 c.setFirstResult(firstResult); 307 c.setMaxResults(maxResults); 308 c.setCacheable(true); 309 return c.list(); 310 311 } catch (HibernateException e) { 312 log.error("getPaginatedFavoriteBlogsTape(User, int, int)", e); 313 throw new BlogunityException(I18NStatusFactory.create( 314 I18N.ERRORS.FETCH_PAGINATED_FAVORITES_TAPE, e)); 315 } 316 } 317 318 public List getPaginatedCommunityBlogsTape(User user, int firstResult, int maxResults) 319 throws BlogunityException { 320 321 Session session = HibernateUtil.getSession(); 322 323 try { 324 325 Criteria c = session.createCriteria(Entry.class); 326 327 Set blogs = user.getContributedBlogs(); 328 329 if (blogs.size() == 0) return Collections.EMPTY_LIST; 330 331 c.add(Expression.in("blog", blogs)); 332 c.addOrder(Order.desc("createTime")); 333 c.setFirstResult(firstResult); 334 c.setMaxResults(maxResults); 335 c.setCacheable(true); 336 337 return c.list(); 338 339 } catch (HibernateException e) { 340 log.error("getPaginatedFavoriteBlogsTape(User, int, int)", e); 341 throw new BlogunityException(I18NStatusFactory.create( 342 I18N.ERRORS.FETCH_PAGINATED_COMMUNITY_BLOGS_TAPE, e)); 343 } 344 } 345 346 public List getPaginatedFoundedBlogsTape(User user, int firstResult, int maxResults) 347 throws BlogunityException { 348 349 Session session = HibernateUtil.getSession(); 350 351 try { 352 353 Criteria c = session.createCriteria(Entry.class); 354 355 Set blogs = user.getFoundedBlogs(); 356 357 if (blogs.size() == 0) return Collections.EMPTY_LIST; 358 359 c.add(Expression.in("blog", blogs)); 360 c.addOrder(Order.desc("createTime")); 361 c.setFirstResult(firstResult); 362 c.setMaxResults(maxResults); 363 c.setCacheable(true); 364 return c.list(); 365 366 } catch (HibernateException e) { 367 log.error("getPaginatedFoundedBlogsTape(User, int, int)", e); 368 throw new BlogunityException(I18NStatusFactory.create( 369 I18N.ERRORS.FETCH_PAGINATED_FOUNDED_BLOGS_TAPE, e)); 370 } 371 } 372 373 public List getEntriesForDay(String blogName, int year, int month, int day) 374 throws BlogunityException { 375 376 Calendar dayObj = Calendar.getInstance(); 377 dayObj.set(year, month, day, 0, 0, 0); 378 379 return geEntriesForDay(blogName, dayObj); 380 381 } 382 383 public List geEntriesForDay(String blogName, Calendar day) throws BlogunityException { 384 385 Session session = HibernateUtil.getSession(); 386 387 try { 388 389 Calendar dayEnd = Calendar.getInstance(); 390 dayEnd.setTimeInMillis(day.getTimeInMillis()); 391 dayEnd.add(Calendar.DATE, 1); 392 393 Query q = session 394 .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.createTime between :begin and :end order by entry.createTime desc"); 395 q.setString("blogName", blogName); 396 q.setDate("begin", day.getTime()); 397 q.setDate("end", dayEnd.getTime()); 398 q.setCacheable(true); 399 400 List returnList = q.list(); 401 if (log.isDebugEnabled()) { 402 log.debug("getBlogEntriesForDay(String, Calendar) - end"); 403 } 404 return returnList; 405 406 } catch (HibernateException e) { 407 log.error("getBlogEntriesForDay(String, Calendar)", e); 408 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.ENTRY_BY_DAY, e)); 409 } 410 411 } 412 413 public List getEntriesForMonth(String blogName, int year, int month) throws BlogunityException { 414 415 Calendar monthObj = Calendar.getInstance(); 416 monthObj.set(year, month, 1, 0, 0, 0); 417 418 return getEntriesForMonth(blogName, monthObj); 419 420 } 421 422 public List getEntriesForMonth(String blogName, Calendar month) throws BlogunityException { 423 424 Session session = HibernateUtil.getSession(); 425 try { 426 427 Calendar monthEnd = Calendar.getInstance(); 428 monthEnd.setTimeInMillis(month.getTimeInMillis()); 429 monthEnd.add(Calendar.MONTH, 1); 430 431 Query q = session 432 .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.createTime between :begin and :end order by entry.createTime desc"); 433 q.setString("blogName", blogName); 434 q.setDate("begin", month.getTime()); 435 q.setDate("end", monthEnd.getTime()); 436 q.setCacheable(true); 437 438 return q.list(); 439 440 } catch (HibernateException e) { 441 log.error("getBlogEntriesForMonth(String, Calendar)", e); 442 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.ENTRY_BY_MONTH, e)); 443 } 444 } 445 446 public List getEntriesForYear(String blogName, int year) throws BlogunityException { 447 Calendar yearObj = Calendar.getInstance(); 448 yearObj.set(year, 0, 1, 0, 0, 0); 449 450 return getEntriesForYear(blogName, yearObj); 451 452 } 453 454 public List getEntriesForYear(String blogName, Calendar year) throws BlogunityException { 455 456 Session session = HibernateUtil.getSession(); 457 458 try { 459 460 Calendar yearEnd = Calendar.getInstance(); 461 yearEnd.setTimeInMillis(year.getTimeInMillis()); 462 yearEnd.add(Calendar.YEAR, 1); 463 464 Query q = session 465 .createQuery("from Entry entry where entry.blog.urlName = :blogName and entry.createTime between :begin and :end order by entry.createTime desc"); 466 q.setString("blogName", blogName); 467 q.setDate("begin", year.getTime()); 468 q.setDate("end", yearEnd.getTime()); 469 q.setCacheable(true); 470 471 return q.list(); 472 473 } catch (HibernateException e) { 474 log.error("getBlogEntriesForYear(String, Calendar)", e); 475 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.ENTRY_BY_YEAR, e)); 476 } 477 } 478 479 public Serializable createEntry(Entry entry) throws BlogunityException { 480 481 Session session = HibernateUtil.getSession(); 482 try { 483 Serializable result = session.save(entry); 484 485 Date d = entry.getCreateTime(); 487 488 CalendarEntryDAO calendarDAO = new CalendarEntryDAO(); 489 CalendarEntry ce = null; 490 491 ce = calendarDAO.getCalendarEntryForBlogByDate(entry.getBlog(), d); 492 if (ce == null) { 493 ce = new CalendarEntry(); 494 ce.setDate(d); 495 ce.setBlog(entry.getBlog()); 496 } 497 498 calendarDAO.incrementCalendarEntry(ce); 499 500 BlogDAO blogDAO = new BlogDAO(); 502 Blog b = entry.getBlog(); 503 b.setLastModified(new Date ()); 504 blogDAO.updateBlog(b); 505 506 return result; 507 } catch (HibernateException e) { 508 log.error("createEntry(Entry)", e); 509 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE, "Entry", e)); 510 } 511 } 512 513 public void deleteEntry(Entry entry) throws BlogunityException { 514 515 Session session = HibernateUtil.getSession(); 516 try { 517 518 session.delete(entry); 519 520 Date d = entry.getCreateTime(); 522 523 CalendarEntryDAO calendarDAO = new CalendarEntryDAO(); 524 CalendarEntry ce = calendarDAO.getCalendarEntryForBlogByDate(entry.getBlog(), d); 525 526 if (ce != null) { 527 if (ce.getNumberOfMessages() > 1) { 528 calendarDAO.decrementCalendarEntry(ce); 529 } else { 530 calendarDAO.deleteCalendarEntry(ce); 531 } 532 } 533 534 BlogDAO blogDAO = new BlogDAO(); 536 Blog b = entry.getBlog(); 537 b.setLastModified(new Date ()); 538 blogDAO.updateBlog(b); 539 540 } catch (HibernateException e) { 541 log.error("deleteEntry(Entry)", e); 542 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE, "Entry", e)); 543 } 544 545 } 546 547 public void updateEntry(Entry entry) throws BlogunityException { 548 549 Session session = HibernateUtil.getSession(); 550 try { 551 session.update(entry); 552 553 BlogDAO blogDAO = new BlogDAO(); 555 Blog b = entry.getBlog(); 556 b.setLastModified(new Date ()); 557 blogDAO.updateBlog(b); 558 559 } catch (HibernateException e) { 560 log.error("updateEntry(Entry)", e); 561 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE, "Entry", e)); 562 } 563 564 } 565 566 public List getLastPostedEntries(int count) throws BlogunityException { 567 568 Session session = HibernateUtil.getSession(); 569 try { 570 Query q = session.createQuery("from Entry e order by e.createTime desc"); 571 q.setFirstResult(0); 572 q.setMaxResults(count); 573 574 return q.list(); 575 } catch (HibernateException e) { 576 log.error("getLastPostedEntries(int)", e); 577 throw new BlogunityException(I18NStatusFactory.create( 578 I18N.ERRORS.ENTRY_LAST_POSTED_LIST, e)); 579 } 580 } 581 582 589 public int getNumberOfEntries(Blog b) throws BlogunityException { 590 591 Session session = HibernateUtil.getSession(); 592 try { 593 return ((Integer ) session.createFilter(b.getEntries(), "select count(*)").iterate() 594 .next()).intValue(); 595 } catch (HibernateException e) { 596 log.error("getNumberOfEntries(Blog)", e); 597 throw new BlogunityException(I18NStatusFactory 598 .create(I18N.ERRORS.ENTRY_TOTAL_NUMBER, e)); 599 } 600 } 601 602 } | Popular Tags |