1
22
23 package com.liferay.portlet.polls.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portlet.polls.DuplicateVoteException;
28 import com.liferay.portlet.polls.NoSuchQuestionException;
29 import com.liferay.portlet.polls.QuestionExpiredException;
30 import com.liferay.portlet.polls.model.PollsChoice;
31 import com.liferay.portlet.polls.model.PollsQuestion;
32 import com.liferay.portlet.polls.model.PollsVote;
33 import com.liferay.portlet.polls.service.base.PollsVoteLocalServiceBaseImpl;
34
35 import java.util.Date;
36 import java.util.List;
37
38
43 public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
44
45 public PollsVote addVote(long userId, long questionId, long choiceId)
46 throws PortalException, SystemException {
47
48
50 Date now = new Date();
51
52 PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
53
54 if (choice.getQuestionId() != questionId) {
55 throw new NoSuchQuestionException();
56 }
57
58
60 PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
61 questionId);
62
63 if (question.isExpired()) {
64 throw new QuestionExpiredException();
65 }
66
67 question.setLastVoteDate(now);
68
69 pollsQuestionPersistence.update(question, false);
70
71
73 PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
74
75 if (vote != null) {
76 throw new DuplicateVoteException();
77 }
78 else {
79 long voteId = counterLocalService.increment();
80
81 vote = pollsVotePersistence.create(voteId);
82
83 vote.setUserId(userId);
84 vote.setQuestionId(questionId);
85 vote.setChoiceId(choiceId);
86 vote.setVoteDate(now);
87
88 pollsVotePersistence.update(vote, false);
89 }
90
91 return vote;
92 }
93
94 public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
95 throws SystemException {
96
97 return pollsVotePersistence.findByChoiceId(choiceId, start, end);
98 }
99
100 public int getChoiceVotesCount(long choiceId) throws SystemException {
101 return pollsVotePersistence.countByChoiceId(choiceId);
102 }
103
104 public PollsVote getVote(long questionId, long userId)
105 throws PortalException, SystemException {
106
107 return pollsVotePersistence.findByQ_U(questionId, userId);
108 }
109
110 public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
111 throws SystemException {
112
113 return pollsVotePersistence.findByQuestionId(questionId, start, end);
114 }
115
116 public int getQuestionVotesCount(long questionId) throws SystemException {
117 return pollsVotePersistence.countByQuestionId(questionId);
118 }
119
120 }