1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PollsVoteLocalServiceImpl extends PollsVoteLocalServiceBaseImpl {
44  
45      public PollsVote addVote(long userId, long questionId, long choiceId)
46          throws PortalException, SystemException {
47  
48          // Choice
49  
50          Date now = new Date();
51  
52          PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
53  
54          if (choice.getQuestionId() != questionId) {
55              throw new NoSuchQuestionException();
56          }
57  
58          // Question
59  
60          PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
61              questionId);
62  
63          if (question.isExpired()) {
64              throw new QuestionExpiredException();
65          }
66  
67          question.setLastVoteDate(now);
68  
69          pollsQuestionPersistence.update(question, false);
70  
71          // Vote
72  
73          PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);
74  
75          if (vote != null) {
76              throw new DuplicateVoteException();
77          }
78          else {
79              long voteId = counterLocalService.increment();
80  
81              vote = pollsVotePersistence.create(voteId);
82  
83              vote.setUserId(userId);
84              vote.setQuestionId(questionId);
85              vote.setChoiceId(choiceId);
86              vote.setVoteDate(now);
87  
88              pollsVotePersistence.update(vote, false);
89          }
90  
91          return vote;
92      }
93  
94      public List<PollsVote> getChoiceVotes(long choiceId, int start, int end)
95          throws SystemException {
96  
97          return pollsVotePersistence.findByChoiceId(choiceId,  start, end);
98      }
99  
100     public int getChoiceVotesCount(long choiceId) throws SystemException {
101         return pollsVotePersistence.countByChoiceId(choiceId);
102     }
103 
104     public PollsVote getVote(long questionId, long userId)
105         throws PortalException, SystemException {
106 
107         return pollsVotePersistence.findByQ_U(questionId, userId);
108     }
109 
110     public List<PollsVote> getQuestionVotes(long questionId, int start, int end)
111         throws SystemException {
112 
113         return pollsVotePersistence.findByQuestionId(questionId, start, end);
114     }
115 
116     public int getQuestionVotesCount(long questionId) throws SystemException {
117         return pollsVotePersistence.countByQuestionId(questionId);
118     }
119 
120 }