1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
31   * <a href="PollsVoteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
36  
37      public PollsVote addVote(long userId, long questionId, long choiceId)
38          throws PortalException, SystemException {
39  
40          // Choice
41  
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          // Question
51  
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          // Vote
64  
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 }