1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.polls.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portlet.polls.QuestionChoiceException;
26  import com.liferay.portlet.polls.model.PollsChoice;
27  import com.liferay.portlet.polls.service.base.PollsChoiceLocalServiceBaseImpl;
28  
29  import java.util.List;
30  
31  /**
32   * <a href="PollsChoiceLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class PollsChoiceLocalServiceImpl
38      extends PollsChoiceLocalServiceBaseImpl {
39  
40      public PollsChoice addChoice(
41              long questionId, String name, String description)
42          throws PortalException, SystemException {
43  
44          return addChoice(null, questionId, name, description);
45      }
46  
47      public PollsChoice addChoice(
48              String uuid, long questionId, String name, String description)
49          throws PortalException, SystemException {
50  
51          validate(name, description);
52  
53          pollsQuestionPersistence.findByPrimaryKey(questionId);
54  
55          long choiceId = counterLocalService.increment();
56  
57          PollsChoice choice = pollsChoicePersistence.create(choiceId);
58  
59          choice.setUuid(uuid);
60          choice.setQuestionId(questionId);
61          choice.setName(name);
62          choice.setDescription(description);
63  
64          pollsChoicePersistence.update(choice, false);
65  
66          return choice;
67      }
68  
69      public PollsChoice getChoice(long choiceId)
70          throws PortalException, SystemException {
71  
72          return pollsChoicePersistence.findByPrimaryKey(choiceId);
73      }
74  
75      public List<PollsChoice> getChoices(long questionId)
76          throws SystemException {
77  
78          return pollsChoicePersistence.findByQuestionId(questionId);
79      }
80  
81      public int getChoicesCount(long questionId) throws SystemException {
82          return pollsChoicePersistence.countByQuestionId(questionId);
83      }
84  
85      public PollsChoice updateChoice(
86              long choiceId, long questionId, String name, String description)
87          throws PortalException, SystemException {
88  
89          validate(name, description);
90  
91          pollsQuestionPersistence.findByPrimaryKey(questionId);
92  
93          PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);
94  
95          choice.setQuestionId(questionId);
96          choice.setName(name);
97          choice.setDescription(description);
98  
99          pollsChoicePersistence.update(choice, false);
100 
101         return choice;
102     }
103 
104     protected void validate(String name, String description)
105         throws PortalException {
106 
107         if (Validator.isNull(name) || Validator.isNull(description)) {
108             throw new QuestionChoiceException();
109         }
110     }
111 
112 }