001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.service.ServiceContext;
020    import com.liferay.portlet.polls.DuplicateVoteException;
021    import com.liferay.portlet.polls.NoSuchQuestionException;
022    import com.liferay.portlet.polls.QuestionExpiredException;
023    import com.liferay.portlet.polls.model.PollsChoice;
024    import com.liferay.portlet.polls.model.PollsQuestion;
025    import com.liferay.portlet.polls.model.PollsVote;
026    import com.liferay.portlet.polls.service.base.PollsVoteLocalServiceBaseImpl;
027    
028    import java.util.Date;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
035    
036            public PollsVote addVote(
037                            long userId, long questionId, long choiceId,
038                            ServiceContext serviceContext)
039                    throws PortalException, SystemException {
040    
041                    // Choice
042    
043                    Date now = new Date();
044    
045                    PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
046    
047                    if (choice.getQuestionId() != questionId) {
048                            throw new NoSuchQuestionException();
049                    }
050    
051                    // Question
052    
053                    PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
054                            questionId);
055    
056                    if (question.isExpired()) {
057                            throw new QuestionExpiredException();
058                    }
059    
060                    question.setLastVoteDate(serviceContext.getCreateDate(now));
061    
062                    pollsQuestionPersistence.update(question, false);
063    
064                    // Vote
065    
066                    PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
067    
068                    if (vote != null) {
069                            throw new DuplicateVoteException();
070                    }
071                    else {
072                            long voteId = counterLocalService.increment();
073    
074                            vote = pollsVotePersistence.create(voteId);
075    
076                            vote.setUserId(userId);
077                            vote.setQuestionId(questionId);
078                            vote.setChoiceId(choiceId);
079                            vote.setVoteDate(serviceContext.getCreateDate(now));
080    
081                            pollsVotePersistence.update(vote, false);
082                    }
083    
084                    return vote;
085            }
086    
087            public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
088                    throws SystemException {
089    
090                    return pollsVotePersistence.findByChoiceId(choiceId,  start, end);
091            }
092    
093            public int getChoiceVotesCount(long choiceId) throws SystemException {
094                    return pollsVotePersistence.countByChoiceId(choiceId);
095            }
096    
097            public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
098                    throws SystemException {
099    
100                    return pollsVotePersistence.findByQuestionId(questionId, start, end);
101            }
102    
103            public int getQuestionVotesCount(long questionId) throws SystemException {
104                    return pollsVotePersistence.countByQuestionId(questionId);
105            }
106    
107            public PollsVote getVote(long questionId, long userId)
108                    throws PortalException, SystemException {
109    
110                    return pollsVotePersistence.findByQ_U(questionId, userId);
111            }
112    
113    }