1 18 19 package org.apache.roller.business.hibernate; 20 21 import java.util.ArrayList ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.TreeMap ; 29 import org.apache.roller.pojos.StatCount; 30 import org.hibernate.Criteria; 31 import org.hibernate.HibernateException; 32 import org.hibernate.Session; 33 import org.hibernate.criterion.Expression; 34 import org.hibernate.criterion.MatchMode; 35 import org.hibernate.criterion.Order; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.hibernate.criterion.SimpleExpression; 39 import org.apache.roller.RollerException; 40 import org.apache.roller.config.RollerConfig; 41 import org.apache.roller.model.AutoPingManager; 42 import org.apache.roller.model.BookmarkManager; 43 import org.apache.roller.model.PingTargetManager; 44 import org.apache.roller.model.RollerFactory; 45 import org.apache.roller.model.UserManager; 46 import org.apache.roller.model.WeblogManager; 47 import org.apache.roller.pojos.AutoPingData; 48 import org.apache.roller.pojos.BookmarkData; 49 import org.apache.roller.pojos.FolderData; 50 import org.apache.roller.pojos.WeblogTemplate; 51 import org.apache.roller.pojos.PermissionsData; 52 import org.apache.roller.pojos.PingQueueEntryData; 53 import org.apache.roller.pojos.PingTargetData; 54 import org.apache.roller.pojos.RefererData; 55 import org.apache.roller.pojos.UserData; 56 import org.apache.roller.pojos.WeblogCategoryData; 57 import org.apache.roller.pojos.WeblogEntryData; 58 import org.apache.roller.pojos.WebsiteData; 59 import org.hibernate.Query; 60 61 64 public class HibernateUserManagerImpl implements UserManager { 65 66 static final long serialVersionUID = -5128460637997081121L; 67 private static Log log = LogFactory.getLog(HibernateUserManagerImpl.class); 68 private HibernatePersistenceStrategy strategy = null; 69 70 private Map weblogHandleToIdMap = new Hashtable (); 72 73 private Map userNameToIdMap = new Hashtable (); 75 76 public HibernateUserManagerImpl(HibernatePersistenceStrategy strat) { 77 log.debug("Instantiating Hibernate User Manager"); 78 79 this.strategy = strat; 80 } 81 82 85 public void saveWebsite(WebsiteData website) throws RollerException { 86 87 website.setLastModified(new java.util.Date ()); 88 strategy.store(website); 89 } 90 91 public void removeWebsite(WebsiteData weblog) throws RollerException { 92 93 this.removeWebsiteContents(weblog); 95 this.strategy.remove(weblog); 96 97 this.weblogHandleToIdMap.remove(weblog.getHandle()); 99 } 100 101 105 private void removeWebsiteContents(WebsiteData website) 106 throws HibernateException, RollerException { 107 108 Session session = this.strategy.getSession(); 109 110 BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager(); 111 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 112 113 Criteria criteria = session.createCriteria(PingQueueEntryData.class); 115 criteria.add(Expression.eq("website", website)); 116 List queueEntries = criteria.list(); 117 118 AutoPingManager autoPingMgr = RollerFactory.getRoller().getAutopingManager(); 120 List autopings = autoPingMgr.getAutoPingsByWebsite(website); 121 Iterator it = autopings.iterator(); 122 while(it.hasNext()) { 123 this.strategy.remove((AutoPingData) it.next()); 124 } 125 126 PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager(); 128 List pingtargets = pingTargetMgr.getCustomPingTargets(website); 129 it = pingtargets.iterator(); 130 while(it.hasNext()) { 131 this.strategy.remove((PingTargetData) it.next()); 132 } 133 134 Criteria entryQuery = session.createCriteria(WeblogEntryData.class); 136 entryQuery.add(Expression.eq("website", website)); 137 List entries = entryQuery.list(); 138 for (Iterator iter = entries.iterator(); iter.hasNext();) { 139 WeblogEntryData entry = (WeblogEntryData) iter.next(); 140 141 this.strategy.remove(entry); 142 } 143 144 Criteria refererQuery = session.createCriteria(RefererData.class); 146 refererQuery.add(Expression.eq("website", website)); 147 List referers = refererQuery.list(); 148 for (Iterator iter = referers.iterator(); iter.hasNext();) { 149 RefererData referer = (RefererData) iter.next(); 150 this.strategy.remove(referer); 151 } 152 153 154 Criteria pageQuery = session.createCriteria(WeblogTemplate.class); 156 pageQuery.add(Expression.eq("website", website)); 157 List pages = pageQuery.list(); 158 for (Iterator iter = pages.iterator(); iter.hasNext();) { 159 WeblogTemplate page = (WeblogTemplate) iter.next(); 160 this.strategy.remove(page); 161 } 162 163 FolderData rootFolder = bmgr.getRootFolder(website); 165 if (null != rootFolder) { 166 this.strategy.remove(rootFolder); 167 168 Iterator allFolders = bmgr.getAllFolders(website).iterator(); 170 while (allFolders.hasNext()) { 171 FolderData aFolder = (FolderData)allFolders.next(); 172 bmgr.removeFolderContents(aFolder); 173 this.strategy.remove(aFolder); 174 } 175 } 176 177 WeblogCategoryData rootCat = wmgr.getRootWeblogCategory(website); 179 if (null != rootCat) { 180 this.strategy.remove(rootCat); 181 } 182 183 } 184 185 public void saveUser(UserData data) throws RollerException { 186 this.strategy.store(data); 187 } 188 189 public void removeUser(UserData user) throws RollerException { 190 this.strategy.remove(user); 191 192 this.userNameToIdMap.remove(user.getUserName()); 194 } 195 196 public void savePermissions(PermissionsData perms) throws RollerException { 197 this.strategy.store(perms); 198 } 199 200 public void removePermissions(PermissionsData perms) throws RollerException { 201 this.strategy.remove(perms); 202 } 203 204 207 public void savePage(WeblogTemplate page) throws RollerException { 208 this.strategy.store(page); 209 210 RollerFactory.getRoller().getUserManager().saveWebsite(page.getWebsite()); 212 } 213 214 public void removePage(WeblogTemplate page) throws RollerException { 215 this.strategy.remove(page); 216 } 217 218 public void addUser(UserData newUser) throws RollerException { 219 220 if(newUser == null) 221 throw new RollerException("cannot add null user"); 222 223 boolean adminUser = false; 225 List existingUsers = this.getUsers(0, 1); 226 if(existingUsers.size() == 0) { 227 adminUser = true; 229 } 230 231 if(getUserByUserName(newUser.getUserName()) != null || 232 getUserByUserName(newUser.getUserName().toLowerCase()) != null) { 233 throw new RollerException("error.add.user.userNameInUse"); 234 } 235 236 newUser.grantRole("editor"); 237 if(adminUser) { 238 newUser.grantRole("admin"); 239 } 240 241 this.strategy.store(newUser); 242 } 243 244 public void addWebsite(WebsiteData newWeblog) throws RollerException { 245 246 this.strategy.store(newWeblog); 247 this.addWeblogContents(newWeblog); 248 } 249 250 private void addWeblogContents(WebsiteData newWeblog) throws RollerException { 251 252 UserManager umgr = RollerFactory.getRoller().getUserManager(); 253 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 254 255 PermissionsData perms = new PermissionsData(); 257 perms.setUser(newWeblog.getCreator()); 258 perms.setWebsite(newWeblog); 259 perms.setPending(false); 260 perms.setPermissionMask(PermissionsData.ADMIN); 261 this.strategy.store(perms); 262 263 WeblogCategoryData rootCat = new WeblogCategoryData( 265 null, newWeblog, null, "root", "root", null ); this.strategy.store(rootCat); 272 273 String cats = RollerConfig.getProperty("newuser.categories"); 274 WeblogCategoryData firstCat = rootCat; 275 if (cats != null) { 276 String [] splitcats = cats.split(","); 277 for (int i=0; i<splitcats.length; i++) { 278 WeblogCategoryData c = new WeblogCategoryData( 279 null, newWeblog, rootCat, splitcats[i], splitcats[i], null ); if (i == 0) firstCat = c; 286 this.strategy.store(c); 287 } 288 } 289 newWeblog.setBloggerCategory(firstCat); 291 292 newWeblog.setDefaultCategory(rootCat); 294 295 this.strategy.store(newWeblog); 296 297 FolderData root = new FolderData( 299 null, "root", "root", newWeblog); 300 this.strategy.store(root); 301 302 Integer zero = new Integer (0); 303 String blogroll = RollerConfig.getProperty("newuser.blogroll"); 304 if (blogroll != null) { 305 String [] splitroll = blogroll.split(","); 306 for (int i=0; i<splitroll.length; i++) { 307 String [] rollitems = splitroll[i].split("\\|"); 308 if (rollitems != null && rollitems.length > 1) { 309 BookmarkData b = new BookmarkData( 310 root, rollitems[0], "", rollitems[1].trim(), null, zero, zero, null); this.strategy.store(b); 319 } 320 } 321 } 322 323 PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager(); 325 AutoPingManager autoPingMgr = RollerFactory.getRoller().getAutopingManager(); 326 327 Iterator pingTargets = pingTargetMgr.getCommonPingTargets().iterator(); 328 PingTargetData pingTarget = null; 329 while(pingTargets.hasNext()) { 330 pingTarget = (PingTargetData) pingTargets.next(); 331 332 if(pingTarget.isAutoEnabled()) { 333 AutoPingData autoPing = new AutoPingData(null, pingTarget, newWeblog); 334 autoPingMgr.saveAutoPing(autoPing); 335 } 336 } 337 } 338 339 343 public PermissionsData inviteUser(WebsiteData website, 344 UserData user, short mask) throws RollerException { 345 346 if (website == null) throw new RollerException("Website cannot be null"); 347 if (user == null) throw new RollerException("User cannot be null"); 348 349 PermissionsData perms = new PermissionsData(); 350 perms.setWebsite(website); 351 perms.setUser(user); 352 perms.setPermissionMask(mask); 353 this.strategy.store(perms); 354 355 return perms; 356 } 357 358 363 public void retireUser(WebsiteData website, UserData user) throws RollerException { 364 365 if (website == null) throw new RollerException("Website cannot be null"); 366 if (user == null) throw new RollerException("User cannot be null"); 367 368 Iterator perms = website.getPermissions().iterator(); 369 PermissionsData target = null; 370 while (perms.hasNext()) { 371 PermissionsData pd = (PermissionsData)perms.next(); 372 if (pd.getUser().getId().equals(user.getId())) { 373 target = pd; 374 break; 375 } 376 } 377 if (target == null) throw new RollerException("User not member of website"); 378 379 website.removePermission(target); 380 this.strategy.remove(target); 381 } 382 383 public WebsiteData getWebsite(String id) throws RollerException { 384 return (WebsiteData) this.strategy.load(id,WebsiteData.class); 385 } 386 387 public WebsiteData getWebsiteByHandle(String handle) throws RollerException { 388 return getWebsiteByHandle(handle, Boolean.TRUE); 389 } 390 391 394 public WebsiteData getWebsiteByHandle(String handle, Boolean enabled) 395 throws RollerException { 396 397 if (handle==null ) 398 throw new RollerException("Handle cannot be null"); 399 400 if(this.weblogHandleToIdMap.containsKey(handle)) { 403 404 WebsiteData weblog = this.getWebsite((String ) this.weblogHandleToIdMap.get(handle)); 405 if(weblog != null) { 406 if(enabled == null || enabled.equals(weblog.getEnabled())) { 408 log.debug("weblogHandleToId CACHE HIT - "+handle); 409 return weblog; 410 } 411 } else { 412 this.weblogHandleToIdMap.remove(handle); 414 } 415 } 416 417 try { 419 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 420 Criteria criteria = session.createCriteria(WebsiteData.class); 421 422 if (enabled != null) { 423 criteria.add( 424 Expression.conjunction() 425 .add(new IgnoreCaseEqExpression("handle", handle)) 426 .add(Expression.eq("enabled", enabled))); 427 } else { 428 criteria.add( 429 Expression.conjunction() 430 .add(Expression.eq("handle", handle))); 431 } 432 433 WebsiteData website = (WebsiteData) criteria.uniqueResult(); 434 435 if(website != null) { 437 log.debug("weblogHandleToId CACHE MISS - "+handle); 438 this.weblogHandleToIdMap.put(website.getHandle(), website.getId()); 439 } 440 441 return website; 442 } catch (HibernateException e) { 443 throw new RollerException(e); 444 } 445 } 446 447 450 public List getWebsites( 451 UserData user, Boolean enabled, Boolean active, 452 Date startDate, Date endDate, int offset, int length) 453 throws RollerException { 454 if (endDate == null) endDate = new Date (); 456 try { 457 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 458 Criteria criteria = session.createCriteria(WebsiteData.class); 459 if (user != null) { 460 criteria.createAlias("permissions","permissions"); 461 criteria.add(Expression.eq("permissions.user", user)); 462 criteria.add(Expression.eq("permissions.pending", Boolean.FALSE)); 463 } criteria.add(Expression.lt("dateCreated", endDate)); 464 if (startDate != null) { 465 criteria.add(Expression.gt("dateCreated", startDate)); 466 } 467 if (enabled != null) { 468 criteria.add(Expression.eq("enabled", enabled)); 469 } 470 if (active != null) { 471 criteria.add(Expression.eq("active", active)); 472 } 473 if (offset != 0) { 474 criteria.setFirstResult(offset); 475 } 476 if (length != -1) { 477 criteria.setMaxResults(length); 478 } 479 criteria.addOrder(Order.desc("dateCreated")); 480 return criteria.list(); 481 } catch (HibernateException e) { 482 throw new RollerException(e); 483 } 484 } 485 486 public UserData getUser(String id) throws RollerException { 487 return (UserData)this.strategy.load(id,UserData.class); 488 } 489 490 public UserData getUserByUserName(String userName) throws RollerException { 491 return getUserByUserName(userName, Boolean.TRUE); 492 } 493 494 public UserData getUserByUserName(String userName, Boolean enabled) 495 throws RollerException { 496 497 if (userName==null ) 498 throw new RollerException("userName cannot be null"); 499 500 if(this.userNameToIdMap.containsKey(userName)) { 503 504 UserData user = this.getUser((String ) this.userNameToIdMap.get(userName)); 505 if(user != null) { 506 if(enabled == null || enabled.equals(user.getEnabled())) { 508 log.debug("userNameToIdMap CACHE HIT - "+userName); 509 return user; 510 } 511 } else { 512 this.userNameToIdMap.remove(userName); 514 } 515 } 516 517 try { 519 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 520 Criteria criteria = session.createCriteria(UserData.class); 521 522 if (enabled != null) { 523 criteria.add( 524 Expression.conjunction() 525 .add(Expression.eq("userName", userName)) 526 .add(Expression.eq("enabled", enabled))); 527 } else { 528 criteria.add( 529 Expression.conjunction() 530 .add(Expression.eq("userName", userName))); 531 } 532 533 UserData user = (UserData) criteria.uniqueResult(); 534 535 if(user != null) { 537 log.debug("userNameToIdMap CACHE MISS - "+userName); 538 this.userNameToIdMap.put(user.getUserName(), user.getId()); 539 } 540 541 return user; 542 } catch (HibernateException e) { 543 throw new RollerException(e); 544 } 545 } 546 547 public List getUsers(int offset, int length) throws RollerException { 548 return getUsers(Boolean.TRUE, null, null, offset, length); 549 } 550 551 public List getUsers(Boolean enabled, Date startDate, Date endDate, int offset, int length) throws RollerException { 552 if (endDate == null) endDate = new Date (); 553 try { 554 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 555 Criteria criteria = session.createCriteria(UserData.class); 556 criteria.add(Expression.lt("dateCreated", endDate)); 557 if (startDate != null) { 558 criteria.add(Expression.gt("dateCreated", startDate)); 559 } 560 if (enabled != null) { 561 criteria.add(Expression.eq("enabled", enabled)); 562 } 563 if (offset != 0) { 564 criteria.setFirstResult(offset); 565 } 566 if (length != -1) { 567 criteria.setMaxResults(length); 568 } 569 criteria.addOrder(Order.desc("dateCreated")); 570 return criteria.list(); 571 } catch (HibernateException e) { 572 throw new RollerException(e); 573 } 574 } 575 576 579 public List getUsers(WebsiteData website, Boolean enabled, int offset, int length) throws RollerException { 580 581 try { 582 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 583 Criteria criteria = session.createCriteria(UserData.class); 584 if (website != null) { 585 criteria.createAlias("permissions","permissions"); 586 criteria.add(Expression.eq("permissions.website", website)); 587 } 588 if (enabled != null) { 589 criteria.add(Expression.eq("enabled", enabled)); 590 } 591 if (offset != 0) { 592 criteria.setFirstResult(offset); 593 } 594 if (length != -1) { 595 criteria.setMaxResults(length); 596 } 597 return criteria.list(); 598 } catch (HibernateException e) { 599 throw new RollerException(e); 600 } 601 } 602 603 public List getUsersStartingWith(String startsWith, Boolean enabled, 604 int offset, int length) throws RollerException { 605 606 List results = new ArrayList (); 607 try { 608 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 609 Criteria criteria = session.createCriteria(UserData.class); 610 611 if (enabled != null) { 612 criteria.add(Expression.eq("enabled", enabled)); 613 } 614 if (startsWith != null) { 615 criteria.add(Expression.disjunction() 616 .add(Expression.like("userName", startsWith, MatchMode.START)) 617 .add(Expression.like("emailAddress", startsWith, MatchMode.START))); 618 } 619 if (offset != 0) { 620 criteria.setFirstResult(offset); 621 } 622 if (length != -1) { 623 criteria.setMaxResults(length); 624 } 625 results = criteria.list(); 626 } catch (HibernateException e) { 627 throw new RollerException(e); 628 } 629 return results; 630 } 631 632 public WeblogTemplate getPage(String id) throws RollerException { 633 if (id != null && id.endsWith(".vm")) return null; 635 636 return (WeblogTemplate)this.strategy.load(id,WeblogTemplate.class); 637 } 638 639 642 public WeblogTemplate getPageByLink(WebsiteData website, String pagelink) 643 throws RollerException { 644 645 if (website == null) 646 throw new RollerException("userName is null"); 647 648 if (pagelink == null) 649 throw new RollerException("Pagelink is null"); 650 651 try { 652 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 653 Criteria criteria = session.createCriteria(WeblogTemplate.class); 654 criteria.add(Expression.eq("website",website)); 655 criteria.add(Expression.eq("link",pagelink)); 656 criteria.setMaxResults(1); 657 658 List list = criteria.list(); 659 return list.size()!=0 ? (WeblogTemplate)list.get(0) : null; 660 } catch (HibernateException e) { 661 throw new RollerException(e); 662 } 663 } 664 665 668 public WeblogTemplate getPageByName(WebsiteData website, String pagename) 669 throws RollerException { 670 671 if (website == null) 672 throw new RollerException("website is null"); 673 674 if (pagename == null) 675 throw new RollerException("Page name is null"); 676 677 try { 678 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 679 Criteria criteria = session.createCriteria(WeblogTemplate.class); 680 criteria.add(Expression.eq("website", website)); 681 criteria.add(Expression.eq("name", pagename)); 682 criteria.setMaxResults(1); 683 684 List list = criteria.list(); 685 return list.size()!=0? (WeblogTemplate)list.get(0) : null; 686 } catch (HibernateException e) { 687 throw new RollerException(e); 688 } 689 } 690 691 694 public List getPages(WebsiteData website) throws RollerException { 695 696 if (website == null) 697 throw new RollerException("website is null"); 698 699 try { 700 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 701 Criteria criteria = session.createCriteria(WeblogTemplate.class); 702 criteria.add(Expression.eq("website",website)); 703 criteria.addOrder(Order.asc("name")); 704 705 return criteria.list(); 706 } catch (HibernateException e) { 707 throw new RollerException(e); 708 } 709 } 710 711 public PermissionsData getPermissions(String inviteId) throws RollerException { 712 return (PermissionsData)this.strategy.load(inviteId, PermissionsData.class); 713 } 714 715 718 public PermissionsData getPermissions( 719 WebsiteData website, UserData user) throws RollerException { 720 721 try { 722 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 723 Criteria criteria = session.createCriteria(PermissionsData.class); 724 criteria.add(Expression.eq("website", website)); 725 criteria.add(Expression.eq("user", user)); 726 727 List list = criteria.list(); 728 return list.size()!=0 ? (PermissionsData)list.get(0) : null; 729 } catch (HibernateException e) { 730 throw new RollerException(e); 731 } 732 } 733 734 737 public List getPendingPermissions(UserData user) throws RollerException { 738 739 try { 740 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 741 Criteria criteria = session.createCriteria(PermissionsData.class); 742 criteria.add(Expression.eq("user", user)); 743 criteria.add(Expression.eq("pending", Boolean.TRUE)); 744 745 return criteria.list(); 746 } catch (HibernateException e) { 747 throw new RollerException(e); 748 } 749 } 750 751 754 public List getPendingPermissions(WebsiteData website) throws RollerException { 755 756 try { 757 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 758 Criteria criteria = session.createCriteria(PermissionsData.class); 759 criteria.add(Expression.eq("website", website)); 760 criteria.add(Expression.eq("pending", Boolean.TRUE)); 761 762 return criteria.list(); 763 } catch (HibernateException e) { 764 throw new RollerException(e); 765 } 766 } 767 768 771 public List getAllPermissions(WebsiteData website) throws RollerException { 772 773 try { 774 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 775 Criteria criteria = session.createCriteria(PermissionsData.class); 776 criteria.add(Expression.eq("website", website)); 777 criteria.add(Expression.eq("pending", Boolean.FALSE)); 778 779 return criteria.list(); 780 } catch (HibernateException e) { 781 throw new RollerException(e); 782 } 783 } 784 785 788 public List getAllPermissions(UserData user) throws RollerException { 789 790 try { 791 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 792 Criteria criteria = session.createCriteria(PermissionsData.class); 793 criteria.add(Expression.eq("user", user)); 794 criteria.add(Expression.eq("pending", Boolean.FALSE)); 795 796 return criteria.list(); 797 } catch (HibernateException e) { 798 throw new RollerException(e); 799 } 800 } 801 802 public void release() {} 803 804 public Map getUserNameLetterMap() throws RollerException { 805 String msg = "Getting username letter map"; 807 try { 808 String lc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 809 Map results = new TreeMap (); 810 Session session = 811 ((HibernatePersistenceStrategy)strategy).getSession(); 812 for (int i=0; i<26; i++) { 813 Query query = session.createQuery( 814 "select count(user) from UserData user where upper(user.userName) like '"+lc.charAt(i)+"%'"); 815 List row = query.list(); 816 Integer count = (Integer )row.get(0); 817 results.put(new String (new char[]{lc.charAt(i)}), count); 818 } 819 return results; 820 } catch (Throwable pe) { 821 log.error(msg, pe); 822 throw new RollerException(msg, pe); 823 } 824 } 825 826 public List getUsersByLetter(char letter, int offset, int length) 827 throws RollerException { 828 try { 830 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 831 Criteria criteria = session.createCriteria(UserData.class); 832 criteria.add(Expression.ilike("userName", new String (new char[]{letter}) + "%", MatchMode.START)); 833 criteria.addOrder(Order.asc("userName")); 834 if (offset != 0) { 835 criteria.setFirstResult(offset); 836 } 837 if (length != -1) { 838 criteria.setMaxResults(length); 839 } 840 return criteria.list(); 841 } catch (HibernateException e) { 842 throw new RollerException(e); 843 } 844 } 845 846 public Map getWeblogHandleLetterMap() throws RollerException { 847 String msg = "Getting weblog letter map"; 849 try { 850 String lc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 851 Map results = new TreeMap (); 852 Session session = 853 ((HibernatePersistenceStrategy)strategy).getSession(); 854 for (int i=0; i<26; i++) { 855 Query query = session.createQuery( 856 "select count(website) from WebsiteData website where upper(website.handle) like '"+lc.charAt(i)+"%'"); 857 List row = query.list(); 858 Integer count = (Integer )row.get(0); 859 results.put(new String (new char[]{lc.charAt(i)}), count); 860 } 861 return results; 862 } catch (Throwable pe) { 863 log.error(msg, pe); 864 throw new RollerException(msg, pe); 865 } 866 } 867 868 public List getWeblogsByLetter(char letter, int offset, int length) 869 throws RollerException { 870 try { 872 Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); 873 Criteria criteria = session.createCriteria(WebsiteData.class); 874 criteria.add(Expression.ilike("handle", new String (new char[]{letter}) + "%", MatchMode.START)); 875 criteria.addOrder(Order.asc("handle")); 876 if (offset != 0) { 877 criteria.setFirstResult(offset); 878 } 879 if (length != -1) { 880 criteria.setMaxResults(length); 881 } 882 return criteria.list(); 883 } catch (HibernateException e) { 884 throw new RollerException(e); 885 } 886 } 887 888 public List getMostCommentedWebsites(Date startDate, Date endDate, int offset, int length) 889 throws RollerException { 890 String msg = "Getting most commented websites"; 892 if (endDate == null) endDate = new Date (); 893 try { 894 Session session = 895 ((HibernatePersistenceStrategy)strategy).getSession(); 896 StringBuffer sb = new StringBuffer (); 897 sb.append("select count(distinct c), c.weblogEntry.website.id, c.weblogEntry.website.handle, c.weblogEntry.website.name "); 898 sb.append("from CommentData c where c.weblogEntry.pubTime < :endDate "); 899 if (startDate != null) { 900 sb.append("and c.weblogEntry.pubTime > :startDate "); 901 } 902 sb.append("group by c.weblogEntry.website.id, c.weblogEntry.website.name, c.weblogEntry.website.description order by col_0_0_ desc"); 903 Query query = session.createQuery(sb.toString()); 904 query.setParameter("endDate", endDate); 905 if (startDate != null) { 906 query.setParameter("startDate", startDate); 907 } 908 if (offset != 0) { 909 query.setFirstResult(offset); 910 } 911 if (length != -1) { 912 query.setMaxResults(length); 913 } 914 List results = new ArrayList (); 915 for (Iterator iter = query.list().iterator(); iter.hasNext();) { 916 Object [] row = (Object []) iter.next(); 917 results.add(new StatCount( 918 (String )row[1], 919 (String )row[2], 920 (String )row[3], 921 "statCount.weblogCommentCountType", 922 new Long (((Integer )row[0]).intValue()).longValue())); 923 } 924 return results; 925 } catch (Throwable pe) { 926 log.error(msg, pe); 927 throw new RollerException(msg, pe); 928 } 929 } 930 931 932 class IgnoreCaseEqExpression extends SimpleExpression { 933 public IgnoreCaseEqExpression(String property, Object value) { 934 super(property, value, "=", true); 935 } 936 } 937 } 938 939 940 941 942 943 944 | Popular Tags |