1 43 package net.jforum.dao.generic; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.sql.SQLException ; 48 import java.sql.Timestamp ; 49 import java.util.ArrayList ; 50 import java.util.Date ; 51 import java.util.HashMap ; 52 import java.util.List ; 53 import java.util.Map ; 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 65 public class GenericKarmaDAO implements net.jforum.dao.KarmaDAO 66 { 67 70 public void addKarma(Karma karma) throws Exception 71 { 72 PreparedStatement 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 ((new Date ()).getTime())); 79 p.executeUpdate(); 80 p.close(); 81 82 this.updateUserKarma(karma.getPostUserId()); 83 } 84 85 88 public KarmaStatus getUserKarma(int userId) throws Exception 89 { 90 KarmaStatus status = new KarmaStatus(); 91 92 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.getUserKarma")); 93 p.setInt(1, userId); 94 95 ResultSet 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 109 public void updateUserKarma(int userId) throws Exception 110 { 111 PreparedStatement 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 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 147 public void update(Karma karma) throws Exception 148 { 149 PreparedStatement 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 159 public KarmaStatus getPostKarma(int postId) throws Exception 160 { 161 KarmaStatus karma = new KarmaStatus(); 162 163 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.getPostKarma")); 164 p.setInt(1, postId); 165 166 ResultSet 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 180 public boolean userCanAddKarma(int userId, int postId) throws Exception 181 { 182 boolean status = true; 183 184 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 185 SystemGlobals.getSql("KarmaModel.userCanAddKarma")); 186 p.setInt(1, postId); 187 p.setInt(2, userId); 188 189 ResultSet 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 203 public Map getUserVotes(int topicId, int userId) throws Exception 204 { 205 Map m = new HashMap (); 206 207 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.getUserVotes")); 208 p.setInt(1, topicId); 209 p.setInt(2, userId); 210 211 ResultSet rs = p.executeQuery(); 212 while (rs.next()) { 213 m.put(new Integer (rs.getInt("post_id")), new Integer (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 223 { 224 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 225 SystemGlobals.getSql("KarmaModel.getUserTotalVotes")); 226 p.setInt(1, user.getId()); 227 228 ResultSet 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) 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 248 { 249 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 250 SystemGlobals.getSql("KarmaModel.getUserGivenVotes")); 251 p.setInt(1, user.getId()); 252 253 ResultSet 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 266 public List getMostRatedUserByPeriod(int start, Date firstPeriod, Date lastPeriod, String orderField) 267 throws Exception 268 { 269 String sql = SystemGlobals.getSql("KarmaModel.getMostRatedUserByPeriod"); 270 String orderby = " ORDER BY " + orderField + " DESC"; 271 sql += orderby; 272 273 return this.getMostRatedUserByPeriod(sql, start, firstPeriod, lastPeriod, orderField); 274 } 275 276 286 protected List getMostRatedUserByPeriod(String sql, int start, Date firstPeriod, Date lastPeriod, String orderField) 287 throws Exception 288 { 289 if (firstPeriod.after(lastPeriod)) { 290 throw new Exception ("First Date needs to be before the Last Date"); 291 } 292 293 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(sql); 294 p.setTimestamp(1, new Timestamp (firstPeriod.getTime())); 295 p.setTimestamp(2, new Timestamp (lastPeriod.getTime())); 296 297 ResultSet rs = p.executeQuery(); 298 List usersAndPoints = this.fillUser(rs); 299 rs.close(); 300 p.close(); 301 302 return usersAndPoints; 303 } 304 305 protected List fillUser(ResultSet rs) throws SQLException 306 { 307 List usersAndPoints = new ArrayList (); 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 |