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