GenericKarmaDAO.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 10k
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 Jan 11, 2005 11:22:19 PM
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum.dao.generic;
  44. import java.sql.PreparedStatement;
  45. import java.sql.ResultSet;
  46. import java.sql.SQLException;
  47. import java.sql.Timestamp;
  48. import java.util.ArrayList;
  49. import java.util.Date;
  50. import java.util.HashMap;
  51. import java.util.List;
  52. import java.util.Map;
  53. import net.jforum.JForumExecutionContext;
  54. import net.jforum.entities.Karma;
  55. import net.jforum.entities.KarmaStatus;
  56. import net.jforum.entities.User;
  57. import net.jforum.exceptions.DatabaseException;
  58. import net.jforum.util.DbUtils;
  59. import net.jforum.util.preferences.SystemGlobals;
  60. /**
  61.  * @author Rafael Steil
  62.  * @version $Id: GenericKarmaDAO.java,v 1.12 2007/08/01 22:30:03 rafaelsteil Exp $
  63.  */
  64. public class GenericKarmaDAO implements net.jforum.dao.KarmaDAO
  65. {
  66. /**
  67.  * @see net.jforum.dao.KarmaDAO#addKarma(net.jforum.entities.Karma)
  68.  */
  69. public void addKarma(Karma karma)
  70. {
  71. PreparedStatement p = null;
  72. try {
  73. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.add"));
  74. p.setInt(1, karma.getPostId());
  75. p.setInt(2, karma.getPostUserId());
  76. p.setInt(3, karma.getFromUserId());
  77. p.setInt(4, karma.getPoints());
  78. p.setInt(5, karma.getTopicId());
  79. p.setTimestamp(6, new Timestamp((new Date()).getTime()));
  80. p.executeUpdate();
  81. this.updateUserKarma(karma.getPostUserId());
  82. }
  83. catch (SQLException e) {
  84. throw new DatabaseException(e);
  85. }
  86. finally {
  87. DbUtils.close(p);
  88. }
  89. }
  90. /**
  91.  * @see net.jforum.dao.KarmaDAO#selectKarmaStatus(int)
  92.  */
  93. public KarmaStatus getUserKarma(int userId)
  94. {
  95. KarmaStatus status = new KarmaStatus();
  96. PreparedStatement p = null;
  97. ResultSet rs = null;
  98. try {
  99. p = JForumExecutionContext.getConnection()
  100. .prepareStatement(SystemGlobals.getSql("KarmaModel.getUserKarma"));
  101. p.setInt(1, userId);
  102. rs = p.executeQuery();
  103. if (rs.next()) {
  104. status.setKarmaPoints(Math.round(rs.getDouble("user_karma")));
  105. }
  106. return status;
  107. }
  108. catch (SQLException e) {
  109. throw new DatabaseException(e);
  110. }
  111. finally {
  112. DbUtils.close(rs, p);
  113. }
  114. }
  115. /**
  116.  * @see net.jforum.dao.KarmaDAO#updateUserKarma(int)
  117.  */
  118. public void updateUserKarma(int userId)
  119. {
  120. PreparedStatement p = null;
  121. ResultSet rs = null;
  122. try {
  123. p = JForumExecutionContext.getConnection().prepareStatement(
  124. SystemGlobals.getSql("KarmaModel.getUserKarmaPoints"));
  125. p.setInt(1, userId);
  126. int totalRecords = 0;
  127. double totalPoints = 0;
  128. rs = p.executeQuery();
  129. while (rs.next()) {
  130. int points = rs.getInt("points");
  131. int votes = rs.getInt("votes");
  132. totalPoints += ((double) points / votes);
  133. totalRecords++;
  134. }
  135. rs.close();
  136. rs = null;
  137. p.close();
  138. p = null;
  139. p = JForumExecutionContext.getConnection().prepareStatement(
  140. SystemGlobals.getSql("KarmaModel.updateUserKarma"));
  141. double karmaPoints = totalPoints / totalRecords;
  142. if (Double.isNaN(karmaPoints)) {
  143. karmaPoints = 0;
  144. }
  145. p.setDouble(1, karmaPoints);
  146. p.setInt(2, userId);
  147. p.executeUpdate();
  148. }
  149. catch (SQLException e) {
  150. throw new DatabaseException(e);
  151. }
  152. finally {
  153. DbUtils.close(rs, p);
  154. }
  155. }
  156. /**
  157.  * @see net.jforum.dao.KarmaDAO#update(net.jforum.entities.Karma)
  158.  */
  159. public void update(Karma karma)
  160. {
  161. PreparedStatement p = null;
  162. try {
  163. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.update"));
  164. p.setInt(1, karma.getPoints());
  165. p.setInt(2, karma.getId());
  166. p.executeUpdate();
  167. }
  168. catch (SQLException e) {
  169. throw new DatabaseException(e);
  170. }
  171. finally {
  172. DbUtils.close(p);
  173. }
  174. }
  175. /**
  176.  * @see net.jforum.dao.KarmaDAO#getPostKarma(int)
  177.  */
  178. public KarmaStatus getPostKarma(int postId)
  179. {
  180. KarmaStatus karma = new KarmaStatus();
  181. PreparedStatement p = null;
  182. ResultSet rs = null;
  183. try {
  184. p = JForumExecutionContext.getConnection()
  185. .prepareStatement(SystemGlobals.getSql("KarmaModel.getPostKarma"));
  186. p.setInt(1, postId);
  187. rs = p.executeQuery();
  188. if (rs.next()) {
  189. karma.setKarmaPoints(Math.round(rs.getDouble(1)));
  190. }
  191. return karma;
  192. }
  193. catch (SQLException e) {
  194. throw new DatabaseException(e);
  195. }
  196. finally {
  197. DbUtils.close(rs, p);
  198. }
  199. }
  200. /**
  201.  * @see net.jforum.dao.KarmaDAO#userCanAddKarma(int, int)
  202.  */
  203. public boolean userCanAddKarma(int userId, int postId)
  204. {
  205. boolean status = true;
  206. PreparedStatement p = null;
  207. ResultSet rs = null;
  208. try {
  209. p = JForumExecutionContext.getConnection().prepareStatement(
  210. SystemGlobals.getSql("KarmaModel.userCanAddKarma"));
  211. p.setInt(1, postId);
  212. p.setInt(2, userId);
  213. rs = p.executeQuery();
  214. if (rs.next()) {
  215. status = rs.getInt(1) < 1;
  216. }
  217. return status;
  218. }
  219. catch (SQLException e) {
  220. throw new DatabaseException(e);
  221. }
  222. finally {
  223. DbUtils.close(rs, p);
  224. }
  225. }
  226. /**
  227.  * @see net.jforum.dao.KarmaDAO#getUserVotes(int, int)
  228.  */
  229. public Map getUserVotes(int topicId, int userId)
  230. {
  231. Map m = new HashMap();
  232. PreparedStatement p = null;
  233. ResultSet rs = null;
  234. try {
  235. p = JForumExecutionContext.getConnection()
  236. .prepareStatement(SystemGlobals.getSql("KarmaModel.getUserVotes"));
  237. p.setInt(1, topicId);
  238. p.setInt(2, userId);
  239. rs = p.executeQuery();
  240. while (rs.next()) {
  241. m.put(new Integer(rs.getInt("post_id")), new Integer(rs.getInt("points")));
  242. }
  243. return m;
  244. }
  245. catch (SQLException e) {
  246. throw new DatabaseException(e);
  247. }
  248. finally {
  249. DbUtils.close(rs, p);
  250. }
  251. }
  252. public void getUserTotalKarma(User user)
  253. {
  254. PreparedStatement p = null;
  255. ResultSet rs = null;
  256. try {
  257. p = JForumExecutionContext.getConnection().prepareStatement(
  258. SystemGlobals.getSql("KarmaModel.getUserTotalVotes"));
  259. p.setInt(1, user.getId());
  260. rs = p.executeQuery();
  261. user.setKarma(new KarmaStatus());
  262. if (rs.next()) {
  263. user.getKarma().setTotalPoints(rs.getInt("points"));
  264. user.getKarma().setVotesReceived(rs.getInt("votes"));
  265. }
  266. if (user.getKarma().getVotesReceived() != 0)// prevetns division by
  267. // zero.
  268. user.getKarma().setKarmaPoints(user.getKarma().getTotalPoints() / user.getKarma().getVotesReceived());
  269. this.getVotesGiven(user);
  270. }
  271. catch (SQLException e) {
  272. throw new DatabaseException(e);
  273. }
  274. finally {
  275. DbUtils.close(rs, p);
  276. }
  277. }
  278. private void getVotesGiven(User user)
  279. {
  280. PreparedStatement p = null;
  281. ResultSet rs = null;
  282. try {
  283. p = JForumExecutionContext.getConnection().prepareStatement(
  284. SystemGlobals.getSql("KarmaModel.getUserGivenVotes"));
  285. p.setInt(1, user.getId());
  286. rs = p.executeQuery();
  287. if (rs.next()) {
  288. user.getKarma().setVotesGiven(rs.getInt("votes"));
  289. }
  290. }
  291. catch (SQLException e) {
  292. throw new DatabaseException(e);
  293. }
  294. finally {
  295. DbUtils.close(rs, p);
  296. }
  297. }
  298. /**
  299.  * @see net.jforum.dao.KarmaDAO#getMostRatedUserByPeriod(int, java.util.Date, java.util.Date,
  300.  *      String)
  301.  */
  302. public List getMostRatedUserByPeriod(int start, Date firstPeriod, Date lastPeriod, String orderField)
  303. {
  304. String sql = SystemGlobals.getSql("KarmaModel.getMostRatedUserByPeriod");
  305. sql += " ORDER BY " + orderField + " DESC";
  306. return this.getMostRatedUserByPeriod(sql, firstPeriod, lastPeriod);
  307. }
  308. /**
  309.  * 
  310.  * @param sql String
  311.  * @param firstPeriod Date
  312.  * @param lastPeriod Date
  313.  * @return List
  314.  */
  315. protected List getMostRatedUserByPeriod(String sql, Date firstPeriod, Date lastPeriod)
  316. {
  317. if (firstPeriod.after(lastPeriod)) {
  318. throw new DatabaseException("First Date needs to be before the Last Date");
  319. }
  320. PreparedStatement p = null;
  321. ResultSet rs = null;
  322. try {
  323. p = JForumExecutionContext.getConnection().prepareStatement(sql);
  324. p.setTimestamp(1, new Timestamp(firstPeriod.getTime()));
  325. p.setTimestamp(2, new Timestamp(lastPeriod.getTime()));
  326. rs = p.executeQuery();
  327. return this.fillUser(rs);
  328. }
  329. catch (SQLException e) {
  330. throw new DatabaseException(e);
  331. }
  332. finally {
  333. DbUtils.close(rs, p);
  334. }
  335. }
  336. protected List fillUser(ResultSet rs) throws SQLException
  337. {
  338. List usersAndPoints = new ArrayList();
  339. KarmaStatus karma = null;
  340. while (rs.next()) {
  341. User user = new User();
  342. karma = new KarmaStatus();
  343. karma.setTotalPoints(rs.getInt("total"));
  344. karma.setVotesReceived(rs.getInt("votes_received"));
  345. karma.setKarmaPoints(rs.getDouble("user_karma"));
  346. karma.setVotesGiven(rs.getInt("votes_given"));
  347. user.setUsername(rs.getString("username"));
  348. user.setId(rs.getInt("user_id"));
  349. user.setKarma(karma);
  350. usersAndPoints.add(user);
  351. }
  352. return usersAndPoints;
  353. }
  354. }