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.portal.kernel.util.Validator;
026: import com.liferay.portlet.polls.QuestionChoiceException;
027: import com.liferay.portlet.polls.model.PollsChoice;
028: import com.liferay.portlet.polls.service.base.PollsChoiceLocalServiceBaseImpl;
029:
030: import java.util.List;
031:
032: /**
033: * <a href="PollsChoiceLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: *
037: */
038: public class PollsChoiceLocalServiceImpl extends
039: PollsChoiceLocalServiceBaseImpl {
040:
041: public PollsChoice addChoice(long questionId, String name,
042: String description) throws PortalException, SystemException {
043:
044: return addChoice(null, questionId, name, description);
045: }
046:
047: public PollsChoice addChoice(String uuid, long questionId,
048: String name, String description) throws PortalException,
049: SystemException {
050:
051: validate(name, description);
052:
053: pollsQuestionPersistence.findByPrimaryKey(questionId);
054:
055: long choiceId = counterLocalService.increment();
056:
057: PollsChoice choice = pollsChoicePersistence.create(choiceId);
058:
059: choice.setUuid(uuid);
060: choice.setQuestionId(questionId);
061: choice.setName(name);
062: choice.setDescription(description);
063:
064: pollsChoicePersistence.update(choice);
065:
066: return choice;
067: }
068:
069: public PollsChoice getChoice(long choiceId) throws PortalException,
070: SystemException {
071:
072: return pollsChoicePersistence.findByPrimaryKey(choiceId);
073: }
074:
075: public List getChoices(long questionId) throws SystemException {
076: return pollsChoicePersistence.findByQuestionId(questionId);
077: }
078:
079: public int getChoicesCount(long questionId) throws SystemException {
080: return pollsChoicePersistence.countByQuestionId(questionId);
081: }
082:
083: public PollsChoice updateChoice(long choiceId, long questionId,
084: String name, String description) throws PortalException,
085: SystemException {
086:
087: validate(name, description);
088:
089: pollsQuestionPersistence.findByPrimaryKey(questionId);
090:
091: PollsChoice choice = pollsChoicePersistence
092: .findByPrimaryKey(choiceId);
093:
094: choice.setQuestionId(questionId);
095: choice.setName(name);
096: choice.setDescription(description);
097:
098: pollsChoicePersistence.update(choice);
099:
100: return choice;
101: }
102:
103: protected void validate(String name, String description)
104: throws PortalException {
105:
106: if (Validator.isNull(name) || Validator.isNull(description)) {
107: throw new QuestionChoiceException();
108: }
109: }
110:
111: }
|