1
14
15 package com.liferay.portlet.polls.service.impl;
16
17 import com.liferay.portal.PortalException;
18 import com.liferay.portal.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
41 public PollsVote addVote(long userId, long questionId, long choiceId)
42 throws PortalException, SystemException {
43
44 return addVote(userId, questionId, choiceId, new ServiceContext());
45 }
46
47 public PollsVote addVote(
48 long userId, long questionId, long choiceId,
49 ServiceContext serviceContext)
50 throws PortalException, SystemException {
51
52
54 Date now = new Date();
55
56 PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
57
58 if (choice.getQuestionId() != questionId) {
59 throw new NoSuchQuestionException();
60 }
61
62
64 PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
65 questionId);
66
67 if (question.isExpired()) {
68 throw new QuestionExpiredException();
69 }
70
71 question.setLastVoteDate(serviceContext.getCreateDate(now));
72
73 pollsQuestionPersistence.update(question, false);
74
75
77 PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
78
79 if (vote != null) {
80 throw new DuplicateVoteException();
81 }
82 else {
83 long voteId = counterLocalService.increment();
84
85 vote = pollsVotePersistence.create(voteId);
86
87 vote.setUserId(userId);
88 vote.setQuestionId(questionId);
89 vote.setChoiceId(choiceId);
90 vote.setVoteDate(serviceContext.getCreateDate(now));
91
92 pollsVotePersistence.update(vote, false);
93 }
94
95 return vote;
96 }
97
98 public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
99 throws SystemException {
100
101 return pollsVotePersistence.findByChoiceId(choiceId, start, end);
102 }
103
104 public int getChoiceVotesCount(long choiceId) throws SystemException {
105 return pollsVotePersistence.countByChoiceId(choiceId);
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 public PollsVote getVote(long questionId, long userId)
119 throws PortalException, SystemException {
120
121 return pollsVotePersistence.findByQ_U(questionId, userId);
122 }
123
124 }