001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.polls.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.portlet.polls.DuplicateVoteException;
022    import com.liferay.portlet.polls.NoSuchQuestionException;
023    import com.liferay.portlet.polls.QuestionExpiredException;
024    import com.liferay.portlet.polls.model.PollsChoice;
025    import com.liferay.portlet.polls.model.PollsQuestion;
026    import com.liferay.portlet.polls.model.PollsVote;
027    import com.liferay.portlet.polls.service.base.PollsVoteLocalServiceBaseImpl;
028    
029    import java.util.Date;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
036    
037            public PollsVote addVote(
038                            long userId, long questionId, long choiceId,
039                            ServiceContext serviceContext)
040                    throws PortalException, SystemException {
041    
042                    // Choice
043    
044                    Date now = new Date();
045    
046                    PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
047    
048                    if (choice.getQuestionId() != questionId) {
049                            throw new NoSuchQuestionException();
050                    }
051    
052                    // Question
053    
054                    PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
055                            questionId);
056    
057                    if (question.isExpired(serviceContext, now)) {
058                            throw new QuestionExpiredException();
059                    }
060    
061                    question.setLastVoteDate(serviceContext.getCreateDate(now));
062    
063                    pollsQuestionPersistence.update(question, false);
064    
065                    // Vote
066    
067                    PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
068    
069                    if (vote != null) {
070                            throw new DuplicateVoteException();
071                    }
072                    else {
073                            User user = userPersistence.findByPrimaryKey(userId);
074    
075                            long voteId = counterLocalService.increment();
076    
077                            vote = pollsVotePersistence.create(voteId);
078    
079                            vote.setCompanyId(user.getCompanyId());
080                            vote.setUserId(user.getUserId());
081                            vote.setUserName(user.getFullName());
082                            vote.setCreateDate(serviceContext.getCreateDate(now));
083                            vote.setModifiedDate(serviceContext.getModifiedDate(now));
084                            vote.setQuestionId(questionId);
085                            vote.setChoiceId(choiceId);
086                            vote.setVoteDate(serviceContext.getCreateDate(now));
087    
088                            pollsVotePersistence.update(vote, false);
089                    }
090    
091                    return vote;
092            }
093    
094            public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
095                    throws SystemException {
096    
097                    return pollsVotePersistence.findByChoiceId(choiceId,  start, end);
098            }
099    
100            public int getChoiceVotesCount(long choiceId) throws SystemException {
101                    return pollsVotePersistence.countByChoiceId(choiceId);
102            }
103    
104            public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
105                    throws SystemException {
106    
107                    return pollsVotePersistence.findByQuestionId(questionId, start, end);
108            }
109    
110            public int getQuestionVotesCount(long questionId) throws SystemException {
111                    return pollsVotePersistence.countByQuestionId(questionId);
112            }
113    
114            public PollsVote getVote(long questionId, long userId)
115                    throws PortalException, SystemException {
116    
117                    return pollsVotePersistence.findByQ_U(questionId, userId);
118            }
119    
120    }