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