/src/main/java/com/google/ie/business/service/impl/CommentVoteServiceImpl.java

http://thoughtsite.googlecode.com/ · Java · 216 lines · 121 code · 23 blank · 72 comment · 15 complexity · aa7553037ca4ec48d1eebf12391e7cbe MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.business.service.impl;
  16. import com.google.ie.business.dao.VoteDao;
  17. import com.google.ie.business.domain.Comment;
  18. import com.google.ie.business.domain.CommentVote;
  19. import com.google.ie.business.domain.IdeaComment;
  20. import com.google.ie.business.domain.IdeaVote;
  21. import com.google.ie.business.domain.User;
  22. import com.google.ie.business.domain.Vote;
  23. import com.google.ie.business.service.CommentService;
  24. import com.google.ie.business.service.ServiceConstants;
  25. import com.google.ie.business.service.ShardedCounterService;
  26. import com.google.ie.business.service.VoteService;
  27. import com.google.ie.common.audit.AuditManager;
  28. import com.google.ie.common.constants.IdeaExchangeConstants;
  29. import com.google.ie.common.constants.IdeaExchangeErrorCodes;
  30. import com.google.ie.common.exception.IdeasExchangeException;
  31. import org.apache.log4j.Logger;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Service;
  34. /**
  35. * A Service implementation of {@link VoteService}
  36. *
  37. * @author asirohi
  38. *
  39. */
  40. @Service
  41. public class CommentVoteServiceImpl implements VoteService {
  42. private static Logger log = Logger.getLogger(CommentVoteServiceImpl.class);
  43. @Autowired
  44. private VoteDao voteDao;
  45. @Autowired
  46. private AuditManager auditManager;
  47. @Autowired
  48. private CommentService commentService;
  49. @Autowired
  50. private ShardedCounterService shardedCounterService;
  51. @Override
  52. public Vote addVote(Vote vote, User user) throws IdeasExchangeException {
  53. if (vote != null && user != null) {
  54. CommentVote commentVote = (CommentVote) vote;
  55. log.info("Adding vote to a comment with key : " + commentVote.getCommentKey());
  56. /**
  57. * Called comment service to get Comment object containing the
  58. * provided key
  59. */
  60. IdeaComment comment = (IdeaComment) commentService.getCommentById(commentVote
  61. .getCommentKey());
  62. if (comment != null) {
  63. if (isUserAllowedToVote(user.getUserKey(), comment)) {
  64. commentVote = (CommentVote) voteDao.saveVote(commentVote);
  65. if (commentVote != null) {
  66. /** Audit user action of adding vote */
  67. auditManager.audit(user.getUserKey(), commentVote.getKey(),
  68. IdeaVote.class.getSimpleName(), ServiceConstants.SAVE);
  69. updateComment(commentVote, comment);
  70. shardPoints(commentVote, comment);
  71. log.info("Vote is successfully added for Comment");
  72. }
  73. return commentVote;
  74. }
  75. }
  76. }
  77. log.warn("Voting is failed for the Comment");
  78. return null;
  79. }
  80. /**
  81. * Update the comment object
  82. *
  83. * @param commentVote the {@link CommentVote} object to be updated
  84. * @param comment the {@link IdeaComment} object
  85. */
  86. private void updateComment(CommentVote commentVote, IdeaComment comment) {
  87. long newVotes;
  88. long totalVotes = comment.getTotalVotes();
  89. if (commentVote.isPositiveVote()) {
  90. newVotes = comment.getTotalPositiveVotes() + 1;
  91. comment.setTotalPositiveVotes(newVotes);
  92. } else {
  93. newVotes = comment.getTotalNegativeVotes() + 1;
  94. comment.setTotalNegativeVotes(newVotes);
  95. }
  96. comment.setTotalVotes(totalVotes + 1);
  97. commentService.updateComment(comment);
  98. }
  99. /**
  100. * Use Shard Counter to increment/decrement total
  101. * positive/negative votes of Comment entity and to
  102. * update total points of User entity.
  103. *
  104. * @param commentVote the {@link CommentVote} object for which the points
  105. * are to be sharded
  106. * @param commentOwnerKey the key of the owner of the comment
  107. */
  108. protected void shardPoints(CommentVote commentVote, IdeaComment comment) {
  109. if (commentVote.isPositiveVote()) {
  110. shardedCounterService.incrementPositivePoints(comment.getIdeaKey());
  111. shardedCounterService.incrementPositivePoints(commentVote
  112. .getCommentKey());
  113. shardedCounterService.updateTotalPoints(comment.getCreatorKey(),
  114. commentVote.getVotePoints());
  115. } else {
  116. shardedCounterService.incrementNegativePoints(comment.getIdeaKey());
  117. shardedCounterService.incrementNegativePoints(commentVote
  118. .getCommentKey());
  119. shardedCounterService.updateTotalPoints(comment.getCreatorKey(),
  120. ServiceConstants.MINUSONE * commentVote.getVotePoints());
  121. }
  122. }
  123. /**
  124. * Check whether a user has permissions to vote
  125. *
  126. * @param userKey the key of the user
  127. * @param comment the comment on which the user wants to vote
  128. * @return boolean specifying the permission
  129. * @throws IdeasExchangeException
  130. */
  131. protected boolean isUserAllowedToVote(String userKey, Comment comment)
  132. throws IdeasExchangeException {
  133. if (!userKey.equals(comment.getCreatorKey())) {
  134. if (!voteDao.isCommentAlreadyVotedByUser(userKey, comment.getKey())) {
  135. return true;
  136. }
  137. throw new IdeasExchangeException(IdeaExchangeErrorCodes.REPEAT_VOTE_EXCEPTION,
  138. IdeaExchangeConstants.Messages.REPEAT_VOTE_MESSAGE);
  139. }
  140. log.warn("User is not allowed to vote on his own idea or on Idea" +
  141. " for which he/she has already voted");
  142. throw new IdeasExchangeException(IdeaExchangeErrorCodes.OWNER_VOTE_EXCEPTION,
  143. IdeaExchangeConstants.Messages.OWNER_VOTE_MESSAGE);
  144. }
  145. /**
  146. * @return the voteDao
  147. */
  148. public VoteDao getVoteDao() {
  149. return voteDao;
  150. }
  151. /**
  152. * @param voteDao the voteDao to set
  153. */
  154. public void setVoteDao(VoteDao voteDao) {
  155. this.voteDao = voteDao;
  156. }
  157. /**
  158. * @return the auditManager
  159. */
  160. public AuditManager getAuditManager() {
  161. return auditManager;
  162. }
  163. /**
  164. * @param auditManager the auditManager to set
  165. */
  166. public void setAuditManager(AuditManager auditManager) {
  167. this.auditManager = auditManager;
  168. }
  169. /**
  170. * @return the commentService
  171. */
  172. public CommentService getCommentService() {
  173. return commentService;
  174. }
  175. /**
  176. * @param commentService the commentService to set
  177. */
  178. public void setCommentService(CommentService commentService) {
  179. this.commentService = commentService;
  180. }
  181. /**
  182. * @return the shardedCounterService
  183. */
  184. public ShardedCounterService getShardedCounterService() {
  185. return shardedCounterService;
  186. }
  187. /**
  188. * @param shardedCounterService the shardedCounterService to set
  189. */
  190. public void setShardedCounterService(ShardedCounterService shardedCounterService) {
  191. this.shardedCounterService = shardedCounterService;
  192. }
  193. }