KickJava   Java API By Example, From Geeks To Geeks.

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


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 Jan 11, 2005 11:22:19 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.dao.generic;
44
45 import java.sql.PreparedStatement JavaDoc;
46 import java.sql.ResultSet JavaDoc;
47 import java.sql.SQLException JavaDoc;
48 import java.sql.Timestamp JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Date JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Map JavaDoc;
54
55 import net.jforum.JForumExecutionContext;
56 import net.jforum.entities.Karma;
57 import net.jforum.entities.KarmaStatus;
58 import net.jforum.entities.User;
59 import net.jforum.util.preferences.SystemGlobals;
60
61 /**
62  * @author Rafael Steil
63  * @version $Id: GenericKarmaDAO.java,v 1.7 2006/01/29 15:06:21 rafaelsteil Exp $
64  */

65 public class GenericKarmaDAO implements net.jforum.dao.KarmaDAO
66 {
67     /**
68      * @see net.jforum.dao.KarmaDAO#addKarma(net.jforum.entities.Karma)
69      */

70     public void addKarma(Karma karma) throws Exception JavaDoc
71     {
72         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.add"));
73         p.setInt(1, karma.getPostId());
74         p.setInt(2, karma.getPostUserId());
75         p.setInt(3, karma.getFromUserId());
76         p.setInt(4, karma.getPoints());
77         p.setInt(5, karma.getTopicId());
78         p.setTimestamp(6, new Timestamp JavaDoc((new Date JavaDoc()).getTime()));
79         p.executeUpdate();
80         p.close();
81
82         this.updateUserKarma(karma.getPostUserId());
83     }
84
85     /**
86      * @see net.jforum.dao.KarmaDAO#selectKarmaStatus(int)
87      */

88     public KarmaStatus getUserKarma(int userId) throws Exception JavaDoc
89     {
90         KarmaStatus status = new KarmaStatus();
91
92         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.getUserKarma"));
93         p.setInt(1, userId);
94
95         ResultSet JavaDoc rs = p.executeQuery();
96         if (rs.next()) {
97             status.setKarmaPoints(Math.round(rs.getDouble("user_karma")));
98         }
99
100         rs.close();
101         p.close();
102
103         return status;
104     }
105
106     /**
107      * @see net.jforum.dao.KarmaDAO#updateUserKarma(int)
108      */

109     public void updateUserKarma(int userId) throws Exception JavaDoc
110     {
111         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
112                 SystemGlobals.getSql("KarmaModel.getUserKarmaPoints"));
113         p.setInt(1, userId);
114
115         int totalRecords = 0;
116         double totalPoints = 0;
117         ResultSet JavaDoc rs = p.executeQuery();
118         
119         while (rs.next()) {
120             int points = rs.getInt("points");
121             int votes = rs.getInt("votes");
122
123             totalPoints += ((double) points / votes);
124             totalRecords++;
125         }
126
127         rs.close();
128         p.close();
129
130         p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.updateUserKarma"));
131         
132         double karmaPoints = totalPoints / totalRecords;
133         
134         if (Double.isNaN(karmaPoints)) {
135             karmaPoints = 0;
136         }
137         
138         p.setDouble(1, karmaPoints);
139         p.setInt(2, userId);
140         p.executeUpdate();
141         p.close();
142     }
143
144     /**
145      * @see net.jforum.dao.KarmaDAO#update(net.jforum.entities.Karma)
146      */

147     public void update(Karma karma) throws Exception JavaDoc
148     {
149         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.update"));
150         p.setInt(1, karma.getPoints());
151         p.setInt(2, karma.getId());
152         p.executeUpdate();
153         p.close();
154     }
155
156     /**
157      * @see net.jforum.dao.KarmaDAO#getPostKarma(int)
158      */

159     public KarmaStatus getPostKarma(int postId) throws Exception JavaDoc
160     {
161         KarmaStatus karma = new KarmaStatus();
162
163         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.getPostKarma"));
164         p.setInt(1, postId);
165
166         ResultSet JavaDoc rs = p.executeQuery();
167         if (rs.next()) {
168             karma.setKarmaPoints(Math.round(rs.getDouble(1)));
169         }
170
171         rs.close();
172         p.close();
173
174         return karma;
175     }
176
177     /**
178      * @see net.jforum.dao.KarmaDAO#userCanAddKarma(int, int)
179      */

180     public boolean userCanAddKarma(int userId, int postId) throws Exception JavaDoc
181     {
182         boolean status = true;
183
184         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
185                 SystemGlobals.getSql("KarmaModel.userCanAddKarma"));
186         p.setInt(1, postId);
187         p.setInt(2, userId);
188
189         ResultSet JavaDoc rs = p.executeQuery();
190         if (rs.next()) {
191             status = rs.getInt(1) < 1;
192         }
193
194         rs.close();
195         p.close();
196
197         return status;
198     }
199
200     /**
201      * @see net.jforum.dao.KarmaDAO#getUserVotes(int, int)
202      */

203     public Map JavaDoc getUserVotes(int topicId, int userId) throws Exception JavaDoc
204     {
205         Map JavaDoc m = new HashMap JavaDoc();
206
207         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.getUserVotes"));
208         p.setInt(1, topicId);
209         p.setInt(2, userId);
210
211         ResultSet JavaDoc rs = p.executeQuery();
212         while (rs.next()) {
213             m.put(new Integer JavaDoc(rs.getInt("post_id")), new Integer JavaDoc(rs.getInt("points")));
214         }
215
216         rs.close();
217         p.close();
218
219         return m;
220     }
221
222     public void getUserTotalKarma(User user) throws SQLException JavaDoc
223     {
224         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
225                 SystemGlobals.getSql("KarmaModel.getUserTotalVotes"));
226         p.setInt(1, user.getId());
227
228         ResultSet JavaDoc rs = p.executeQuery();
229
230         user.setKarma(new KarmaStatus());
231
232         if (rs.next()) {
233             user.getKarma().setTotalPoints(rs.getInt("points"));
234             user.getKarma().setVotesReceived(rs.getInt("votes"));
235         }
236
237         if (user.getKarma().getVotesReceived() != 0)// prevetns division by
238
// zero.
239
user.getKarma().setKarmaPoints(user.getKarma().getTotalPoints() / user.getKarma().getVotesReceived());
240
241         this.getVotesGiven(user);
242
243         rs.close();
244         p.close();
245     }
246
247     private void getVotesGiven(User user) throws SQLException JavaDoc
248     {
249         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
250                 SystemGlobals.getSql("KarmaModel.getUserGivenVotes"));
251         p.setInt(1, user.getId());
252
253         ResultSet JavaDoc rs = p.executeQuery();
254
255         if (rs.next()) {
256             user.getKarma().setVotesGiven(rs.getInt("votes"));
257         }
258
259         rs.close();
260         p.close();
261     }
262
263     /**
264      * @see net.jforum.dao.KarmaDAO#getMostRatedUserByPeriod(java.util.Date, java.util.Date)
265      */

266     public List JavaDoc getMostRatedUserByPeriod(int start, Date JavaDoc firstPeriod, Date JavaDoc lastPeriod, String JavaDoc orderField)
267             throws Exception JavaDoc
268     {
269         String JavaDoc sql = SystemGlobals.getSql("KarmaModel.getMostRatedUserByPeriod");
270         String JavaDoc orderby = " ORDER BY " + orderField + " DESC";
271         sql += orderby;
272         
273         return this.getMostRatedUserByPeriod(sql, start, firstPeriod, lastPeriod, orderField);
274     }
275
276     /**
277      *
278      * @param sql
279      * @param start
280      * @param firstPeriod
281      * @param lastPeriod
282      * @param orderField
283      * @return
284      * @throws Exception
285      */

286     protected List JavaDoc getMostRatedUserByPeriod(String JavaDoc sql, int start, Date JavaDoc firstPeriod, Date JavaDoc lastPeriod, String JavaDoc orderField)
287             throws Exception JavaDoc
288     {
289         if (firstPeriod.after(lastPeriod)) {
290             throw new Exception JavaDoc("First Date needs to be before the Last Date");
291         }
292         
293         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(sql);
294         p.setTimestamp(1, new Timestamp JavaDoc(firstPeriod.getTime()));
295         p.setTimestamp(2, new Timestamp JavaDoc(lastPeriod.getTime()));
296
297         ResultSet JavaDoc rs = p.executeQuery();
298         List JavaDoc usersAndPoints = this.fillUser(rs);
299         rs.close();
300         p.close();
301
302         return usersAndPoints;
303     }
304
305     protected List JavaDoc fillUser(ResultSet JavaDoc rs) throws SQLException JavaDoc
306     {
307         List JavaDoc usersAndPoints = new ArrayList JavaDoc();
308         User user = null;
309         KarmaStatus karma = null;
310         while (rs.next()) {
311             user = new User();
312             karma = new KarmaStatus();
313             karma.setTotalPoints(rs.getInt("total"));
314             karma.setVotesReceived(rs.getInt("votes_received"));
315             karma.setKarmaPoints(rs.getDouble("user_karma"));
316             karma.setVotesGiven(rs.getInt("votes_given"));
317             user.setUsername(rs.getString("username"));
318             user.setId(rs.getInt("user_id"));
319             user.setKarma(karma);
320             usersAndPoints.add(user);
321         }
322         return usersAndPoints;
323     }
324 }
Popular Tags