GenericForumDAO.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 22k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  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: 30/03/2003 / 02:37:20
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum.dao.generic;
  44. import java.sql.Connection;
  45. import java.sql.PreparedStatement;
  46. import java.sql.ResultSet;
  47. import java.sql.SQLException;
  48. import java.sql.Statement;
  49. import java.sql.Timestamp;
  50. import java.text.SimpleDateFormat;
  51. import java.util.ArrayList;
  52. import java.util.Date;
  53. import java.util.List;
  54. import net.jforum.JForumExecutionContext;
  55. import net.jforum.SessionFacade;
  56. import net.jforum.dao.DataAccessDriver;
  57. import net.jforum.dao.GroupSecurityDAO;
  58. import net.jforum.dao.TopicDAO;
  59. import net.jforum.entities.Forum;
  60. import net.jforum.entities.ForumStats;
  61. import net.jforum.entities.LastPostInfo;
  62. import net.jforum.entities.ModeratorInfo;
  63. import net.jforum.entities.Topic;
  64. import net.jforum.entities.User;
  65. import net.jforum.exceptions.DatabaseException;
  66. import net.jforum.util.DbUtils;
  67. import net.jforum.util.preferences.ConfigKeys;
  68. import net.jforum.util.preferences.SystemGlobals;
  69. /**
  70.  * @author Rafael Steil
  71.  * @author Vanessa Sabino
  72.  * @author socialnetwork@gmail.com, adding "watch forum" methods.
  73.  * 
  74.  * @version $Id: GenericForumDAO.java,v 1.33 2007/08/24 23:11:35 rafaelsteil Exp $
  75.  */
  76. public class GenericForumDAO extends AutoKeys implements net.jforum.dao.ForumDAO
  77. {
  78. /**
  79.  * @see net.jforum.dao.ForumDAO#selectById(int)
  80.  */
  81. public Forum selectById(int forumId)
  82. {
  83. PreparedStatement p = null;
  84. ResultSet rs = null;
  85. try {
  86. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.selectById"));
  87. p.setInt(1, forumId);
  88. rs = p.executeQuery();
  89. Forum f = new Forum();
  90. if (rs.next()) {
  91. f = this.fillForum(rs);
  92. }
  93. return f;
  94. }
  95. catch (SQLException e) {
  96. throw new DatabaseException(e);
  97. }
  98. finally {
  99. DbUtils.close(rs, p);
  100. }
  101. }
  102. protected Forum fillForum(ResultSet rs) throws SQLException
  103. {
  104. Forum f = new Forum();
  105. f.setId(rs.getInt("forum_id"));
  106. f.setIdCategories(rs.getInt("categories_id"));
  107. f.setName(rs.getString("forum_name"));
  108. f.setDescription(rs.getString("forum_desc"));
  109. f.setOrder(rs.getInt("forum_order"));
  110. f.setTotalTopics(rs.getInt("forum_topics"));
  111. f.setLastPostId(rs.getInt("forum_last_post_id"));
  112. f.setModerated(rs.getInt("moderated") > 0);
  113. f.setTotalPosts(this.countForumPosts(f.getId()));
  114. return f;
  115. }
  116. protected int countForumPosts(int forumId)
  117. {
  118. PreparedStatement p = null;
  119. ResultSet rs = null;
  120. try {
  121. p = JForumExecutionContext.getConnection().prepareStatement(
  122. SystemGlobals.getSql("ForumModel.countForumPosts"));
  123. p.setInt(1, forumId);
  124. rs = p.executeQuery();
  125. if (rs.next()) {
  126. return rs.getInt(1);
  127. }
  128. return 0;
  129. }
  130. catch (SQLException e) {
  131. throw new DatabaseException(e);
  132. }
  133. finally {
  134. DbUtils.close(rs, p);
  135. }
  136. }
  137. /**
  138.  * @see net.jforum.dao.ForumDAO#selectAll()
  139.  */
  140. public List selectAll()
  141. {
  142. PreparedStatement p = null;
  143. ResultSet rs = null;
  144. try {
  145. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.selectAll"));
  146. List l = new ArrayList();
  147. rs = p.executeQuery();
  148. while (rs.next()) {
  149. l.add(this.fillForum(rs));
  150. }
  151. return l;
  152. }
  153. catch (SQLException e) {
  154. throw new DatabaseException(e);
  155. }
  156. finally {
  157. DbUtils.close(rs, p);
  158. }
  159. }
  160. /**
  161.  * @see net.jforum.dao.ForumDAO#setOrderUp(Forum, Forum)
  162.  */
  163. public Forum setOrderUp(Forum forum, Forum related)
  164. {
  165. return this.changeForumOrder(forum, related);
  166. }
  167. /**
  168.  * @see net.jforum.dao.ForumDAO#setOrderDown(Forum, Forum)
  169.  */
  170. public Forum setOrderDown(Forum forum, Forum related)
  171. {
  172. return this.changeForumOrder(forum, related);
  173. }
  174. private Forum changeForumOrder(Forum forum, Forum related)
  175. {
  176. int tmpOrder = related.getOrder();
  177. related.setOrder(forum.getOrder());
  178. forum.setOrder(tmpOrder);
  179. PreparedStatement p = null;
  180. try {
  181. p = JForumExecutionContext.getConnection()
  182. .prepareStatement(SystemGlobals.getSql("ForumModel.setOrderById"));
  183. p.setInt(1, forum.getOrder());
  184. p.setInt(2, forum.getId());
  185. p.executeUpdate();
  186. p.close();
  187. p = null;
  188. p = JForumExecutionContext.getConnection()
  189. .prepareStatement(SystemGlobals.getSql("ForumModel.setOrderById"));
  190. p.setInt(1, related.getOrder());
  191. p.setInt(2, related.getId());
  192. p.executeUpdate();
  193. return this.selectById(forum.getId());
  194. }
  195. catch (SQLException e) {
  196. throw new DatabaseException(e);
  197. }
  198. finally {
  199. DbUtils.close(p);
  200. }
  201. }
  202. /**
  203.  * @see net.jforum.dao.ForumDAO#delete(int)
  204.  */
  205. public void delete(int forumId)
  206. {
  207. PreparedStatement p = null;
  208. try {
  209. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.delete"));
  210. p.setInt(1, forumId);
  211. p.executeUpdate();
  212. GroupSecurityDAO groupSecurity = DataAccessDriver.getInstance().newGroupSecurityDAO();
  213. groupSecurity.deleteForumRoles(forumId);
  214. }
  215. catch (SQLException e) {
  216. throw new DatabaseException(e);
  217. }
  218. finally {
  219. DbUtils.close(p);
  220. }
  221. }
  222. /**
  223.  * @see net.jforum.dao.ForumDAO#update(net.jforum.entities.Forum)
  224.  */
  225. public void update(Forum forum)
  226. {
  227. PreparedStatement p = null;
  228. try {
  229. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.update"));
  230. p.setInt(1, forum.getCategoryId());
  231. p.setString(2, forum.getName());
  232. p.setString(3, forum.getDescription());
  233. p.setInt(4, forum.isModerated() ? 1 : 0);
  234. p.setInt(5, forum.getId());
  235. p.executeUpdate();
  236. }
  237. catch (SQLException e) {
  238. throw new DatabaseException(e);
  239. }
  240. finally {
  241. DbUtils.close(p);
  242. }
  243. }
  244. /**
  245.  * @see net.jforum.dao.ForumDAO#addNew(net.jforum.entities.Forum)
  246.  */
  247. public int addNew(Forum forum)
  248. {
  249. // Gets the higher order
  250. PreparedStatement pOrder = null;
  251. ResultSet rs = null;
  252. try {
  253. pOrder = JForumExecutionContext.getConnection().prepareStatement(
  254. SystemGlobals.getSql("ForumModel.getMaxOrder"));
  255. rs = pOrder.executeQuery();
  256. if (rs.next()) {
  257. forum.setOrder(rs.getInt(1) + 1);
  258. }
  259. rs.close();
  260. rs = null;
  261. pOrder.close();
  262. pOrder = null;
  263. pOrder = this.getStatementForAutoKeys("ForumModel.addNew");
  264. pOrder.setInt(1, forum.getCategoryId());
  265. pOrder.setString(2, forum.getName());
  266. pOrder.setString(3, forum.getDescription());
  267. pOrder.setInt(4, forum.getOrder());
  268. pOrder.setInt(5, forum.isModerated() ? 1 : 0);
  269. this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("ForumModel.lastGeneratedForumId"));
  270. int forumId = this.executeAutoKeysQuery(pOrder);
  271. forum.setId(forumId);
  272. return forumId;
  273. }
  274. catch (SQLException e) {
  275. throw new DatabaseException(e);
  276. }
  277. finally {
  278. DbUtils.close(rs, pOrder);
  279. }
  280. }
  281. /**
  282.  * @see net.jforum.dao.ForumDAO#setLastPost(int, int)
  283.  */
  284. public void setLastPost(int forumId, int postId)
  285. {
  286. PreparedStatement p = null;
  287. try {
  288. p = JForumExecutionContext.getConnection().prepareStatement(
  289. SystemGlobals.getSql("ForumModel.updateLastPost"));
  290. p.setInt(1, postId);
  291. p.setInt(2, forumId);
  292. p.executeUpdate();
  293. }
  294. catch (SQLException e) {
  295. throw new DatabaseException(e);
  296. }
  297. finally {
  298. DbUtils.close(p);
  299. }
  300. }
  301. /**
  302.  * @see net.jforum.dao.ForumDAO#setTotalTopics(int)
  303.  */
  304. public void incrementTotalTopics(int forumId, int count)
  305. {
  306. PreparedStatement p = null;
  307. try {
  308. p = JForumExecutionContext.getConnection().prepareStatement(
  309. SystemGlobals.getSql("ForumModel.incrementTotalTopics"));
  310. p.setInt(1, count);
  311. p.setInt(2, forumId);
  312. p.executeUpdate();
  313. }
  314. catch (SQLException e) {
  315. throw new DatabaseException(e);
  316. }
  317. finally {
  318. DbUtils.close(p);
  319. }
  320. }
  321. /**
  322.  * @see net.jforum.dao.ForumDAO#setTotalTopics(int)
  323.  */
  324. public void decrementTotalTopics(int forumId, int count)
  325. {
  326. PreparedStatement p = null;
  327. try {
  328. p = JForumExecutionContext.getConnection().prepareStatement(
  329. SystemGlobals.getSql("ForumModel.decrementTotalTopics"));
  330. p.setInt(1, count);
  331. p.setInt(2, forumId);
  332. p.executeUpdate();
  333. // If there are no more topics, then clean the
  334. // last post id information
  335. int totalTopics = this.getTotalTopics(forumId);
  336. if (totalTopics < 1) {
  337. this.setLastPost(forumId, 0);
  338. }
  339. }
  340. catch (SQLException e) {
  341. throw new DatabaseException(e);
  342. }
  343. finally {
  344. DbUtils.close(p);
  345. }
  346. }
  347. private LastPostInfo getLastPostInfo(int forumId, boolean tryFix)
  348. {
  349. LastPostInfo lpi = new LastPostInfo();
  350. PreparedStatement p = null;
  351. ResultSet rs = null;
  352. try {
  353. p = JForumExecutionContext.getConnection()
  354. .prepareStatement(SystemGlobals.getSql("ForumModel.lastPostInfo"));
  355. p.setInt(1, forumId);
  356. rs = p.executeQuery();
  357. if (rs.next()) {
  358. lpi.setUsername(rs.getString("username"));
  359. lpi.setUserId(rs.getInt("user_id"));
  360. SimpleDateFormat df = new SimpleDateFormat(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
  361. lpi.setPostDate(df.format(rs.getTimestamp("post_time")));
  362. lpi.setPostId(rs.getInt("post_id"));
  363. lpi.setTopicId(rs.getInt("topic_id"));
  364. lpi.setPostTimeMillis(rs.getTimestamp("post_time").getTime());
  365. lpi.setTopicReplies(rs.getInt("topic_replies"));
  366. lpi.setHasInfo(true);
  367. // Check if the topic is consistent
  368. TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
  369. Topic t = tm.selectById(lpi.getTopicId());
  370. if (t.getId() == 0) {
  371. // Hm, that's not good. Try to fix it
  372. tm.fixFirstLastPostId(lpi.getTopicId());
  373. }
  374. tryFix = false;
  375. }
  376. else if (tryFix) {
  377. rs.close();
  378. rs = null;
  379. p.close();
  380. p = null;
  381. int postId = this.getMaxPostId(forumId);
  382. p = JForumExecutionContext.getConnection().prepareStatement(
  383. SystemGlobals.getSql("ForumModel.latestTopicIdForfix"));
  384. p.setInt(1, forumId);
  385. rs = p.executeQuery();
  386. if (rs.next()) {
  387. int topicId;
  388. topicId = rs.getInt("topic_id");
  389. rs.close();
  390. rs = null;
  391. p.close();
  392. p = null;
  393. // Topic
  394. p = JForumExecutionContext.getConnection().prepareStatement(
  395. SystemGlobals.getSql("ForumModel.fixLatestPostData"));
  396. p.setInt(1, postId);
  397. p.setInt(2, topicId);
  398. p.executeUpdate();
  399. p.close();
  400. p = null;
  401. // Forum
  402. p = JForumExecutionContext.getConnection().prepareStatement(
  403. SystemGlobals.getSql("ForumModel.fixForumLatestPostData"));
  404. p.setInt(1, postId);
  405. p.setInt(2, forumId);
  406. p.executeUpdate();
  407. }
  408. }
  409. return (tryFix ? this.getLastPostInfo(forumId, false) : lpi);
  410. }
  411. catch (SQLException e) {
  412. throw new DatabaseException(e);
  413. }
  414. finally {
  415. DbUtils.close(rs, p);
  416. }
  417. }
  418. /**
  419.  * @see net.jforum.dao.ForumDAO#getLastPostInfo(int)
  420.  */
  421. public LastPostInfo getLastPostInfo(int forumId)
  422. {
  423. return this.getLastPostInfo(forumId, true);
  424. }
  425. /**
  426.  * @see net.jforum.dao.ForumDAO#getModeratorList(int)
  427.  */
  428. public List getModeratorList(int forumId)
  429. {
  430. List l = new ArrayList();
  431. PreparedStatement p = null;
  432. ResultSet rs = null;
  433. try {
  434. p = JForumExecutionContext.getConnection().prepareStatement(
  435. SystemGlobals.getSql("ForumModel.getModeratorList"));
  436. p.setInt(1, forumId);
  437. rs = p.executeQuery();
  438. while (rs.next()) {
  439. ModeratorInfo mi = new ModeratorInfo();
  440. mi.setId(rs.getInt("id"));
  441. mi.setName(rs.getString("name"));
  442. l.add(mi);
  443. }
  444. return l;
  445. }
  446. catch (SQLException e) {
  447. throw new DatabaseException(e);
  448. }
  449. finally {
  450. DbUtils.close(rs, p);
  451. }
  452. }
  453. /**
  454.  * @see net.jforum.dao.ForumDAO#getTotalMessages()
  455.  */
  456. public int getTotalMessages()
  457. {
  458. PreparedStatement p = null;
  459. ResultSet rs = null;
  460. try {
  461. p = JForumExecutionContext.getConnection().prepareStatement(
  462. SystemGlobals.getSql("ForumModel.totalMessages"));
  463. rs = p.executeQuery();
  464. if (rs.next()) {
  465. return rs.getInt("total_messages");
  466. }
  467. return 0;
  468. }
  469. catch (SQLException e) {
  470. throw new DatabaseException(e);
  471. }
  472. finally {
  473. DbUtils.close(rs, p);
  474. }
  475. }
  476. /**
  477.  * @see net.jforum.dao.ForumDAO#getTotalTopics(int)
  478.  */
  479. public int getTotalTopics(int forumId)
  480. {
  481. int total = 0;
  482. PreparedStatement p = null;
  483. ResultSet rs = null;
  484. try {
  485. p = JForumExecutionContext.getConnection().prepareStatement(
  486. SystemGlobals.getSql("ForumModel.getTotalTopics"));
  487. p.setInt(1, forumId);
  488. rs = p.executeQuery();
  489. if (rs.next()) {
  490. total = rs.getInt(1);
  491. }
  492. return total;
  493. }
  494. catch (SQLException e) {
  495. throw new DatabaseException(e);
  496. }
  497. finally {
  498. DbUtils.close(rs, p);
  499. }
  500. }
  501. /**
  502.  * @see net.jforum.dao.ForumDAO#getMaxPostId(int)
  503.  */
  504. public int getMaxPostId(int forumId)
  505. {
  506. int id = -1;
  507. PreparedStatement p = null;
  508. ResultSet rs = null;
  509. try {
  510. p = JForumExecutionContext.getConnection()
  511. .prepareStatement(SystemGlobals.getSql("ForumModel.getMaxPostId"));
  512. p.setInt(1, forumId);
  513. rs = p.executeQuery();
  514. if (rs.next()) {
  515. id = rs.getInt("post_id");
  516. }
  517. return id;
  518. }
  519. catch (SQLException e) {
  520. throw new DatabaseException(e);
  521. }
  522. finally {
  523. DbUtils.close(rs, p);
  524. }
  525. }
  526. /**
  527.  * @see net.jforum.dao.ForumDAO#moveTopics(java.lang.String[], int, int)
  528.  */
  529. public void moveTopics(String[] topics, int fromForumId, int toForumId)
  530. {
  531. PreparedStatement p = null;
  532. PreparedStatement t = null;
  533. try {
  534. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.moveTopics"));
  535. t = JForumExecutionContext.getConnection().prepareStatement(
  536. SystemGlobals.getSql("PostModel.setForumByTopic"));
  537. p.setInt(1, toForumId);
  538. p.setInt(2, fromForumId);
  539. t.setInt(1, toForumId);
  540. TopicDAO tdao = DataAccessDriver.getInstance().newTopicDAO();
  541. Forum f = this.selectById(toForumId);
  542. for (int i = 0; i < topics.length; i++) {
  543. int topicId = Integer.parseInt(topics[i]);
  544. p.setInt(3, topicId);
  545. t.setInt(2, topicId);
  546. p.executeUpdate();
  547. t.executeUpdate();
  548. tdao.setModerationStatusByTopic(topicId, f.isModerated());
  549. }
  550. this.decrementTotalTopics(fromForumId, topics.length);
  551. this.incrementTotalTopics(toForumId, topics.length);
  552. this.setLastPost(fromForumId, this.getMaxPostId(fromForumId));
  553. this.setLastPost(toForumId, this.getMaxPostId(toForumId));
  554. }
  555. catch (SQLException e) {
  556. throw new DatabaseException(e);
  557. }
  558. finally {
  559. DbUtils.close(p);
  560. DbUtils.close(t);
  561. }
  562. }
  563. /**
  564.  * @see net.jforum.dao.ForumDAO#hasUnreadTopics(int, long)
  565.  */
  566. public List checkUnreadTopics(int forumId, long lastVisit)
  567. {
  568. List l = new ArrayList();
  569. PreparedStatement p = null;
  570. ResultSet rs = null;
  571. try {
  572. p = JForumExecutionContext.getConnection().prepareStatement(
  573. SystemGlobals.getSql("ForumModel.checkUnreadTopics"));
  574. p.setInt(1, forumId);
  575. p.setTimestamp(2, new Timestamp(lastVisit));
  576. rs = p.executeQuery();
  577. while (rs.next()) {
  578. Topic t = new Topic();
  579. t.setId(rs.getInt("topic_id"));
  580. t.setTime(new Date(rs.getTimestamp(1).getTime()));
  581. l.add(t);
  582. }
  583. return l;
  584. }
  585. catch (SQLException e) {
  586. throw new DatabaseException(e);
  587. }
  588. finally {
  589. DbUtils.close(rs, p);
  590. }
  591. }
  592. /**
  593.  * @see net.jforum.dao.ForumDAO#setModerated(int, boolean)
  594.  */
  595. public void setModerated(int categoryId, boolean status)
  596. {
  597. PreparedStatement p = null;
  598. try {
  599. p = JForumExecutionContext.getConnection()
  600. .prepareStatement(SystemGlobals.getSql("ForumModel.setModerated"));
  601. p.setInt(1, status ? 1 : 0);
  602. p.setInt(2, categoryId);
  603. p.executeUpdate();
  604. }
  605. catch (SQLException e) {
  606. throw new DatabaseException(e);
  607. }
  608. finally {
  609. DbUtils.close(p);
  610. }
  611. }
  612. /**
  613.  * @see net.jforum.dao.ForumDAO#getBoardStatus()
  614.  */
  615. public ForumStats getBoardStatus()
  616. {
  617. ForumStats fs = new ForumStats();
  618. fs.setPosts(this.getTotalMessages());
  619. Connection c = JForumExecutionContext.getConnection();
  620. // Total Users
  621. Statement s = null;
  622. ResultSet rs = null;
  623. try {
  624. s = c.createStatement();
  625. rs = s.executeQuery(SystemGlobals.getSql("UserModel.totalUsers"));
  626. rs.next();
  627. fs.setUsers(rs.getInt(1));
  628. rs.close();
  629. rs = null;
  630. s.close();
  631. s = null;
  632. // Total Topics
  633. s = c.createStatement();
  634. rs = s.executeQuery(SystemGlobals.getSql("TopicModel.totalTopics"));
  635. rs.next();
  636. fs.setTopics(rs.getInt(1));
  637. rs.close();
  638. rs = null;
  639. s.close();
  640. s = null;
  641. // Posts per day
  642. double postPerDay = 0;
  643. // Topics per day
  644. double topicPerDay = 0;
  645. // user per day
  646. double userPerDay = 0;
  647. s = c.createStatement();
  648. rs = s.executeQuery(SystemGlobals.getSql("ForumModel.statsFirstPostTime"));
  649. if (rs.next()) {
  650. Timestamp firstTime = rs.getTimestamp(1);
  651. if (rs.wasNull()) {
  652. firstTime = null;
  653. }
  654. rs.close();
  655. rs = null;
  656. s.close();
  657. s = null;
  658. Date today = new Date();
  659. postPerDay = firstTime != null ? fs.getPosts() / this.daysUntilToday(today, firstTime) : 0;
  660. if (fs.getPosts() > 0 && postPerDay < 1) {
  661. postPerDay = 1;
  662. }
  663. topicPerDay = firstTime != null ? fs.getTopics() / this.daysUntilToday(today, firstTime) : 0;
  664. // Users per day
  665. s = c.createStatement();
  666. rs = s.executeQuery(SystemGlobals.getSql("ForumModel.statsFirstRegisteredUserTime"));
  667. if (rs.next()) {
  668. firstTime = rs.getTimestamp(1);
  669. if (rs.wasNull()) {
  670. firstTime = null;
  671. }
  672. }
  673. rs.close();
  674. rs = null;
  675. s.close();
  676. s = null;
  677. userPerDay = firstTime != null ? fs.getUsers() / this.daysUntilToday(today, firstTime) : 0;
  678. }
  679. fs.setPostsPerDay(postPerDay);
  680. fs.setTopicsPerDay(topicPerDay);
  681. fs.setUsersPerDay(userPerDay);
  682. return fs;
  683. }
  684. catch (SQLException e) {
  685. throw new DatabaseException(e);
  686. }
  687. finally {
  688. DbUtils.close(rs, s);
  689. }
  690. }
  691. private int daysUntilToday(Date today, Date from)
  692. {
  693. int days = (int) ((today.getTime() - from.getTime()) / (24 * 60 * 60 * 1000));
  694. return days == 0 ? 1 : days;
  695. }
  696. /**
  697.  * This code is writen by looking at GenericTopicDAO.java
  698.  * 
  699.  * @see
  700.  */
  701. public List notifyUsers(Forum forum)
  702. {
  703. int posterId = SessionFacade.getUserSession().getUserId();
  704. int anonUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
  705. PreparedStatement p = null;
  706. ResultSet rs = null;
  707. try {
  708. p = JForumExecutionContext.getConnection().prepareStatement(
  709. SystemGlobals.getSql("ForumModel.notifyUsers"));
  710. p.setInt(1, forum.getId());
  711. p.setInt(2, posterId); // don't notify the poster
  712. p.setInt(3, anonUser); // don't notify the anonimous user
  713. rs = p.executeQuery();
  714. List users = new ArrayList();
  715. while (rs.next()) {
  716. User user = new User();
  717. user.setId(rs.getInt("user_id"));
  718. user.setEmail(rs.getString("user_email"));
  719. user.setUsername(rs.getString("username"));
  720. user.setLang(rs.getString("user_lang"));
  721. user.setNotifyAlways(rs.getInt("user_notify_always") == 1);
  722. user.setNotifyText(rs.getInt("user_notify_text") == 1);
  723. users.add(user);
  724. }
  725. return users;
  726. }
  727. catch (SQLException e) {
  728. throw new DatabaseException(e);
  729. }
  730. finally {
  731. DbUtils.close(rs, p);
  732. }
  733. }
  734. public void subscribeUser(int forumId, int userId)
  735. {
  736. PreparedStatement p = null;
  737. try {
  738. p = JForumExecutionContext.getConnection().prepareStatement(
  739. SystemGlobals.getSql("ForumModel.subscribeUser"));
  740. p.setInt(1, forumId);
  741. p.setInt(2, userId);
  742. p.executeUpdate();
  743. }
  744. catch (SQLException e) {
  745. throw new DatabaseException(e);
  746. }
  747. finally {
  748. DbUtils.close(p);
  749. }
  750. }
  751. public boolean isUserSubscribed(int forumId, int userId)
  752. {
  753. PreparedStatement stmt = null;
  754. ResultSet rs = null;
  755. try {
  756. stmt = JForumExecutionContext.getConnection().prepareStatement(
  757. SystemGlobals.getSql("ForumModel.isUserSubscribed"));
  758. stmt.setInt(1, forumId);
  759. stmt.setInt(2, userId);
  760. rs = stmt.executeQuery();
  761. return rs.next();
  762. }
  763. catch (SQLException e) {
  764. throw new DatabaseException(e);
  765. }
  766. finally {
  767. DbUtils.close(rs, stmt);
  768. }
  769. }
  770. public void removeSubscription(int forumId, int userId)
  771. {
  772. PreparedStatement p = null;
  773. try {
  774. p = JForumExecutionContext.getConnection().prepareStatement(
  775. SystemGlobals.getSql("ForumModel.removeSubscription"));
  776. p.setInt(1, forumId);
  777. p.setInt(2, userId);
  778. p.executeUpdate();
  779. }
  780. catch (SQLException e) {
  781. throw new DatabaseException(e);
  782. }
  783. finally {
  784. DbUtils.close(p);
  785. }
  786. }
  787. /**
  788.  * Remove all subscriptions on a forum, such as when a forum is locked. It is not used now.
  789.  * 
  790.  * @param forumId int
  791.  */
  792. public void removeSubscriptionByForum(int forumId)
  793. {
  794. PreparedStatement p = null;
  795. try {
  796. p = JForumExecutionContext.getConnection().prepareStatement(
  797. SystemGlobals.getSql("ForumModel.removeSubscriptionByForum"));
  798. p.setInt(1, forumId);
  799. p.executeUpdate();
  800. }
  801. catch (SQLException e) {
  802. throw new DatabaseException(e);
  803. }
  804. finally {
  805. DbUtils.close(p);
  806. }
  807. }
  808. /**
  809.  * @see net.jforum.dao.ForumDAO#discoverForumId(java.lang.String)
  810.  */
  811. public int discoverForumId(String listEmail)
  812. {
  813. int forumId = 0;
  814. PreparedStatement p = null;
  815. ResultSet rs = null;
  816. try {
  817. p = JForumExecutionContext.getConnection().prepareStatement(
  818. SystemGlobals.getSql("ForumModel.discoverForumId"));
  819. p.setString(1, listEmail);
  820. rs = p.executeQuery();
  821. if (rs.next()) {
  822. forumId = rs.getInt(1);
  823. }
  824. }
  825. catch (SQLException e) {
  826. }
  827. finally {
  828. DbUtils.close(rs, p);
  829. }
  830. return forumId;
  831. }
  832. }