001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.polls.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portlet.polls.DuplicateVoteException;
026: import com.liferay.portlet.polls.NoSuchQuestionException;
027: import com.liferay.portlet.polls.QuestionExpiredException;
028: import com.liferay.portlet.polls.model.PollsChoice;
029: import com.liferay.portlet.polls.model.PollsQuestion;
030: import com.liferay.portlet.polls.model.PollsVote;
031: import com.liferay.portlet.polls.service.base.PollsVoteLocalServiceBaseImpl;
032:
033: import java.util.Date;
034: import java.util.List;
035:
036: /**
037: * <a href="PollsVoteLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class PollsVoteLocalServiceImpl extends
043: PollsVoteLocalServiceBaseImpl {
044:
045: public PollsVote addVote(long userId, long questionId, long choiceId)
046: throws PortalException, SystemException {
047:
048: // Choice
049:
050: Date now = new Date();
051:
052: PollsChoice choice = pollsChoicePersistence
053: .findByPrimaryKey(choiceId);
054:
055: if (choice.getQuestionId() != questionId) {
056: throw new NoSuchQuestionException();
057: }
058:
059: // Question
060:
061: PollsQuestion question = pollsQuestionPersistence
062: .findByPrimaryKey(questionId);
063:
064: if (question.isExpired()) {
065: throw new QuestionExpiredException();
066: }
067:
068: question.setLastVoteDate(now);
069:
070: pollsQuestionPersistence.update(question);
071:
072: // Vote
073:
074: PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId,
075: userId);
076:
077: if (vote != null) {
078: throw new DuplicateVoteException();
079: } else {
080: long voteId = counterLocalService.increment();
081:
082: vote = pollsVotePersistence.create(voteId);
083:
084: vote.setUserId(userId);
085: vote.setQuestionId(questionId);
086: vote.setChoiceId(choiceId);
087: vote.setVoteDate(now);
088:
089: pollsVotePersistence.update(vote);
090: }
091:
092: return vote;
093: }
094:
095: public List getChoiceVotes(long choiceId, int begin, int end)
096: throws SystemException {
097:
098: return pollsVotePersistence
099: .findByChoiceId(choiceId, begin, end);
100: }
101:
102: public int getChoiceVotesCount(long choiceId)
103: throws SystemException {
104: return pollsVotePersistence.countByChoiceId(choiceId);
105: }
106:
107: public PollsVote getVote(long questionId, long userId)
108: throws PortalException, SystemException {
109:
110: return pollsVotePersistence.findByQ_U(questionId, userId);
111: }
112:
113: public List getQuestionVotes(long questionId, int begin, int end)
114: throws SystemException {
115:
116: return pollsVotePersistence.findByQuestionId(questionId, begin,
117: end);
118: }
119:
120: public int getQuestionVotesCount(long questionId)
121: throws SystemException {
122: return pollsVotePersistence.countByQuestionId(questionId);
123: }
124:
125: }
|