KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > generic > GenericPollDAO


1 /*
2  * Copyright (c) Rafael Steil
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
45 import java.sql.Connection JavaDoc;
46 import java.sql.PreparedStatement JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
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 /**
58  * @author David Almilli
59  * @version $Id: GenericPollDAO.java,v 1.4 2006/01/29 15:06:24 rafaelsteil Exp $
60  */

61 public class GenericPollDAO extends AutoKeys implements PollDAO {
62
63     /**
64      * @see net.jforum.dao.PollDAO#addNew(net.jforum.entities.Poll)
65      */

66     public int addNew(Poll poll) throws Exception JavaDoc {
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 JavaDoc {
74         PreparedStatement JavaDoc 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 JavaDoc options) throws Exception JavaDoc {
87         Connection JavaDoc connection = JForumExecutionContext.getConnection();
88         
89         PreparedStatement JavaDoc p = connection.prepareStatement(SystemGlobals.getSql("PollModel.addNewPollOption"));
90         PreparedStatement JavaDoc max = connection.prepareStatement(SystemGlobals.getSql("PollModel.selectMaxVoteId"));
91         
92         max.setInt(1, pollId);
93         ResultSet JavaDoc rs = max.executeQuery();
94         rs.next();
95         
96         int optionId = rs.getInt(1);
97         
98         rs.close();
99         max.close();
100         
101         for (Iterator JavaDoc 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     /**
115      * @see net.jforum.dao.PollDAO#selectById(int)
116      */

117     public Poll selectById(int pollId) throws Exception JavaDoc {
118         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectById"));
119         PreparedStatement JavaDoc o = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectOptionsByPollId"));
120         
121         p.setInt(1, pollId);
122         o.setInt(1, pollId);
123         
124         ResultSet JavaDoc prs = p.executeQuery();
125         
126         Poll poll = null;
127         
128         if (prs.next()) {
129             poll = this.makePoll(prs);
130             
131             ResultSet JavaDoc 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 JavaDoc rs) throws Exception JavaDoc {
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 JavaDoc rs) throws Exception JavaDoc {
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     /**
169      * @see net.jforum.dao.PollDAO#voteOnPoll(int, int, int, java.lang.String)
170      */

171     public void voteOnPoll(int pollId, int optionId, int userId, String JavaDoc ipAddress) throws Exception JavaDoc {
172         Connection JavaDoc connection = JForumExecutionContext.getConnection();
173         
174         PreparedStatement JavaDoc p = connection.prepareStatement(SystemGlobals.getSql("PollModel.incrementVoteCount"));
175         PreparedStatement JavaDoc 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     /**
192      * @see net.jforum.dao.PollDAO#hasVotedOnPoll(int, int)
193      */

194     public boolean hasUserVotedOnPoll(int pollId, int userId) throws Exception JavaDoc {
195         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectVoter"));
196         p.setInt(1, pollId);
197         p.setInt(2, userId);
198         
199         ResultSet JavaDoc rs = p.executeQuery();
200         
201         boolean hasVotedOnPoll = rs.next();
202         
203         rs.close();
204         p.close();
205         
206         return hasVotedOnPoll;
207     }
208
209     
210     /**
211      * Tells if the anonymous user has already voted on the given poll from the given IP
212      * @param pollId the poll id that is being checked
213      * @param ipAddress the IP address of the anonymoususer to check the vote for
214      * @return true if the user has already voted on the given poll
215      * @throws Exception
216      */

217     public boolean hasUserVotedOnPoll(int pollId, String JavaDoc ipAddress) throws Exception JavaDoc {
218         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectVoterByIP"));
219         p.setInt(1, pollId);
220         p.setString(2, ipAddress);
221         
222         ResultSet JavaDoc rs = p.executeQuery();
223         
224         boolean hasVotedOnPoll = rs.next();
225         
226         rs.close();
227         p.close();
228         
229         return hasVotedOnPoll;
230     }
231     
232     /**
233      * @see net.jforum.dao.PollDAO#delete(int)
234      */

235     public void deleteByTopicId(int topicId) throws Exception JavaDoc {
236         //first, lookup the poll id, then delete it
237
PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.selectPollByTopicId"));
238         
239         p.setInt(1, topicId);
240         
241         ResultSet JavaDoc 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     /**
256      * @see net.jforum.dao.PollDAO#delete(int)
257      */

258     public void delete(int pollId) throws Exception JavaDoc {
259         this.deletePollVotes(pollId);
260         this.deleteAllPollOptions(pollId);
261         this.deletePoll(pollId);
262     }
263     
264     protected void deletePoll(int pollId) throws Exception JavaDoc {
265         PreparedStatement JavaDoc 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 JavaDoc {
273         PreparedStatement JavaDoc 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 JavaDoc
281     {
282         PreparedStatement JavaDoc 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 JavaDoc deleted) throws Exception JavaDoc {
290         Connection JavaDoc connection = JForumExecutionContext.getConnection();
291         
292         PreparedStatement JavaDoc options = connection.prepareStatement(SystemGlobals.getSql("PollModel.deletePollOption"));
293
294         for (Iterator JavaDoc iter = deleted.iterator(); iter.hasNext(); ) {
295             PollOption o = (PollOption)iter.next();
296     
297             // Option
298
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 JavaDoc options) throws Exception JavaDoc
307     {
308         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PollModel.updatePollOption"));
309         
310         for (Iterator JavaDoc 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     /**
324      * @see net.jforum.dao.PollDAO#update(net.jforum.Poll)
325      */

326     public void update(Poll poll) throws Exception JavaDoc {
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 JavaDoc {
337         PreparedStatement JavaDoc 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