001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.systemevent.SystemEvent;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.ResourceConstants;
023    import com.liferay.portal.model.SystemEventConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.polls.QuestionChoiceException;
028    import com.liferay.portlet.polls.QuestionDescriptionException;
029    import com.liferay.portlet.polls.QuestionExpirationDateException;
030    import com.liferay.portlet.polls.QuestionTitleException;
031    import com.liferay.portlet.polls.model.PollsChoice;
032    import com.liferay.portlet.polls.model.PollsQuestion;
033    import com.liferay.portlet.polls.service.base.PollsQuestionLocalServiceBaseImpl;
034    
035    import java.util.Date;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Julio Camarero
043     */
044    public class PollsQuestionLocalServiceImpl
045            extends PollsQuestionLocalServiceBaseImpl {
046    
047            @Override
048            public PollsQuestion addQuestion(
049                            long userId, Map<Locale, String> titleMap,
050                            Map<Locale, String> descriptionMap, int expirationDateMonth,
051                            int expirationDateDay, int expirationDateYear,
052                            int expirationDateHour, int expirationDateMinute,
053                            boolean neverExpire, List<PollsChoice> choices,
054                            ServiceContext serviceContext)
055                    throws PortalException, SystemException {
056    
057                    // Question
058    
059                    User user = userPersistence.findByPrimaryKey(userId);
060                    long groupId = serviceContext.getScopeGroupId();
061    
062                    Date expirationDate = null;
063    
064                    if (!neverExpire) {
065                            expirationDate = PortalUtil.getDate(
066                                    expirationDateMonth, expirationDateDay, expirationDateYear,
067                                    expirationDateHour, expirationDateMinute, user.getTimeZone(),
068                                    QuestionExpirationDateException.class);
069                    }
070    
071                    Date now = new Date();
072    
073                    validate(titleMap, descriptionMap, choices);
074    
075                    long questionId = counterLocalService.increment();
076    
077                    PollsQuestion question = pollsQuestionPersistence.create(questionId);
078    
079                    question.setUuid(serviceContext.getUuid());
080                    question.setGroupId(groupId);
081                    question.setCompanyId(user.getCompanyId());
082                    question.setUserId(user.getUserId());
083                    question.setUserName(user.getFullName());
084                    question.setCreateDate(serviceContext.getCreateDate(now));
085                    question.setModifiedDate(serviceContext.getModifiedDate(now));
086                    question.setTitleMap(titleMap);
087                    question.setDescriptionMap(descriptionMap);
088                    question.setExpirationDate(expirationDate);
089    
090                    pollsQuestionPersistence.update(question);
091    
092                    // Resources
093    
094                    if (serviceContext.isAddGroupPermissions() ||
095                            serviceContext.isAddGuestPermissions()) {
096    
097                            addQuestionResources(
098                                    question, serviceContext.isAddGroupPermissions(),
099                                    serviceContext.isAddGuestPermissions());
100                    }
101                    else {
102                            addQuestionResources(
103                                    question, serviceContext.getGroupPermissions(),
104                                    serviceContext.getGuestPermissions());
105                    }
106    
107                    // Choices
108    
109                    if (choices != null) {
110                            for (PollsChoice choice : choices) {
111                                    pollsChoiceLocalService.addChoice(
112                                            userId, questionId, choice.getName(),
113                                            choice.getDescription(), serviceContext);
114                            }
115                    }
116    
117                    return question;
118            }
119    
120            @Override
121            public void addQuestionResources(
122                            long questionId, boolean addGroupPermissions,
123                            boolean addGuestPermissions)
124                    throws PortalException, SystemException {
125    
126                    PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
127                            questionId);
128    
129                    addQuestionResources(
130                            question, addGroupPermissions, addGuestPermissions);
131            }
132    
133            @Override
134            public void addQuestionResources(
135                            long questionId, String[] groupPermissions,
136                            String[] guestPermissions)
137                    throws PortalException, SystemException {
138    
139                    PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
140                            questionId);
141    
142                    addQuestionResources(question, groupPermissions, guestPermissions);
143            }
144    
145            @Override
146            public void addQuestionResources(
147                            PollsQuestion question, boolean addGroupPermissions,
148                            boolean addGuestPermissions)
149                    throws PortalException, SystemException {
150    
151                    resourceLocalService.addResources(
152                            question.getCompanyId(), question.getGroupId(),
153                            question.getUserId(), PollsQuestion.class.getName(),
154                            question.getQuestionId(), false, addGroupPermissions,
155                            addGuestPermissions);
156            }
157    
158            @Override
159            public void addQuestionResources(
160                            PollsQuestion question, String[] groupPermissions,
161                            String[] guestPermissions)
162                    throws PortalException, SystemException {
163    
164                    resourceLocalService.addModelResources(
165                            question.getCompanyId(), question.getGroupId(),
166                            question.getUserId(), PollsQuestion.class.getName(),
167                            question.getQuestionId(), groupPermissions, guestPermissions);
168            }
169    
170            @Override
171            public void deleteQuestion(long questionId)
172                    throws PortalException, SystemException {
173    
174                    PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
175                            questionId);
176    
177                    pollsQuestionLocalService.deleteQuestion(question);
178            }
179    
180            @Override
181            @SystemEvent(
182                    action = SystemEventConstants.ACTION_SKIP,
183                    type = SystemEventConstants.TYPE_DELETE)
184            public void deleteQuestion(PollsQuestion question)
185                    throws PortalException, SystemException {
186    
187                    // Question
188    
189                    pollsQuestionPersistence.remove(question);
190    
191                    // Resources
192    
193                    resourceLocalService.deleteResource(
194                            question.getCompanyId(), PollsQuestion.class.getName(),
195                            ResourceConstants.SCOPE_INDIVIDUAL, question.getQuestionId());
196    
197                    // Choices
198    
199                    pollsChoicePersistence.removeByQuestionId(question.getQuestionId());
200    
201                    // Votes
202    
203                    pollsVotePersistence.removeByQuestionId(question.getQuestionId());
204            }
205    
206            @Override
207            public void deleteQuestions(long groupId)
208                    throws PortalException, SystemException {
209    
210                    for (PollsQuestion question :
211                                    pollsQuestionPersistence.findByGroupId(groupId)) {
212    
213                            pollsQuestionLocalService.deleteQuestion(question);
214                    }
215            }
216    
217            @Override
218            public PollsQuestion getQuestion(long questionId)
219                    throws PortalException, SystemException {
220    
221                    return pollsQuestionPersistence.findByPrimaryKey(questionId);
222            }
223    
224            @Override
225            public List<PollsQuestion> getQuestions(long groupId)
226                    throws SystemException {
227    
228                    return pollsQuestionPersistence.findByGroupId(groupId);
229            }
230    
231            @Override
232            public List<PollsQuestion> getQuestions(long groupId, int start, int end)
233                    throws SystemException {
234    
235                    return pollsQuestionPersistence.findByGroupId(groupId, start, end);
236            }
237    
238            @Override
239            public int getQuestionsCount(long groupId) throws SystemException {
240                    return pollsQuestionPersistence.countByGroupId(groupId);
241            }
242    
243            @Override
244            public PollsQuestion updateQuestion(
245                            long userId, long questionId, Map<Locale, String> titleMap,
246                            Map<Locale, String> descriptionMap, int expirationDateMonth,
247                            int expirationDateDay, int expirationDateYear,
248                            int expirationDateHour, int expirationDateMinute,
249                            boolean neverExpire, List<PollsChoice> choices,
250                            ServiceContext serviceContext)
251                    throws PortalException, SystemException {
252    
253                    // Question
254    
255                    User user = userPersistence.findByPrimaryKey(userId);
256    
257                    Date expirationDate = null;
258    
259                    if (!neverExpire) {
260                            expirationDate = PortalUtil.getDate(
261                                    expirationDateMonth, expirationDateDay, expirationDateYear,
262                                    expirationDateHour, expirationDateMinute, user.getTimeZone(),
263                                    QuestionExpirationDateException.class);
264                    }
265    
266                    validate(titleMap, descriptionMap, choices);
267    
268                    PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
269                            questionId);
270    
271                    question.setModifiedDate(serviceContext.getModifiedDate(null));
272                    question.setTitleMap(titleMap);
273                    question.setDescriptionMap(descriptionMap);
274                    question.setExpirationDate(expirationDate);
275    
276                    pollsQuestionPersistence.update(question);
277    
278                    // Choices
279    
280                    if (choices == null) {
281                            return question;
282                    }
283    
284                    int oldChoicesCount = pollsChoicePersistence.countByQuestionId(
285                            questionId);
286    
287                    if (oldChoicesCount > choices.size()) {
288                            throw new QuestionChoiceException();
289                    }
290    
291                    for (PollsChoice choice : choices) {
292                            String choiceName = choice.getName();
293                            String choiceDescription = choice.getDescription();
294    
295                            choice = pollsChoicePersistence.fetchByQ_N(questionId, choiceName);
296    
297                            if (choice == null) {
298                                    pollsChoiceLocalService.addChoice(
299                                            userId, questionId, choiceName, choiceDescription,
300                                            new ServiceContext());
301                            }
302                            else {
303                                    pollsChoiceLocalService.updateChoice(
304                                            choice.getChoiceId(), questionId, choiceName,
305                                            choiceDescription, new ServiceContext());
306                            }
307                    }
308    
309                    return question;
310            }
311    
312            protected void validate(
313                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
314                            List<PollsChoice> choices)
315                    throws PortalException {
316    
317                    Locale locale = LocaleUtil.getSiteDefault();
318    
319                    String title = titleMap.get(locale);
320    
321                    if (Validator.isNull(title)) {
322                            throw new QuestionTitleException();
323                    }
324    
325                    String description = descriptionMap.get(locale);
326    
327                    if (Validator.isNull(description)) {
328                            throw new QuestionDescriptionException();
329                    }
330    
331                    if ((choices != null) && (choices.size() < 2)) {
332                            throw new QuestionChoiceException();
333                    }
334    
335                    if (choices != null) {
336                            for (PollsChoice choice : choices) {
337                                    String choiceDescription = choice.getDescription(locale);
338    
339                                    if (Validator.isNull(choiceDescription)) {
340                                            throw new QuestionChoiceException();
341                                    }
342                            }
343                    }
344            }
345    
346    }