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