001
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
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
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 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.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 public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
108 throws SystemException {
109
110 return pollsVotePersistence.findByChoiceId(choiceId, start, end);
111 }
112
113 public int getChoiceVotesCount(long choiceId) throws SystemException {
114 return pollsVotePersistence.countByChoiceId(choiceId);
115 }
116
117 public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
118 throws SystemException {
119
120 return pollsVotePersistence.findByQuestionId(questionId, start, end);
121 }
122
123 public int getQuestionVotesCount(long questionId) throws SystemException {
124 return pollsVotePersistence.countByQuestionId(questionId);
125 }
126
127 public PollsVote getVote(long questionId, long userId)
128 throws PortalException, SystemException {
129
130 return pollsVotePersistence.findByQ_U(questionId, userId);
131 }
132
133 }