1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.polls.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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              long questionId, String name, String description)
36          throws PortalException, SystemException {
37  
38          return addChoice(null, questionId, name, description);
39      }
40  
41      public PollsChoice addChoice(
42              String uuid, long questionId, String name, String description)
43          throws PortalException, SystemException {
44  
45          validate(name, description);
46  
47          pollsQuestionPersistence.findByPrimaryKey(questionId);
48  
49          long choiceId = counterLocalService.increment();
50  
51          PollsChoice choice = pollsChoicePersistence.create(choiceId);
52  
53          choice.setUuid(uuid);
54          choice.setQuestionId(questionId);
55          choice.setName(name);
56          choice.setDescription(description);
57  
58          pollsChoicePersistence.update(choice, false);
59  
60          return choice;
61      }
62  
63      public PollsChoice getChoice(long choiceId)
64          throws PortalException, SystemException {
65  
66          return pollsChoicePersistence.findByPrimaryKey(choiceId);
67      }
68  
69      public List<PollsChoice> getChoices(long questionId)
70          throws SystemException {
71  
72          return pollsChoicePersistence.findByQuestionId(questionId);
73      }
74  
75      public int getChoicesCount(long questionId) throws SystemException {
76          return pollsChoicePersistence.countByQuestionId(questionId);
77      }
78  
79      public PollsChoice updateChoice(
80              long choiceId, long questionId, String name, String description)
81          throws PortalException, SystemException {
82  
83          validate(name, description);
84  
85          pollsQuestionPersistence.findByPrimaryKey(questionId);
86  
87          PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
88  
89          choice.setQuestionId(questionId);
90          choice.setName(name);
91          choice.setDescription(description);
92  
93          pollsChoicePersistence.update(choice, false);
94  
95          return choice;
96      }
97  
98      protected void validate(String name, String description)
99          throws PortalException {
100 
101         if (Validator.isNull(name) || Validator.isNull(description)) {
102             throw new QuestionChoiceException();
103         }
104     }
105 
106 }