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
44 public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
45
46 public PollsVote addVote(long userId, long questionId, long choiceId)
47 throws PortalException, SystemException {
48
49
51 Date now = new Date();
52
53 PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
54
55 if (choice.getQuestionId() != questionId) {
56 throw new NoSuchQuestionException();
57 }
58
59
61 PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
62 questionId);
63
64 if (question.isExpired()) {
65 throw new QuestionExpiredException();
66 }
67
68 question.setLastVoteDate(now);
69
70 pollsQuestionPersistence.update(question, false);
71
72
74 PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
75
76 if (vote != null) {
77 throw new DuplicateVoteException();
78 }
79 else {
80 long voteId = counterLocalService.increment();
81
82 vote = pollsVotePersistence.create(voteId);
83
84 vote.setUserId(userId);
85 vote.setQuestionId(questionId);
86 vote.setChoiceId(choiceId);
87 vote.setVoteDate(now);
88
89 pollsVotePersistence.update(vote, false);
90 }
91
92 return vote;
93 }
94
95 public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
96 throws SystemException {
97
98 return pollsVotePersistence.findByChoiceId(choiceId, start, end);
99 }
100
101 public int getChoiceVotesCount(long choiceId) throws SystemException {
102 return pollsVotePersistence.countByChoiceId(choiceId);
103 }
104
105 public PollsVote getVote(long questionId, long userId)
106 throws PortalException, SystemException {
107
108 return pollsVotePersistence.findByQ_U(questionId, userId);
109 }
110
111 public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
112 throws SystemException {
113
114 return pollsVotePersistence.findByQuestionId(questionId, start, end);
115 }
116
117 public int getQuestionVotesCount(long questionId) throws SystemException {
118 return pollsVotePersistence.countByQuestionId(questionId);
119 }
120
121 }