001    /**
002     * Copyright (c) 2000-2013 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            @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    }