001
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
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
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
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
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 }