1   /**
2    * Copyright (c) 2000-2009 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.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portlet.polls.DuplicateVoteException;
28  import com.liferay.portlet.polls.NoSuchQuestionException;
29  import com.liferay.portlet.polls.QuestionExpiredException;
30  import com.liferay.portlet.polls.model.PollsChoice;
31  import com.liferay.portlet.polls.model.PollsQuestion;
32  import com.liferay.portlet.polls.model.PollsVote;
33  import com.liferay.portlet.polls.service.base.PollsVoteLocalServiceBaseImpl;
34  
35  import java.util.Date;
36  import java.util.List;
37  
38  /**
39   * <a href="PollsVoteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
45  
46      public PollsVote addVote(long userId, long questionId, long choiceId)
47          throws PortalException, SystemException {
48  
49          // Choice
50  
51          Date now = new Date();
52  
53          PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
54  
55          if (choice.getQuestionId() != questionId) {
56              throw new NoSuchQuestionException();
57          }
58  
59          // Question
60  
61          PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
62              questionId);
63  
64          if (question.isExpired()) {
65              throw new QuestionExpiredException();
66          }
67  
68          question.setLastVoteDate(now);
69  
70          pollsQuestionPersistence.update(question, false);
71  
72          // Vote
73  
74          PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
75  
76          if (vote != null) {
77              throw new DuplicateVoteException();
78          }
79          else {
80              long voteId = counterLocalService.increment();
81  
82              vote = pollsVotePersistence.create(voteId);
83  
84              vote.setUserId(userId);
85              vote.setQuestionId(questionId);
86              vote.setChoiceId(choiceId);
87              vote.setVoteDate(now);
88  
89              pollsVotePersistence.update(vote, false);
90          }
91  
92          return vote;
93      }
94  
95      public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
96          throws SystemException {
97  
98          return pollsVotePersistence.findByChoiceId(choiceId,  start, end);
99      }
100 
101     public int getChoiceVotesCount(long choiceId) throws SystemException {
102         return pollsVotePersistence.countByChoiceId(choiceId);
103     }
104 
105     public PollsVote getVote(long questionId, long userId)
106         throws PortalException, SystemException {
107 
108         return pollsVotePersistence.findByQ_U(questionId, userId);
109     }
110 
111     public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
112         throws SystemException {
113 
114         return pollsVotePersistence.findByQuestionId(questionId, start, end);
115     }
116 
117     public int getQuestionVotesCount(long questionId) throws SystemException {
118         return pollsVotePersistence.countByQuestionId(questionId);
119     }
120 
121 }