KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > generic > GenericPostDAO


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: Mar 28, 2003 / 22:57:43 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.dao.generic;
44
45 import java.sql.PreparedStatement JavaDoc;
46 import java.sql.ResultSet JavaDoc;
47 import java.sql.Timestamp JavaDoc;
48 import java.text.SimpleDateFormat JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 import net.jforum.JForumExecutionContext;
54 import net.jforum.dao.DataAccessDriver;
55 import net.jforum.entities.Post;
56 import net.jforum.util.preferences.ConfigKeys;
57 import net.jforum.util.preferences.SystemGlobals;
58 import net.jforum.util.search.SearchFacade;
59
60 /**
61  * @author Rafael Steil
62  * @author Vanessa Sabino
63  * @version $Id: GenericPostDAO.java,v 1.11 2006/01/29 15:06:20 rafaelsteil Exp $
64  */

65 public class GenericPostDAO extends AutoKeys implements net.jforum.dao.PostDAO
66 {
67     /**
68      * @see net.jforum.dao.PostDAO#selectById(int)
69      */

70     public Post selectById(int postId) throws Exception JavaDoc
71     {
72         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.selectById"));
73         p.setInt(1, postId);
74         
75         ResultSet JavaDoc rs = p.executeQuery();
76         
77         Post post = new Post();
78         
79         if (rs.next()) {
80             post = this.makePost(rs);
81         }
82         
83         rs.close();
84         p.close();
85         
86         return post;
87     }
88     
89     protected Post makePost(ResultSet JavaDoc rs) throws Exception JavaDoc
90     {
91         Post post = new Post();
92         post.setId(rs.getInt("post_id"));
93         post.setTopicId(rs.getInt("topic_id"));
94         post.setForumId(rs.getInt("forum_id"));
95         post.setUserId(rs.getInt("user_id"));
96         Timestamp JavaDoc postTime = rs.getTimestamp("post_time");
97         post.setTime(postTime);
98         post.setUserIp(rs.getString("poster_ip"));
99         post.setBbCodeEnabled(rs.getInt("enable_bbcode") > 0);
100         post.setHtmlEnabled(rs.getInt("enable_html") > 0);
101         post.setSmiliesEnabled(rs.getInt("enable_smilies") > 0);
102         post.setSignatureEnabled(rs.getInt("enable_sig") > 0);
103         post.setEditTime(rs.getTimestamp("post_edit_time"));
104         post.setEditCount(rs.getInt("post_edit_count"));
105         post.setSubject(rs.getString("post_subject"));
106         post.setText(this.getPostTextFromResultSet(rs));
107         post.setPostUsername(rs.getString("username"));
108         post.hasAttachments(rs.getInt("attach") > 0);
109         post.setModerate(rs.getInt("need_moderate") == 1);
110         
111         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
112         post.setFormatedTime(df.format(postTime));
113         
114         post.setKarma(DataAccessDriver.getInstance().newKarmaDAO().getPostKarma(post.getId()));
115         
116         return post;
117     }
118     
119     /**
120      * Utility method to read the post text fromt the result set.
121      * This method may be useful when using some "non-standart" way
122      * to store text, like oracle does when using (c|b)lob
123      *
124      * @param rs The resultset to fetch data from
125      * @return The post text string
126      * @throws Exception
127      */

128     protected String JavaDoc getPostTextFromResultSet(ResultSet JavaDoc rs) throws Exception JavaDoc
129     {
130         return rs.getString("post_text");
131     }
132
133     /**
134      * @see net.jforum.dao.PostDAO#delete(Post)
135      */

136     public void delete(Post post) throws Exception JavaDoc
137     {
138         List JavaDoc l = new ArrayList JavaDoc();
139         l.add(post);
140         this.removePosts(l);
141     }
142     
143     private void removePosts(List JavaDoc posts) throws Exception JavaDoc
144     {
145         PreparedStatement JavaDoc post = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.deletePost"));
146         PreparedStatement JavaDoc text = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.deletePostText"));
147         PreparedStatement JavaDoc search = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.deleteWordMatch"));
148         
149         for (Iterator JavaDoc iter = posts.iterator(); iter.hasNext(); ) {
150             Post p = (Post)iter.next();
151
152             post.setInt(1, p.getId());
153             text.setInt(1, p.getId());
154             search.setInt(1, p.getId());
155             
156             text.executeUpdate();
157             search.executeUpdate();
158             post.executeUpdate();
159         }
160         
161         search.close();
162         post.close();
163         text.close();
164     }
165     
166     /**
167      * @set net.jforum.model.PostModel#deleteByTopic(int)
168      */

169     public void deleteByTopic(int topicId) throws Exception JavaDoc
170     {
171         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.deleteByTopic"));
172         p.setInt(1, topicId);
173         ResultSet JavaDoc rs = p.executeQuery();
174         
175         List JavaDoc posts = new ArrayList JavaDoc();
176         while (rs.next()) {
177             Post post = new Post();
178             post.setId(rs.getInt("post_id"));
179             post.setUserId(rs.getInt("user_id"));
180             
181             posts.add(post);
182         }
183         
184         rs.close();
185         p.close();
186         
187         this.removePosts(posts);
188     }
189     
190     /**
191      * @see net.jforum.dao.PostDAO#update(net.jforum.Post)
192      */

193     public void update(Post post) throws Exception JavaDoc
194     {
195         this.updatePostsTable(post);
196         this.updatePostsTextTable(post);
197         
198          SearchFacade.index(post);
199     }
200     
201     protected void updatePostsTextTable(Post post) throws Exception JavaDoc
202     {
203         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.updatePostText"));
204         p.setString(1, post.getText());
205         p.setString(2, post.getSubject());
206         p.setInt(3, post.getId());
207         
208         p.executeUpdate();
209         p.close();
210     }
211     
212     protected void updatePostsTable(Post post) throws Exception JavaDoc
213     {
214         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.updatePost"));
215         p.setInt(1, post.getTopicId());
216         p.setInt(2, post.getForumId());
217         p.setInt(3, post.isBbCodeEnabled() ? 1 : 0);
218         p.setInt(4, post.isHtmlEnabled() ? 1 : 0);
219         p.setInt(5, post.isSmiliesEnabled() ? 1 : 0);
220         p.setInt(6, post.isSignatureEnabled() ? 1 : 0);
221         p.setTimestamp(7, new Timestamp JavaDoc(System.currentTimeMillis()));
222         p.setString(8, post.getUserIp());
223         p.setInt(9, post.getId());
224         
225         p.executeUpdate();
226         p.close();
227     }
228
229     /**
230      * @see net.jforum.dao.PostDAO#addNew(net.jforum.Post)
231      */

232     public int addNew(Post post) throws Exception JavaDoc
233     {
234         this.addNewPost(post);
235         this.addNewPostText(post);
236         
237         // Search
238
SearchFacade.index(post);
239         
240         return post.getId();
241     }
242     
243     protected void addNewPostText(Post post) throws Exception JavaDoc
244     {
245         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.addNewPostText"));
246         p.setInt(1, post.getId());
247         p.setString(2, post.getText());
248         p.setString(3, post.getSubject());
249         p.executeUpdate();
250         p.close();
251     }
252     
253     protected void addNewPost(Post post) throws Exception JavaDoc
254     {
255         PreparedStatement JavaDoc p = this.getStatementForAutoKeys("PostModel.addNewPost");
256         
257         p.setInt(1, post.getTopicId());
258         p.setInt(2, post.getForumId());
259         p.setLong(3, post.getUserId());
260         p.setTimestamp(4, new Timestamp JavaDoc(post.getTime().getTime()));
261         p.setString(5, post.getUserIp());
262         p.setInt(6, post.isBbCodeEnabled() ? 1 : 0);
263         p.setInt(7, post.isHtmlEnabled() ? 1 : 0);
264         p.setInt(8, post.isSmiliesEnabled() ? 1 : 0);
265         p.setInt(9, post.isSignatureEnabled() ? 1 : 0);
266         p.setInt(10, post.isModerationNeeded() ? 1 : 0);
267         
268         this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PostModel.lastGeneratedPostId"));
269         int postId = this.executeAutoKeysQuery(p);
270         post.setId(postId);
271         
272         p.close();
273     }
274     
275     /**
276      * @see net.jforum.dao.PostDAO#selectAllBytTopic(int)
277      */

278     public List JavaDoc selectAllByTopic(int topicId) throws Exception JavaDoc
279     {
280         return this.selectAllByTopicByLimit(topicId, 0, Integer.MAX_VALUE - 1);
281     }
282     
283     /**
284      * @see net.jforum.dao.PostDAO#selectAllBytTopicByLimit(int, int, int)
285      */

286     public List JavaDoc selectAllByTopicByLimit(int topicId, int startFrom, int count) throws Exception JavaDoc
287     {
288         List JavaDoc l = new ArrayList JavaDoc();
289         
290         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.selectAllByTopicByLimit"));
291         p.setInt(1, topicId);
292         p.setInt(2, startFrom);
293         p.setInt(3, count);
294         
295         ResultSet JavaDoc rs = p.executeQuery();
296                         
297         while (rs.next()) {
298             l.add(this.makePost(rs));
299         }
300         
301         rs.close();
302         p.close();
303                 
304         return l;
305     }
306
307     /**
308      * @see net.jforum.dao.PostDAO#selectByUserByLimit(int, int, int)
309      */

310     public List JavaDoc selectByUserByLimit(int userId,int startFrom, int count) throws Exception JavaDoc
311     {
312         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
313                 SystemGlobals.getSql("PostModel.selectByUserByLimit"));
314         
315         p.setInt(1,userId);
316         p.setInt(2, startFrom);
317         p.setInt(3, count);
318     
319         ResultSet JavaDoc rs = p.executeQuery();
320         List JavaDoc l = new ArrayList JavaDoc();
321         
322         while (rs.next()) {
323             l.add(this.makePost(rs));
324         }
325         
326         rs.close();
327         p.close();
328         
329         return l;
330     }
331     
332     /**
333      * @see net.jforum.model.PostModel#countPreviousPosts(int)
334      */

335     public int countPreviousPosts(int postId) throws Exception JavaDoc
336     {
337         int total = 0;
338         
339         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
340                 SystemGlobals.getSql("PostModel.countPreviousPosts"));
341         p.setInt(1, postId);
342         p.setInt(2, postId);
343         
344         ResultSet JavaDoc rs = p.executeQuery();
345         
346         if (rs.next()) {
347             total = rs.getInt(1);
348         }
349         
350         rs.close();
351         p.close();
352         
353         return total;
354     }
355 }
356
Popular Tags