1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
43   * <a href="PollsVoteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
49  
50      public PollsVote addVote(long userId, long questionId, long choiceId)
51          throws PortalException, SystemException {
52  
53          // Choice
54  
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          // Question
64  
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          // Vote
76  
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 }