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