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