001    /**
002     * Copyright (c) 2000-2013 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.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     * @author Mate Thurzo
035     */
036    public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
037    
038            @Override
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);
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                            User user = userPersistence.fetchByPrimaryKey(userId);
078    
079                            if (user != null) {
080                                    userName = user.getFullName();
081                            }
082                            else {
083                                    userName = serviceContext.translate("anonymous");
084                            }
085    
086                            long voteId = counterLocalService.increment();
087    
088                            vote = pollsVotePersistence.create(voteId);
089    
090                            vote.setUuid(serviceContext.getUuid());
091                            vote.setGroupId(serviceContext.getScopeGroupId());
092                            vote.setCompanyId(serviceContext.getCompanyId());
093                            vote.setUserId(userId);
094                            vote.setUserName(userName);
095                            vote.setCreateDate(serviceContext.getCreateDate(now));
096                            vote.setModifiedDate(serviceContext.getModifiedDate(now));
097                            vote.setQuestionId(questionId);
098                            vote.setChoiceId(choiceId);
099                            vote.setVoteDate(serviceContext.getCreateDate(now));
100    
101                            pollsVotePersistence.update(vote);
102                    }
103    
104                    return vote;
105            }
106    
107            @Override
108            public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
109                    throws SystemException {
110    
111                    return pollsVotePersistence.findByChoiceId(choiceId, start, end);
112            }
113    
114            @Override
115            public int getChoiceVotesCount(long choiceId) throws SystemException {
116                    return pollsVotePersistence.countByChoiceId(choiceId);
117            }
118    
119            @Override
120            public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
121                    throws SystemException {
122    
123                    return pollsVotePersistence.findByQuestionId(questionId, start, end);
124            }
125    
126            @Override
127            public int getQuestionVotesCount(long questionId) throws SystemException {
128                    return pollsVotePersistence.countByQuestionId(questionId);
129            }
130    
131            @Override
132            public PollsVote getVote(long questionId, long userId)
133                    throws PortalException, SystemException {
134    
135                    return pollsVotePersistence.findByQ_U(questionId, userId);
136            }
137    
138    }