1
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
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 }