KickJava   Java API By Example, From Geeks To Geeks.

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


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: Apr 6, 2003 / 2:38:28 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.Collection JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55
56 import net.jforum.JForumExecutionContext;
57 import net.jforum.SessionFacade;
58 import net.jforum.dao.DataAccessDriver;
59 import net.jforum.dao.ForumDAO;
60 import net.jforum.dao.PollDAO;
61 import net.jforum.dao.PostDAO;
62 import net.jforum.entities.KarmaStatus;
63 import net.jforum.entities.Topic;
64 import net.jforum.entities.User;
65 import net.jforum.util.preferences.ConfigKeys;
66 import net.jforum.util.preferences.SystemGlobals;
67
68 /**
69  * @author Rafael Steil
70  * @version $Id: GenericTopicDAO.java,v 1.8 2006/02/13 19:15:13 rafaelsteil Exp $
71  */

72 public class GenericTopicDAO extends AutoKeys implements net.jforum.dao.TopicDAO
73 {
74     /**
75      * @see net.jforum.dao.TopicDAO#fixFirstLastPostId(int)
76      */

77     public void fixFirstLastPostId(int topicId) throws Exception JavaDoc
78     {
79         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getFirstLastPostId"));
80         p.setInt(1, topicId);
81         
82         ResultSet JavaDoc rs = p.executeQuery();
83         if (rs.next()) {
84             int first = rs.getInt("first_post_id");
85             int last = rs.getInt("last_post_id");
86             
87             rs.close();
88             p.close();
89             
90             p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.fixFirstLastPostId"));
91             p.setInt(1, first);
92             p.setInt(2, last);
93             p.setInt(3, topicId);
94             p.executeUpdate();
95         }
96         
97         rs.close();
98         p.close();
99     }
100
101     /**
102      * @see net.jforum.dao.TopicDAO#selectById(int)
103      */

104     public Topic selectById(int topicId) throws Exception JavaDoc
105     {
106         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.selectById"));
107         p.setInt(1, topicId);
108         
109         Topic t = new Topic();
110         List JavaDoc l = this.fillTopicsData(p);
111         
112         if (l.size() > 0) {
113             t = (Topic)l.get(0);
114         }
115         
116         return t;
117     }
118     
119     /**
120      * @see net.jforum.dao.TopicDAO#selectRaw(int)
121      */

122     public Topic selectRaw(int topicId) throws Exception JavaDoc
123     {
124         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.selectRaw"));
125         p.setInt(1, topicId);
126         
127         Topic t = new Topic();
128         ResultSet JavaDoc rs = p.executeQuery();
129         if (rs.next()) {
130             t = this.getBaseTopicData(rs);
131         }
132         
133         rs.close();
134         p.close();
135         return t;
136     }
137
138     /**
139      * @see net.jforum.dao.TopicDAO#delete(int)
140      */

141     public void delete(final Topic topic) throws Exception JavaDoc
142     {
143         this.deleteTopics(new ArrayList JavaDoc() {{ add(topic); }});
144     }
145     
146     public void deleteTopics(List JavaDoc topics) throws Exception JavaDoc
147     {
148         // Topic
149
PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.delete"));
150         ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
151         
152         PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
153         PollDAO plm = DataAccessDriver.getInstance().newPollDAO();
154         
155         for (Iterator JavaDoc iter = topics.iterator(); iter.hasNext(); ) {
156             Topic topic = (Topic)iter.next();
157
158             // Remove watches
159
this.removeSubscriptionByTopic(topic.getId());
160
161             // Remove the messages
162
pm.deleteByTopic(topic.getId());
163             
164             // Remove the poll
165
plm.deleteByTopicId(topic.getId());
166             
167             p.setInt(1, topic.getId());
168             p.executeUpdate();
169         }
170         
171         p.close();
172     }
173     
174     /**
175      * @see net.jforum.dao.TopicDAO#deleteByForum(int)
176      */

177     public void deleteByForum(int forumId) throws Exception JavaDoc
178     {
179         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.deleteByForum"));
180         p.setInt(1, forumId);
181         
182         ResultSet JavaDoc rs = p.executeQuery();
183         List JavaDoc topics = new ArrayList JavaDoc();
184         while (rs.next()) {
185             Topic t = new Topic();
186             t.setId(rs.getInt("topic_id"));
187             
188             topics.add(t);
189         }
190         
191         rs.close();
192         p.close();
193         
194         this.deleteTopics(topics);
195     }
196
197     /**
198      * @see net.jforum.dao.TopicDAO#update(net.jforum.Topic)
199      */

200     public void update(Topic topic) throws Exception JavaDoc
201     {
202         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.update"));
203         
204         p.setString(1, topic.getTitle());
205         p.setInt(2, topic.getLastPostId());
206         p.setInt(3, topic.getFirstPostId());
207         p.setInt(4, topic.getType());
208         p.setInt(5, topic.isModerated() ? 1 : 0);
209         p.setInt(6, topic.getVoteId());
210         p.setInt(7, topic.getId());
211         p.executeUpdate();
212         
213         p.close();
214     }
215
216     /**
217      * @see net.jforum.dao.TopicDAO#addNew(net.jforum.Topic)
218      */

219     public int addNew(Topic topic) throws Exception JavaDoc
220     {
221         PreparedStatement JavaDoc p = this.getStatementForAutoKeys("TopicModel.addNew");
222         
223         p.setInt(1, topic.getForumId());
224         p.setString(2, topic.getTitle());
225         p.setInt(3, topic.getPostedBy().getId());
226         p.setTimestamp(4, new Timestamp JavaDoc(topic.getTime().getTime()));
227         p.setInt(5, topic.getFirstPostId());
228         p.setInt(6, topic.getLastPostId());
229         p.setInt(7, topic.getType());
230         p.setInt(8, topic.isModerated() ? 1 : 0);
231         
232         this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("TopicModel.lastGeneratedTopicId"));
233         int topicId = this.executeAutoKeysQuery(p);
234             
235         p.close();
236         return topicId;
237     }
238
239     /**
240      * @see net.jforum.dao.TopicDAO#incrementTotalViews(int)
241      */

242     public void incrementTotalViews(int topicId) throws Exception JavaDoc
243     {
244         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.incrementTotalViews"));
245         p.setInt(1, topicId);
246         p.executeUpdate();
247         p.close();
248     }
249
250     /**
251      * @see net.jforum.dao.TopicDAO#incrementTotalReplies(int)
252      */

253     public void incrementTotalReplies(int topicId) throws Exception JavaDoc
254     {
255         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.incrementTotalReplies"));
256         p.setInt(1, topicId);
257         p.executeUpdate();
258         p.close();
259     }
260
261     /**
262      * @see net.jforum.dao.TopicDAO#decrementTotalReplies(int)
263      */

264     public void decrementTotalReplies(int topicId) throws Exception JavaDoc
265     {
266         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.decrementTotalReplies"));
267         p.setInt(1, topicId);
268         p.executeUpdate();
269         p.close();
270     }
271
272     /**
273      * @see net.jforum.dao.TopicDAO#setLastPostId(int, int)
274      */

275     public void setLastPostId(int topicId, int postId) throws Exception JavaDoc
276     {
277         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.setLastPostId"));
278         p.setInt(1, postId);
279         p.setInt(2, topicId);
280         p.executeUpdate();
281         p.close();
282     }
283
284     /**
285      * @see net.jforum.dao.TopicDAO#selectAllByForum(int)
286      */

287     public List JavaDoc selectAllByForum(int forumId) throws Exception JavaDoc
288     {
289         return this.selectAllByForumByLimit(forumId, 0, Integer.MAX_VALUE);
290     }
291     
292     /**
293      * @see net.jforum.dao.TopicDAO#selectAllByForumByLimit(int, int, int)
294      */

295     public List JavaDoc selectAllByForumByLimit(
296         int forumId,
297         int startFrom,
298         int count)
299         throws Exception JavaDoc
300     {
301         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
302                 SystemGlobals.getSql("TopicModel.selectAllByForumByLimit"));
303         p.setInt(1, forumId);
304         p.setInt(2, startFrom);
305         p.setInt(3, count);
306         
307         return this.fillTopicsData(p);
308     }
309
310     /**
311      * @see net.jforum.dao.TopicDAO#selectByUserByLimit(int, int, int)
312      */

313     public List JavaDoc selectByUserByLimit(int userId, int startFrom, int count) throws Exception JavaDoc
314     {
315         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
316                 SystemGlobals.getSql("TopicModel.selectByUserByLimit"));
317         
318         p.setInt(1,userId);
319         p.setInt(2, startFrom);
320         p.setInt(3, count);
321         
322         return this.fillTopicsData(p);
323     }
324     
325     /**
326      * @see net.jforum.dao.TopicDAO#countUserTopics(int)
327      */

328     public int countUserTopics(int userId) throws Exception JavaDoc
329     {
330         int total = 0;
331         
332         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
333                 SystemGlobals.getSql("TopicModel.countUserTopics"));
334         p.setInt(1, userId);
335         
336         ResultSet JavaDoc rs = p.executeQuery();
337         
338         if (rs.next()) {
339             total = rs.getInt(1);
340         }
341         
342         rs.close();
343         p.close();
344         
345         return total;
346     }
347     
348     protected Topic getBaseTopicData(ResultSet JavaDoc rs) throws Exception JavaDoc
349     {
350         Topic t = new Topic();
351         
352         t.setTitle(rs.getString("topic_title"));
353         t.setId(rs.getInt("topic_id"));
354         t.setTime(rs.getTimestamp("topic_time"));
355         t.setStatus(rs.getInt("topic_status"));
356         t.setTotalViews(rs.getInt("topic_views"));
357         t.setTotalReplies(rs.getInt("topic_replies"));
358         t.setFirstPostId(rs.getInt("topic_first_post_id"));
359         t.setLastPostId(rs.getInt("topic_last_post_id"));
360         t.setType(rs.getInt("topic_type"));
361         t.setForumId(rs.getInt("forum_id"));
362         t.setModerated(rs.getInt("moderated") == 1);
363         t.setVoteId(rs.getInt("topic_vote_id"));
364         
365         return t;
366     }
367
368     /**
369      * @see net.jforum.dao.TopicDAO#autoSetLastPostId(int)
370      */

371     public int getMaxPostId(int topicId) throws Exception JavaDoc
372     {
373         int id = -1;
374         
375         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getMaxPostId"));
376         p.setInt(1, topicId);
377         
378         ResultSet JavaDoc rs = p.executeQuery();
379         if (rs.next()) {
380             id = rs.getInt("post_id");
381         }
382         
383         rs.close();
384         p.close();
385         
386         return id;
387     }
388
389     /**
390      * @see net.jforum.dao.TopicDAO#getTotalPosts(int)
391      */

392     public int getTotalPosts(int topicId) throws Exception JavaDoc
393     {
394         int total = 0;
395         
396         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getTotalPosts"));
397         p.setInt(1, topicId);
398         
399         ResultSet JavaDoc rs = p.executeQuery();
400         if (rs.next()) {
401             total = rs.getInt("total");
402         }
403         
404         rs.close();
405         p.close();
406         
407         return total;
408     }
409
410     /**
411      * @see net.jforum.dao.TopicDAO#selectLastN(int)
412      */

413     public List JavaDoc selectLastN(int count) throws Exception JavaDoc
414     {
415         List JavaDoc topics = new ArrayList JavaDoc();
416         
417         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.selectLastN"));
418         p.setInt(1, count);
419         
420         ResultSet JavaDoc rs = p.executeQuery();
421         
422         // If you want more fields here, just put the code. At the time
423
// this code was written, these were the only needed fields ;)
424
while (rs.next()) {
425             Topic t = new Topic();
426             
427             t.setTitle(rs.getString("topic_title"));
428             t.setId(rs.getInt("topic_id"));
429             t.setTime(rs.getTimestamp("topic_time"));
430             t.setType(rs.getInt("topic_type"));
431             
432             topics.add(t);
433         }
434         
435         rs.close();
436         p.close();
437         
438         return topics;
439     }
440     
441     /**
442      * @see net.jforum.dao.TopicDAO#notifyUsers(int)
443      */

444     public List JavaDoc notifyUsers(Topic topic) throws Exception JavaDoc
445     {
446         int posterId = SessionFacade.getUserSession().getUserId();
447         int anonUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
448         
449         PreparedStatement JavaDoc stmt = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.notifyUsers"));
450         ResultSet JavaDoc rs = null;
451
452         stmt.setInt(1, topic.getId());
453         stmt.setInt(2, posterId); //don't notify the poster
454
stmt.setInt(3, anonUser); //don't notify the anonimous user
455

456         rs = stmt.executeQuery();
457         
458         List JavaDoc users = new ArrayList JavaDoc();
459         while(rs.next()) {
460             User user = new User();
461
462             user.setId(rs.getInt("user_id"));
463             user.setEmail(rs.getString("user_email"));
464             user.setUsername(rs.getString("username"));
465             user.setLang(rs.getString("user_lang"));
466             
467             users.add(user);
468         }
469         
470         // Set read status to false
471
stmt = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.markAllAsUnread"));
472         stmt.setInt(1, topic.getId());
473         stmt.setInt(2, posterId); //don't notify the poster
474
stmt.setInt(3, anonUser); //don't notify the anonimous user
475

476         stmt.executeUpdate();
477             
478         rs.close();
479         stmt.close();
480         
481         return users;
482     }
483     
484     /**
485      * @see net.jforum.dao.TopicDAO#subscribeUser(int, int)
486      */

487     public void subscribeUser(int topicId, int userId) throws Exception JavaDoc
488     {
489         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection(). prepareStatement(SystemGlobals.getSql("TopicModel.subscribeUser"));
490         
491         p.setInt(1, topicId);
492         p.setInt(2, userId);
493             
494         p.executeUpdate();
495         p.close();
496     }
497     
498     /**
499      * @see net.jforum.dao.TopicDAO#isUserSubscribing(int, int)
500      */

501     public boolean isUserSubscribed(int topicId, int userId) throws Exception JavaDoc
502     {
503         PreparedStatement JavaDoc stmt = JForumExecutionContext.getConnection(). prepareStatement( SystemGlobals.getSql("TopicModel.isUserSubscribed"));
504         ResultSet JavaDoc rs = null;
505         
506         stmt.setInt(1, topicId);
507         stmt.setInt(2, userId);
508         
509         rs = stmt.executeQuery();
510         boolean status = rs.next();
511         
512         rs.close();
513         stmt.close();
514                 
515         return status;
516     }
517     
518     /**
519      * @see net.jforum.dao.TopicDAO#removeSubscription(int, int)
520      */

521     public void removeSubscription(int topicId, int userId) throws Exception JavaDoc
522     {
523         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.removeSubscription"));
524         p.setInt(1, topicId);
525         p.setInt(2, userId);
526         
527         p.executeUpdate();
528         p.close();
529     }
530     
531     /**
532      * @see net.jforum.dao.TopicDAO#removeSubscriptionByTopic(int)
533      */

534     public void removeSubscriptionByTopic(int topicId) throws Exception JavaDoc
535     {
536         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.removeSubscriptionByTopic"));
537         p.setInt(1, topicId);
538         
539         p.executeUpdate();
540         p.close();
541     }
542
543     /**
544      * @see net.jforum.dao.TopicDAO#updateReadStatus(int, int, boolean)
545      */

546     public void updateReadStatus(int topicId, int userId, boolean read) throws Exception JavaDoc
547     {
548         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.updateReadStatus"));
549         p.setInt(1, read ? 1 : 0);
550         p.setInt(2, topicId);
551         p.setInt(3, userId);
552         
553         p.executeUpdate();
554         p.close();
555     }
556     
557     /**
558      * @see net.jforum.dao.TopicDAO#lockUnlock(int, int)
559      */

560     public void lockUnlock(int[] topicId, int status) throws Exception JavaDoc
561     {
562         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.lockUnlock"));
563         p.setInt(1, status);
564         
565         for (int i = 0; i < topicId.length; i++) {
566             p.setInt(2, topicId[i]);
567             p.executeUpdate();
568         }
569         p.close();
570     }
571     
572     /**
573      * Fills all topic data.
574      * The method will try to get all fields from the topics table,
575      * as well information about the user who made the first and the
576      * last post in the topic.
577      * <br>
578      * <b>The method <i>will</i> close the <i>PreparedStatement</i></b>
579      * @param p the PreparedStatement to execute
580      * @return A list with all topics found
581      * @throws Exception
582      */

583     public List JavaDoc fillTopicsData(PreparedStatement JavaDoc p) throws Exception JavaDoc
584     {
585         List JavaDoc l = new ArrayList JavaDoc();
586         
587         ResultSet JavaDoc rs = p.executeQuery();
588         
589         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
590         
591         StringBuffer JavaDoc sbFirst = new StringBuffer JavaDoc(128);
592         StringBuffer JavaDoc sbLast = new StringBuffer JavaDoc(128);
593         
594         while (rs.next()) {
595             Topic t = this.getBaseTopicData(rs);
596             
597             // Posted by
598
User u = new User();
599             u.setId(rs.getInt("user_id"));
600             t.setPostedBy(u);
601             
602             // Last post by
603
u = new User();
604             u.setId(rs.getInt("last_user_id"));
605             t.setLastPostBy(u);
606             
607             t.setHasAttach(rs.getInt("attach") > 0);
608             t.setFirstPostTime(df.format(rs.getTimestamp("topic_time")));
609             t.setLastPostTime(df.format(rs.getTimestamp("post_time")));
610             t.setLastPostDate(rs.getTimestamp("post_time"));
611             
612             l.add(t);
613             
614             sbFirst.append(rs.getInt("user_id")).append(',');
615             sbLast.append(rs.getInt("last_user_id")).append(',');
616         }
617         
618         rs.close();
619         p.close();
620         
621         // Users
622
if (sbFirst.length() > 0) {
623             sbLast.delete(sbLast.length() - 1, sbLast.length());
624             
625             String JavaDoc sql = SystemGlobals.getSql("TopicModel.getUserInformation");
626             sql = sql.replaceAll("#ID#", sbFirst.toString() + sbLast.toString());
627             
628             Map JavaDoc users = new HashMap JavaDoc();
629             
630             p = JForumExecutionContext.getConnection().prepareStatement(sql);
631             rs = p.executeQuery();
632
633             while (rs.next()) {
634                 users.put(new Integer JavaDoc(rs.getInt("user_id")), rs.getString("username"));
635             }
636             
637             rs.close();
638             p.close();
639             
640             for (Iterator JavaDoc iter = l.iterator(); iter.hasNext(); ) {
641                 Topic t = (Topic)iter.next();
642                 t.getPostedBy().setUsername((String JavaDoc)users.get(new Integer JavaDoc(t.getPostedBy().getId())));
643                 t.getLastPostBy().setUsername((String JavaDoc)users.get(new Integer JavaDoc(t.getLastPostBy().getId())));
644             }
645         }
646         
647         return l;
648     }
649     
650     /**
651      * @see net.jforum.dao.TopicDAO#selectRecentTopics(int)
652      */

653     public List JavaDoc selectRecentTopics (int limit) throws Exception JavaDoc
654     {
655         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
656                 SystemGlobals.getSql("TopicModel.selectRecentTopicsByLimit"));
657         p.setInt(1, limit);
658         
659         return this.fillTopicsData(p);
660     }
661     
662     /**
663      * @see net.jforum.dao.TopicDAO#setFirstPostId(int, int)
664      */

665     public void setFirstPostId(int topicId, int postId) throws Exception JavaDoc
666     {
667         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.setFirstPostId"));
668         p.setInt(1, postId);
669         p.setInt(2, topicId);
670         p.executeUpdate();
671         p.close();
672     }
673
674     /**
675      * @see net.jforum.dao.TopicDAO#getMinPostId(int)
676      */

677     public int getMinPostId(int topicId) throws Exception JavaDoc
678     {
679         int id = -1;
680         
681         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getMinPostId"));
682         p.setInt(1, topicId);
683         
684         ResultSet JavaDoc rs = p.executeQuery();
685         if (rs.next()) {
686             id = rs.getInt("post_id");
687         }
688         
689         rs.close();
690         p.close();
691         
692         return id;
693     }
694     
695     /**
696      * @see net.jforum.dao.TopicDAO#setModerationStatus(int, boolean)
697      */

698     public void setModerationStatus(int forumId, boolean status) throws Exception JavaDoc
699     {
700         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.setModerationStatus"));
701         p.setInt(1, status ? 1 : 0);
702         p.setInt(2, forumId);
703         p.executeUpdate();
704         p.close();
705     }
706     
707     /**
708      * @see net.jforum.dao.TopicDAO#setModerationStatusByTopic(int, boolean)
709      */

710     public void setModerationStatusByTopic(int topicId, boolean status) throws Exception JavaDoc
711     {
712         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
713                 SystemGlobals.getSql("TopicModel.setModerationStatusByTopic"));
714         p.setInt(1, status ? 1 : 0);
715         p.setInt(2, topicId);
716         p.executeUpdate();
717         p.close();
718     }
719     
720     /**
721      * @see net.jforum.dao.TopicDAO#selectTopicTitlesByIds(java.util.Collection)
722      */

723     public List JavaDoc selectTopicTitlesByIds(Collection JavaDoc idList) throws Exception JavaDoc
724     {
725         List JavaDoc l = new ArrayList JavaDoc();
726         String JavaDoc sql = SystemGlobals.getSql("TopicModel.selectTopicTitlesByIds");
727         
728         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(idList.size() * 2);
729         for (Iterator JavaDoc iter = idList.iterator(); iter.hasNext(); ) {
730             sb.append(iter.next()).append(",");
731         }
732         
733         int len = sb.length();
734         sql = sql.replaceAll(":ids:", len > 0 ? sb.toString().substring(0, len - 1) : "0");
735         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(sql);
736         
737         ResultSet JavaDoc rs = p.executeQuery();
738         while (rs.next()) {
739             Map JavaDoc m = new HashMap JavaDoc();
740             m.put("id", new Integer JavaDoc(rs.getInt("topic_id")));
741             m.put("title", rs.getString("topic_title"));
742             
743             l.add(m);
744         }
745         
746         rs.close();
747         p.close();
748         
749         return l;
750     }
751     
752     /**
753      * @see net.jforum.model.UserModel#topicPosters(int)
754      */

755     public Map JavaDoc topicPosters(int topicId) throws Exception JavaDoc
756     {
757         Map JavaDoc m = new HashMap JavaDoc();
758         PreparedStatement JavaDoc p;
759         
760         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(SystemGlobals.getSql("TopicModel.topicPosters"));
761         
762         if (SystemGlobals.getBoolValue(ConfigKeys.DATABASE_SUPPORT_SUBQUERIES)) {
763             int index = sql.indexOf(":ids:");
764             sql.replace(index, index + 5, SystemGlobals.getSql("TopicModel.distinctPosters"));
765             
766             p = JForumExecutionContext.getConnection().prepareStatement(sql.toString());
767             p.setInt(1, topicId);
768         }
769         else {
770             p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.distinctPosters"));
771             p.setInt(1, topicId);
772             
773             ResultSet JavaDoc rs = p.executeQuery();
774             
775             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
776             
777             while (rs.next()) {
778                 sb.append(rs.getInt("user_id")).append(',');
779             }
780             
781             rs.close();
782             p.close();
783             
784             int index = sql.indexOf(":ids:");
785             sql.replace(index, index + 5, sb.substring(0, sb.length() - 1));
786             
787             p = JForumExecutionContext.getConnection().prepareStatement(sql.toString());
788         }
789         
790         ResultSet JavaDoc rs = p.executeQuery();
791         
792         while (rs.next()) {
793             User u = new User();
794             
795             u.setId(rs.getInt("user_id"));
796             u.setUsername(rs.getString("username"));
797             u.setKarma(new KarmaStatus(u.getId(), rs.getDouble("user_karma")));
798             u.setAvatar(rs.getString("user_avatar"));
799             u.setAvatarEnabled(rs.getInt("user_allowavatar") == 1);
800             u.setRegistrationDate(rs.getTimestamp("user_regdate"));
801             u.setTotalPosts(rs.getInt("user_posts"));
802             u.setFrom(rs.getString("user_from"));
803             u.setEmail(rs.getString("user_email"));
804             u.setRankId(rs.getInt("rank_id"));
805             u.setViewEmailEnabled(rs.getInt("user_viewemail") == 1);
806             u.setIcq(rs.getString("user_icq"));
807             u.setAttachSignatureEnabled(rs.getInt("user_attachsig") == 1);
808             u.setMsnm(rs.getString("user_msnm"));
809             u.setYim(rs.getString("user_yim"));
810             u.setWebSite(rs.getString("user_website"));
811             u.setAim(rs.getString("user_aim"));
812             u.setSignature(rs.getString("user_sig"));
813             
814             m.put(new Integer JavaDoc(u.getId()), u);
815         }
816         
817         rs.close();
818         p.close();
819         
820         return m;
821     }
822 }
823
Popular Tags