KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > forum > hibernate > ForumServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.forum.hibernate;
6
7 import java.util.Date JavaDoc;
8 import java.util.List JavaDoc;
9 import net.sf.hibernate.Hibernate;
10 import net.sf.hibernate.Session;
11 import org.apache.commons.logging.Log;
12 import org.exoplatform.commons.utils.ListenerStack;
13 import org.exoplatform.commons.utils.PageList;
14 import org.exoplatform.services.communication.forum.*;
15 import org.exoplatform.services.database.DBObjectPageList;
16 import org.exoplatform.services.database.HibernateService;
17 import org.exoplatform.services.database.ObjectQuery;
18 import org.exoplatform.services.database.XResources;
19 import org.exoplatform.services.idgenerator.IDGeneratorService;
20 import org.exoplatform.services.log.LogUtil;
21 /**
22  * Created by The eXo Platform SARL .
23  * Author : Tuan Nguyen
24  * tuan08@users.sourceforge.net
25  * Date: Jun 14, 2003
26  * Time: 1:12:22 PM
27  */

28 public class ForumServiceImpl implements ForumService {
29   static protected Log log_ = LogUtil.getLog("org.exoplatform.services.communication.forum");
30   
31   public static final String JavaDoc queryCategoriesByOwner =
32     "from category in class org.exoplatform.services.communication.forum.hibernate.CategoryImpl " +
33     "where category.owner = ? order by category.categoryOrder asc";
34   
35   public static final String JavaDoc queryForumsByCategory =
36     "from forum in class org.exoplatform.services.communication.forum.hibernate.ForumImpl " +
37     "where forum.categoryId = ? order by forum.forumOrder asc";
38  
39   public static final String JavaDoc queryTopicsByForum =
40     "from topic in class org.exoplatform.services.communication.forum.hibernate.TopicImpl " +
41     "where topic.forumId = ?";
42   
43   public static final String JavaDoc queryPostsByForum =
44     "from post in class org.exoplatform.services.communication.forum.hibernate.PostImpl " +
45     "where post.forumId = ?";
46   
47   public static final String JavaDoc queryPostsByTopic =
48     "from post in class org.exoplatform.services.communication.forum.hibernate.PostImpl " +
49     "where post.topicId = ?";
50  
51     private HibernateService hservice_ ;
52   private ForumIndexerPluginImpl indexer_ ;
53   private IDGeneratorService idService_ ;
54   private String JavaDoc forumServiceOwner_ ;
55   private ListenerStack eventListeners_ ;
56   private long modifiedTime_ ;
57
58   public ForumServiceImpl(HibernateService service,
59                           ForumIndexerPluginImpl indexer,
60                           IDGeneratorService idService,
61                           ListenerStack eventListeners ,
62                           String JavaDoc owner) {
63     forumServiceOwner_ = owner ;
64     hservice_ = service ;
65     indexer_ = indexer ;
66     idService_ = idService ;
67     eventListeners_ = eventListeners ;
68   }
69   //--------------------------C A T E G O R Y-------------------------------
70
public List JavaDoc getCategories() throws Exception JavaDoc {
71     Session session = hservice_.openSession();
72     List JavaDoc categories = session.find(queryCategoriesByOwner, forumServiceOwner_, Hibernate.STRING );
73     return categories ;
74   }
75
76   public Category getCategory(String JavaDoc id) throws Exception JavaDoc {
77     return (Category) hservice_.findOne(CategoryImpl.class, id) ;
78   }
79
80   public Category addCategory(Category category) throws Exception JavaDoc {
81     Date JavaDoc now = new Date JavaDoc() ;
82     CategoryImpl impl = (CategoryImpl) category ;
83     impl.setId(idService_.generateStringID(category)) ;
84     impl.setOwner(forumServiceOwner_) ;
85     impl.setCreatedDate(now) ;
86     impl.setModifiedBy(forumServiceOwner_) ;
87     impl.setModifiedDate(now) ;
88     Session session = hservice_.openSession();
89     session.save(impl) ;
90     session.flush();
91     modifiedTime_ = System.currentTimeMillis() ;
92     return impl ;
93   }
94
95   public Category removeCategory(String JavaDoc id) throws Exception JavaDoc {
96     Session session = hservice_.openSession();
97     Category category = (Category) session.get(CategoryImpl.class, id) ;
98     session.delete(category);
99     XResources resources = new XResources() ;
100     resources.addResource(Session.class, session) ;
101     ForumEventListener.onDelete(eventListeners_, resources, category) ;
102     session.flush();
103     indexer_.removeCategory(category) ;
104     modifiedTime_ = System.currentTimeMillis() ;
105     return category ;
106   }
107
108   public Category updateCategory(Category category) throws Exception JavaDoc {
109     Session session = hservice_.openSession();
110     Date JavaDoc now = new Date JavaDoc() ;
111     CategoryImpl impl = (CategoryImpl) category ;
112     impl.setModifiedDate(now) ;
113     session.update(impl);
114     XResources resources = new XResources() ;
115     resources.addResource(Session.class, session) ;
116     ForumEventListener.onSave(eventListeners_, resources, category) ;
117     session.flush();
118     return category ;
119   }
120
121   public Category createCategoryInstance() { return new CategoryImpl(); }
122
123   //--------------------------F O R U M-------------------------------------
124
public List JavaDoc getForums(String JavaDoc categoryId) throws Exception JavaDoc {
125     Session session = hservice_.openSession();
126     List JavaDoc forums = session.find(queryForumsByCategory, categoryId, Hibernate.STRING );
127     return forums ;
128   }
129
130   public Forum getForum(String JavaDoc id) throws Exception JavaDoc {
131     return (Forum)hservice_.findOne(ForumImpl.class, id);
132   }
133
134   public Forum addForum(Category category, Forum forum) throws Exception JavaDoc {
135     Session session = hservice_.openSession();
136     Date JavaDoc now = new Date JavaDoc() ;
137     ForumImpl impl = (ForumImpl) forum ;
138     impl.setId(idService_.generateStringID(impl)) ;
139     impl.setOwner(forumServiceOwner_) ;
140     impl.setCreatedDate(now) ;
141     impl.setModifiedBy(forumServiceOwner_) ;
142     impl.setModifiedDate(now) ;
143     impl.setCategoryId(category.getId()) ;
144     session.save(impl);
145     XResources resources = new XResources() ;
146     resources.addResource(Session.class, session) ;
147     ForumEventListener.onSave(eventListeners_, resources, forum) ;
148     session.flush() ;
149     modifiedTime_ = System.currentTimeMillis() ;
150     return forum ;
151   }
152
153   public Forum removeForum(String JavaDoc id) throws Exception JavaDoc {
154     Session session = hservice_.openSession();
155     Forum forum = (Forum) session.get(ForumImpl.class, id) ;
156     session.delete(forum);
157     XResources resources = new XResources() ;
158     resources.addResource(Session.class, session) ;
159     ForumEventListener.onDelete(eventListeners_, resources, forum) ;
160     session.flush();
161     indexer_.removeForum(forum) ;
162     modifiedTime_ = System.currentTimeMillis() ;
163     return forum ;
164   }
165
166   public Forum updateForum(Forum forum) throws Exception JavaDoc {
167     Session session = hservice_.openSession();
168     Date JavaDoc now = new Date JavaDoc() ;
169     ForumImpl impl = (ForumImpl) forum ;
170     impl.setModifiedDate(now) ;
171     session.update(impl) ;
172     XResources resources = new XResources() ;
173     resources.addResource(Session.class, session) ;
174     ForumEventListener.onSave(eventListeners_, resources, forum) ;
175     session.flush() ;
176     return forum ;
177   }
178
179   public Forum createForumInstance() { return new ForumImpl(); }
180   
181   //--------------------------T O P I C-------------------------------------
182
public PageList getTopics(String JavaDoc forumId) throws Exception JavaDoc {
183     ObjectQuery q = new ObjectQuery(TopicImpl.class).
184                      addEQ("forumId", forumId ).setDescOrderBy("lastPostDate");
185     return new DBObjectPageList(hservice_, q) ;
186   }
187
188   public Topic getTopic(String JavaDoc id) throws Exception JavaDoc {
189     return (Topic) hservice_.findOne(TopicImpl.class, id) ;
190   }
191
192   public Topic addTopic(Forum forum, Topic topic) throws Exception JavaDoc {
193     Date JavaDoc now = new Date JavaDoc() ;
194     TopicImpl impl = (TopicImpl) topic ;
195     ForumImpl forumImpl = (ForumImpl) forum ;
196     impl.setId(idService_.generateStringID(impl)) ;
197     impl.setCreatedDate(now) ;
198     impl.setModifiedDate(now) ;
199     impl.setLastPostBy(topic.getOwner()) ;
200     impl.setLastPostDate(now) ;
201     impl.setForumId(forumImpl.getId()) ;
202     Session session = hservice_.openSession();
203     session.save(impl);
204     forumImpl.addTopicCount(1) ;
205     session.update(forumImpl);
206     session.flush();
207     modifiedTime_ = System.currentTimeMillis() ;
208     return topic ;
209   }
210
211   public Topic removeTopic(String JavaDoc id) throws Exception JavaDoc {
212     Session session = hservice_.openSession();
213     TopicImpl topicImpl = (TopicImpl)session.get(TopicImpl.class, id) ;
214     session.delete(topicImpl);
215     ForumImpl forumImpl = (ForumImpl) session.get(ForumImpl.class, topicImpl.getForumId()) ;
216     forumImpl.addTopicCount(-1) ;
217     session.update(forumImpl);
218     session.flush();
219     indexer_.removeTopic(topicImpl) ;
220     modifiedTime_ = System.currentTimeMillis() ;
221     return topicImpl ;
222   }
223
224   public Topic updateTopic(Topic topic) throws Exception JavaDoc {
225     Date JavaDoc now = new Date JavaDoc() ;
226     TopicImpl impl = (TopicImpl) topic ;
227     impl.setModifiedDate(now) ;
228     hservice_.update(impl);
229     return topic ;
230   }
231
232   public Topic createTopicInstance() { return new TopicImpl() ;}
233
234   //--------------------------P O S T----------------------------------------
235
public PageList getPosts(String JavaDoc topicId) throws Exception JavaDoc {
236     ObjectQuery q = new ObjectQuery(PostImpl.class).addEQ("topicId", topicId );
237     return new DBObjectPageList(hservice_, q) ;
238   }
239
240   public Post getPost(String JavaDoc id) throws Exception JavaDoc {
241     return (Post) hservice_.findOne(PostImpl.class, id);
242   }
243
244   public Post addPost(Topic topic, Post post) throws Exception JavaDoc {
245     Date JavaDoc now = new Date JavaDoc() ;
246     PostImpl postImpl = (PostImpl) post ;
247     postImpl.setId(idService_.generateStringID(postImpl)) ;
248     postImpl.setCreatedDate(now) ;
249     postImpl.setModifiedDate(now) ;
250     Session session = hservice_.openSession();
251     TopicImpl topicImpl = (TopicImpl) topic;
252     topicImpl.addPostCount(1) ;
253     topicImpl.setLastPostBy(postImpl.getOwner()) ;
254     topicImpl.setLastPostDate(now) ;
255     ForumImpl forumImpl = (ForumImpl)session.get(ForumImpl.class, topicImpl.getForumId());
256     forumImpl.addPostCount(1) ;
257     forumImpl.setLastPostBy(postImpl.getOwner()) ;
258     forumImpl.setLastPostDate(now) ;
259     postImpl.setTopicId(topic.getId()) ;
260     postImpl.setForumId(topicImpl.getForumId()) ;
261     session.save(postImpl);
262     session.update(forumImpl);
263     session.update(topicImpl);
264     XResources resources = new XResources() ;
265     resources.addResource(Session.class, session) ;
266     resources.addResource(Forum.class, forumImpl) ;
267     resources.addResource(Topic.class, topicImpl) ;
268     ForumEventListener.onSave(eventListeners_, resources, postImpl) ;
269     session.flush() ;
270     indexer_.createPost(forumImpl, postImpl) ;
271     modifiedTime_ = System.currentTimeMillis() ;
272     return post ;
273   }
274
275   public Post removePost(String JavaDoc id) throws Exception JavaDoc {
276     Session session = hservice_.openSession();
277     PostImpl postImpl = (PostImpl) session.get(PostImpl.class, id);
278     TopicImpl topicImpl = (TopicImpl) session.get(TopicImpl.class, postImpl.getTopicId());
279     session.delete(postImpl);
280     topicImpl.addPostCount(-1) ;
281     session.update(topicImpl);
282     ForumImpl forumImpl = (ForumImpl) session.get(ForumImpl.class, postImpl.getForumId());
283     forumImpl.addPostCount(-1) ;
284     session.update(forumImpl);
285     session.flush();
286     indexer_.removePost(postImpl) ;
287     modifiedTime_ = System.currentTimeMillis() ;
288     return postImpl ;
289   }
290
291   public Post updatePost(Post post) throws Exception JavaDoc {
292     Date JavaDoc now = new Date JavaDoc() ;
293     PostImpl impl = (PostImpl) post ;
294     impl.setModifiedDate(now) ;
295     hservice_.update(impl);
296     Forum forum = getForum(impl.getForumId()) ;
297     indexer_.updatePost(forum, impl) ;
298     return post ;
299   }
300
301   public Post createPostInstance() { return new PostImpl() ; }
302   
303   //=============================Watcher watcher =============================
304
public Watcher createWatcher(Forum forum) { return new WatcherImpl(forum) ; }
305   
306   public Watcher createWatcher(Topic topic) { return new WatcherImpl(topic) ; }
307   
308   public Watcher getWatcher(Topic topic, String JavaDoc userName) throws Exception JavaDoc {
309     ObjectQuery q = new ObjectQuery(WatcherImpl.class).
310                     addEQ("target", WatcherImpl.TOPIC_TARGET).
311                     addEQ("topicId", topic.getId()).
312                     addEQ("userName", userName) ;
313     return (Watcher) hservice_.findOne(q);
314   }
315   
316   public Watcher getWatcher(Forum forum, String JavaDoc userName) throws Exception JavaDoc {
317     ObjectQuery q = new ObjectQuery(WatcherImpl.class).
318                     addEQ("target", WatcherImpl.FORUM_TARGET).
319                     addEQ("forumId", forum.getId()).
320                     addEQ("userName", userName) ;
321     return (Watcher) hservice_.findOne(q);
322   }
323   
324   public void saveWatcher(Watcher watcher) throws Exception JavaDoc {
325     WatcherImpl impl = (WatcherImpl) watcher ;
326     if(impl.getId() == null) {
327       impl.setId(idService_.generateStringID(impl)) ;
328       hservice_.create(impl) ;
329     } else {
330       hservice_.update(impl);
331     }
332   }
333   
334   public void removeWatcher(Watcher watcher) throws Exception JavaDoc {
335     hservice_.remove(watcher) ;
336   }
337   //======================================================================
338
public long getLastModifiedTime() { return modifiedTime_ ; }
339 }
340
Popular Tags