001    /**
002     * Copyright (c) 2000-2013 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            @Override
034            public PollsChoice addChoice(
035                            long questionId, String name, String description,
036                            ServiceContext serviceContext)
037                    throws PortalException, SystemException {
038    
039                    validate(name, description);
040    
041                    pollsQuestionPersistence.findByPrimaryKey(questionId);
042    
043                    long choiceId = counterLocalService.increment();
044    
045                    PollsChoice choice = pollsChoicePersistence.create(choiceId);
046    
047                    choice.setUuid(serviceContext.getUuid());
048                    choice.setQuestionId(questionId);
049                    choice.setName(name);
050                    choice.setDescription(description);
051    
052                    pollsChoicePersistence.update(choice, false);
053    
054                    return choice;
055            }
056    
057            @Override
058            public PollsChoice getChoice(long choiceId)
059                    throws PortalException, SystemException {
060    
061                    return pollsChoicePersistence.findByPrimaryKey(choiceId);
062            }
063    
064            @Override
065            public List<PollsChoice> getChoices(long questionId)
066                    throws SystemException {
067    
068                    return pollsChoicePersistence.findByQuestionId(questionId);
069            }
070    
071            @Override
072            public int getChoicesCount(long questionId) throws SystemException {
073                    return pollsChoicePersistence.countByQuestionId(questionId);
074            }
075    
076            @Override
077            public PollsChoice updateChoice(
078                            long choiceId, long questionId, String name, String description)
079                    throws PortalException, SystemException {
080    
081                    validate(name, description);
082    
083                    pollsQuestionPersistence.findByPrimaryKey(questionId);
084    
085                    PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
086    
087                    choice.setQuestionId(questionId);
088                    choice.setName(name);
089                    choice.setDescription(description);
090    
091                    pollsChoicePersistence.update(choice, false);
092    
093                    return choice;
094            }
095    
096            protected void validate(String name, String description)
097                    throws PortalException {
098    
099                    if (Validator.isNull(name) || Validator.isNull(description)) {
100                            throw new QuestionChoiceException();
101                    }
102            }
103    
104    }