001
014
015 package com.liferay.portlet.polls.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.LocaleUtil;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.model.ResourceConstants;
022 import com.liferay.portal.model.User;
023 import com.liferay.portal.service.ServiceContext;
024 import com.liferay.portal.util.PortalUtil;
025 import com.liferay.portlet.polls.QuestionChoiceException;
026 import com.liferay.portlet.polls.QuestionDescriptionException;
027 import com.liferay.portlet.polls.QuestionExpirationDateException;
028 import com.liferay.portlet.polls.QuestionTitleException;
029 import com.liferay.portlet.polls.model.PollsChoice;
030 import com.liferay.portlet.polls.model.PollsQuestion;
031 import com.liferay.portlet.polls.service.base.PollsQuestionLocalServiceBaseImpl;
032
033 import java.util.Date;
034 import java.util.List;
035 import java.util.Locale;
036 import java.util.Map;
037
038
042 public class PollsQuestionLocalServiceImpl
043 extends PollsQuestionLocalServiceBaseImpl {
044
045 @Override
046 public PollsQuestion addQuestion(
047 long userId, Map<Locale, String> titleMap,
048 Map<Locale, String> descriptionMap, int expirationDateMonth,
049 int expirationDateDay, int expirationDateYear,
050 int expirationDateHour, int expirationDateMinute,
051 boolean neverExpire, List<PollsChoice> choices,
052 ServiceContext serviceContext)
053 throws PortalException, SystemException {
054
055
056
057 User user = userPersistence.findByPrimaryKey(userId);
058 long groupId = serviceContext.getScopeGroupId();
059
060 Date expirationDate = null;
061
062 if (!neverExpire) {
063 expirationDate = PortalUtil.getDate(
064 expirationDateMonth, expirationDateDay, expirationDateYear,
065 expirationDateHour, expirationDateMinute, user.getTimeZone(),
066 QuestionExpirationDateException.class);
067 }
068
069 Date now = new Date();
070
071 validate(titleMap, descriptionMap, choices);
072
073 long questionId = counterLocalService.increment();
074
075 PollsQuestion question = pollsQuestionPersistence.create(questionId);
076
077 question.setUuid(serviceContext.getUuid());
078 question.setGroupId(groupId);
079 question.setCompanyId(user.getCompanyId());
080 question.setUserId(user.getUserId());
081 question.setUserName(user.getFullName());
082 question.setCreateDate(serviceContext.getCreateDate(now));
083 question.setModifiedDate(serviceContext.getModifiedDate(now));
084 question.setTitleMap(titleMap);
085 question.setDescriptionMap(descriptionMap);
086 question.setExpirationDate(expirationDate);
087
088 pollsQuestionPersistence.update(question, false);
089
090
091
092 if (serviceContext.isAddGroupPermissions() ||
093 serviceContext.isAddGuestPermissions()) {
094
095 addQuestionResources(
096 question, serviceContext.isAddGroupPermissions(),
097 serviceContext.isAddGuestPermissions());
098 }
099 else {
100 addQuestionResources(
101 question, serviceContext.getGroupPermissions(),
102 serviceContext.getGuestPermissions());
103 }
104
105
106
107 if (choices != null) {
108 for (PollsChoice choice : choices) {
109 pollsChoiceLocalService.addChoice(
110 questionId, choice.getName(), choice.getDescription(),
111 new ServiceContext());
112 }
113 }
114
115 return question;
116 }
117
118 @Override
119 public void addQuestionResources(
120 long questionId, boolean addGroupPermissions,
121 boolean addGuestPermissions)
122 throws PortalException, SystemException {
123
124 PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
125 questionId);
126
127 addQuestionResources(
128 question, addGroupPermissions, addGuestPermissions);
129 }
130
131 @Override
132 public void addQuestionResources(
133 long questionId, String[] groupPermissions,
134 String[] guestPermissions)
135 throws PortalException, SystemException {
136
137 PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
138 questionId);
139
140 addQuestionResources(question, groupPermissions, guestPermissions);
141 }
142
143 @Override
144 public void addQuestionResources(
145 PollsQuestion question, boolean addGroupPermissions,
146 boolean addGuestPermissions)
147 throws PortalException, SystemException {
148
149 resourceLocalService.addResources(
150 question.getCompanyId(), question.getGroupId(),
151 question.getUserId(), PollsQuestion.class.getName(),
152 question.getQuestionId(), false, addGroupPermissions,
153 addGuestPermissions);
154 }
155
156 @Override
157 public void addQuestionResources(
158 PollsQuestion question, String[] groupPermissions,
159 String[] guestPermissions)
160 throws PortalException, SystemException {
161
162 resourceLocalService.addModelResources(
163 question.getCompanyId(), question.getGroupId(),
164 question.getUserId(), PollsQuestion.class.getName(),
165 question.getQuestionId(), groupPermissions, guestPermissions);
166 }
167
168 @Override
169 public void deleteQuestion(long questionId)
170 throws PortalException, SystemException {
171
172 PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
173 questionId);
174
175 deleteQuestion(question);
176 }
177
178 @Override
179 public void deleteQuestion(PollsQuestion question)
180 throws PortalException, SystemException {
181
182
183
184 pollsQuestionPersistence.remove(question);
185
186
187
188 resourceLocalService.deleteResource(
189 question.getCompanyId(), PollsQuestion.class.getName(),
190 ResourceConstants.SCOPE_INDIVIDUAL, question.getQuestionId());
191
192
193
194 pollsChoicePersistence.removeByQuestionId(question.getQuestionId());
195
196
197
198 pollsVotePersistence.removeByQuestionId(question.getQuestionId());
199 }
200
201 @Override
202 public void deleteQuestions(long groupId)
203 throws PortalException, SystemException {
204
205 for (PollsQuestion question :
206 pollsQuestionPersistence.findByGroupId(groupId)) {
207
208 deleteQuestion(question);
209 }
210 }
211
212 @Override
213 public PollsQuestion getQuestion(long questionId)
214 throws PortalException, SystemException {
215
216 return pollsQuestionPersistence.findByPrimaryKey(questionId);
217 }
218
219 @Override
220 public List<PollsQuestion> getQuestions(long groupId)
221 throws SystemException {
222
223 return pollsQuestionPersistence.findByGroupId(groupId);
224 }
225
226 @Override
227 public List<PollsQuestion> getQuestions(long groupId, int start, int end)
228 throws SystemException {
229
230 return pollsQuestionPersistence.findByGroupId(groupId, start, end);
231 }
232
233 @Override
234 public int getQuestionsCount(long groupId) throws SystemException {
235 return pollsQuestionPersistence.countByGroupId(groupId);
236 }
237
238 @Override
239 public PollsQuestion updateQuestion(
240 long userId, long questionId, Map<Locale, String> titleMap,
241 Map<Locale, String> descriptionMap, int expirationDateMonth,
242 int expirationDateDay, int expirationDateYear,
243 int expirationDateHour, int expirationDateMinute,
244 boolean neverExpire, List<PollsChoice> choices,
245 ServiceContext serviceContext)
246 throws PortalException, SystemException {
247
248
249
250 User user = userPersistence.findByPrimaryKey(userId);
251
252 Date expirationDate = null;
253
254 if (!neverExpire) {
255 expirationDate = PortalUtil.getDate(
256 expirationDateMonth, expirationDateDay, expirationDateYear,
257 expirationDateHour, expirationDateMinute, user.getTimeZone(),
258 QuestionExpirationDateException.class);
259 }
260
261 validate(titleMap, descriptionMap, choices);
262
263 PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
264 questionId);
265
266 question.setModifiedDate(serviceContext.getModifiedDate(null));
267 question.setTitleMap(titleMap);
268 question.setDescriptionMap(descriptionMap);
269 question.setExpirationDate(expirationDate);
270
271 pollsQuestionPersistence.update(question, false);
272
273
274
275 if (choices != null) {
276 int oldChoicesCount = pollsChoicePersistence.countByQuestionId(
277 questionId);
278
279 if (oldChoicesCount > choices.size()) {
280 throw new QuestionChoiceException();
281 }
282
283 for (PollsChoice choice : choices) {
284 String choiceName = choice.getName();
285 String choiceDescription = choice.getDescription();
286
287 choice = pollsChoicePersistence.fetchByQ_N(
288 questionId, choiceName);
289
290 if (choice == null) {
291 pollsChoiceLocalService.addChoice(
292 questionId, choiceName, choiceDescription,
293 new ServiceContext());
294 }
295 else {
296 pollsChoiceLocalService.updateChoice(
297 choice.getChoiceId(), questionId, choiceName,
298 choiceDescription);
299 }
300 }
301 }
302
303 return question;
304 }
305
306 protected void validate(
307 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
308 List<PollsChoice> choices)
309 throws PortalException {
310
311 Locale locale = LocaleUtil.getDefault();
312
313 String title = titleMap.get(locale);
314
315 if (Validator.isNull(title)) {
316 throw new QuestionTitleException();
317 }
318
319 String description = descriptionMap.get(locale);
320
321 if (Validator.isNull(description)) {
322 throw new QuestionDescriptionException();
323 }
324
325 if ((choices != null) && (choices.size() < 2)) {
326 throw new QuestionChoiceException();
327 }
328
329 if (choices != null) {
330 for (PollsChoice choice : choices) {
331 String choiceDescription = choice.getDescription(locale);
332
333 if (Validator.isNull(choiceDescription)) {
334 throw new QuestionChoiceException();
335 }
336 }
337 }
338 }
339
340 }