001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 30/03/2003 / 02:37:20
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.dao.generic;
044:
045: import java.sql.Connection;
046: import java.sql.PreparedStatement;
047: import java.sql.ResultSet;
048: import java.sql.SQLException;
049: import java.sql.Statement;
050: import java.sql.Timestamp;
051: import java.text.SimpleDateFormat;
052: import java.util.ArrayList;
053: import java.util.Date;
054: import java.util.List;
055:
056: import net.jforum.JForumExecutionContext;
057: import net.jforum.SessionFacade;
058: import net.jforum.dao.DataAccessDriver;
059: import net.jforum.dao.GroupSecurityDAO;
060: import net.jforum.dao.TopicDAO;
061: import net.jforum.entities.Forum;
062: import net.jforum.entities.ForumStats;
063: import net.jforum.entities.LastPostInfo;
064: import net.jforum.entities.ModeratorInfo;
065: import net.jforum.entities.Topic;
066: import net.jforum.entities.User;
067: import net.jforum.exceptions.DatabaseException;
068: import net.jforum.util.DbUtils;
069: import net.jforum.util.preferences.ConfigKeys;
070: import net.jforum.util.preferences.SystemGlobals;
071:
072: /**
073: * @author Rafael Steil
074: * @author Vanessa Sabino
075: * @author socialnetwork@gmail.com, adding "watch forum" methods.
076: *
077: * @version $Id: GenericForumDAO.java,v 1.33 2007/08/24 23:11:35 rafaelsteil Exp $
078: */
079: public class GenericForumDAO extends AutoKeys implements
080: net.jforum.dao.ForumDAO {
081: /**
082: * @see net.jforum.dao.ForumDAO#selectById(int)
083: */
084: public Forum selectById(int forumId) {
085: PreparedStatement p = null;
086: ResultSet rs = null;
087: try {
088: p = JForumExecutionContext.getConnection()
089: .prepareStatement(
090: SystemGlobals
091: .getSql("ForumModel.selectById"));
092: p.setInt(1, forumId);
093:
094: rs = p.executeQuery();
095:
096: Forum f = new Forum();
097:
098: if (rs.next()) {
099: f = this .fillForum(rs);
100: }
101: return f;
102: } catch (SQLException e) {
103: throw new DatabaseException(e);
104: } finally {
105: DbUtils.close(rs, p);
106: }
107: }
108:
109: protected Forum fillForum(ResultSet rs) throws SQLException {
110: Forum f = new Forum();
111:
112: f.setId(rs.getInt("forum_id"));
113: f.setIdCategories(rs.getInt("categories_id"));
114: f.setName(rs.getString("forum_name"));
115: f.setDescription(rs.getString("forum_desc"));
116: f.setOrder(rs.getInt("forum_order"));
117: f.setTotalTopics(rs.getInt("forum_topics"));
118: f.setLastPostId(rs.getInt("forum_last_post_id"));
119: f.setModerated(rs.getInt("moderated") > 0);
120: f.setTotalPosts(this .countForumPosts(f.getId()));
121:
122: return f;
123: }
124:
125: protected int countForumPosts(int forumId) {
126: PreparedStatement p = null;
127: ResultSet rs = null;
128: try {
129: p = JForumExecutionContext
130: .getConnection()
131: .prepareStatement(
132: SystemGlobals
133: .getSql("ForumModel.countForumPosts"));
134: p.setInt(1, forumId);
135: rs = p.executeQuery();
136:
137: if (rs.next()) {
138: return rs.getInt(1);
139: }
140:
141: return 0;
142: } catch (SQLException e) {
143: throw new DatabaseException(e);
144: } finally {
145: DbUtils.close(rs, p);
146: }
147: }
148:
149: /**
150: * @see net.jforum.dao.ForumDAO#selectAll()
151: */
152: public List selectAll() {
153: PreparedStatement p = null;
154: ResultSet rs = null;
155: try {
156: p = JForumExecutionContext.getConnection()
157: .prepareStatement(
158: SystemGlobals
159: .getSql("ForumModel.selectAll"));
160: List l = new ArrayList();
161:
162: rs = p.executeQuery();
163:
164: while (rs.next()) {
165: l.add(this .fillForum(rs));
166: }
167:
168: return l;
169: } catch (SQLException e) {
170: throw new DatabaseException(e);
171: } finally {
172: DbUtils.close(rs, p);
173: }
174: }
175:
176: /**
177: * @see net.jforum.dao.ForumDAO#setOrderUp(Forum, Forum)
178: */
179: public Forum setOrderUp(Forum forum, Forum related) {
180: return this .changeForumOrder(forum, related);
181: }
182:
183: /**
184: * @see net.jforum.dao.ForumDAO#setOrderDown(Forum, Forum)
185: */
186: public Forum setOrderDown(Forum forum, Forum related) {
187: return this .changeForumOrder(forum, related);
188: }
189:
190: private Forum changeForumOrder(Forum forum, Forum related) {
191: int tmpOrder = related.getOrder();
192: related.setOrder(forum.getOrder());
193: forum.setOrder(tmpOrder);
194:
195: PreparedStatement p = null;
196: try {
197: p = JForumExecutionContext.getConnection()
198: .prepareStatement(
199: SystemGlobals
200: .getSql("ForumModel.setOrderById"));
201: p.setInt(1, forum.getOrder());
202: p.setInt(2, forum.getId());
203: p.executeUpdate();
204: p.close();
205: p = null;
206:
207: p = JForumExecutionContext.getConnection()
208: .prepareStatement(
209: SystemGlobals
210: .getSql("ForumModel.setOrderById"));
211: p.setInt(1, related.getOrder());
212: p.setInt(2, related.getId());
213: p.executeUpdate();
214:
215: return this .selectById(forum.getId());
216: } catch (SQLException e) {
217: throw new DatabaseException(e);
218: } finally {
219: DbUtils.close(p);
220: }
221: }
222:
223: /**
224: * @see net.jforum.dao.ForumDAO#delete(int)
225: */
226: public void delete(int forumId) {
227: PreparedStatement p = null;
228: try {
229: p = JForumExecutionContext.getConnection()
230: .prepareStatement(
231: SystemGlobals.getSql("ForumModel.delete"));
232: p.setInt(1, forumId);
233:
234: p.executeUpdate();
235:
236: GroupSecurityDAO groupSecurity = DataAccessDriver
237: .getInstance().newGroupSecurityDAO();
238: groupSecurity.deleteForumRoles(forumId);
239: } catch (SQLException e) {
240: throw new DatabaseException(e);
241: } finally {
242: DbUtils.close(p);
243: }
244: }
245:
246: /**
247: * @see net.jforum.dao.ForumDAO#update(net.jforum.entities.Forum)
248: */
249: public void update(Forum forum) {
250: PreparedStatement p = null;
251: try {
252: p = JForumExecutionContext.getConnection()
253: .prepareStatement(
254: SystemGlobals.getSql("ForumModel.update"));
255:
256: p.setInt(1, forum.getCategoryId());
257: p.setString(2, forum.getName());
258: p.setString(3, forum.getDescription());
259: p.setInt(4, forum.isModerated() ? 1 : 0);
260: p.setInt(5, forum.getId());
261:
262: p.executeUpdate();
263: } catch (SQLException e) {
264: throw new DatabaseException(e);
265: } finally {
266: DbUtils.close(p);
267: }
268: }
269:
270: /**
271: * @see net.jforum.dao.ForumDAO#addNew(net.jforum.entities.Forum)
272: */
273: public int addNew(Forum forum) {
274: // Gets the higher order
275: PreparedStatement pOrder = null;
276: ResultSet rs = null;
277: try {
278: pOrder = JForumExecutionContext.getConnection()
279: .prepareStatement(
280: SystemGlobals
281: .getSql("ForumModel.getMaxOrder"));
282: rs = pOrder.executeQuery();
283:
284: if (rs.next()) {
285: forum.setOrder(rs.getInt(1) + 1);
286: }
287:
288: rs.close();
289: rs = null;
290: pOrder.close();
291: pOrder = null;
292:
293: pOrder = this .getStatementForAutoKeys("ForumModel.addNew");
294:
295: pOrder.setInt(1, forum.getCategoryId());
296: pOrder.setString(2, forum.getName());
297: pOrder.setString(3, forum.getDescription());
298: pOrder.setInt(4, forum.getOrder());
299: pOrder.setInt(5, forum.isModerated() ? 1 : 0);
300:
301: this .setAutoGeneratedKeysQuery(SystemGlobals
302: .getSql("ForumModel.lastGeneratedForumId"));
303: int forumId = this .executeAutoKeysQuery(pOrder);
304:
305: forum.setId(forumId);
306: return forumId;
307: } catch (SQLException e) {
308: throw new DatabaseException(e);
309: } finally {
310: DbUtils.close(rs, pOrder);
311: }
312: }
313:
314: /**
315: * @see net.jforum.dao.ForumDAO#setLastPost(int, int)
316: */
317: public void setLastPost(int forumId, int postId) {
318: PreparedStatement p = null;
319: try {
320: p = JForumExecutionContext
321: .getConnection()
322: .prepareStatement(
323: SystemGlobals
324: .getSql("ForumModel.updateLastPost"));
325:
326: p.setInt(1, postId);
327: p.setInt(2, forumId);
328:
329: p.executeUpdate();
330: } catch (SQLException e) {
331: throw new DatabaseException(e);
332: } finally {
333: DbUtils.close(p);
334: }
335: }
336:
337: /**
338: * @see net.jforum.dao.ForumDAO#setTotalTopics(int)
339: */
340: public void incrementTotalTopics(int forumId, int count) {
341: PreparedStatement p = null;
342: try {
343: p = JForumExecutionContext
344: .getConnection()
345: .prepareStatement(
346: SystemGlobals
347: .getSql("ForumModel.incrementTotalTopics"));
348: p.setInt(1, count);
349: p.setInt(2, forumId);
350: p.executeUpdate();
351: } catch (SQLException e) {
352: throw new DatabaseException(e);
353: } finally {
354: DbUtils.close(p);
355: }
356: }
357:
358: /**
359: * @see net.jforum.dao.ForumDAO#setTotalTopics(int)
360: */
361: public void decrementTotalTopics(int forumId, int count) {
362: PreparedStatement p = null;
363: try {
364: p = JForumExecutionContext
365: .getConnection()
366: .prepareStatement(
367: SystemGlobals
368: .getSql("ForumModel.decrementTotalTopics"));
369: p.setInt(1, count);
370: p.setInt(2, forumId);
371: p.executeUpdate();
372:
373: // If there are no more topics, then clean the
374: // last post id information
375: int totalTopics = this .getTotalTopics(forumId);
376: if (totalTopics < 1) {
377: this .setLastPost(forumId, 0);
378: }
379: } catch (SQLException e) {
380: throw new DatabaseException(e);
381: } finally {
382: DbUtils.close(p);
383: }
384: }
385:
386: private LastPostInfo getLastPostInfo(int forumId, boolean tryFix) {
387: LastPostInfo lpi = new LastPostInfo();
388:
389: PreparedStatement p = null;
390: ResultSet rs = null;
391: try {
392: p = JForumExecutionContext.getConnection()
393: .prepareStatement(
394: SystemGlobals
395: .getSql("ForumModel.lastPostInfo"));
396: p.setInt(1, forumId);
397:
398: rs = p.executeQuery();
399:
400: if (rs.next()) {
401: lpi.setUsername(rs.getString("username"));
402: lpi.setUserId(rs.getInt("user_id"));
403:
404: SimpleDateFormat df = new SimpleDateFormat(
405: SystemGlobals
406: .getValue(ConfigKeys.DATE_TIME_FORMAT));
407: lpi
408: .setPostDate(df.format(rs
409: .getTimestamp("post_time")));
410: lpi.setPostId(rs.getInt("post_id"));
411: lpi.setTopicId(rs.getInt("topic_id"));
412: lpi.setPostTimeMillis(rs.getTimestamp("post_time")
413: .getTime());
414: lpi.setTopicReplies(rs.getInt("topic_replies"));
415:
416: lpi.setHasInfo(true);
417:
418: // Check if the topic is consistent
419: TopicDAO tm = DataAccessDriver.getInstance()
420: .newTopicDAO();
421: Topic t = tm.selectById(lpi.getTopicId());
422:
423: if (t.getId() == 0) {
424: // Hm, that's not good. Try to fix it
425: tm.fixFirstLastPostId(lpi.getTopicId());
426: }
427:
428: tryFix = false;
429: } else if (tryFix) {
430: rs.close();
431: rs = null;
432: p.close();
433: p = null;
434:
435: int postId = this .getMaxPostId(forumId);
436:
437: p = JForumExecutionContext
438: .getConnection()
439: .prepareStatement(
440: SystemGlobals
441: .getSql("ForumModel.latestTopicIdForfix"));
442: p.setInt(1, forumId);
443: rs = p.executeQuery();
444:
445: if (rs.next()) {
446: int topicId;
447: topicId = rs.getInt("topic_id");
448:
449: rs.close();
450: rs = null;
451: p.close();
452: p = null;
453:
454: // Topic
455: p = JForumExecutionContext
456: .getConnection()
457: .prepareStatement(
458: SystemGlobals
459: .getSql("ForumModel.fixLatestPostData"));
460: p.setInt(1, postId);
461: p.setInt(2, topicId);
462: p.executeUpdate();
463: p.close();
464: p = null;
465:
466: // Forum
467: p = JForumExecutionContext
468: .getConnection()
469: .prepareStatement(
470: SystemGlobals
471: .getSql("ForumModel.fixForumLatestPostData"));
472: p.setInt(1, postId);
473: p.setInt(2, forumId);
474: p.executeUpdate();
475: }
476: }
477:
478: return (tryFix ? this .getLastPostInfo(forumId, false) : lpi);
479: } catch (SQLException e) {
480: throw new DatabaseException(e);
481: } finally {
482: DbUtils.close(rs, p);
483: }
484: }
485:
486: /**
487: * @see net.jforum.dao.ForumDAO#getLastPostInfo(int)
488: */
489: public LastPostInfo getLastPostInfo(int forumId) {
490: return this .getLastPostInfo(forumId, true);
491: }
492:
493: /**
494: * @see net.jforum.dao.ForumDAO#getModeratorList(int)
495: */
496: public List getModeratorList(int forumId) {
497: List l = new ArrayList();
498:
499: PreparedStatement p = null;
500: ResultSet rs = null;
501: try {
502: p = JForumExecutionContext
503: .getConnection()
504: .prepareStatement(
505: SystemGlobals
506: .getSql("ForumModel.getModeratorList"));
507: p.setInt(1, forumId);
508:
509: rs = p.executeQuery();
510:
511: while (rs.next()) {
512: ModeratorInfo mi = new ModeratorInfo();
513:
514: mi.setId(rs.getInt("id"));
515: mi.setName(rs.getString("name"));
516:
517: l.add(mi);
518: }
519:
520: return l;
521: } catch (SQLException e) {
522: throw new DatabaseException(e);
523: } finally {
524: DbUtils.close(rs, p);
525: }
526: }
527:
528: /**
529: * @see net.jforum.dao.ForumDAO#getTotalMessages()
530: */
531: public int getTotalMessages() {
532: PreparedStatement p = null;
533: ResultSet rs = null;
534: try {
535: p = JForumExecutionContext
536: .getConnection()
537: .prepareStatement(
538: SystemGlobals
539: .getSql("ForumModel.totalMessages"));
540: rs = p.executeQuery();
541:
542: if (rs.next()) {
543: return rs.getInt("total_messages");
544: }
545:
546: return 0;
547: } catch (SQLException e) {
548: throw new DatabaseException(e);
549: } finally {
550: DbUtils.close(rs, p);
551: }
552: }
553:
554: /**
555: * @see net.jforum.dao.ForumDAO#getTotalTopics(int)
556: */
557: public int getTotalTopics(int forumId) {
558: int total = 0;
559: PreparedStatement p = null;
560: ResultSet rs = null;
561: try {
562: p = JForumExecutionContext
563: .getConnection()
564: .prepareStatement(
565: SystemGlobals
566: .getSql("ForumModel.getTotalTopics"));
567: p.setInt(1, forumId);
568: rs = p.executeQuery();
569:
570: if (rs.next()) {
571: total = rs.getInt(1);
572: }
573:
574: return total;
575: } catch (SQLException e) {
576: throw new DatabaseException(e);
577: } finally {
578: DbUtils.close(rs, p);
579: }
580: }
581:
582: /**
583: * @see net.jforum.dao.ForumDAO#getMaxPostId(int)
584: */
585: public int getMaxPostId(int forumId) {
586: int id = -1;
587:
588: PreparedStatement p = null;
589: ResultSet rs = null;
590: try {
591: p = JForumExecutionContext.getConnection()
592: .prepareStatement(
593: SystemGlobals
594: .getSql("ForumModel.getMaxPostId"));
595: p.setInt(1, forumId);
596:
597: rs = p.executeQuery();
598: if (rs.next()) {
599: id = rs.getInt("post_id");
600: }
601:
602: return id;
603: } catch (SQLException e) {
604: throw new DatabaseException(e);
605: } finally {
606: DbUtils.close(rs, p);
607: }
608: }
609:
610: /**
611: * @see net.jforum.dao.ForumDAO#moveTopics(java.lang.String[], int, int)
612: */
613: public void moveTopics(String[] topics, int fromForumId,
614: int toForumId) {
615: PreparedStatement p = null;
616: PreparedStatement t = null;
617: try {
618: p = JForumExecutionContext.getConnection()
619: .prepareStatement(
620: SystemGlobals
621: .getSql("ForumModel.moveTopics"));
622: t = JForumExecutionContext
623: .getConnection()
624: .prepareStatement(
625: SystemGlobals
626: .getSql("PostModel.setForumByTopic"));
627:
628: p.setInt(1, toForumId);
629: p.setInt(2, fromForumId);
630:
631: t.setInt(1, toForumId);
632:
633: TopicDAO tdao = DataAccessDriver.getInstance()
634: .newTopicDAO();
635:
636: Forum f = this .selectById(toForumId);
637:
638: for (int i = 0; i < topics.length; i++) {
639: int topicId = Integer.parseInt(topics[i]);
640: p.setInt(3, topicId);
641: t.setInt(2, topicId);
642:
643: p.executeUpdate();
644: t.executeUpdate();
645:
646: tdao.setModerationStatusByTopic(topicId, f
647: .isModerated());
648: }
649:
650: this .decrementTotalTopics(fromForumId, topics.length);
651: this .incrementTotalTopics(toForumId, topics.length);
652:
653: this .setLastPost(fromForumId, this
654: .getMaxPostId(fromForumId));
655: this .setLastPost(toForumId, this .getMaxPostId(toForumId));
656: } catch (SQLException e) {
657: throw new DatabaseException(e);
658: } finally {
659: DbUtils.close(p);
660: DbUtils.close(t);
661: }
662: }
663:
664: /**
665: * @see net.jforum.dao.ForumDAO#hasUnreadTopics(int, long)
666: */
667: public List checkUnreadTopics(int forumId, long lastVisit) {
668: List l = new ArrayList();
669:
670: PreparedStatement p = null;
671: ResultSet rs = null;
672: try {
673: p = JForumExecutionContext
674: .getConnection()
675: .prepareStatement(
676: SystemGlobals
677: .getSql("ForumModel.checkUnreadTopics"));
678: p.setInt(1, forumId);
679: p.setTimestamp(2, new Timestamp(lastVisit));
680:
681: rs = p.executeQuery();
682: while (rs.next()) {
683: Topic t = new Topic();
684: t.setId(rs.getInt("topic_id"));
685: t.setTime(new Date(rs.getTimestamp(1).getTime()));
686:
687: l.add(t);
688: }
689:
690: return l;
691: } catch (SQLException e) {
692: throw new DatabaseException(e);
693: } finally {
694: DbUtils.close(rs, p);
695: }
696: }
697:
698: /**
699: * @see net.jforum.dao.ForumDAO#setModerated(int, boolean)
700: */
701: public void setModerated(int categoryId, boolean status) {
702: PreparedStatement p = null;
703: try {
704: p = JForumExecutionContext.getConnection()
705: .prepareStatement(
706: SystemGlobals
707: .getSql("ForumModel.setModerated"));
708: p.setInt(1, status ? 1 : 0);
709: p.setInt(2, categoryId);
710: p.executeUpdate();
711: } catch (SQLException e) {
712: throw new DatabaseException(e);
713: } finally {
714: DbUtils.close(p);
715: }
716: }
717:
718: /**
719: * @see net.jforum.dao.ForumDAO#getBoardStatus()
720: */
721: public ForumStats getBoardStatus() {
722: ForumStats fs = new ForumStats();
723: fs.setPosts(this .getTotalMessages());
724:
725: Connection c = JForumExecutionContext.getConnection();
726:
727: // Total Users
728: Statement s = null;
729: ResultSet rs = null;
730:
731: try {
732: s = c.createStatement();
733: rs = s.executeQuery(SystemGlobals
734: .getSql("UserModel.totalUsers"));
735: rs.next();
736: fs.setUsers(rs.getInt(1));
737: rs.close();
738: rs = null;
739: s.close();
740: s = null;
741:
742: // Total Topics
743: s = c.createStatement();
744: rs = s.executeQuery(SystemGlobals
745: .getSql("TopicModel.totalTopics"));
746: rs.next();
747: fs.setTopics(rs.getInt(1));
748: rs.close();
749: rs = null;
750: s.close();
751: s = null;
752:
753: // Posts per day
754: double postPerDay = 0;
755:
756: // Topics per day
757: double topicPerDay = 0;
758:
759: // user per day
760: double userPerDay = 0;
761:
762: s = c.createStatement();
763: rs = s.executeQuery(SystemGlobals
764: .getSql("ForumModel.statsFirstPostTime"));
765: if (rs.next()) {
766:
767: Timestamp firstTime = rs.getTimestamp(1);
768: if (rs.wasNull()) {
769: firstTime = null;
770: }
771: rs.close();
772: rs = null;
773: s.close();
774: s = null;
775:
776: Date today = new Date();
777:
778: postPerDay = firstTime != null ? fs.getPosts()
779: / this .daysUntilToday(today, firstTime) : 0;
780:
781: if (fs.getPosts() > 0 && postPerDay < 1) {
782: postPerDay = 1;
783: }
784:
785: topicPerDay = firstTime != null ? fs.getTopics()
786: / this .daysUntilToday(today, firstTime) : 0;
787:
788: // Users per day
789: s = c.createStatement();
790: rs = s
791: .executeQuery(SystemGlobals
792: .getSql("ForumModel.statsFirstRegisteredUserTime"));
793: if (rs.next()) {
794: firstTime = rs.getTimestamp(1);
795: if (rs.wasNull()) {
796: firstTime = null;
797: }
798: }
799: rs.close();
800: rs = null;
801: s.close();
802: s = null;
803:
804: userPerDay = firstTime != null ? fs.getUsers()
805: / this .daysUntilToday(today, firstTime) : 0;
806: }
807:
808: fs.setPostsPerDay(postPerDay);
809: fs.setTopicsPerDay(topicPerDay);
810: fs.setUsersPerDay(userPerDay);
811:
812: return fs;
813: } catch (SQLException e) {
814: throw new DatabaseException(e);
815: } finally {
816: DbUtils.close(rs, s);
817: }
818: }
819:
820: private int daysUntilToday(Date today, Date from) {
821: int days = (int) ((today.getTime() - from.getTime()) / (24 * 60 * 60 * 1000));
822: return days == 0 ? 1 : days;
823: }
824:
825: /**
826: * This code is writen by looking at GenericTopicDAO.java
827: *
828: * @see
829: */
830: public List notifyUsers(Forum forum) {
831: int posterId = SessionFacade.getUserSession().getUserId();
832: int anonUser = SystemGlobals
833: .getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
834:
835: PreparedStatement p = null;
836: ResultSet rs = null;
837:
838: try {
839: p = JForumExecutionContext.getConnection()
840: .prepareStatement(
841: SystemGlobals
842: .getSql("ForumModel.notifyUsers"));
843:
844: p.setInt(1, forum.getId());
845: p.setInt(2, posterId); // don't notify the poster
846: p.setInt(3, anonUser); // don't notify the anonimous user
847:
848: rs = p.executeQuery();
849: List users = new ArrayList();
850:
851: while (rs.next()) {
852: User user = new User();
853:
854: user.setId(rs.getInt("user_id"));
855: user.setEmail(rs.getString("user_email"));
856: user.setUsername(rs.getString("username"));
857: user.setLang(rs.getString("user_lang"));
858: user
859: .setNotifyAlways(rs
860: .getInt("user_notify_always") == 1);
861: user.setNotifyText(rs.getInt("user_notify_text") == 1);
862:
863: users.add(user);
864: }
865:
866: return users;
867: } catch (SQLException e) {
868: throw new DatabaseException(e);
869: } finally {
870: DbUtils.close(rs, p);
871: }
872:
873: }
874:
875: public void subscribeUser(int forumId, int userId) {
876: PreparedStatement p = null;
877: try {
878: p = JForumExecutionContext
879: .getConnection()
880: .prepareStatement(
881: SystemGlobals
882: .getSql("ForumModel.subscribeUser"));
883:
884: p.setInt(1, forumId);
885: p.setInt(2, userId);
886:
887: p.executeUpdate();
888: } catch (SQLException e) {
889: throw new DatabaseException(e);
890: } finally {
891: DbUtils.close(p);
892: }
893:
894: }
895:
896: public boolean isUserSubscribed(int forumId, int userId) {
897: PreparedStatement stmt = null;
898: ResultSet rs = null;
899: try {
900: stmt = JForumExecutionContext
901: .getConnection()
902: .prepareStatement(
903: SystemGlobals
904: .getSql("ForumModel.isUserSubscribed"));
905:
906: stmt.setInt(1, forumId);
907: stmt.setInt(2, userId);
908:
909: rs = stmt.executeQuery();
910:
911: return rs.next();
912: } catch (SQLException e) {
913: throw new DatabaseException(e);
914: } finally {
915: DbUtils.close(rs, stmt);
916: }
917: }
918:
919: public void removeSubscription(int forumId, int userId) {
920: PreparedStatement p = null;
921: try {
922: p = JForumExecutionContext
923: .getConnection()
924: .prepareStatement(
925: SystemGlobals
926: .getSql("ForumModel.removeSubscription"));
927: p.setInt(1, forumId);
928: p.setInt(2, userId);
929:
930: p.executeUpdate();
931: } catch (SQLException e) {
932: throw new DatabaseException(e);
933: } finally {
934: DbUtils.close(p);
935: }
936:
937: }
938:
939: /**
940: * Remove all subscriptions on a forum, such as when a forum is locked. It is not used now.
941: *
942: * @param forumId int
943: */
944: public void removeSubscriptionByForum(int forumId) {
945: PreparedStatement p = null;
946: try {
947: p = JForumExecutionContext
948: .getConnection()
949: .prepareStatement(
950: SystemGlobals
951: .getSql("ForumModel.removeSubscriptionByForum"));
952: p.setInt(1, forumId);
953:
954: p.executeUpdate();
955: } catch (SQLException e) {
956: throw new DatabaseException(e);
957: } finally {
958: DbUtils.close(p);
959: }
960:
961: }
962:
963: /**
964: * @see net.jforum.dao.ForumDAO#discoverForumId(java.lang.String)
965: */
966: public int discoverForumId(String listEmail) {
967: int forumId = 0;
968:
969: PreparedStatement p = null;
970: ResultSet rs = null;
971:
972: try {
973: p = JForumExecutionContext
974: .getConnection()
975: .prepareStatement(
976: SystemGlobals
977: .getSql("ForumModel.discoverForumId"));
978: p.setString(1, listEmail);
979: rs = p.executeQuery();
980:
981: if (rs.next()) {
982: forumId = rs.getInt(1);
983: }
984: } catch (SQLException e) {
985:
986: } finally {
987: DbUtils.close(rs, p);
988: }
989:
990: return forumId;
991: }
992: }
|