KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > forums > impl > ForumsModuleImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

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 JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import java.util.Date JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40 /**
41  * @author <a HREF="mailto:theute@jboss.org">Thomas Heute </a>
42  * @version $Revision: 1.12 $
43  * @jmx.mbean
44  * @jboss.xmbean
45  */

46 public class ForumsModuleImpl
47       extends AbstractModule
48       implements ForumsModule
49 {
50    private final Logger log = Logger.getLogger(getClass());
51
52    /**
53     * DOCUMENT_ME
54     *
55     * @param id DOCUMENT_ME
56     * @return DOCUMENT_ME
57     * @throws ModuleException DOCUMENT_ME
58     * @throws IllegalArgumentException DOCUMENT_ME
59     */

60    public Forum findForumByID(Integer JavaDoc 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 JavaDoc 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 JavaDoc("id cannot be null");
86       }
87    }
88
89    /**
90     * DOCUMENT_ME
91     *
92     * @param id DOCUMENT_ME
93     * @return DOCUMENT_ME
94     * @throws ModuleException DOCUMENT_ME
95     * @throws IllegalArgumentException DOCUMENT_ME
96     */

97    public Category findCategoryByID(Integer JavaDoc 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 JavaDoc 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 JavaDoc("id cannot be null");
123       }
124    }
125
126    /**
127     * DOCUMENT_ME
128     *
129     * @param userID DOCUMENT_ME
130     * @return DOCUMENT_ME
131     * @throws ModuleException DOCUMENT_ME
132     * @throws IllegalArgumentException DOCUMENT_ME
133     */

134    public Poster findPosterByUserID(Integer JavaDoc 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 JavaDoc 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 JavaDoc("user name cannot be null");
157       }
158    }
159
160    /**
161     * DOCUMENT_ME
162     *
163     * @param userID DOCUMENT_ME
164     * @return DOCUMENT_ME
165     * @throws ModuleException DOCUMENT_ME
166     * @throws IllegalArgumentException DOCUMENT_ME
167     */

168    public Poster createPoster(Integer JavaDoc 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 JavaDoc message = "Cannot create Poster";
184             log.error(message, e);
185             throw new ModuleException(message, e);
186          }
187       }
188       else
189       {
190          throw new IllegalArgumentException JavaDoc("user name cannot be null");
191       }
192    }
193
194    /**
195     * DOCUMENT_ME
196     *
197     * @return DOCUMENT_ME
198     * @throws ModuleException DOCUMENT_ME
199     */

200    public List JavaDoc 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 JavaDoc iterator = query.iterate();
208          return Tools.toList(iterator);
209       }
210       catch(HibernateException e)
211       {
212          String JavaDoc message = "Cannot find categories";
213          log.error(message, e);
214          throw new ModuleException(message, e);
215       }
216    }
217
218    /**
219     * DOCUMENT_ME
220     *
221     * @return DOCUMENT_ME
222     * @throws ModuleException DOCUMENT_ME
223     */

224    public List JavaDoc 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 JavaDoc iterator = query.iterate();
232          return Tools.toList(iterator);
233       }
234       catch(HibernateException e)
235       {
236          String JavaDoc message = "Cannot find forums";
237          log.error(message, e);
238          throw new ModuleException(message, e);
239       }
240    }
241
242    /**
243     * DOCUMENT_ME
244     *
245     * @param categoryID DOCUMENT_ME
246     * @return DOCUMENT_ME
247     * @throws ModuleException DOCUMENT_ME
248     */

249    public List JavaDoc findForumsByCategoryID(Integer JavaDoc 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 JavaDoc iterator = query.iterate();
260          return Tools.toList(iterator);
261       }
262       catch(HibernateException e)
263       {
264          String JavaDoc message = "Cannot find forums";
265          log.error(message, e);
266          throw new ModuleException(message, e);
267       }
268    }
269
270    /**
271     * DOCUMENT_ME
272     *
273     * @param forum DOCUMENT_ME
274     * @return DOCUMENT_ME
275     * @throws ModuleException DOCUMENT_ME
276     */

277    public List JavaDoc 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 JavaDoc iterator = query.iterate();
288          return Tools.toList(iterator);
289       }
290       catch(HibernateException e)
291       {
292          String JavaDoc message = "Cannot find forums";
293          log.error(message, e);
294          throw new ModuleException(message, e);
295       }
296    }
297
298    private List JavaDoc findTopics(Forum forum,
299                            int type,
300                            int start,
301                            int perPage,
302                            String JavaDoc 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 JavaDoc iterator = query.iterate();
316          return Tools.toList(iterator);
317       }
318       catch(HibernateException e)
319       {
320          String JavaDoc message = "Cannot find topics";
321          log.error(message, e);
322          throw new ModuleException(message, e);
323       }
324    }
325
326    /**
327     * DOCUMENT_ME
328     *
329     * @param forum DOCUMENT_ME
330     * @param type DOCUMENT_ME
331     * @param start DOCUMENT_ME
332     * @param perPage DOCUMENT_ME
333     * @return DOCUMENT_ME
334     * @throws ModuleException DOCUMENT_ME
335     */

336    public List JavaDoc 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    /**
346     * DOCUMENT_ME
347     *
348     * @param forum DOCUMENT_ME
349     * @param type DOCUMENT_ME
350     * @param start DOCUMENT_ME
351     * @param perPage DOCUMENT_ME
352     * @return DOCUMENT_ME
353     * @throws ModuleException DOCUMENT_ME
354     */

355    public List JavaDoc 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    /**
365     * DOCUMENT_ME
366     *
367     * @param forum DOCUMENT_ME
368     * @param type DOCUMENT_ME
369     * @param start DOCUMENT_ME
370     * @param perPage DOCUMENT_ME
371     * @param date DOCUMENT_ME
372     * @return DOCUMENT_ME
373     * @throws ModuleException DOCUMENT_ME
374     */

375    public List JavaDoc findTopicsBefore(Forum forum,
376                                 int type,
377                                 int start,
378                                 int perPage,
379                                 Date JavaDoc date)
380          throws ModuleException
381    {
382       return null;
383    }
384
385    /**
386     * DOCUMENT_ME
387     *
388     * @param forum DOCUMENT_ME
389     * @param message DOCUMENT_ME
390     * @param creationDate DOCUMENT_ME
391     * @param poster DOCUMENT_ME
392     * @return DOCUMENT_ME
393     * @throws ModuleException DOCUMENT_ME
394     */

395    public Post createTopic(Forum forum,
396                            Message message,
397                            Date JavaDoc 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 JavaDoc errorMessage = "Cannot create topic";
433          log.error(errorMessage, e);
434          throw new ModuleException(errorMessage, e);
435       }
436    }
437
438    /**
439     * DOCUMENT_ME
440     *
441     * @param topic DOCUMENT_ME
442     * @param forum DOCUMENT_ME
443     * @param message DOCUMENT_ME
444     * @param creationDate DOCUMENT_ME
445     * @param poster DOCUMENT_ME
446     * @return DOCUMENT_ME
447     * @throws ModuleException DOCUMENT_ME
448     */

449    public Post createPost(Topic topic,
450                           Forum forum,
451                           Message message,
452                           Date JavaDoc 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 JavaDoc 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 JavaDoc lastCategoryOrder = (Integer JavaDoc) query.uniqueResult();
490          return (lastCategoryOrder != null) ? lastCategoryOrder.intValue() : 0;
491       }
492       catch(HibernateException e)
493       {
494          return 0;
495       }
496    }
497
498    /**
499     * DOCUMENT_ME
500     *
501     * @param name DOCUMENT_ME
502     * @return DOCUMENT_ME
503     * @throws ModuleException DOCUMENT_ME
504     */

505    public Category createCategory(String JavaDoc 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 JavaDoc errorMessage = "Cannot create topic";
521          log.error(errorMessage, e);
522          throw new ModuleException(errorMessage, e);
523       }
524    }
525
526    /**
527     * DOCUMENT_ME
528     *
529     * @param category DOCUMENT_ME
530     * @throws ModuleException DOCUMENT_ME
531     */

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 JavaDoc errorMessage = "Cannot delete category";
543          log.error(errorMessage, e);
544          throw new ModuleException(errorMessage, e);
545       }
546    }
547
548    /**
549     * DOCUMENT_ME
550     *
551     * @param forum DOCUMENT_ME
552     * @throws ModuleException DOCUMENT_ME
553     */

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 JavaDoc errorMessage = "Cannot delete forum";
565          log.error(errorMessage, e);
566          throw new ModuleException(errorMessage, e);
567       }
568    }
569
570    /**
571     * DOCUMENT_ME
572     *
573     * @param post DOCUMENT_ME
574     * @throws ModuleException DOCUMENT_ME
575     */

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 JavaDoc errorMessage = "Cannot delete post";
587          log.error(errorMessage, e);
588          throw new ModuleException(errorMessage, e);
589       }
590    }
591
592    /**
593     * DOCUMENT_ME
594     *
595     * @param topic DOCUMENT_ME
596     * @throws ModuleException DOCUMENT_ME
597     */

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 JavaDoc 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 JavaDoc lastForumOrder = (Integer JavaDoc) query.uniqueResult();
623          return (lastForumOrder != null) ? lastForumOrder.intValue() : 0;
624       }
625       catch(HibernateException e)
626       {
627          return 0;
628       }
629    }
630
631    /**
632     * DOCUMENT_ME
633     *
634     * @param category DOCUMENT_ME
635     * @param name DOCUMENT_ME
636     * @param description DOCUMENT_ME
637     * @return DOCUMENT_ME
638     * @throws ModuleException DOCUMENT_ME
639     */

640    public Forum createForum(Category category,
641                             String JavaDoc name,
642                             String JavaDoc 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 JavaDoc errorMessage = "Cannot create forum";
660          log.error(errorMessage, e);
661          throw new ModuleException(errorMessage, e);
662       }
663    }
664
665    /**
666     * Return the hibernate session. Testcase can overload this method to provide
667     * their own session.
668     */

669    protected Session getSession()
670    {
671       try
672       {
673          SessionFactory sf = (SessionFactory)new InitialContext JavaDoc().lookup("java:portal/ForumSessionFactory");
674          Session session = sf.getCurrentSession();
675          return session;
676       }
677       catch (NamingException JavaDoc e)
678       {
679          throw new RuntimeException JavaDoc(e);
680       }
681    }
682
683    /**
684     * DOCUMENT_ME
685     *
686     * @param id DOCUMENT_ME
687     * @return DOCUMENT_ME
688     * @throws ModuleException DOCUMENT_ME
689     * @throws IllegalArgumentException DOCUMENT_ME
690     */

691    public Topic findTopicByID(Integer JavaDoc 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 JavaDoc 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 JavaDoc("id cannot be null");
717       }
718    }
719
720    /**
721     * DOCUMENT_ME
722     *
723     * @param id DOCUMENT_ME
724     * @return DOCUMENT_ME
725     * @throws ModuleException DOCUMENT_ME
726     * @throws IllegalArgumentException DOCUMENT_ME
727     */

728    public Post findPostByID(Integer JavaDoc 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 JavaDoc 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 JavaDoc("id cannot be null");
754       }
755    }
756
757    private List JavaDoc findPostsByTopicID(Integer JavaDoc topicID,
758                                    int start,
759                                    int limit,
760                                    String JavaDoc 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 JavaDoc iterator = query.iterate();
773          return Tools.toList(iterator);
774       }
775       catch(HibernateException e)
776       {
777          String JavaDoc message = "Cannot find posts";
778          log.error(message, e);
779          throw new ModuleException(message, e);
780       }
781    }
782
783    /**
784     * DOCUMENT_ME
785     *
786     * @param source DOCUMENT_ME
787     * @param target DOCUMENT_ME
788     */

789    public void addAllForums(Category source,
790                             Category target)
791    {
792       List JavaDoc list = source.getForums();
793       target.getForums().addAll(source.getForums());
794       Iterator JavaDoc iterator = target.getForums().iterator();
795       while(iterator.hasNext())
796       {
797          Forum forum = (Forum) iterator.next();
798          forum.setCategory(target);
799       }
800    }
801
802    /**
803     * DOCUMENT_ME
804     *
805     * @param topicID DOCUMENT_ME
806     * @param start DOCUMENT_ME
807     * @param limit DOCUMENT_ME
808     * @return DOCUMENT_ME
809     * @throws ModuleException DOCUMENT_ME
810     */

811    public List JavaDoc findPostsByTopicIDAsc(Integer JavaDoc topicID,
812                                      int start,
813                                      int limit)
814          throws ModuleException
815    {
816       return findPostsByTopicID(topicID, start, limit, "asc");
817    }
818
819    /**
820     * DOCUMENT_ME
821     *
822     * @param topicID DOCUMENT_ME
823     * @param start DOCUMENT_ME
824     * @param limit DOCUMENT_ME
825     * @return DOCUMENT_ME
826     * @throws ModuleException DOCUMENT_ME
827     */

828    public List JavaDoc findPostsByTopicIDDesc(Integer JavaDoc topicID,
829                                       int start,
830                                       int limit)
831          throws ModuleException
832    {
833       return findPostsByTopicID(topicID, start, limit, "desc");
834    }
835
836    /**
837     * DOCUMENT_ME
838     *
839     * @param user DOCUMENT_ME
840     * @return DOCUMENT_ME
841     * @throws ModuleException DOCUMENT_ME
842     */

843    public Date JavaDoc 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 JavaDoc lastPostDate = (Date JavaDoc) query.uniqueResult();
853          return lastPostDate;
854       }
855       catch(HibernateException e)
856       {
857          e.printStackTrace();
858          return null;
859       }
860    }
861
862    /**
863     * DOCUMENT_ME
864     *
865     * @param forum DOCUMENT_ME
866     * @return DOCUMENT_ME
867     * @throws ModuleException DOCUMENT_ME
868     */

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    /**
891     * DOCUMENT_ME
892     *
893     * @param topic DOCUMENT_ME
894     * @return DOCUMENT_ME
895     * @throws ModuleException DOCUMENT_ME
896     */

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    /**
919     * DOCUMENT_ME
920     *
921     * @param topic DOCUMENT_ME
922     * @return DOCUMENT_ME
923     * @throws ModuleException DOCUMENT_ME
924     */

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    /**
947     * DOCUMENT_ME
948     *
949     * @param user DOCUMENT_ME
950     * @return DOCUMENT_ME
951     * @throws ModuleException DOCUMENT_ME
952     */

953    public List JavaDoc 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 JavaDoc iterator = query.iterate();
961          return Tools.toList(iterator);
962       }
963       catch(HibernateException e)
964       {
965          String JavaDoc message = "Cannot find forum watch";
966          log.error(message, e);
967          throw new ModuleException(message, e);
968       }
969    }
970
971    /**
972     * DOCUMENT_ME
973     *
974     * @param poster DOCUMENT_ME
975     * @param forum DOCUMENT_ME
976     * @param mode DOCUMENT_ME
977     * @throws ModuleException DOCUMENT_ME
978     */

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 JavaDoc errorMessage = "Cannot create forum watch";
1005         log.error(errorMessage, e);
1006         throw new ModuleException(errorMessage, e);
1007      }
1008   }
1009
1010   /**
1011    * DOCUMENT_ME
1012    *
1013    * @param watch DOCUMENT_ME
1014    * @throws ModuleException DOCUMENT_ME
1015    */

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 JavaDoc errorMessage = "Cannot delete watch";
1026         log.error(errorMessage, e);
1027         throw new ModuleException(errorMessage, e);
1028      }
1029   }
1030
1031   public ForumWatch findForumWatchByID(Integer JavaDoc 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 JavaDoc 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 JavaDoc errorMessage = "Cannot create topic watch";
1075         log.error(errorMessage, e);
1076         throw new ModuleException(errorMessage, e);
1077      }
1078   }
1079
1080   public TopicWatch findTopicWatchByID(Integer JavaDoc 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 JavaDoc message = "Cannot find topic watch";
1094         log.error(message, e);
1095         throw new ModuleException(message, e);
1096      }
1097   }
1098}
Popular Tags