001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.polls.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.portlet.polls.QuestionChoiceException;
022    import com.liferay.portlet.polls.model.PollsChoice;
023    import com.liferay.portlet.polls.service.base.PollsChoiceLocalServiceBaseImpl;
024    
025    import java.util.List;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PollsChoiceLocalServiceImpl
031            extends PollsChoiceLocalServiceBaseImpl {
032    
033            public PollsChoice addChoice(
034                            long questionId, String name, String description,
035                            ServiceContext serviceContext)
036                    throws PortalException, SystemException {
037    
038                    validate(name, description);
039    
040                    pollsQuestionPersistence.findByPrimaryKey(questionId);
041    
042                    long choiceId = counterLocalService.increment();
043    
044                    PollsChoice choice = pollsChoicePersistence.create(choiceId);
045    
046                    choice.setUuid(serviceContext.getUuid());
047                    choice.setQuestionId(questionId);
048                    choice.setName(name);
049                    choice.setDescription(description);
050    
051                    pollsChoicePersistence.update(choice, false);
052    
053                    return choice;
054            }
055    
056            public PollsChoice getChoice(long choiceId)
057                    throws PortalException, SystemException {
058    
059                    return pollsChoicePersistence.findByPrimaryKey(choiceId);
060            }
061    
062            public List<PollsChoice> getChoices(long questionId)
063                    throws SystemException {
064    
065                    return pollsChoicePersistence.findByQuestionId(questionId);
066            }
067    
068            public int getChoicesCount(long questionId) throws SystemException {
069                    return pollsChoicePersistence.countByQuestionId(questionId);
070            }
071    
072            public PollsChoice updateChoice(
073                            long choiceId, long questionId, String name, String description)
074                    throws PortalException, SystemException {
075    
076                    validate(name, description);
077    
078                    pollsQuestionPersistence.findByPrimaryKey(questionId);
079    
080                    PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
081    
082                    choice.setQuestionId(questionId);
083                    choice.setName(name);
084                    choice.setDescription(description);
085    
086                    pollsChoicePersistence.update(choice, false);
087    
088                    return choice;
089            }
090    
091            protected void validate(String name, String description)
092                    throws PortalException {
093    
094                    if (Validator.isNull(name) || Validator.isNull(description)) {
095                            throw new QuestionChoiceException();
096                    }
097            }
098    
099    }