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            public PollsChoice addChoice(
036                            long userId, long questionId, String name, String description,
037                            ServiceContext serviceContext)
038                    throws PortalException, SystemException {
039    
040                    validate(name, description);
041    
042                    User user = userPersistence.findByPrimaryKey(userId);
043                    Date now = new Date();
044    
045                    long choiceId = counterLocalService.increment();
046    
047                    PollsChoice choice = pollsChoicePersistence.create(choiceId);
048    
049                    choice.setUuid(serviceContext.getUuid());
050                    choice.setGroupId(serviceContext.getScopeGroupId());
051                    choice.setCompanyId(user.getCompanyId());
052                    choice.setUserId(user.getUserId());
053                    choice.setUserName(user.getFullName());
054                    choice.setCreateDate(serviceContext.getCreateDate(now));
055                    choice.setModifiedDate(serviceContext.getModifiedDate(now));
056                    choice.setQuestionId(questionId);
057                    choice.setName(name);
058                    choice.setDescription(description);
059    
060                    pollsChoicePersistence.update(choice);
061    
062                    return choice;
063            }
064    
065            public PollsChoice getChoice(long choiceId)
066                    throws PortalException, SystemException {
067    
068                    return pollsChoicePersistence.findByPrimaryKey(choiceId);
069            }
070    
071            public List<PollsChoice> getChoices(long questionId)
072                    throws SystemException {
073    
074                    return pollsChoicePersistence.findByQuestionId(questionId);
075            }
076    
077            public int getChoicesCount(long questionId) throws SystemException {
078                    return pollsChoicePersistence.countByQuestionId(questionId);
079            }
080    
081            public PollsChoice updateChoice(
082                            long choiceId, long questionId, String name, String description,
083                            ServiceContext serviceContext)
084                    throws PortalException, SystemException {
085    
086                    validate(name, description);
087    
088                    pollsQuestionPersistence.findByPrimaryKey(questionId);
089    
090                    PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
091    
092                    choice.setModifiedDate(serviceContext.getModifiedDate(null));
093                    choice.setQuestionId(questionId);
094                    choice.setName(name);
095                    choice.setDescription(description);
096    
097                    pollsChoicePersistence.update(choice);
098    
099                    return choice;
100            }
101    
102            protected void validate(String name, String description)
103                    throws PortalException {
104    
105                    if (Validator.isNull(name) || Validator.isNull(description)) {
106                            throw new QuestionChoiceException();
107                    }
108            }
109    
110    }