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