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