GenericPostDAO.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 13k
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: Mar 28, 2003 / 22:57:43 PM
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum.dao.generic;
  44. import java.sql.PreparedStatement;
  45. import java.sql.ResultSet;
  46. import java.sql.SQLException;
  47. import java.sql.Timestamp;
  48. import java.text.SimpleDateFormat;
  49. import java.util.ArrayList;
  50. import java.util.Date;
  51. import java.util.Iterator;
  52. import java.util.List;
  53. import net.jforum.JForumExecutionContext;
  54. import net.jforum.dao.DataAccessDriver;
  55. import net.jforum.entities.Post;
  56. import net.jforum.exceptions.DatabaseException;
  57. import net.jforum.repository.ForumRepository;
  58. import net.jforum.search.SearchFacade;
  59. import net.jforum.util.DbUtils;
  60. import net.jforum.util.preferences.ConfigKeys;
  61. import net.jforum.util.preferences.SystemGlobals;
  62. /**
  63.  * @author Rafael Steil
  64.  * @author Vanessa Sabino
  65.  * @version $Id: GenericPostDAO.java,v 1.29 2007/09/12 14:43:15 rafaelsteil Exp $
  66.  */
  67. public class GenericPostDAO extends AutoKeys implements net.jforum.dao.PostDAO
  68. {
  69. /**
  70.  * @see net.jforum.dao.PostDAO#selectById(int)
  71.  */
  72. public Post selectById(int postId)
  73. {
  74. PreparedStatement p = null;
  75. ResultSet rs = null;
  76. try {
  77. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.selectById"));
  78. p.setInt(1, postId);
  79. rs = p.executeQuery();
  80. Post post = new Post();
  81. if (rs.next()) {
  82. post = this.makePost(rs);
  83. }
  84. return post;
  85. }
  86. catch (SQLException e) {
  87. throw new DatabaseException(e);
  88. }
  89. finally {
  90. DbUtils.close(rs, p);
  91. }
  92. }
  93. protected Post makePost(ResultSet rs) throws SQLException
  94. {
  95. Post post = new Post();
  96. post.setId(rs.getInt("post_id"));
  97. post.setTopicId(rs.getInt("topic_id"));
  98. post.setForumId(rs.getInt("forum_id"));
  99. post.setUserId(rs.getInt("user_id"));
  100. Timestamp postTime = rs.getTimestamp("post_time");
  101. post.setTime(new Date(postTime.getTime()));
  102. post.setUserIp(rs.getString("poster_ip"));
  103. post.setBbCodeEnabled(rs.getInt("enable_bbcode") > 0);
  104. post.setHtmlEnabled(rs.getInt("enable_html") > 0);
  105. post.setSmiliesEnabled(rs.getInt("enable_smilies") > 0);
  106. post.setSignatureEnabled(rs.getInt("enable_sig") > 0);
  107. post.setEditCount(rs.getInt("post_edit_count"));
  108. Timestamp editTime = rs.getTimestamp("post_edit_time");
  109. post.setEditTime(editTime != null ? new Date(editTime.getTime()) : null);
  110. post.setSubject(rs.getString("post_subject"));
  111. post.setText(this.getPostTextFromResultSet(rs));
  112. post.setPostUsername(rs.getString("username"));
  113. post.hasAttachments(rs.getInt("attach") > 0);
  114. post.setModerate(rs.getInt("need_moderate") == 1);
  115. SimpleDateFormat df = new SimpleDateFormat(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
  116. post.setFormatedTime(df.format(postTime));
  117. post.setKarma(DataAccessDriver.getInstance().newKarmaDAO().getPostKarma(post.getId()));
  118. return post;
  119. }
  120. /**
  121.  * Utility method to read the post text fromt the result set. This method may be useful when
  122.  * using some "non-standart" way 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 SQLException
  127.  */
  128. protected String getPostTextFromResultSet(ResultSet rs) throws SQLException
  129. {
  130. return rs.getString("post_text");
  131. }
  132. /**
  133.  * @see net.jforum.dao.PostDAO#delete(Post)
  134.  */
  135. public void delete(Post post)
  136. {
  137. List l = new ArrayList();
  138. l.add(post);
  139. this.removePosts(l);
  140. }
  141. private void removePosts(List posts)
  142. {
  143. PreparedStatement post = null;
  144. PreparedStatement text = null;
  145. try {
  146. post = JForumExecutionContext.getConnection()
  147. .prepareStatement(SystemGlobals.getSql("PostModel.deletePost"));
  148. text = JForumExecutionContext.getConnection().prepareStatement(
  149. SystemGlobals.getSql("PostModel.deletePostText"));
  150. for (Iterator iter = posts.iterator(); iter.hasNext();) {
  151. Post p = (Post) iter.next();
  152. post.setInt(1, p.getId());
  153. text.setInt(1, p.getId());
  154. text.executeUpdate();
  155. post.executeUpdate();
  156. SearchFacade.delete(p);
  157. }
  158. }
  159. catch (SQLException e) {
  160. throw new DatabaseException(e);
  161. }
  162. finally {
  163. DbUtils.close(post);
  164. DbUtils.close(text);
  165. }
  166. }
  167. /**
  168.  * @see net.jforum.model.PostModel#deleteByTopic(int)
  169.  */
  170. public void deleteByTopic(int topicId)
  171. {
  172. PreparedStatement p = null;
  173. ResultSet rs = null;
  174. try {
  175. p = JForumExecutionContext.getConnection()
  176. .prepareStatement(SystemGlobals.getSql("PostModel.deleteByTopic"));
  177. p.setInt(1, topicId);
  178. rs = p.executeQuery();
  179. List posts = new ArrayList();
  180. while (rs.next()) {
  181. Post post = new Post();
  182. post.setId(rs.getInt("post_id"));
  183. post.setUserId(rs.getInt("user_id"));
  184. posts.add(post);
  185. }
  186. this.removePosts(posts);
  187. }
  188. catch (SQLException e) {
  189. throw new DatabaseException(e);
  190. }
  191. finally {
  192. DbUtils.close(rs, p);
  193. }
  194. }
  195. /**
  196.  * @see net.jforum.dao.PostDAO#update(net.jforum.entities.Post)
  197.  */
  198. public void update(Post post)
  199. {
  200. this.updatePostsTable(post);
  201. this.updatePostsTextTable(post);
  202. SearchFacade.update(post);
  203. }
  204. protected void updatePostsTextTable(Post post)
  205. {
  206. PreparedStatement p = null;
  207. try {
  208. p = JForumExecutionContext.getConnection().prepareStatement(
  209. SystemGlobals.getSql("PostModel.updatePostText"));
  210. p.setString(1, post.getText());
  211. p.setString(2, post.getSubject());
  212. p.setInt(3, post.getId());
  213. p.executeUpdate();
  214. }
  215. catch (SQLException e) {
  216. throw new DatabaseException(e);
  217. }
  218. finally {
  219. DbUtils.close(p);
  220. }
  221. }
  222. protected void updatePostsTable(Post post)
  223. {
  224. PreparedStatement p = null;
  225. try {
  226. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.updatePost"));
  227. p.setInt(1, post.getTopicId());
  228. p.setInt(2, post.getForumId());
  229. p.setInt(3, post.isBbCodeEnabled() ? 1 : 0);
  230. p.setInt(4, post.isHtmlEnabled() ? 1 : 0);
  231. p.setInt(5, post.isSmiliesEnabled() ? 1 : 0);
  232. p.setInt(6, post.isSignatureEnabled() ? 1 : 0);
  233. p.setTimestamp(7, new Timestamp(System.currentTimeMillis()));
  234. p.setInt(8, post.getEditCount() + 1);
  235. p.setString(9, post.getUserIp());
  236. p.setInt(10, post.getId());
  237. p.executeUpdate();
  238. }
  239. catch (SQLException e) {
  240. throw new DatabaseException(e);
  241. }
  242. finally {
  243. DbUtils.close(p);
  244. }
  245. }
  246. /**
  247.  * @see net.jforum.dao.PostDAO#addNew(net.jforum.entities.Post)
  248.  */
  249. public int addNew(Post post)
  250. {
  251. try {
  252. this.addNewPost(post);
  253. this.addNewPostText(post);
  254. // Search
  255. SearchFacade.create(post);
  256. return post.getId();
  257. }
  258. catch (Exception e) {
  259. throw new DatabaseException(e);
  260. }
  261. }
  262. protected void addNewPostText(Post post) throws Exception
  263. {
  264. PreparedStatement p = null;
  265. try {
  266. p = JForumExecutionContext.getConnection().prepareStatement(
  267. SystemGlobals.getSql("PostModel.addNewPostText"));
  268. p.setInt(1, post.getId());
  269. p.setString(2, post.getText());
  270. p.setString(3, post.getSubject());
  271. p.executeUpdate();
  272. }
  273. finally {
  274. DbUtils.close(p);
  275. }
  276. }
  277. protected void addNewPost(Post post)
  278. {
  279. PreparedStatement p = null;
  280. try {
  281. p = this.getStatementForAutoKeys("PostModel.addNewPost");
  282. p.setInt(1, post.getTopicId());
  283. p.setInt(2, post.getForumId());
  284. p.setLong(3, post.getUserId());
  285. p.setTimestamp(4, new Timestamp(post.getTime().getTime()));
  286. p.setString(5, post.getUserIp());
  287. p.setInt(6, post.isBbCodeEnabled() ? 1 : 0);
  288. p.setInt(7, post.isHtmlEnabled() ? 1 : 0);
  289. p.setInt(8, post.isSmiliesEnabled() ? 1 : 0);
  290. p.setInt(9, post.isSignatureEnabled() ? 1 : 0);
  291. p.setInt(10, post.isModerationNeeded() ? 1 : 0);
  292. this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PostModel.lastGeneratedPostId"));
  293. int postId = this.executeAutoKeysQuery(p);
  294. post.setId(postId);
  295. }
  296. catch (SQLException e) {
  297. throw new DatabaseException(e);
  298. }
  299. finally {
  300. DbUtils.close(p);
  301. }
  302. }
  303. /**
  304.  * @see net.jforum.dao.PostDAO#selectAllBytTopic(int)
  305.  */
  306. public List selectAllByTopic(int topicId)
  307. {
  308. return this.selectAllByTopicByLimit(topicId, 0, Integer.MAX_VALUE - 1);
  309. }
  310. /**
  311.  * @see net.jforum.dao.PostDAO#selectAllBytTopicByLimit(int, int, int)
  312.  */
  313. public List selectAllByTopicByLimit(int topicId, int startFrom, int count)
  314. {
  315. List l = new ArrayList();
  316. String sql = SystemGlobals.getSql("PostModel.selectAllByTopicByLimit");
  317. PreparedStatement p = null;
  318. ResultSet rs = null;
  319. try {
  320. p = JForumExecutionContext.getConnection().prepareStatement(sql);
  321. p.setInt(1, topicId);
  322. p.setInt(2, startFrom);
  323. p.setInt(3, count);
  324. rs = p.executeQuery();
  325. while (rs.next()) {
  326. l.add(this.makePost(rs));
  327. }
  328. return l;
  329. }
  330. catch (SQLException e) {
  331. throw new DatabaseException(e);
  332. }
  333. finally {
  334. DbUtils.close(rs, p);
  335. }
  336. }
  337. /**
  338.  * @see net.jforum.dao.PostDAO#selectByUserByLimit(int, int, int)
  339.  */
  340. public List selectByUserByLimit(int userId, int startFrom, int count)
  341. {
  342. String sql = SystemGlobals.getSql("PostModel.selectByUserByLimit");
  343. sql = sql.replaceAll(":fids:", ForumRepository.getListAllowedForums());
  344. PreparedStatement p = null;
  345. ResultSet rs = null;
  346. try {
  347. p = JForumExecutionContext.getConnection().prepareStatement(sql);
  348. p.setInt(1, userId);
  349. p.setInt(2, startFrom);
  350. p.setInt(3, count);
  351. rs = p.executeQuery();
  352. List l = new ArrayList();
  353. while (rs.next()) {
  354. l.add(this.makePost(rs));
  355. }
  356. return l;
  357. }
  358. catch (SQLException e) {
  359. throw new DatabaseException(e);
  360. }
  361. finally {
  362. DbUtils.close(rs, p);
  363. }
  364. }
  365. public int countUserPosts(int userId)
  366. {
  367. int total = 0;
  368. PreparedStatement p = null;
  369. ResultSet rs = null;
  370. try {
  371. p = JForumExecutionContext.getConnection().prepareStatement(
  372. SystemGlobals.getSql("PostModel.countUserPosts").replaceAll(":fids:",
  373. ForumRepository.getListAllowedForums()));
  374. p.setInt(1, userId);
  375. rs = p.executeQuery();
  376. if (rs.next()) {
  377. total = rs.getInt(1);
  378. }
  379. return total;
  380. }
  381. catch (SQLException e) {
  382. throw new DatabaseException(e);
  383. }
  384. finally {
  385. DbUtils.close(rs, p);
  386. }
  387. }
  388. /**
  389.  * @see net.jforum.model.PostModel#countPreviousPosts(int)
  390.  */
  391. public int countPreviousPosts(int postId)
  392. {
  393. int total = 0;
  394. PreparedStatement p = null;
  395. ResultSet rs = null;
  396. try {
  397. p = JForumExecutionContext.getConnection().prepareStatement(
  398. SystemGlobals.getSql("PostModel.countPreviousPosts"));
  399. p.setInt(1, postId);
  400. p.setInt(2, postId);
  401. rs = p.executeQuery();
  402. if (rs.next()) {
  403. total = rs.getInt(1);
  404. }
  405. return total;
  406. }
  407. catch (SQLException e) {
  408. throw new DatabaseException(e);
  409. }
  410. finally {
  411. DbUtils.close(rs, p);
  412. }
  413. }
  414. public List selectLatestByForumForRSS(int forumId, int limit) 
  415. {
  416. List l = new ArrayList();
  417. PreparedStatement p = null;
  418. ResultSet rs = null;
  419. try {
  420. p = JForumExecutionContext.getConnection().prepareStatement(
  421. SystemGlobals.getSql("PostModel.selectLatestByForumForRSS"));
  422. p.setInt(1, forumId);
  423. p.setInt(2, limit);
  424. rs = p.executeQuery();
  425. while (rs.next()) {
  426. Post post = this.buildPostForRSS(rs);
  427. l.add(post);
  428. }
  429. }
  430. catch (SQLException e) {
  431. throw new DatabaseException(e);
  432. }
  433. finally {
  434. DbUtils.close(rs, p);
  435. }
  436. return l;
  437. }
  438. public List selectHotForRSS(int limit) 
  439. {
  440. List l = new ArrayList();
  441. PreparedStatement p = null;
  442. ResultSet rs = null;
  443. try {
  444. p = JForumExecutionContext.getConnection().prepareStatement(
  445. SystemGlobals.getSql("PostModel.selectHotForRSS"));
  446. p.setInt(1, limit);
  447. rs = p.executeQuery();
  448. while (rs.next()) {
  449. Post post = this.buildPostForRSS(rs);
  450. l.add(post);
  451. }
  452. }
  453. catch (SQLException e) {
  454. throw new DatabaseException(e);
  455. }
  456. finally {
  457. DbUtils.close(rs, p);
  458. }
  459. return l;
  460. }
  461. private Post buildPostForRSS(ResultSet rs) throws SQLException 
  462. {
  463. Post post = new Post();
  464. post.setId(rs.getInt("post_id"));
  465. post.setSubject(rs.getString("subject"));
  466. post.setText(rs.getString("post_text"));
  467. post.setTopicId(rs.getInt("topic_id"));
  468. post.setForumId(rs.getInt("forum_id")); 
  469. post.setUserId(rs.getInt("user_id"));
  470. post.setPostUsername(rs.getString("username"));
  471. post.setTime(new Date(rs.getTimestamp("post_time").getTime()));
  472. return post;
  473. }
  474. }