1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.polls.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portlet.polls.QuestionChoiceException;
21  import com.liferay.portlet.polls.model.PollsChoice;
22  import com.liferay.portlet.polls.service.base.PollsChoiceLocalServiceBaseImpl;
23  
24  import java.util.List;
25  
26  /**
27   * <a href="PollsChoiceLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class PollsChoiceLocalServiceImpl
32      extends PollsChoiceLocalServiceBaseImpl {
33  
34      public PollsChoice addChoice(
35              String uuid, long questionId, String name, String description)
36          throws PortalException, SystemException {
37  
38          validate(name, description);
39  
40          pollsQuestionPersistence.findByPrimaryKey(questionId);
41  
42          long choiceId = counterLocalService.increment();
43  
44          PollsChoice choice = pollsChoicePersistence.create(choiceId);
45  
46          choice.setUuid(uuid);
47          choice.setQuestionId(questionId);
48          choice.setName(name);
49          choice.setDescription(description);
50  
51          pollsChoicePersistence.update(choice, false);
52  
53          return choice;
54      }
55  
56      public PollsChoice getChoice(long choiceId)
57          throws PortalException, SystemException {
58  
59          return pollsChoicePersistence.findByPrimaryKey(choiceId);
60      }
61  
62      public List<PollsChoice> getChoices(long questionId)
63          throws SystemException {
64  
65          return pollsChoicePersistence.findByQuestionId(questionId);
66      }
67  
68      public int getChoicesCount(long questionId) throws SystemException {
69          return pollsChoicePersistence.countByQuestionId(questionId);
70      }
71  
72      public PollsChoice updateChoice(
73              long choiceId, long questionId, String name, String description)
74          throws PortalException, SystemException {
75  
76          validate(name, description);
77  
78          pollsQuestionPersistence.findByPrimaryKey(questionId);
79  
80          PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
81  
82          choice.setQuestionId(questionId);
83          choice.setName(name);
84          choice.setDescription(description);
85  
86          pollsChoicePersistence.update(choice, false);
87  
88          return choice;
89      }
90  
91      protected void validate(String name, String description)
92          throws PortalException {
93  
94          if (Validator.isNull(name) || Validator.isNull(description)) {
95              throw new QuestionChoiceException();
96          }
97      }
98  
99  }