1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.polls.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portlet.polls.DuplicateVoteException;
25  import com.liferay.portlet.polls.NoSuchQuestionException;
26  import com.liferay.portlet.polls.QuestionExpiredException;
27  import com.liferay.portlet.polls.model.PollsChoice;
28  import com.liferay.portlet.polls.model.PollsQuestion;
29  import com.liferay.portlet.polls.model.PollsVote;
30  import com.liferay.portlet.polls.service.base.PollsVoteLocalServiceBaseImpl;
31  
32  import java.util.Date;
33  import java.util.List;
34  
35  /**
36   * <a href="PollsVoteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
42  
43      public PollsVote addVote(long userId, long questionId, long choiceId)
44          throws PortalException, SystemException {
45  
46          // Choice
47  
48          Date now = new Date();
49  
50          PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
51  
52          if (choice.getQuestionId() != questionId) {
53              throw new NoSuchQuestionException();
54          }
55  
56          // Question
57  
58          PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
59              questionId);
60  
61          if (question.isExpired()) {
62              throw new QuestionExpiredException();
63          }
64  
65          question.setLastVoteDate(now);
66  
67          pollsQuestionPersistence.update(question, false);
68  
69          // Vote
70  
71          PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
72  
73          if (vote != null) {
74              throw new DuplicateVoteException();
75          }
76          else {
77              long voteId = counterLocalService.increment();
78  
79              vote = pollsVotePersistence.create(voteId);
80  
81              vote.setUserId(userId);
82              vote.setQuestionId(questionId);
83              vote.setChoiceId(choiceId);
84              vote.setVoteDate(now);
85  
86              pollsVotePersistence.update(vote, false);
87          }
88  
89          return vote;
90      }
91  
92      public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
93          throws SystemException {
94  
95          return pollsVotePersistence.findByChoiceId(choiceId,  start, end);
96      }
97  
98      public int getChoiceVotesCount(long choiceId) throws SystemException {
99          return pollsVotePersistence.countByChoiceId(choiceId);
100     }
101 
102     public PollsVote getVote(long questionId, long userId)
103         throws PortalException, SystemException {
104 
105         return pollsVotePersistence.findByQ_U(questionId, userId);
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 }