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.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            @Override
040            public PollsVote addVote(
041                            long userId, long questionId, long choiceId,
042                            ServiceContext serviceContext)
043                    throws PortalException, SystemException {
044    
045                    // Choice
046    
047                    Date now = new Date();
048    
049                    PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
050    
051                    if (choice.getQuestionId() != questionId) {
052                            throw new NoSuchQuestionException();
053                    }
054    
055                    // Question
056    
057                    PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
058                            questionId);
059    
060                    if (question.isExpired(serviceContext, now)) {
061                            throw new QuestionExpiredException();
062                    }
063    
064                    question.setLastVoteDate(serviceContext.getCreateDate(now));
065    
066                    pollsQuestionPersistence.update(question, false);
067    
068                    // Vote
069    
070                    PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
071    
072                    if (vote != null) {
073                            throw new DuplicateVoteException();
074                    }
075                    else {
076                            String userName = null;
077    
078                            try {
079                                    User user = userPersistence.findByPrimaryKey(userId);
080    
081                                    userName = user.getFullName();
082                            }
083                            catch (NoSuchUserException nsue) {
084                                    userName = serviceContext.translate("anonymous");
085                            }
086    
087                            long voteId = counterLocalService.increment();
088    
089                            vote = pollsVotePersistence.create(voteId);
090    
091                            vote.setCompanyId(serviceContext.getCompanyId());
092                            vote.setUserId(userId);
093                            vote.setUserName(userName);
094                            vote.setCreateDate(serviceContext.getCreateDate(now));
095                            vote.setModifiedDate(serviceContext.getModifiedDate(now));
096                            vote.setQuestionId(questionId);
097                            vote.setChoiceId(choiceId);
098                            vote.setVoteDate(serviceContext.getCreateDate(now));
099    
100                            pollsVotePersistence.update(vote, false);
101                    }
102    
103                    return vote;
104            }
105    
106            @Override
107            public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
108                    throws SystemException {
109    
110                    return pollsVotePersistence.findByChoiceId(choiceId, start, end);
111            }
112    
113            @Override
114            public int getChoiceVotesCount(long choiceId) throws SystemException {
115                    return pollsVotePersistence.countByChoiceId(choiceId);
116            }
117    
118            @Override
119            public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
120                    throws SystemException {
121    
122                    return pollsVotePersistence.findByQuestionId(questionId, start, end);
123            }
124    
125            @Override
126            public int getQuestionVotesCount(long questionId) throws SystemException {
127                    return pollsVotePersistence.countByQuestionId(questionId);
128            }
129    
130            @Override
131            public PollsVote getVote(long questionId, long userId)
132                    throws PortalException, SystemException {
133    
134                    return pollsVotePersistence.findByQ_U(questionId, userId);
135            }
136    
137    }