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