1 43 package net.jforum.dao.generic; 44 45 import java.sql.Connection ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 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.util.preferences.SystemGlobals; 56 57 61 public class GenericPollDAO extends AutoKeys implements PollDAO { 62 63 66 public int addNew(Poll poll) throws Exception { 67 this.addNewPoll(poll); 68 this.addNewPollOptions(poll.getId(), poll.getOptions()); 69 70 return poll.getId(); 71 } 72 73 protected void addNewPoll(Poll poll) throws Exception { 74 PreparedStatement p = this.getStatementForAutoKeys("PollModel.addNewPoll"); 75 p.setInt(1, poll.getTopicId()); 76 p.setString(2, poll.getLabel()); 77 p.setInt(3, poll.getLength()); 78 79 this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PollModel.lastGeneratedPollId")); 80 int pollId = this.executeAutoKeysQuery(p); 81 poll.setId(pollId); 82 83 p.close(); 84 } 85 86 protected void addNewPollOptions(int pollId, List options) throws Exception { 87 Connection connection = JForumExecutionContext.getConnection(); 88 89 PreparedStatement p = connection.prepareStatement(SystemGlobals.getSql("PollModel.addNewPollOption")); 90 PreparedStatement max = connection.prepareStatement(SystemGlobals.getSql("PollModel.selectMaxVoteId")); 91 92 max.setInt(1, pollId); 93 ResultSet rs = max.executeQuery(); 94 rs.next(); 95 96 int optionId = rs.getInt(1); 97 98 rs.close(); 99 max.close(); 100 101 for (Iterator iter = options.iterator(); iter.hasNext(); ) { 102 PollOption option = (PollOption)iter.next(); 103 104 p.setInt(1, pollId); 105 p.setInt(2, ++optionId); 106 p.setString(3, option.getText()); 107 108 p.executeUpdate(); 109 } 110 111 p.close(); 112 } 113 114 117 public Poll selectById(int pollId) throws Exception { 118 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectById")); 119 PreparedStatement o = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectOptionsByPollId")); 120 121 p.setInt(1, pollId); 122 o.setInt(1, pollId); 123 124 ResultSet prs = p.executeQuery(); 125 126 Poll poll = null; 127 128 if (prs.next()) { 129 poll = this.makePoll(prs); 130 131 ResultSet ors = o.executeQuery(); 132 133 while (ors.next()) { 134 poll.addOption(this.makePollOption(ors)); 135 } 136 137 ors.close(); 138 } 139 140 prs.close(); 141 p.close(); 142 o.close(); 143 144 return poll; 145 } 146 147 protected Poll makePoll(ResultSet rs) throws Exception { 148 Poll poll = new Poll(); 149 poll.setId(rs.getInt("vote_id")); 150 poll.setTopicId(rs.getInt("topic_id")); 151 poll.setLabel(rs.getString("vote_text")); 152 poll.setStartTime(rs.getTimestamp("vote_start")); 153 poll.setLength(rs.getInt("vote_length")); 154 155 return poll; 156 } 157 158 protected PollOption makePollOption(ResultSet rs) throws Exception { 159 PollOption option = new PollOption(); 160 option.setPollId(rs.getInt("vote_id")); 161 option.setId(rs.getInt("vote_option_id")); 162 option.setText(rs.getString("vote_option_text")); 163 option.setVoteCount(rs.getInt("vote_result")); 164 165 return option; 166 } 167 168 171 public void voteOnPoll(int pollId, int optionId, int userId, String ipAddress) throws Exception { 172 Connection connection = JForumExecutionContext.getConnection(); 173 174 PreparedStatement p = connection.prepareStatement(SystemGlobals.getSql("PollModel.incrementVoteCount")); 175 PreparedStatement v = connection.prepareStatement(SystemGlobals.getSql("PollModel.addNewVoter")); 176 177 p.setInt(1, pollId); 178 p.setInt(2, optionId); 179 180 v.setInt(1, pollId); 181 v.setInt(2, userId); 182 v.setString(3, ipAddress); 183 184 p.executeUpdate(); 185 v.executeUpdate(); 186 187 p.close(); 188 v.close(); 189 } 190 191 194 public boolean hasUserVotedOnPoll(int pollId, int userId) throws Exception { 195 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectVoter")); 196 p.setInt(1, pollId); 197 p.setInt(2, userId); 198 199 ResultSet rs = p.executeQuery(); 200 201 boolean hasVotedOnPoll = rs.next(); 202 203 rs.close(); 204 p.close(); 205 206 return hasVotedOnPoll; 207 } 208 209 210 217 public boolean hasUserVotedOnPoll(int pollId, String ipAddress) throws Exception { 218 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectVoterByIP")); 219 p.setInt(1, pollId); 220 p.setString(2, ipAddress); 221 222 ResultSet rs = p.executeQuery(); 223 224 boolean hasVotedOnPoll = rs.next(); 225 226 rs.close(); 227 p.close(); 228 229 return hasVotedOnPoll; 230 } 231 232 235 public void deleteByTopicId(int topicId) throws Exception { 236 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectPollByTopicId")); 238 239 p.setInt(1, topicId); 240 241 ResultSet rs = p.executeQuery(); 242 243 int pollId = 0; 244 if (rs.next()) { 245 pollId = rs.getInt("vote_id"); 246 } 247 rs.close(); 248 p.close(); 249 250 if (pollId != 0) { 251 delete(pollId); 252 } 253 } 254 255 258 public void delete(int pollId) throws Exception { 259 this.deletePollVotes(pollId); 260 this.deleteAllPollOptions(pollId); 261 this.deletePoll(pollId); 262 } 263 264 protected void deletePoll(int pollId) throws Exception { 265 PreparedStatement poll = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.deletePoll")); 266 267 poll.setInt(1, pollId); 268 poll.executeUpdate(); 269 poll.close(); 270 } 271 272 protected void deletePollVotes(int pollId) throws Exception { 273 PreparedStatement poll = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.deletePollVoters")); 274 275 poll.setInt(1, pollId); 276 poll.executeUpdate(); 277 poll.close(); 278 } 279 280 protected void deleteAllPollOptions(int pollId) throws Exception 281 { 282 PreparedStatement poll = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.deleteAllPollOptions")); 283 284 poll.setInt(1, pollId); 285 poll.executeUpdate(); 286 poll.close(); 287 } 288 289 protected void deletePollOptions(int pollId, List deleted) throws Exception { 290 Connection connection = JForumExecutionContext.getConnection(); 291 292 PreparedStatement options = connection.prepareStatement(SystemGlobals.getSql("PollModel.deletePollOption")); 293 294 for (Iterator iter = deleted.iterator(); iter.hasNext(); ) { 295 PollOption o = (PollOption)iter.next(); 296 297 options.setInt(1, pollId); 299 options.setInt(2, o.getId()); 300 options.executeUpdate(); 301 } 302 303 options.close(); 304 } 305 306 protected void updatePollOptions(int pollId, List options) throws Exception 307 { 308 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.updatePollOption")); 309 310 for (Iterator iter = options.iterator(); iter.hasNext(); ) { 311 PollOption o = (PollOption)iter.next(); 312 313 p.setString(1, o.getText()); 314 p.setInt(2, o.getId()); 315 p.setInt(3, pollId); 316 317 p.executeUpdate(); 318 } 319 320 p.close(); 321 } 322 323 326 public void update(Poll poll) throws Exception { 327 this.updatePoll(poll); 328 329 if (poll.getChanges() != null) { 330 this.deletePollOptions(poll.getId(), poll.getChanges().getDeletedOptions()); 331 this.updatePollOptions(poll.getId(), poll.getChanges().getChangedOptions()); 332 this.addNewPollOptions(poll.getId(), poll.getChanges().getNewOptions()); 333 } 334 } 335 336 protected void updatePoll(Poll poll) throws Exception { 337 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.updatePoll")); 338 339 p.setString(1, poll.getLabel()); 340 p.setInt(2, poll.getLength()); 341 p.setInt(3, poll.getId()); 342 343 p.executeUpdate(); 344 p.close(); 345 } 346 } 347 | Popular Tags |