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