KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > dao > hibernate > TopicHibernateDAO


1 package org.javabb.dao.hibernate;
2
3 import java.util.Date JavaDoc;
4 import java.util.List JavaDoc;
5
6 import net.sf.hibernate.HibernateException;
7 import net.sf.hibernate.Session;
8
9 import org.javabb.dao.entity.ITopicDAO;
10 import org.javabb.vo.AnswerNotify;
11 import org.javabb.vo.AnswerNotifyPK;
12 import org.javabb.vo.FavUserTopic;
13 import org.javabb.vo.Forum;
14 import org.javabb.vo.Topic;
15 import org.springframework.orm.hibernate.HibernateCallback;
16
17 /*
18  * Copyright 2004 JavaFree.org
19  *
20  * Licensed under the Apache License, Version 2.0 (the "License");
21  * you may not use this file except in compliance with the License.
22  * You may obtain a copy of the License at
23  *
24  * http://www.apache.org/licenses/LICENSE-2.0
25  *
26  * Unless required by applicable law or agreed to in writing, software
27  * distributed under the License is distributed on an "AS IS" BASIS,
28  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  * See the License for the specific language governing permissions and
30  * limitations under the License.
31  */

32
33 /**
34  * $Id: TopicHibernateDAO.java,v 1.21.10.7 2006/08/28 21:00:21 daltoncamargo Exp $
35  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org </a> <br>
36  * @author Ronald Tetsuo Miura
37  */

38 public class TopicHibernateDAO extends HibernateDAO implements ITopicDAO {
39     
40     /**
41      * @see org.javabb.dao.entity.ITopicDAO#findLastTopic()
42      */

43     public Topic findLastTopic() {
44         String JavaDoc sql = " from {vo}Topic as p " + " order by p.idTopic desc";
45         Topic t = null;
46         List JavaDoc lst = getList(sql, 0, 1);
47
48         if (!lst.isEmpty()) {
49             t = (Topic) lst.get(0);
50         }
51
52         lst = null;
53
54         return t;
55     }
56
57     /**
58      * @see org.javabb.dao.entity.ITopicDAO#findCountOfTopicsByForum(org.javabb.vo.Forum)
59      */

60     public Integer JavaDoc findCountOfTopicsByForum(Forum forum) {
61         String JavaDoc sql = "Topic as t " + " where t.forum.idForum =" + forum.getIdForum();
62
63         return this.countRowsOfTable(sql, "t.idTopic");
64     }
65
66     /**
67      * @see org.javabb.dao.entity.ITopicDAO#load(java.lang.Long)
68      */

69     public Topic load(Long JavaDoc id) {
70         return (Topic) getHibernateTemplate().load(Topic.class, id);
71     }
72
73     /**
74      * @see org.javabb.dao.entity.ITopicDAO#create(org.javabb.vo.Topic)
75      */

76     public Long JavaDoc create(Topic topic) {
77         return (Long JavaDoc) getHibernateTemplate().save(topic);
78     }
79
80     /**
81      * @see org.javabb.dao.entity.ITopicDAO#delete(java.lang.Long)
82      */

83     public void delete(Long JavaDoc topicId) {
84         getHibernateTemplate().delete(load(topicId));
85     }
86
87     /**
88      * @param topicId
89      */

90     public void deleteAllPostOfTopic(Long JavaDoc topicId) {
91         // TODO pode ser usada deleção em cascata aqui?
92
Topic topic = load(topicId);
93         this.getHibernateTemplate().deleteAll(topic.getPosts());
94     }
95
96     /**
97      * @see org.javabb.dao.entity.ITopicDAO#lockTopic(org.javabb.vo.Topic, java.lang.Integer)
98      */

99     public void lockTopic(Topic topic, Integer JavaDoc lock) {
100         topic = load(topic.getId());
101         topic.setTopicStatus(lock);
102     }
103
104     /**
105      * @see org.javabb.dao.entity.ITopicDAO#moveTopic(org.javabb.vo.Topic, java.lang.Long)
106      */

107     public void moveTopic(Topic topic, Long JavaDoc idForumDest) {
108         
109         String JavaDoc sql = "update jbb_topics set id_forum=" + idForumDest
110             + " where id_forum="
111             + topic.getForum().getIdForum()
112             + " and id_topic="
113             + topic.getIdTopic();
114         this.executeSQL(sql);
115         
116         
117     }
118
119     /**
120      * @see org.javabb.dao.entity.ITopicDAO#findAll()
121      */

122     public List JavaDoc findAll() {
123         return findAll(Topic.class, "o.idTopic", ALL_PAGES, 0);
124     }
125
126     /**
127      * @see org.javabb.dao.entity.ITopicDAO#countTopicsByForum(java.lang.Long)
128      */

129     public int countTopicsByForum(Long JavaDoc forumId, Integer JavaDoc forumModel) {
130         return countRowsWhere(Topic.class,
131             "o.idTopic",
132             "o.forum.idForum=? and o.topicModel=?",
133             new Object JavaDoc[] { forumId, forumModel});
134     }
135
136     /**
137      * @see org.javabb.dao.entity.ITopicDAO#findByForum(java.lang.Long, Long, int, int)
138      */

139     public List JavaDoc findByForum(Long JavaDoc forumId, Integer JavaDoc forumModel, int pageNumber, int itemsPerPage) {
140         /*
141         
142         List topics = find(Topic.class,
143                 "o.forum.idForum=? and o.topicModel=?",
144                 new Object[] { forumId, forumModel },
145                 "o.lastPostDate desc, o.idTopic desc",
146                 pageNumber,
147                 itemsPerPage);
148 */

149         
150         String JavaDoc sql = " SELECT o FROM Topic as o " +
151         " inner join fetch o.user as user " +
152         " inner join fetch o.forum as forum " +
153         " inner join fetch forum.category as cat " +
154         " WHERE o.forum.idForum=? and o.topicModel=? " +
155         " ORDER BY o.lastPostDate desc, o.idTopic desc " +
156         " ";
157
158         List JavaDoc topics = find(sql, new Object JavaDoc[] { forumId, forumModel },pageNumber,itemsPerPage);
159                 
160         return topics;
161         
162     }
163
164     /**
165      * @see org.javabb.dao.entity.ITopicDAO#findByForum(java.lang.Long, Long)
166      */

167     public List JavaDoc findByForum(Long JavaDoc forumId, Integer JavaDoc forumModel) {
168         
169         return find(Topic.class,
170             "o.forum.idForum=? and o.topicModel=?",
171             new Object JavaDoc[] { forumId, forumModel },
172             "o.lastPostDate desc, o.idTopic desc",
173             ALL_PAGES, 0);
174     }
175     
176     
177     /**
178      * @see org.javabb.dao.entity.ITopicDAO#findPostedAfter(java.util.Date, int, int)
179      */

180     public List JavaDoc findPostedAfter(Date JavaDoc date, int pageNumber, int itemsPerPage) {
181
182         return find(Topic.class,
183             "o.lastPostDate >= ?",
184             new Object JavaDoc[] { date },
185             "o.lastPostDate DESC",
186             pageNumber,
187             itemsPerPage);
188     }
189
190     /**
191      * @see org.javabb.dao.entity.ITopicDAO#countPostedAfter(java.util.Date)
192      */

193     public int countPostedAfter(Date JavaDoc date) {
194         if (logger.isDebugEnabled()) {
195             logger.debug("count posts after " + date);
196         }
197         return countRowsWhere(Topic.class,
198             "o.idTopic",
199             "o.lastPostDate >= ?",
200             new Object JavaDoc[] { date });
201     }
202     
203     /**
204      * @see org.javabb.dao.entity.ITopicDAO#update(org.javabb.vo.Topic)
205      */

206     public void update( Topic topic ) {
207         getHibernateTemplate().update(topic);
208     }
209     
210     /**
211      * @see org.javabb.dao.entity.ITopicDAO#public List wathTopicByTopicUser(Long, Long)
212      */

213     public List JavaDoc wathTopicByTopicUser(Long JavaDoc userId, Long JavaDoc topicId) {
214         return find(AnswerNotify.class,
215                 "o.user.idUser = ? AND o.topic.idTopic = ?",
216                 new Object JavaDoc[] { userId, topicId },
217                 "o.user.idUser asc",
218                 ALL_PAGES, 0);
219     }
220     
221     
222     public List JavaDoc favoriteTopicByTopicUser(Long JavaDoc userId, Long JavaDoc topicId) {
223         return find(FavUserTopic.class,
224                 "o.user.idUser = ? AND o.topic.idTopic = ?",
225                 new Object JavaDoc[] { userId, topicId },
226                 "o.user.idUser asc",
227                 ALL_PAGES, 0);
228     }
229     
230     
231     public List JavaDoc favoriteTopicByTopic(Long JavaDoc topicId) {
232         return find(FavUserTopic.class,
233                 "o.topic.idTopic = ?",
234                 new Object JavaDoc[] { topicId },
235                 "o.topic.idTopic asc",
236                 ALL_PAGES, 0);
237     }
238     
239     /**
240      * @see org.javabb.dao.entity.ITopicDAO#public void insertWatchTopicUser(Long, Long)
241      */

242     public void insertWatchTopicUser(Long JavaDoc topicId, Long JavaDoc userId){
243         AnswerNotify answerNotify = new AnswerNotify(new AnswerNotifyPK(topicId, userId));
244         this.getHibernateTemplate().save(answerNotify);
245     }
246     
247     /**
248      * @see org.javabb.dao.entity.ITopicDAO#public void deleteWatchTopicUser(Long, Long)
249      */

250     public void deleteWatchTopicUser(Long JavaDoc topicId, Long JavaDoc userId){
251         AnswerNotify answerNotify = new AnswerNotify(new AnswerNotifyPK(topicId, userId));
252         this.getHibernateTemplate().delete(answerNotify);
253     }
254     
255     /**
256      * @see org.javabb.dao.entity.ITopicDAO#public List wathTopicByUser(Long)
257      */

258     public List JavaDoc wathTopicByUser(Long JavaDoc userId) {
259         return find(AnswerNotify.class,
260                 "o.user.idUser = ?",
261                 new Object JavaDoc[] { userId },
262                 "o.user.idUser asc",
263                 ALL_PAGES, 0);
264     }
265     
266
267     public List JavaDoc favoriteTopicByUser(Long JavaDoc userId) {
268         return find(FavUserTopic.class,
269                 "o.user.idUser = ?",
270                 new Object JavaDoc[] { userId },
271                 "o.user.idUser asc",
272                 ALL_PAGES, 0);
273     }
274     
275     
276     /**
277      * @see org.javabb.dao.entity.ITopicDAO#public List wathTopicByTopic(Long)
278      */

279     public List JavaDoc wathTopicByTopic(Long JavaDoc topicId) {
280         return find(AnswerNotify.class,
281                 "o.topic.idTopic = ?",
282                 new Object JavaDoc[] { topicId },
283                 "o.topic.idTopic asc",
284                 ALL_PAGES, 0);
285     }
286     
287     /**
288      * @see org.javabb.dao.entity.ITopicDAO#findLastTopics(int)
289      */

290     public List JavaDoc findLastTopics(int limit) {
291         String JavaDoc sql = " SELECT o FROM Topic as o " +
292         " inner join fetch o.user as user " +
293         " inner join fetch o.forum as forum " +
294         " ORDER BY o.lastPostId desc ";
295         return find(sql, new Object JavaDoc[] {}, 1, limit);
296     }
297
298     /**
299      * Favorite Topics listed by Rank
300      */

301     public List JavaDoc favoriteTopics() {
302         HibernateCallback callback = new HibernateCallback() {
303             public Object JavaDoc doInHibernate(Session session)
304                     throws HibernateException {
305                 String JavaDoc hql = "SELECT distinct f.topic.idTopic from FavUserTopic as f order by f.topic.idTopic desc";
306                 List JavaDoc favs = find(hql, new Object JavaDoc[] {}, ALL_PAGES, 50);
307                 
308                 if (!favs.isEmpty()) {
309                     hql = "SELECT t from Topic as t where t.idTopic in (";
310                     for (int i = 0; i < favs.size(); i++) {
311                         Long JavaDoc fav = (Long JavaDoc)favs.get(i);
312                         hql += fav + ",";
313                     }
314                     hql = hql.substring(0, hql.length() - 1);
315                     hql += ")";
316
317                     favs = session.find(hql);
318                 }
319                 return favs;
320             }
321         };
322         return getHibernateTemplate().executeFind(callback);
323     }
324     
325 }
326
Popular Tags