KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.javabb.dao.hibernate;
2
3 import java.sql.ResultSet JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.javabb.dao.entity.IForumDAO;
13 import org.javabb.infra.ConfigurationFactory;
14 import org.javabb.infra.DateUtil;
15 import org.javabb.infra.Paging;
16 import org.javabb.transaction.PostTransaction;
17 import org.javabb.transaction.UserTransaction;
18 import org.javabb.vo.Forum;
19 import org.javabb.vo.Post;
20 import org.javabb.vo.Topic;
21 import org.javabb.vo.User;
22
23 /*
24  * Copyright 2004 JavaFree.org
25  *
26  * Licensed under the Apache License, Version 2.0 (the "License");
27  * you may not use this file except in compliance with the License.
28  * You may obtain a copy of the License at
29  *
30  * http://www.apache.org/licenses/LICENSE-2.0
31  *
32  * Unless required by applicable law or agreed to in writing, software
33  * distributed under the License is distributed on an "AS IS" BASIS,
34  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35  * See the License for the specific language governing permissions and
36  * limitations under the License.
37  */

38
39 /**
40  * $Id: ForumHibernateDAO.java,v 1.37.6.1.2.2 2006/04/17 17:46:28 daltoncamargo Exp $
41  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org </a> <br>
42  * @author Ronald Tetsuo Miura
43  */

44 public class ForumHibernateDAO extends HibernateDAO implements IForumDAO {
45
46     private final Log log = LogFactory.getLog(ForumHibernateDAO.class);
47     
48 // Dependency Injection
49
private PostTransaction postTransaction;
50     public void setPostTransaction(PostTransaction postTransaction) {
51         this.postTransaction = postTransaction;
52     }
53     
54 // Dependency Injection
55
private UserTransaction userTransaction;
56     public void setUserTransaction(UserTransaction userTransaction) {
57         this.userTransaction = userTransaction;
58     }
59     
60     /**
61      * @see org.javabb.dao.entity.IForumDAO#load(java.lang.Long)
62      */

63     public Forum load(Long JavaDoc id) {
64         return (Forum) load(Forum.class, id);
65     }
66
67     /**
68      * @see org.javabb.dao.entity.IForumDAO#create(org.javabb.vo.Forum)
69      */

70     public Forum insertForum(Forum forum) {
71         return (Forum)getHibernateTemplate().saveOrUpdateCopy(forum);
72     }
73     
74     /**
75      * @see org.javabb.dao.entity.IForumDAO#findAll()
76      */

77     public List JavaDoc findAll() {
78         return findAll(Forum.class, "o.forumOrder ASC", ALL_PAGES, ALL_PAGES);
79     }
80
81     public List JavaDoc findByCategoryOrderAsc(Long JavaDoc id) {
82         return this.findAll(Forum.class,
83                 new String JavaDoc[]{"category.idCategory"},
84                 new String JavaDoc[]{""+id},
85                 new String JavaDoc[]{"forumOrder"},
86                 new String JavaDoc[]{"asc"});
87     }
88     
89     
90     /**
91      * @see org.javabb.dao.entity.IForumDAO#findByCategory(java.lang.Long)
92      */

93     public List JavaDoc findByCategory(Long JavaDoc id) {
94         return findByAttribute(Forum.class, "o.category.idCategory", id);
95     }
96     
97     /**
98      * @see org.javabb.dao.entity.IForumDAO#update(org.javabb.vo.Forum)
99      */

100     public void update(Forum forum) {
101         getHibernateTemplate().update(forum);
102     }
103
104     /**
105      * @see org.javabb.dao.entity.IForumDAO#countAllForums()
106      */

107     public int countAllForums() {
108         return countRows(Forum.class, "idForum");
109     }
110
111     /**
112      * @param forum
113      * @param forumTo
114      * @throws Exception
115      * @see org.javabb.dao.entity.IForumDAO#transferForum(org.javabb.vo.Forum, int)
116      */

117     public void transferForum(Forum forum, int forumTo) throws Exception JavaDoc {
118         executeSQL("update jbb_topics set id_forum = " + forumTo
119             + " where id_forum = "
120             + forum.getIdForum());
121     }
122
123     /**
124      * Delete all records connected at Forum table
125      * @param forum
126      * @throws Exception
127      */

128     public void deleteForum(Forum forum) throws Exception JavaDoc {
129         deleteFrom("from org.javabb.vo.Post as p where p.topic.forum.idForum = " + forum.getIdForum());
130         deleteFrom("from org.javabb.vo.Topic as t where t.forum.idForum = " + forum.getIdForum());
131         deleteFrom("from org.javabb.vo.Forum as f where f.idForum = " + forum.getIdForum());
132     }
133
134     /**
135      * @see org.javabb.dao.entity.IForumDAO#refreshForum(java.lang.Long)
136      */

137     public void refreshForum(Long JavaDoc forumId) {
138
139         log.debug("Refreshing forums...");
140         
141         Forum forum = load(forumId);
142
143         // Number of topics
144
String JavaDoc customHql = "Topic as t where t.forum.idForum=" + forum.getIdForum();
145
146         Integer JavaDoc cntTopics = countRowsOfTable(customHql, "t.idTopic");
147         forum.setTopicCount(new Long JavaDoc(cntTopics.intValue()));
148
149         // Number of posts
150

151         customHql = "Post as p where p.topic.forum.idForum=" + forum.getIdForum();
152         Integer JavaDoc postCount = countRowsOfTable(customHql, "p.idPost");
153         forum.setPostCount(new Long JavaDoc(postCount.intValue()));
154
155         // Last post of forum
156
String JavaDoc[] whereField = { "topic.forum.idForum" };
157         String JavaDoc[] whereValue = { "" + forum.getIdForum() };
158         String JavaDoc[] orderField = { "postDate" };
159         String JavaDoc[] orderValue = { "desc" };
160
161         Post lastPost = null;
162         List JavaDoc lstPosts = findAll(Post.class, whereField, whereValue, orderField, orderValue, 0, 1);
163
164         if (!lstPosts.isEmpty()) {
165             lastPost = (Post) lstPosts.get(0);
166             //if have a post in this forum
167
forum.setLastPostId(lastPost.getId());
168             forum.setLastPostUserId(lastPost.getUser().getId());
169             forum.setLastPostUserName(lastPost.getUser().getUser());
170             forum.setLastPostDate(lastPost.getPostDate());
171         } else {
172             //No have post
173
forum.setLastPostId(null);
174             forum.setLastPostUserId(null);
175             forum.setLastPostUserName(null);
176             forum.setLastPostDate(null);
177         }
178
179         lstPosts = null;
180
181         log.debug("Forums: ok!");
182         
183         /** TODO CALCULAR A PÁGINA DO ÚLTIMO POST* */
184         // forum.setLastPagePost(new Long(totalPages));
185
// update(forum);
186
}
187
188     /**
189      * @see org.javabb.dao.entity.IForumDAO#refreshTopic(java.lang.Long)
190      */

191     public void refreshTopic(Long JavaDoc topicId) {
192
193         log.debug("Refreshing topics...");
194         // XXX
195
Topic topic = new Topic();
196         topic.setId(topicId);
197         topic = (Topic) load(topic);
198
199         Forum forum = topic.getForum();
200         forum = (Forum) load(forum);
201
202         String JavaDoc customHql = "Topic as t where t.forum.idForum=" + forum.getIdForum();
203         Integer JavaDoc cntTopics = countRowsOfTable(customHql, "t.idTopic");
204         forum.setTopicCount(new Long JavaDoc(cntTopics.intValue()));
205
206         String JavaDoc hqlRowsByTopic = "Post as p where p.topic.idTopic=" + topicId;
207         Integer JavaDoc cntPosts = countRowsOfTable(hqlRowsByTopic, "p.idPost");
208         topic.setRespostas(new Integer JavaDoc(cntPosts.intValue() - 1));
209         
210         Post post = postTransaction.findbyTopicDesc(topic);
211         
212         User user = null;
213         if(post.getUser().getUser() == null){
214             user = userTransaction.getUser(post.getUser().getIdUser());
215             topic.setLastPostUserName(user.getUser());
216         } else {
217             topic.setLastPostUserName(post.getUser().getUser());
218         }
219
220         topic.setLastPostId(post.getId());
221         topic.setLastPostDate(post.getPostDate());
222         topic.setLastPostUserId(post.getUser().getIdUser());
223         
224         Integer JavaDoc pageLastPost = postTransaction.getPageOfLastPostByTopic(topic);
225         topic.setPageLastPost(pageLastPost);
226
227         log.debug("Topics: ok!");
228     }
229
230     /**
231      * @see org.javabb.dao.entity.IForumDAO#refreshPost(java.lang.Long)
232      */

233     public void refreshPost(Long JavaDoc postId) {
234
235         log.debug("Refreshing posts...");
236         // XXX
237
Post post = new Post();
238         post.setId(postId);
239         post = (Post) load(post);
240
241         Topic topic = post.getTopic();
242         topic = (Topic) load(topic);
243         Forum forum = topic.getForum();
244         forum = (Forum) load(forum);
245
246         String JavaDoc customHql = "Post as p where p.topic.forum.idForum=" + forum.getIdForum();
247         Integer JavaDoc postCount = countRowsOfTable(customHql, "p.idPost");
248         forum.setPostCount(new Long JavaDoc(postCount.intValue()));
249
250         // Last post of forum
251
String JavaDoc[] whereField = { "topic.forum.idForum" };
252         String JavaDoc[] whereValue = { "" + forum.getIdForum() };
253         String JavaDoc[] orderField = { "postDate" };
254         String JavaDoc[] orderValue = { "desc" };
255
256         Post lastPost = null;
257         List JavaDoc lstPosts = findAll(Post.class, whereField, whereValue, orderField, orderValue, 0, 1);
258
259         if (!lstPosts.isEmpty()) {
260             lastPost = (Post) lstPosts.get(0);
261         }
262         lstPosts = null;
263
264         User usr = (User) load(lastPost.getUser());
265
266         forum.setLastPostId(lastPost.getId());
267         forum.setLastPostUserId(usr.getIdUser());
268
269         forum.setLastPostUserName(usr.getUser());
270         forum.setLastPostDate(lastPost.getPostDate());
271
272         usr = null;
273
274         // past page by topic
275
String JavaDoc[] whereFieldPage = { "topic.idTopic" };
276         String JavaDoc[] whereValuePage = { topic.getIdTopic().toString() };
277
278         // PAGING ** Obtendo informações
279
int rowsPerPage = ConfigurationFactory.getConf().postsPage.intValue();
280         int nroRecords = countRowsByWhere("Post", "idPost", whereFieldPage, whereValuePage).intValue();
281         int totalPages = Paging.getNroPages(rowsPerPage, nroRecords);
282
283         forum.setLastPagePost(new Long JavaDoc(totalPages));
284         
285         log.debug("Posts: ok!");
286     }
287
288
289     /**
290      **TODO CHANGE THIS METHOD TO HQL
291      * Get all id foruns with unread topics
292      * @param readTopics - Topics reads
293      * @param lastUserVisit - Last user visit of user
294      * @param userId - id of user
295      * @see org.javabb.dao.entity.IForumDAO#obtainUnreadForuns(Set, Date)
296      */

297     public List JavaDoc obtainUnreadForuns(Set JavaDoc readTopics, Date JavaDoc lastUserVisit, Long JavaDoc userId) throws Exception JavaDoc{
298         Iterator JavaDoc itTopics = readTopics.iterator();
299         List JavaDoc forumIds = new ArrayList JavaDoc();
300         
301         String JavaDoc lastUserStr = DateUtil.dateFormat(lastUserVisit, "yyyy-MM-dd HH:mm:ss");
302         
303         if(userId == null){
304             userId = new Long JavaDoc(0);
305         }
306         
307         /*TODO CHANGE TO HQL*/
308         String JavaDoc sql = " SELECT distinct(t.id_forum) " +
309                     " FROM jbb_posts p, jbb_topics t " +
310                     " WHERE p.id_post = last_post_id " +
311                     " AND p.data_post > '" + lastUserStr + "'" +
312                     " AND t.last_post_user_id <> " + userId;
313
314         while(itTopics.hasNext()){
315             Long JavaDoc topicId = (Long JavaDoc)itTopics.next();
316             sql += " AND t.id_topic <> " + topicId;
317         }
318
319
320         logger.debug("native:" + sql);
321         
322         ResultSet JavaDoc rs = this.getSession().connection().createStatement().executeQuery(sql);
323         while(rs.next()){
324             forumIds.add(new Long JavaDoc(rs.getLong(1)));
325         }
326         return forumIds;
327     }
328
329
330     
331 }
332
Popular Tags