1 4 package org.roller.business.hibernate; 5 6 import net.sf.hibernate.Criteria; 7 import net.sf.hibernate.HibernateException; 8 import net.sf.hibernate.Session; 9 import net.sf.hibernate.expression.Expression; 10 import net.sf.hibernate.expression.Junction; 11 import net.sf.hibernate.expression.Order; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.roller.RollerException; 16 import org.roller.business.PersistenceStrategy; 17 import org.roller.business.WeblogManagerImpl; 18 import org.roller.model.Roller; 19 import org.roller.model.RollerFactory; 20 import org.roller.pojos.Assoc; 21 import org.roller.pojos.CommentData; 22 import org.roller.pojos.RefererData; 23 import org.roller.pojos.WeblogCategoryAssoc; 24 import org.roller.pojos.WeblogCategoryData; 25 import org.roller.pojos.WeblogEntryData; 26 import org.roller.pojos.WebsiteData; 27 import org.roller.util.StringUtils; 28 29 import java.util.ArrayList ; 30 import java.util.Date ; 31 import java.util.Iterator ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 35 39 public class HibernateWeblogManagerImpl extends WeblogManagerImpl 40 { 41 static final long serialVersionUID = -3730860865389981439L; 42 43 private static Log mLogger = 44 LogFactory.getFactory().getInstance(HibernateWeblogManagerImpl.class); 45 46 50 public HibernateWeblogManagerImpl(PersistenceStrategy strategy) 51 { 52 super(strategy); 53 mLogger.debug("Instantiating Weblog Manager"); 54 } 55 56 public List getComments(String entryId, boolean nospam) throws RollerException 57 { 58 if (entryId == null) 59 throw new RollerException("entryId is null"); 60 61 try 62 { 63 Session session = ((HibernateStrategy)mStrategy).getSession(); 64 Criteria criteria = session.createCriteria(CommentData.class); 65 criteria.createAlias("weblogEntry","e"); 66 criteria.add(Expression.eq("e.id", entryId)); 67 if (nospam) 68 { 69 criteria.add( 70 Expression.not(Expression.eq("spam", Boolean.TRUE))); 71 } 72 criteria.addOrder(Order.asc("postTime")); 73 return criteria.list(); 74 } 75 catch (HibernateException e) 76 { 77 throw new RollerException(e); 78 } 79 } 80 81 82 85 public List getNextPrevEntries( 86 WeblogEntryData current, String catName, int maxEntries, boolean next) 87 throws RollerException 88 { 89 if (catName != null && catName.trim().equals("/")) 90 { 91 catName = null; 92 } 93 Junction conjunction = Expression.conjunction(); 94 conjunction.add(Expression.eq("website", current.getWebsite())); 95 conjunction.add(Expression.eq("publishEntry", Boolean.TRUE)); 96 97 if (next) 98 { 99 conjunction.add(Expression.gt("pubTime", current.getPubTime())); 100 } 101 else 102 { 103 conjunction.add(Expression.lt("pubTime", current.getPubTime())); 104 } 105 106 if (catName != null) 107 { 108 WeblogCategoryData category = 109 getWeblogCategoryByPath(current.getWebsite(), null, catName); 110 if (category != null) 111 { 112 conjunction.add(Expression.eq("category", category)); 113 } 114 else 115 { 116 throw new RollerException("Cannot find category: "+catName); 117 } 118 } 119 120 try 121 { 122 Session session = ((HibernateStrategy)mStrategy).getSession(); 123 Criteria criteria = session.createCriteria(WeblogEntryData.class); 124 criteria.addOrder(Order.desc("pubTime")); 125 criteria.add(conjunction); 126 criteria.setMaxResults(maxEntries); 127 List results = criteria.list(); 128 return results; 129 } 130 catch (HibernateException e) 131 { 132 throw new RollerException(e); 133 } 134 } 135 136 139 public WeblogCategoryData getRootWeblogCategory(WebsiteData website) 140 throws RollerException 141 { 142 if (website == null) 143 throw new RollerException("website is null"); 144 145 try 146 { 147 Session session = ((HibernateStrategy)mStrategy).getSession(); 148 Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); 149 criteria.createAlias("category","c"); 150 151 criteria.add(Expression.eq("c.website", website)); 152 criteria.add(Expression.isNull("ancestorCategory")); 153 criteria.add(Expression.eq("relation", WeblogCategoryAssoc.PARENT)); 154 155 criteria.setMaxResults(1); 156 157 List list = criteria.list(); 158 return ((WeblogCategoryAssoc)list.get(0)).getCategory(); 159 } 160 catch (HibernateException e) 161 { 162 throw new RollerException(e); 163 } 164 } 165 166 170 public List getWeblogCategories(WebsiteData website, boolean includeRoot) 171 throws RollerException 172 { 173 if (website == null) 174 throw new RollerException("website is null"); 175 176 if (includeRoot) return getWeblogCategories(website); 177 178 try 179 { 180 Session session = ((HibernateStrategy)mStrategy).getSession(); 181 Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); 182 criteria.createAlias("category", "c"); 183 criteria.add(Expression.eq("c.website", website)); 184 criteria.add(Expression.isNotNull("ancestorCategory")); 185 criteria.add(Expression.eq("relation", "PARENT")); 186 Iterator assocs = criteria.list().iterator(); 187 List cats = new ArrayList (); 188 while (assocs.hasNext()) 189 { 190 WeblogCategoryAssoc assoc = (WeblogCategoryAssoc) assocs.next(); 191 cats.add(assoc.getCategory()); 192 } 193 return cats; 194 } 195 catch (HibernateException e) 196 { 197 throw new RollerException(e); 198 } 199 } 200 201 public List getWeblogCategories(WebsiteData website) throws RollerException 202 { 203 if (website == null) 204 throw new RollerException("website is null"); 205 206 try 207 { 208 Session session = ((HibernateStrategy)mStrategy).getSession(); 209 Criteria criteria = session.createCriteria(WeblogCategoryData.class); 210 criteria.add(Expression.eq("website", website)); 211 return criteria.list(); 212 } 213 catch (HibernateException e) 214 { 215 throw new RollerException(e); 216 } 217 } 218 219 228 public List getWeblogEntries( 229 WebsiteData website, 230 Date startDate, 231 Date endDate, 232 String catName, 233 String status, 234 Integer maxEntries, 235 Boolean pinned) throws RollerException 236 { 237 WeblogCategoryData cat = null; 238 if (StringUtils.isNotEmpty(catName) && website != null) 239 { 240 cat = getWeblogCategoryByPath(website, catName); 241 if (cat == null) catName = null; 242 } 243 if (catName != null && catName.trim().equals("/")) 244 { 245 catName = null; 246 } 247 248 try 249 { 250 Session session = ((HibernateStrategy)mStrategy).getSession(); 251 Criteria criteria = session.createCriteria(WeblogEntryData.class); 252 253 if (website != null) 254 { 255 criteria.add(Expression.eq("website", website)); 256 } 257 else 258 { 259 criteria.createAlias("website","w"); 260 criteria.add(Expression.eq("w.isEnabled", Boolean.TRUE)); 261 } 262 263 if (startDate != null) 264 { 265 criteria.add( 266 Expression.ge("pubTime", startDate)); 267 } 268 269 if (endDate != null) 270 { 271 criteria.add( 272 Expression.le("pubTime", endDate)); 273 } 274 275 if (cat != null && website != null) 276 { 277 criteria.add(Expression.eq("category", cat)); 278 } 279 280 if (status != null && status.equals(DRAFT_ONLY)) 281 { 282 criteria.add( 283 Expression.eq("publishEntry", Boolean.FALSE)); 284 } 285 else if (status != null && status.equals(PUB_ONLY)) 286 { 287 criteria.add( 288 Expression.eq("publishEntry", Boolean.TRUE)); 289 } 290 291 if (pinned != null) 292 { 293 criteria.add(Expression.eq("pinnedToMain", pinned)); 294 } 295 296 criteria.addOrder(Order.desc("pubTime")); 297 298 if (maxEntries != null) 299 { 300 criteria.setMaxResults(maxEntries.intValue()); 301 } 302 return criteria.list(); 303 } 304 catch (HibernateException e) 305 { 306 mLogger.error(e); 307 throw new RollerException(e); 308 } 309 } 310 311 314 public WeblogEntryData getWeblogEntryByAnchor( 315 WebsiteData website, String anchor) throws RollerException 316 { 317 if (website == null) 318 throw new RollerException("Website is null"); 319 320 if (anchor == null) 321 throw new RollerException("Anchor is null"); 322 323 Session session = ((HibernateStrategy)mStrategy).getSession(); 324 Criteria criteria = session.createCriteria(WeblogEntryData.class); 325 criteria.add(Expression.conjunction() 326 .add(Expression.eq("website",website)) 327 .add(Expression.eq("anchor",anchor))); 328 criteria.addOrder(Order.desc("pubTime")); 329 criteria.setMaxResults(1); 330 try 331 { 332 List list = criteria.list(); 333 return list.size()!=0 ? (WeblogEntryData)list.get(0) : null; 334 } 335 catch (HibernateException e) 336 { 337 throw new RollerException(e); 338 } 339 } 340 348 public Date getWeblogLastPublishTime( String userName, String catName ) 349 throws RollerException 350 { 351 WeblogCategoryData cat = null; 352 Roller mRoller = RollerFactory.getRoller(); 353 if (userName != null) 354 { 355 WebsiteData website = mRoller.getUserManager().getWebsite(userName); 356 if (catName != null && website != null) 357 { 358 cat = getWeblogCategoryByPath(website, null, catName); 359 if (cat == null) catName = null; 360 } 361 if (catName != null && catName.trim().equals("/")) 362 { 363 catName = null; 364 } 365 } 366 367 Session session = ((HibernateStrategy)mStrategy).getSession(); 368 Criteria criteria = session.createCriteria(WeblogEntryData.class); 369 criteria.add(Expression.eq("publishEntry", Boolean.TRUE)); 370 criteria.add(Expression.le("pubTime", new Date ())); 371 372 try 373 { 374 if ( userName != null ) 375 { 376 WebsiteData website = mRoller.getUserManager().getWebsite(userName); 377 criteria.add(Expression.eq("website", website)); 378 } 379 380 if ( cat != null ) 381 { 382 criteria.add(Expression.eq("category", cat)); 383 } 384 385 criteria.addOrder(Order.desc("pubTime")); 386 criteria.setMaxResults(1); 387 List list = criteria.list(); 388 if (list.size() > 0) 389 { 390 return ((WeblogEntryData)list.get(0)).getPubTime(); 391 } 392 else 393 { 394 return null; 395 } 396 } 397 catch (HibernateException e) 398 { 399 throw new RollerException(e); 400 } 401 } 402 403 public void moveWeblogCategoryContents(String srcId, String destId) 404 throws RollerException 405 { 406 WeblogCategoryData srcCd = 407 (WeblogCategoryData) mStrategy.load( 408 srcId, WeblogCategoryData.class); 409 410 WeblogCategoryData destCd = 411 (WeblogCategoryData) mStrategy.load( 412 destId, WeblogCategoryData.class); 413 414 if (destCd.descendentOf(srcCd)) 415 { 416 throw new RollerException( 417 "ERROR cannot move parent category into it's own child"); 418 } 419 420 List results = retrieveWeblogEntries(srcCd, true); 422 423 Iterator iter = results.iterator(); 425 WebsiteData website = destCd.getWebsite(); 426 while (iter.hasNext()) 427 { 428 WeblogEntryData entry = (WeblogEntryData) iter.next(); 429 entry.setCategory(destCd); 430 entry.setWebsite(website); 431 entry.save(); 432 } 433 434 437 if (srcCd.getWebsite().getDefaultCategory().getId().equals(srcId) 438 || srcCd.getWebsite().getDefaultCategory().descendentOf(srcCd)) 439 { 440 srcCd.getWebsite().setDefaultCategory(destCd); 441 } 442 443 if (srcCd.getWebsite().getBloggerCategory().getId().equals(srcId) 444 || srcCd.getWebsite().getBloggerCategory().descendentOf(srcCd)) 445 { 446 srcCd.getWebsite().setBloggerCategory(destCd); 447 } 448 } 449 450 public List retrieveWeblogEntries(WeblogCategoryData cat, boolean subcats) 451 throws RollerException 452 { 453 try 454 { 455 Session session = ((HibernateStrategy)mStrategy).getSession(); 456 List entries = new LinkedList (); 457 458 if (subcats) 459 { 460 Criteria assocsQuery = 462 session.createCriteria(WeblogCategoryAssoc.class); 463 assocsQuery.add(Expression.eq("ancestorCategory", cat)); 464 Iterator assocs = assocsQuery.list().iterator(); 465 while (assocs.hasNext()) 466 { 467 WeblogCategoryAssoc assoc = (WeblogCategoryAssoc)assocs.next(); 468 Criteria entriesQuery = 469 session.createCriteria(WeblogEntryData.class); 470 entriesQuery.add( 471 Expression.eq("category", assoc.getCategory())); 472 Iterator entryIter = entriesQuery.list().iterator(); 473 while (entryIter.hasNext()) 474 { 475 WeblogEntryData entry = (WeblogEntryData)entryIter.next(); 476 entries.add(entry); 477 } 478 } 479 } 480 481 Criteria entriesQuery = 483 session.createCriteria(WeblogEntryData.class); 484 entriesQuery.add(Expression.eq("category", cat)); 485 Iterator entryIter = entriesQuery.list().iterator(); 486 while (entryIter.hasNext()) 487 { 488 WeblogEntryData entry = (WeblogEntryData)entryIter.next(); 489 entries.add(entry); 490 } 491 return entries; 492 } 493 catch (HibernateException e) 494 { 495 throw new RollerException(e); 496 } 497 } 498 499 503 public void removeWeblogEntryContents(WeblogEntryData entry) 504 throws RollerException 505 { 506 try 507 { 508 Session session = ((HibernateStrategy)mStrategy).getSession(); 509 510 Criteria refererQuery = session.createCriteria(RefererData.class); 512 refererQuery.add(Expression.eq("weblogEntry", entry)); 513 List entries = refererQuery.list(); 514 for (Iterator iter = entries.iterator(); iter.hasNext();) 515 { 516 RefererData referer = (RefererData) iter.next(); 517 referer.remove(); 518 } 519 520 526 List comments = RollerFactory.getRoller().getWeblogManager().getComments(entry.getId(), false); 527 for (Iterator iter = comments.iterator(); iter.hasNext();) 528 { 529 CommentData comment = (CommentData) iter.next(); 530 comment.remove(); 531 } 532 } 533 catch (HibernateException e) 534 { 535 throw new RollerException(e); 536 } 537 } 538 539 543 public String createAnchor(WeblogEntryData entry) throws RollerException 544 { 545 try 546 { 547 String base = entry.createAnchorBase(); 549 String name = base; 550 int count = 0; 551 552 while (true) 553 { 554 if (count > 0) 555 { 556 name = base + count; 557 } 558 559 Session session = ((HibernateStrategy)mStrategy).getSession(); 560 Criteria criteria = session.createCriteria(WeblogEntryData.class); 561 criteria.add(Expression.eq("website", entry.getWebsite())); 562 criteria.add(Expression.eq("anchor", name)); 563 564 List results = criteria.list(); 565 566 if (results.size() < 1) 567 { 568 break; 569 } 570 else 571 { 572 count++; 573 } 574 } 575 return name; 576 } 577 catch (HibernateException e) 578 { 579 throw new RollerException(e); 580 } 581 } 582 583 586 public boolean isDuplicateWeblogCategoryName(WeblogCategoryData cat) 587 throws RollerException 588 { 589 WeblogCategoryData parent = 591 null == cat.getId() ? (WeblogCategoryData)cat.getNewParent() : cat.getParent(); 592 593 if (null != parent) { 595 List sameNames; 596 try 597 { 598 Session session = ((HibernateStrategy)mStrategy).getSession(); 599 Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); 600 criteria.createAlias("category", "c"); 601 criteria.add(Expression.eq("c.name", cat.getName())); 602 criteria.add(Expression.eq("ancestorCategory", parent)); 603 criteria.add(Expression.eq("relation", Assoc.PARENT)); 604 sameNames = criteria.list(); 605 } 606 catch (HibernateException e) 607 { 608 throw new RollerException(e); 609 } 610 if (sameNames.size() > 1) 611 { 612 return true; 613 } 614 } 615 return false; 616 } 617 618 621 public boolean isWeblogCategoryInUse(WeblogCategoryData cat) 622 throws RollerException 623 { 624 try 625 { 626 Session session = ((HibernateStrategy)mStrategy).getSession(); 627 Criteria criteria = session.createCriteria(WeblogEntryData.class); 628 criteria.add(Expression.eq("category", cat)); 629 criteria.setMaxResults(1); 630 int entryCount = criteria.list().size(); 631 632 if (entryCount > 0) 633 { 634 return true; 635 } 636 637 Iterator cats = cat.getWeblogCategories().iterator(); 638 while (cats.hasNext()) 639 { 640 WeblogCategoryData childCat = (WeblogCategoryData)cats.next(); 641 if (childCat.isInUse()) 642 { 643 return true; 644 } 645 } 646 647 if (cat.getWebsite().getBloggerCategory().equals(cat)) 648 { 649 return true; 650 } 651 652 if (cat.getWebsite().getDefaultCategory().equals(cat)) 653 { 654 return true; 655 } 656 657 return false; 658 } 659 catch (HibernateException e) 660 { 661 throw new RollerException(e); 662 } 663 } 664 665 public boolean isDescendentOf( 666 WeblogCategoryData child, WeblogCategoryData ancestor) 667 throws RollerException 668 { 669 boolean ret = false; 670 try 671 { 672 Session session = ((HibernateStrategy)mStrategy).getSession(); 673 Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); 674 criteria.add(Expression.eq("category", child)); 675 criteria.add(Expression.eq("ancestorCategory", ancestor)); 676 ret = criteria.list().size() > 0; 677 } 678 catch (HibernateException e) 679 { 680 throw new RollerException(e); 681 } 682 return ret; 683 } 684 685 689 public Assoc getWeblogCategoryParentAssoc(WeblogCategoryData cat) 690 throws RollerException 691 { 692 try 693 { 694 Session session = ((HibernateStrategy)mStrategy).getSession(); 695 Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); 696 criteria.add(Expression.eq("category", cat)); 697 criteria.add(Expression.eq("relation", Assoc.PARENT)); 698 List parents = criteria.list(); 699 if (parents.size() > 1) 700 { 701 throw new RollerException("ERROR: more than one parent"); 702 } 703 else if (parents.size() == 1) 704 { 705 return (Assoc) parents.get(0); 706 } 707 else 708 { 709 return null; 710 } 711 } 712 catch (HibernateException e) 713 { 714 throw new RollerException(e); 715 } 716 } 717 718 722 public List getWeblogCategoryChildAssocs(WeblogCategoryData cat) 723 throws RollerException 724 { 725 try 726 { 727 Session session = ((HibernateStrategy)mStrategy).getSession(); 728 Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); 729 criteria.add(Expression.eq("ancestorCategory", cat)); 730 criteria.add(Expression.eq("relation", Assoc.PARENT)); 731 return criteria.list(); 732 } 733 catch (HibernateException e) 734 { 735 throw new RollerException(e); 736 } 737 } 738 739 743 public List getAllWeblogCategoryDecscendentAssocs(WeblogCategoryData cat) 744 throws RollerException 745 { 746 try 747 { 748 Session session = ((HibernateStrategy)mStrategy).getSession(); 749 Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); 750 criteria.add(Expression.eq("ancestorCategory", cat)); 751 return criteria.list(); 752 } 753 catch (HibernateException e) 754 { 755 throw new RollerException(e); 756 } 757 } 758 759 762 public List getWeblogCategoryAncestorAssocs(WeblogCategoryData cat) 763 throws RollerException 764 { 765 try 766 { 767 Session session = ((HibernateStrategy)mStrategy).getSession(); 768 Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); 769 criteria.add(Expression.eq("category", cat)); 770 return criteria.list(); 771 } 772 catch (HibernateException e) 773 { 774 throw new RollerException(e); 775 } 776 } 777 778 784 public List getRecentComments(WebsiteData website, int maxCount) throws RollerException 785 { 786 try 787 { 788 Session session = ((HibernateStrategy)mStrategy).getSession(); 789 Criteria criteria = session.createCriteria(CommentData.class); 790 criteria.add( 791 Expression.not(Expression.eq("spam", Boolean.TRUE))); 792 if (website != null) 793 { 794 criteria.createAlias("weblogEntry","e"); 795 criteria.add(Expression.eq("e.website", website)); 796 } 797 criteria.addOrder(Order.desc("postTime")); 798 criteria.setMaxResults(maxCount); 799 return criteria.list(); 800 } 801 catch (HibernateException e) 802 { 803 throw new RollerException(e); 804 } 805 } 806 807 } 808 | Popular Tags |