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