1 11 package org.jboss.portlet.forums.impl; 12 13 import org.apache.log4j.Logger; 14 import org.jboss.portal.common.util.Tools; 15 import org.jboss.portal.core.model.User; 16 import org.jboss.portal.core.modules.ModuleException; 17 import org.jboss.portal.core.modules.AbstractModule; 18 import org.jboss.portlet.forums.ForumsConstants; 19 import org.jboss.portlet.forums.ForumsModule; 20 import org.jboss.portlet.forums.model.Forum; 21 import org.jboss.portlet.forums.model.Category; 22 import org.jboss.portlet.forums.model.Post; 23 import org.jboss.portlet.forums.model.Message; 24 import org.jboss.portlet.forums.model.Poster; 25 import org.jboss.portlet.forums.model.Topic; 26 import org.jboss.portlet.forums.model.ForumWatch; 27 import org.jboss.portlet.forums.model.Watch; 28 import org.jboss.portlet.forums.model.TopicWatch; 29 import org.hibernate.Session; 30 import org.hibernate.HibernateException; 31 import org.hibernate.Query; 32 import org.hibernate.SessionFactory; 33 34 import javax.naming.InitialContext ; 35 import javax.naming.NamingException ; 36 import java.util.Date ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 40 46 public class ForumsModuleImpl 47 extends AbstractModule 48 implements ForumsModule 49 { 50 private final Logger log = Logger.getLogger(getClass()); 51 52 60 public Forum findForumByID(Integer id) 61 throws ModuleException 62 { 63 if(id != null) 64 { 65 try 66 { 67 Session session = getSession(); 68 ForumImpl forum = (ForumImpl) session.get(ForumImpl.class, id); 69 if(forum == null) 70 { 71 throw new ModuleException("No forum found for " + id); 72 } 73 74 return forum; 75 } 76 catch(HibernateException e) 77 { 78 String message = "Cannot find forum by id " + id; 79 log.error(message, e); 80 throw new ModuleException(message, e); 81 } 82 } 83 else 84 { 85 throw new IllegalArgumentException ("id cannot be null"); 86 } 87 } 88 89 97 public Category findCategoryByID(Integer id) 98 throws ModuleException 99 { 100 if(id != null) 101 { 102 try 103 { 104 Session session = getSession(); 105 CategoryImpl category = (CategoryImpl) session.get(CategoryImpl.class, id); 106 if(category == null) 107 { 108 throw new ModuleException("No category found for " + id); 109 } 110 111 return category; 112 } 113 catch(HibernateException e) 114 { 115 String message = "Cannot find category by id " + id; 116 log.error(message, e); 117 throw new ModuleException(message, e); 118 } 119 } 120 else 121 { 122 throw new IllegalArgumentException ("id cannot be null"); 123 } 124 } 125 126 134 public Poster findPosterByUserID(Integer userID) 135 throws ModuleException 136 { 137 if(userID != null) 138 { 139 try 140 { 141 Session session = getSession(); 142 Query query = session.createQuery("from PosterImpl as u where u.userID=:userID"); 143 query.setString("userID", userID.toString()); 144 PosterImpl user = (PosterImpl) query.uniqueResult(); 145 return user; 146 } 147 catch(HibernateException e) 148 { 149 String message = "Cannot find poster by name " + userID; 150 log.error(message, e); 151 throw new ModuleException(message, e); 152 } 153 } 154 else 155 { 156 throw new IllegalArgumentException ("user name cannot be null"); 157 } 158 } 159 160 168 public Poster createPoster(Integer userID) 169 throws ModuleException 170 { 171 if(userID != null) 172 { 173 try 174 { 175 Session session = getSession(); 176 PosterImpl user = new PosterImpl(); 177 user.setUserID(userID); 178 session.save(user); 179 return user; 180 } 181 catch(HibernateException e) 182 { 183 String message = "Cannot create Poster"; 184 log.error(message, e); 185 throw new ModuleException(message, e); 186 } 187 } 188 else 189 { 190 throw new IllegalArgumentException ("user name cannot be null"); 191 } 192 } 193 194 200 public List findCategories() 201 throws ModuleException 202 { 203 try 204 { 205 Session session = getSession(); 206 Query query = session.createQuery("from CategoryImpl as c order by c.order asc"); 207 Iterator iterator = query.iterate(); 208 return Tools.toList(iterator); 209 } 210 catch(HibernateException e) 211 { 212 String message = "Cannot find categories"; 213 log.error(message, e); 214 throw new ModuleException(message, e); 215 } 216 } 217 218 224 public List findForums() 225 throws ModuleException 226 { 227 try 228 { 229 Session session = getSession(); 230 Query query = session.createQuery("from ForumImpl as f order by f.order asc"); 231 Iterator iterator = query.iterate(); 232 return Tools.toList(iterator); 233 } 234 catch(HibernateException e) 235 { 236 String message = "Cannot find forums"; 237 log.error(message, e); 238 throw new ModuleException(message, e); 239 } 240 } 241 242 249 public List findForumsByCategoryID(Integer categoryID) 250 throws ModuleException 251 { 252 try 253 { 254 Session session = getSession(); 255 Query query = 256 session.createQuery("from ForumImpl as f where f.category=:categoryID order by f.order asc"); 257 query.setString("categoryID", 258 categoryID.toString()); 259 Iterator iterator = query.iterate(); 260 return Tools.toList(iterator); 261 } 262 catch(HibernateException e) 263 { 264 String message = "Cannot find forums"; 265 log.error(message, e); 266 throw new ModuleException(message, e); 267 } 268 } 269 270 277 public List findAnnouncements(Forum forum) 278 throws ModuleException 279 { 280 try 281 { 282 Session session = getSession(); 283 Query query = 284 session.createQuery("from TopicImpl as t where t.forum=:forumid and t.type = :type order by t.lastPostDate"); 285 query.setString("forumid", "" + forum.getID()); 286 query.setString("type", "" + ForumsConstants.POST_ANNOUNCE); 287 Iterator iterator = query.iterate(); 288 return Tools.toList(iterator); 289 } 290 catch(HibernateException e) 291 { 292 String message = "Cannot find forums"; 293 log.error(message, e); 294 throw new ModuleException(message, e); 295 } 296 } 297 298 private List findTopics(Forum forum, 299 int type, 300 int start, 301 int perPage, 302 String order) 303 throws ModuleException 304 { 305 try 306 { 307 Session session = getSession(); 308 Query query = 309 session.createQuery("from TopicImpl as t where t.forum = :forumid and t.type <> :type order by t.lastPostDate " 310 + order); 311 query.setFirstResult(start); 312 query.setMaxResults(perPage); 313 query.setString("forumid", "" + forum.getID()); 314 query.setString("type", "" + type); 315 Iterator iterator = query.iterate(); 316 return Tools.toList(iterator); 317 } 318 catch(HibernateException e) 319 { 320 String message = "Cannot find topics"; 321 log.error(message, e); 322 throw new ModuleException(message, e); 323 } 324 } 325 326 336 public List findTopicsAsc(Forum forum, 337 int type, 338 int start, 339 int perPage) 340 throws ModuleException 341 { 342 return findTopics(forum, type, start, perPage, "asc"); 343 } 344 345 355 public List findTopicsDesc(Forum forum, 356 int type, 357 int start, 358 int perPage) 359 throws ModuleException 360 { 361 return findTopics(forum, type, start, perPage, "desc"); 362 } 363 364 375 public List findTopicsBefore(Forum forum, 376 int type, 377 int start, 378 int perPage, 379 Date date) 380 throws ModuleException 381 { 382 return null; 383 } 384 385 395 public Post createTopic(Forum forum, 396 Message message, 397 Date creationDate, 398 Poster poster, 399 int type) 400 throws ModuleException 401 { 402 try 403 { 404 Session session = getSession(); 405 406 session.save(poster); 407 408 PostImpl post = new PostImpl(); 409 post.setMessage(message); 410 post.setCreateDate(creationDate); 411 post.setPoster(poster); 412 session.save(post); 413 414 TopicImpl topic = new TopicImpl(); 415 topic.setSubject(message.getSubject()); 416 topic.setForum(forum); 417 topic.setPoster(poster); 418 topic.setFirstPost(post); 419 topic.setLastPost(post); 420 topic.setLastPostDate(creationDate); 421 topic.setType(type); 422 session.save(topic); 423 424 forum.addTopicSize(); 425 forum.addPostSize(); 426 427 post.setTopic(topic); 428 return post; 429 } 430 catch(HibernateException e) 431 { 432 String errorMessage = "Cannot create topic"; 433 log.error(errorMessage, e); 434 throw new ModuleException(errorMessage, e); 435 } 436 } 437 438 449 public Post createPost(Topic topic, 450 Forum forum, 451 Message message, 452 Date creationDate, 453 Poster poster) 454 throws ModuleException 455 { 456 try 457 { 458 Session session = getSession(); 459 session.save(poster); 460 461 PostImpl post = new PostImpl(); 462 post.setMessage(message); 463 post.setCreateDate(creationDate); 464 post.setPoster(poster); 465 post.setTopic(topic); 466 session.save(post); 467 468 topic.setLastPost(post); 469 topic.setLastPostDate(post.getCreateDate()); 470 topic.setReplies(topic.getReplies() + 1); 471 forum.setLastPost(post); 472 forum.addPostSize(); 473 return post; 474 } 475 catch(HibernateException e) 476 { 477 String errorMessage = "Cannot create topic"; 478 log.error(errorMessage, e); 479 throw new ModuleException(errorMessage, e); 480 } 481 } 482 483 private int getLastCategoryOrder() 484 { 485 try 486 { 487 Session session = getSession(); 488 Query query = session.createQuery("select max(c.order) from CategoryImpl as c"); 489 Integer lastCategoryOrder = (Integer ) query.uniqueResult(); 490 return (lastCategoryOrder != null) ? lastCategoryOrder.intValue() : 0; 491 } 492 catch(HibernateException e) 493 { 494 return 0; 495 } 496 } 497 498 505 public Category createCategory(String name) 506 throws ModuleException 507 { 508 try 509 { 510 Session session = getSession(); 511 512 CategoryImpl category = new CategoryImpl(); 513 category.setTitle(name); 514 category.setOrder(getLastCategoryOrder() + 10); 515 session.save(category); 516 return category; 517 } 518 catch(HibernateException e) 519 { 520 String errorMessage = "Cannot create topic"; 521 log.error(errorMessage, e); 522 throw new ModuleException(errorMessage, e); 523 } 524 } 525 526 532 public void removeCategory(Category category) 533 throws ModuleException 534 { 535 Session session = getSession(); 536 try 537 { 538 session.delete(category); 539 } 540 catch(HibernateException e) 541 { 542 String errorMessage = "Cannot delete category"; 543 log.error(errorMessage, e); 544 throw new ModuleException(errorMessage, e); 545 } 546 } 547 548 554 public void removeForum(Forum forum) 555 throws ModuleException 556 { 557 Session session = getSession(); 558 try 559 { 560 session.delete(forum); 561 } 562 catch(HibernateException e) 563 { 564 String errorMessage = "Cannot delete forum"; 565 log.error(errorMessage, e); 566 throw new ModuleException(errorMessage, e); 567 } 568 } 569 570 576 public void removePost(Post post) 577 throws ModuleException 578 { 579 Session session = getSession(); 580 try 581 { 582 session.delete(post); 583 } 584 catch(HibernateException e) 585 { 586 String errorMessage = "Cannot delete post"; 587 log.error(errorMessage, e); 588 throw new ModuleException(errorMessage, e); 589 } 590 } 591 592 598 public void removeTopic(Topic topic) 599 throws ModuleException 600 { 601 Session session = getSession(); 602 try 603 { 604 session.delete(topic); 605 } 606 catch(HibernateException e) 607 { 608 String errorMessage = "Cannot delete topic"; 609 log.error(errorMessage, e); 610 throw new ModuleException(errorMessage, e); 611 } 612 } 613 614 private int getLastForumOrder(Category category) 615 { 616 try 617 { 618 Session session = getSession(); 619 Query query = 620 session.createQuery("select max(f.order) from ForumImpl as f where f.category = :categoryID"); 621 query.setString("categoryID", "" + category.getID()); 622 Integer lastForumOrder = (Integer ) query.uniqueResult(); 623 return (lastForumOrder != null) ? lastForumOrder.intValue() : 0; 624 } 625 catch(HibernateException e) 626 { 627 return 0; 628 } 629 } 630 631 640 public Forum createForum(Category category, 641 String name, 642 String description) 643 throws ModuleException 644 { 645 try 646 { 647 Session session = getSession(); 648 649 ForumImpl forum = new ForumImpl(); 650 forum.setCategory(category); 651 forum.setName(name); 652 forum.setDescription(description); 653 forum.setOrder(getLastForumOrder(category) + 10); 654 session.save(forum); 655 return forum; 656 } 657 catch(HibernateException e) 658 { 659 String errorMessage = "Cannot create forum"; 660 log.error(errorMessage, e); 661 throw new ModuleException(errorMessage, e); 662 } 663 } 664 665 669 protected Session getSession() 670 { 671 try 672 { 673 SessionFactory sf = (SessionFactory)new InitialContext ().lookup("java:portal/ForumSessionFactory"); 674 Session session = sf.getCurrentSession(); 675 return session; 676 } 677 catch (NamingException e) 678 { 679 throw new RuntimeException (e); 680 } 681 } 682 683 691 public Topic findTopicByID(Integer id) 692 throws ModuleException 693 { 694 if(id != null) 695 { 696 try 697 { 698 Session session = getSession(); 699 TopicImpl topic = (TopicImpl) session.get(TopicImpl.class, id); 700 if(topic == null) 701 { 702 throw new ModuleException("No topic found for " + id); 703 } 704 705 return topic; 706 } 707 catch(HibernateException e) 708 { 709 String message = "Cannot find forum by id " + id; 710 log.error(message, e); 711 throw new ModuleException(message, e); 712 } 713 } 714 else 715 { 716 throw new IllegalArgumentException ("id cannot be null"); 717 } 718 } 719 720 728 public Post findPostByID(Integer id) 729 throws ModuleException 730 { 731 if(id != null) 732 { 733 try 734 { 735 Session session = getSession(); 736 PostImpl post = (PostImpl) session.get(PostImpl.class, id); 737 if(post == null) 738 { 739 throw new ModuleException("No post found for " + id); 740 } 741 742 return post; 743 } 744 catch(HibernateException e) 745 { 746 String message = "Cannot find post by id " + id; 747 log.error(message, e); 748 throw new ModuleException(message, e); 749 } 750 } 751 else 752 { 753 throw new IllegalArgumentException ("id cannot be null"); 754 } 755 } 756 757 private List findPostsByTopicID(Integer topicID, 758 int start, 759 int limit, 760 String order) 761 throws ModuleException 762 { 763 try 764 { 765 Session session = getSession(); 766 Query query = 767 session.createQuery("from PostImpl as p where p.topic=:topicID order by p.createDate " + order); 768 query.setString("topicID", 769 topicID.toString()); 770 query.setFirstResult(start); 771 query.setMaxResults(limit); 772 Iterator iterator = query.iterate(); 773 return Tools.toList(iterator); 774 } 775 catch(HibernateException e) 776 { 777 String message = "Cannot find posts"; 778 log.error(message, e); 779 throw new ModuleException(message, e); 780 } 781 } 782 783 789 public void addAllForums(Category source, 790 Category target) 791 { 792 List list = source.getForums(); 793 target.getForums().addAll(source.getForums()); 794 Iterator iterator = target.getForums().iterator(); 795 while(iterator.hasNext()) 796 { 797 Forum forum = (Forum) iterator.next(); 798 forum.setCategory(target); 799 } 800 } 801 802 811 public List findPostsByTopicIDAsc(Integer topicID, 812 int start, 813 int limit) 814 throws ModuleException 815 { 816 return findPostsByTopicID(topicID, start, limit, "asc"); 817 } 818 819 828 public List findPostsByTopicIDDesc(Integer topicID, 829 int start, 830 int limit) 831 throws ModuleException 832 { 833 return findPostsByTopicID(topicID, start, limit, "desc"); 834 } 835 836 843 public Date findLastPostDateForUser(User user) 844 throws ModuleException 845 { 846 try 847 { 848 Session session = getSession(); 849 Query query = 850 session.createQuery("select max(p.createDate) from PostImpl as p where p.poster.userID = :userID"); 851 query.setString("userID", "" + user.getID()); 852 Date lastPostDate = (Date ) query.uniqueResult(); 853 return lastPostDate; 854 } 855 catch(HibernateException e) 856 { 857 e.printStackTrace(); 858 return null; 859 } 860 } 861 862 869 public Post findLastPost(Forum forum) 870 throws ModuleException 871 { 872 try 873 { 874 Session session = getSession(); 875 Query query = 876 session.createQuery("from PostImpl as p where p.topic.forum = :forumID order by p.createDate desc"); 877 query.setString("forumID", "" + forum.getID()); 878 query.setFirstResult(0); 879 query.setMaxResults(1); 880 Post lastPost = (Post) query.uniqueResult(); 881 return lastPost; 882 } 883 catch(HibernateException e) 884 { 885 e.printStackTrace(); 886 return null; 887 } 888 } 889 890 897 public Post findFirstPost(Topic topic) 898 throws ModuleException 899 { 900 try 901 { 902 Session session = getSession(); 903 Query query = 904 session.createQuery("from PostImpl as p where p.topic = :topicID order by p.createDate asc"); 905 query.setString("topicID", "" + topic.getID()); 906 query.setFirstResult(0); 907 query.setMaxResults(1); 908 Post firstPost = (Post) query.uniqueResult(); 909 return firstPost; 910 } 911 catch(HibernateException e) 912 { 913 e.printStackTrace(); 914 return null; 915 } 916 } 917 918 925 public Post findLastPost(Topic topic) 926 throws ModuleException 927 { 928 try 929 { 930 Session session = getSession(); 931 Query query = 932 session.createQuery("from PostImpl as p where p.topic = :topicID order by p.createDate desc"); 933 query.setString("topicID", "" + topic.getID()); 934 query.setFirstResult(0); 935 query.setMaxResults(1); 936 Post lastPost = (Post) query.uniqueResult(); 937 return lastPost; 938 } 939 catch(HibernateException e) 940 { 941 e.printStackTrace(); 942 return null; 943 } 944 } 945 946 953 public List findForumWatchByUser(User user) throws ModuleException 954 { 955 try 956 { 957 Session session = getSession(); 958 Query query = session.createQuery("from ForumWatchImpl as f where f.poster.userID = :userID"); 959 query.setString("userID", user.getID().toString()); 960 Iterator iterator = query.iterate(); 961 return Tools.toList(iterator); 962 } 963 catch(HibernateException e) 964 { 965 String message = "Cannot find forum watch"; 966 log.error(message, e); 967 throw new ModuleException(message, e); 968 } 969 } 970 971 979 public void createWatch(Poster poster, Forum forum, int mode) throws ModuleException 980 { 981 try 982 { 983 Session session = getSession(); 984 985 if(poster == null) 986 { 987 throw new ModuleException("poster must not be null"); 988 } 989 990 if(forum == null) 991 { 992 throw new ModuleException("forum must not be null"); 993 } 994 995 ForumWatch forumWatch = new ForumWatchImpl(); 996 forumWatch.setPoster(poster); 997 forumWatch.setForum(forum); 998 forumWatch.setMode(mode); 999 session.save(forumWatch); 1000 1001 } 1002 catch(HibernateException e) 1003 { 1004 String errorMessage = "Cannot create forum watch"; 1005 log.error(errorMessage, e); 1006 throw new ModuleException(errorMessage, e); 1007 } 1008 } 1009 1010 1016 public void removeWatch(Watch watch) throws ModuleException 1017 { 1018 Session session = getSession(); 1019 try 1020 { 1021 session.delete(watch); 1022 } 1023 catch(HibernateException e) 1024 { 1025 String errorMessage = "Cannot delete watch"; 1026 log.error(errorMessage, e); 1027 throw new ModuleException(errorMessage, e); 1028 } 1029 } 1030 1031 public ForumWatch findForumWatchByID(Integer forumWatchID) 1032 throws ModuleException 1033 { 1034 try 1035 { 1036 Session session = getSession(); 1037 Query query = session.createQuery("from ForumWatchImpl as f where f.id = :forumWatchID"); 1038 query.setString("forumWatchID", 1039 forumWatchID.toString()); 1040 return (ForumWatch) query.uniqueResult(); 1041 } 1042 catch(HibernateException e) 1043 { 1044 String message = "Cannot find forum watch"; 1045 log.error(message, e); 1046 throw new ModuleException(message, e); 1047 } 1048 } 1049 1050 public void createWatch(Poster poster, Topic topic) 1051 throws ModuleException 1052 { 1053 try 1054 { 1055 Session session = getSession(); 1056 1057 if(poster == null) 1058 { 1059 throw new ModuleException("poster must not be null"); 1060 } 1061 1062 if(topic == null) 1063 { 1064 throw new ModuleException("topic must not be null"); 1065 } 1066 1067 TopicWatch topicWatch = new TopicWatchImpl(); 1068 topicWatch.setPoster(poster); 1069 topicWatch.setTopic(topic); 1070 session.save(topicWatch); 1071 } 1072 catch(HibernateException e) 1073 { 1074 String errorMessage = "Cannot create topic watch"; 1075 log.error(errorMessage, e); 1076 throw new ModuleException(errorMessage, e); 1077 } 1078 } 1079 1080 public TopicWatch findTopicWatchByID(Integer topicWatchID) 1081 throws ModuleException 1082 { 1083 try 1084 { 1085 Session session = getSession(); 1086 Query query = session.createQuery("from TopicWatchImpl as f where f.id = :topicWatchID"); 1087 query.setString("topicWatchID", 1088 topicWatchID.toString()); 1089 return (TopicWatch) query.uniqueResult(); 1090 } 1091 catch(HibernateException e) 1092 { 1093 String message = "Cannot find topic watch"; 1094 log.error(message, e); 1095 throw new ModuleException(message, e); 1096 } 1097 } 1098} | Popular Tags |