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