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