PageRenderTime 345ms CodeModel.GetById 131ms app.highlight 118ms RepoModel.GetById 93ms app.codeStats 0ms

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