001    /**
002     * Copyright (c) 2000-2012 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.NoSuchUserException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.polls.DuplicateVoteException;
023    import com.liferay.portlet.polls.NoSuchQuestionException;
024    import com.liferay.portlet.polls.QuestionExpiredException;
025    import com.liferay.portlet.polls.model.PollsChoice;
026    import com.liferay.portlet.polls.model.PollsQuestion;
027    import com.liferay.portlet.polls.model.PollsVote;
028    import com.liferay.portlet.polls.service.base.PollsVoteLocalServiceBaseImpl;
029    
030    import java.util.Date;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Mate Thurzo
036     */
037    public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
038    
039            public PollsVote addVote(
040                            long userId, long questionId, long choiceId,
041                            ServiceContext serviceContext)
042                    throws PortalException, SystemException {
043    
044                    // Choice
045    
046                    Date now = new Date();
047    
048                    PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
049    
050                    if (choice.getQuestionId() != questionId) {
051                            throw new NoSuchQuestionException();
052                    }
053    
054                    // Question
055    
056                    PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
057                            questionId);
058    
059                    if (question.isExpired(serviceContext, now)) {
060                            throw new QuestionExpiredException();
061                    }
062    
063                    question.setLastVoteDate(serviceContext.getCreateDate(now));
064    
065                    pollsQuestionPersistence.update(question, false);
066    
067                    // Vote
068    
069                    PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
070    
071                    if (vote != null) {
072                            throw new DuplicateVoteException();
073                    }
074                    else {
075                            String userName = null;
076    
077                            try {
078                                    User user = userPersistence.findByPrimaryKey(userId);
079    
080                                    userName = user.getFullName();
081                            }
082                            catch (NoSuchUserException nsue) {
083                                    userName = serviceContext.translate("anonymous");
084                            }
085    
086                            long voteId = counterLocalService.increment();
087    
088                            vote = pollsVotePersistence.create(voteId);
089    
090                            vote.setCompanyId(serviceContext.getCompanyId());
091                            vote.setUserId(userId);
092                            vote.setUserName(userName);
093                            vote.setCreateDate(serviceContext.getCreateDate(now));
094                            vote.setModifiedDate(serviceContext.getModifiedDate(now));
095                            vote.setQuestionId(questionId);
096                            vote.setChoiceId(choiceId);
097                            vote.setVoteDate(serviceContext.getCreateDate(now));
098    
099                            pollsVotePersistence.update(vote, false);
100                    }
101    
102                    return vote;
103            }
104    
105            public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
106                    throws SystemException {
107    
108                    return pollsVotePersistence.findByChoiceId(choiceId, start, end);
109            }
110    
111            public int getChoiceVotesCount(long choiceId) throws SystemException {
112                    return pollsVotePersistence.countByChoiceId(choiceId);
113            }
114    
115            public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
116                    throws SystemException {
117    
118                    return pollsVotePersistence.findByQuestionId(questionId, start, end);
119            }
120    
121            public int getQuestionVotesCount(long questionId) throws SystemException {
122                    return pollsVotePersistence.countByQuestionId(questionId);
123            }
124    
125            public PollsVote getVote(long questionId, long userId)
126                    throws PortalException, SystemException {
127    
128                    return pollsVotePersistence.findByQ_U(questionId, userId);
129            }
130    
131    }