GenericPollDAO.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 11k
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.  * Created on 21/05/2004 - 14:19:11
  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.util.Date;
  49. import java.util.Iterator;
  50. import java.util.List;
  51. import net.jforum.JForumExecutionContext;
  52. import net.jforum.dao.PollDAO;
  53. import net.jforum.entities.Poll;
  54. import net.jforum.entities.PollOption;
  55. import net.jforum.exceptions.DatabaseException;
  56. import net.jforum.util.DbUtils;
  57. import net.jforum.util.preferences.SystemGlobals;
  58. /**
  59.  * @author David Almilli
  60.  * @version $Id: GenericPollDAO.java,v 1.9 2007/08/01 22:30:03 rafaelsteil Exp $
  61.  */
  62. public class GenericPollDAO extends AutoKeys implements PollDAO
  63. {
  64. /**
  65.  * @see net.jforum.dao.PollDAO#addNew(net.jforum.entities.Poll)
  66.  */
  67. public int addNew(Poll poll)
  68. {
  69. this.addNewPoll(poll);
  70. this.addNewPollOptions(poll.getId(), poll.getOptions());
  71. return poll.getId();
  72. }
  73. protected void addNewPoll(Poll poll)
  74. {
  75. PreparedStatement p = null;
  76. try {
  77. p = this.getStatementForAutoKeys("PollModel.addNewPoll");
  78. p.setInt(1, poll.getTopicId());
  79. p.setString(2, poll.getLabel());
  80. p.setInt(3, poll.getLength());
  81. this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PollModel.lastGeneratedPollId"));
  82. int pollId = this.executeAutoKeysQuery(p);
  83. poll.setId(pollId);
  84. }
  85. catch (SQLException e) {
  86. throw new DatabaseException(e);
  87. }
  88. finally {
  89. DbUtils.close(p);
  90. }
  91. }
  92. protected void addNewPollOptions(int pollId, List options)
  93. {
  94. Connection connection = JForumExecutionContext.getConnection();
  95. PreparedStatement p = null;
  96. ResultSet rs = null;
  97. try {
  98. p = connection.prepareStatement(SystemGlobals.getSql("PollModel.selectMaxVoteId"));
  99. p.setInt(1, pollId);
  100. rs = p.executeQuery();
  101. rs.next();
  102. int optionId = rs.getInt(1);
  103. rs.close();
  104. rs = null;
  105. p.close();
  106. p = null;
  107. p = connection.prepareStatement(SystemGlobals.getSql("PollModel.addNewPollOption"));
  108. for (Iterator iter = options.iterator(); iter.hasNext();) {
  109. PollOption option = (PollOption) iter.next();
  110. p.setInt(1, pollId);
  111. p.setInt(2, ++optionId);
  112. p.setString(3, option.getText());
  113. p.executeUpdate();
  114. }
  115. }
  116. catch (SQLException e) {
  117. throw new DatabaseException(e);
  118. }
  119. finally {
  120. DbUtils.close(rs, p);
  121. }
  122. }
  123. /**
  124.  * @see net.jforum.dao.PollDAO#selectById(int)
  125.  */
  126. public Poll selectById(int pollId)
  127. {
  128. PreparedStatement p = null;
  129. PreparedStatement o = null;
  130. ResultSet ors = null;
  131. ResultSet prs = null;
  132. try {
  133. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectById"));
  134. p.setInt(1, pollId);
  135. prs = p.executeQuery();
  136. Poll poll = null;
  137. if (prs.next()) {
  138. poll = this.makePoll(prs);
  139. o = JForumExecutionContext.getConnection().prepareStatement(
  140. SystemGlobals.getSql("PollModel.selectOptionsByPollId"));
  141. o.setInt(1, pollId);
  142. ors = o.executeQuery();
  143. while (ors.next()) {
  144. poll.addOption(this.makePollOption(ors));
  145. }
  146. }
  147. return poll;
  148. }
  149. catch (SQLException e) {
  150. throw new DatabaseException(e);
  151. }
  152. finally {
  153. DbUtils.close(prs, p);
  154. DbUtils.close(ors, o);
  155. }
  156. }
  157. protected Poll makePoll(ResultSet rs) throws SQLException
  158. {
  159. Poll poll = new Poll();
  160. poll.setId(rs.getInt("vote_id"));
  161. poll.setTopicId(rs.getInt("topic_id"));
  162. poll.setLabel(rs.getString("vote_text"));
  163. poll.setStartTime(new Date(rs.getTimestamp("vote_start").getTime()));
  164. poll.setLength(rs.getInt("vote_length"));
  165. return poll;
  166. }
  167. protected PollOption makePollOption(ResultSet rs) throws SQLException
  168. {
  169. PollOption option = new PollOption();
  170. option.setPollId(rs.getInt("vote_id"));
  171. option.setId(rs.getInt("vote_option_id"));
  172. option.setText(rs.getString("vote_option_text"));
  173. option.setVoteCount(rs.getInt("vote_result"));
  174. return option;
  175. }
  176. /**
  177.  * @see net.jforum.dao.PollDAO#voteOnPoll(int, int, int, java.lang.String)
  178.  */
  179. public void voteOnPoll(int pollId, int optionId, int userId, String ipAddress)
  180. {
  181. Connection connection = JForumExecutionContext.getConnection();
  182. PreparedStatement v = null;
  183. PreparedStatement p = null;
  184. try {
  185. p = connection.prepareStatement(SystemGlobals.getSql("PollModel.incrementVoteCount"));
  186. v = connection.prepareStatement(SystemGlobals.getSql("PollModel.addNewVoter"));
  187. p.setInt(1, pollId);
  188. p.setInt(2, optionId);
  189. v.setInt(1, pollId);
  190. v.setInt(2, userId);
  191. v.setString(3, ipAddress);
  192. p.executeUpdate();
  193. v.executeUpdate();
  194. }
  195. catch (SQLException e) {
  196. throw new DatabaseException(e);
  197. }
  198. finally {
  199. DbUtils.close(p);
  200. DbUtils.close(v);
  201. }
  202. }
  203. /**
  204.  * @see net.jforum.dao.PollDAO#hasVotedOnPoll(int, int)
  205.  */
  206. public boolean hasUserVotedOnPoll(int pollId, int userId)
  207. {
  208. PreparedStatement p = null;
  209. ResultSet rs = null;
  210. try {
  211. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectVoter"));
  212. p.setInt(1, pollId);
  213. p.setInt(2, userId);
  214. rs = p.executeQuery();
  215. return rs.next();
  216. }
  217. catch (SQLException e) {
  218. throw new DatabaseException(e);
  219. }
  220. finally {
  221. DbUtils.close(rs, p);
  222. }
  223. }
  224. /**
  225.  * Tells if the anonymous user has already voted on the given poll from the given IP
  226.  * 
  227.  * @param pollId
  228.  *            the poll id that is being checked
  229.  * @param ipAddress
  230.  *            the IP address of the anonymoususer to check the vote for
  231.  * @return true if the user has already voted on the given poll
  232.  */
  233. public boolean hasUserVotedOnPoll(int pollId, String ipAddress)
  234. {
  235. PreparedStatement p = null;
  236. ResultSet rs = null;
  237. try {
  238. p = JForumExecutionContext.getConnection().prepareStatement(
  239. SystemGlobals.getSql("PollModel.selectVoterByIP"));
  240. p.setInt(1, pollId);
  241. p.setString(2, ipAddress);
  242. rs = p.executeQuery();
  243. return rs.next();
  244. }
  245. catch (SQLException e) {
  246. throw new DatabaseException(e);
  247. }
  248. finally {
  249. DbUtils.close(rs, p);
  250. }
  251. }
  252. /**
  253.  * @see net.jforum.dao.PollDAO#delete(int)
  254.  */
  255. public void deleteByTopicId(int topicId)
  256. {
  257. // first, lookup the poll id, then delete it
  258. PreparedStatement p = null;
  259. ResultSet rs = null;
  260. try {
  261. p = JForumExecutionContext.getConnection().prepareStatement(
  262. SystemGlobals.getSql("PollModel.selectPollByTopicId"));
  263. p.setInt(1, topicId);
  264. rs = p.executeQuery();
  265. int pollId = 0;
  266. if (rs.next()) {
  267. pollId = rs.getInt("vote_id");
  268. }
  269. if (pollId != 0) {
  270. delete(pollId);
  271. }
  272. }
  273. catch (SQLException e) {
  274. throw new DatabaseException(e);
  275. }
  276. finally {
  277. DbUtils.close(rs, p);
  278. }
  279. }
  280. /**
  281.  * @see net.jforum.dao.PollDAO#delete(int)
  282.  */
  283. public void delete(int pollId)
  284. {
  285. this.deletePollVotes(pollId);
  286. this.deleteAllPollOptions(pollId);
  287. this.deletePoll(pollId);
  288. }
  289. protected void deletePoll(int pollId)
  290. {
  291. PreparedStatement poll = null;
  292. try {
  293. poll = JForumExecutionContext.getConnection()
  294. .prepareStatement(SystemGlobals.getSql("PollModel.deletePoll"));
  295. poll.setInt(1, pollId);
  296. poll.executeUpdate();
  297. }
  298. catch (SQLException e) {
  299. throw new DatabaseException(e);
  300. }
  301. finally {
  302. DbUtils.close(poll);
  303. }
  304. }
  305. protected void deletePollVotes(int pollId)
  306. {
  307. PreparedStatement poll = null;
  308. try {
  309. poll = JForumExecutionContext.getConnection().prepareStatement(
  310. SystemGlobals.getSql("PollModel.deletePollVoters"));
  311. poll.setInt(1, pollId);
  312. poll.executeUpdate();
  313. }
  314. catch (SQLException e) {
  315. throw new DatabaseException(e);
  316. }
  317. finally {
  318. DbUtils.close(poll);
  319. }
  320. }
  321. protected void deleteAllPollOptions(int pollId)
  322. {
  323. PreparedStatement poll = null;
  324. try {
  325. poll = JForumExecutionContext.getConnection().prepareStatement(
  326. SystemGlobals.getSql("PollModel.deleteAllPollOptions"));
  327. poll.setInt(1, pollId);
  328. poll.executeUpdate();
  329. }
  330. catch (SQLException e) {
  331. throw new DatabaseException(e);
  332. }
  333. finally {
  334. DbUtils.close(poll);
  335. }
  336. }
  337. protected void deletePollOptions(int pollId, List deleted) throws SQLException
  338. {
  339. Connection connection = JForumExecutionContext.getConnection();
  340. PreparedStatement options = null;
  341. try {
  342. options = connection.prepareStatement(SystemGlobals.getSql("PollModel.deletePollOption"));
  343. for (Iterator iter = deleted.iterator(); iter.hasNext();) {
  344. PollOption o = (PollOption) iter.next();
  345. // Option
  346. options.setInt(1, pollId);
  347. options.setInt(2, o.getId());
  348. options.executeUpdate();
  349. }
  350. }
  351. finally {
  352. DbUtils.close(options);
  353. }
  354. }
  355. protected void updatePollOptions(int pollId, List options) throws SQLException
  356. {
  357. PreparedStatement p = null;
  358. try {
  359. p = JForumExecutionContext.getConnection().prepareStatement(
  360. SystemGlobals.getSql("PollModel.updatePollOption"));
  361. for (Iterator iter = options.iterator(); iter.hasNext();) {
  362. PollOption o = (PollOption) iter.next();
  363. p.setString(1, o.getText());
  364. p.setInt(2, o.getId());
  365. p.setInt(3, pollId);
  366. p.executeUpdate();
  367. }
  368. }
  369. finally {
  370. DbUtils.close(p);
  371. }
  372. }
  373. /**
  374.  * @see net.jforum.dao.PollDAO#update(net.jforum.entities.Poll)
  375.  */
  376. public void update(Poll poll)
  377. {
  378. try {
  379. this.updatePoll(poll);
  380. if (poll.getChanges() != null) {
  381. this.deletePollOptions(poll.getId(), poll.getChanges().getDeletedOptions());
  382. this.updatePollOptions(poll.getId(), poll.getChanges().getChangedOptions());
  383. this.addNewPollOptions(poll.getId(), poll.getChanges().getNewOptions());
  384. }
  385. }
  386. catch (SQLException e) {
  387. throw new DatabaseException(e);
  388. }
  389. }
  390. protected void updatePoll(Poll poll) throws SQLException
  391. {
  392. PreparedStatement p = null;
  393. try {
  394. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.updatePoll"));
  395. p.setString(1, poll.getLabel());
  396. p.setInt(2, poll.getLength());
  397. p.setInt(3, poll.getId());
  398. p.executeUpdate();
  399. }
  400. finally {
  401. DbUtils.close(p);
  402. }
  403. }
  404. }