001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }