1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.portal.model.ResourceConstants;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.polls.QuestionChoiceException;
33  import com.liferay.portlet.polls.QuestionDescriptionException;
34  import com.liferay.portlet.polls.QuestionExpirationDateException;
35  import com.liferay.portlet.polls.QuestionTitleException;
36  import com.liferay.portlet.polls.model.PollsChoice;
37  import com.liferay.portlet.polls.model.PollsQuestion;
38  import com.liferay.portlet.polls.service.base.PollsQuestionLocalServiceBaseImpl;
39  
40  import java.util.Date;
41  import java.util.List;
42  
43  /**
44   * <a href="PollsQuestionLocalServiceImpl.java.html"><b><i>View Source</i></b>
45   * </a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class PollsQuestionLocalServiceImpl
51      extends PollsQuestionLocalServiceBaseImpl {
52  
53      public PollsQuestion addQuestion(
54              long userId, String title, String description,
55              int expirationDateMonth, int expirationDateDay,
56              int expirationDateYear, int expirationDateHour,
57              int expirationDateMinute, boolean neverExpire,
58              List<PollsChoice> choices, ServiceContext serviceContext)
59          throws PortalException, SystemException {
60  
61          return addQuestion(
62              null, userId, title, description, expirationDateMonth,
63              expirationDateDay, expirationDateYear, expirationDateHour,
64              expirationDateMinute, neverExpire, choices, serviceContext);
65      }
66  
67      public PollsQuestion addQuestion(
68              String uuid, long userId, String title, String description,
69              int expirationDateMonth, int expirationDateDay,
70              int expirationDateYear, int expirationDateHour,
71              int expirationDateMinute, boolean neverExpire,
72              List<PollsChoice> choices, ServiceContext serviceContext)
73          throws PortalException, SystemException {
74  
75          // Question
76  
77          User user = userPersistence.findByPrimaryKey(userId);
78          long groupId = serviceContext.getScopeGroupId();
79  
80          Date expirationDate = null;
81  
82          if (!neverExpire) {
83              expirationDate = PortalUtil.getDate(
84                  expirationDateMonth, expirationDateDay, expirationDateYear,
85                  expirationDateHour, expirationDateMinute, user.getTimeZone(),
86                  new QuestionExpirationDateException());
87          }
88  
89          Date now = new Date();
90  
91          validate(title, description, choices);
92  
93          long questionId = counterLocalService.increment();
94  
95          PollsQuestion question = pollsQuestionPersistence.create(questionId);
96  
97          question.setUuid(uuid);
98          question.setGroupId(groupId);
99          question.setCompanyId(user.getCompanyId());
100         question.setUserId(user.getUserId());
101         question.setUserName(user.getFullName());
102         question.setCreateDate(now);
103         question.setModifiedDate(now);
104         question.setTitle(title);
105         question.setDescription(description);
106         question.setExpirationDate(expirationDate);
107 
108         pollsQuestionPersistence.update(question, false);
109 
110         // Resources
111 
112         if (serviceContext.getAddCommunityPermissions() ||
113             serviceContext.getAddGuestPermissions()) {
114 
115             addQuestionResources(
116                 question, serviceContext.getAddCommunityPermissions(),
117                 serviceContext.getAddGuestPermissions());
118         }
119         else {
120             addQuestionResources(
121                 question, serviceContext.getCommunityPermissions(),
122                 serviceContext.getGuestPermissions());
123         }
124 
125         // Choices
126 
127         if (choices != null) {
128             for (PollsChoice choice : choices) {
129                 pollsChoiceLocalService.addChoice(
130                     questionId, choice.getName(), choice.getDescription());
131             }
132         }
133 
134         return question;
135     }
136 
137     public void addQuestionResources(
138             long questionId, boolean addCommunityPermissions,
139             boolean addGuestPermissions)
140         throws PortalException, SystemException {
141 
142         PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
143             questionId);
144 
145         addQuestionResources(
146             question, addCommunityPermissions, addGuestPermissions);
147     }
148 
149     public void addQuestionResources(
150             PollsQuestion question, boolean addCommunityPermissions,
151             boolean addGuestPermissions)
152         throws PortalException, SystemException {
153 
154         resourceLocalService.addResources(
155             question.getCompanyId(), question.getGroupId(),
156             question.getUserId(), PollsQuestion.class.getName(),
157             question.getQuestionId(), false, addCommunityPermissions,
158             addGuestPermissions);
159     }
160 
161     public void addQuestionResources(
162             long questionId, String[] communityPermissions,
163             String[] guestPermissions)
164         throws PortalException, SystemException {
165 
166         PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
167             questionId);
168 
169         addQuestionResources(question, communityPermissions, guestPermissions);
170     }
171 
172     public void addQuestionResources(
173             PollsQuestion question, String[] communityPermissions,
174             String[] guestPermissions)
175         throws PortalException, SystemException {
176 
177         resourceLocalService.addModelResources(
178             question.getCompanyId(), question.getGroupId(),
179             question.getUserId(), PollsQuestion.class.getName(),
180             question.getQuestionId(), communityPermissions, guestPermissions);
181     }
182 
183     public void deleteQuestion(long questionId)
184         throws PortalException, SystemException {
185 
186         PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
187             questionId);
188 
189         deleteQuestion(question);
190     }
191 
192     public void deleteQuestion(PollsQuestion question)
193         throws PortalException, SystemException {
194 
195         // Votes
196 
197         pollsVotePersistence.removeByQuestionId(question.getQuestionId());
198 
199         // Choices
200 
201         pollsChoicePersistence.removeByQuestionId(question.getQuestionId());
202 
203         // Resources
204 
205         resourceLocalService.deleteResource(
206             question.getCompanyId(), PollsQuestion.class.getName(),
207             ResourceConstants.SCOPE_INDIVIDUAL, question.getQuestionId());
208 
209         // Question
210 
211         pollsQuestionPersistence.remove(question);
212     }
213 
214     public void deleteQuestions(long groupId)
215         throws PortalException, SystemException {
216 
217         for (PollsQuestion question :
218                 pollsQuestionPersistence.findByGroupId(groupId)) {
219 
220             deleteQuestion(question);
221         }
222     }
223 
224     public PollsQuestion getQuestion(long questionId)
225         throws PortalException, SystemException {
226 
227         return pollsQuestionPersistence.findByPrimaryKey(questionId);
228     }
229 
230     public List<PollsQuestion> getQuestions(long groupId)
231         throws SystemException {
232 
233         return pollsQuestionPersistence.findByGroupId(groupId);
234     }
235 
236     public List<PollsQuestion> getQuestions(long groupId, int start, int end)
237         throws SystemException {
238 
239         return pollsQuestionPersistence.findByGroupId(groupId, start, end);
240     }
241 
242     public int getQuestionsCount(long groupId) throws SystemException {
243         return pollsQuestionPersistence.countByGroupId(groupId);
244     }
245 
246     public PollsQuestion updateQuestion(
247             long userId, long questionId, String title, String description,
248             int expirationDateMonth, int expirationDateDay,
249             int expirationDateYear, int expirationDateHour,
250             int expirationDateMinute, boolean neverExpire)
251         throws PortalException, SystemException {
252 
253         return updateQuestion(
254             userId, questionId, title, description, expirationDateMonth,
255             expirationDateDay, expirationDateYear, expirationDateHour,
256             expirationDateMinute, neverExpire, null, null);
257     }
258 
259     public PollsQuestion updateQuestion(
260             long userId, long questionId, String title, String description,
261             int expirationDateMonth, int expirationDateDay,
262             int expirationDateYear, int expirationDateHour,
263             int expirationDateMinute, boolean neverExpire,
264             List<PollsChoice> choices, ServiceContext serviceContext)
265         throws PortalException, SystemException {
266 
267         // Question
268 
269         User user = userPersistence.findByPrimaryKey(userId);
270 
271         Date expirationDate = null;
272 
273         if (!neverExpire) {
274             expirationDate = PortalUtil.getDate(
275                 expirationDateMonth, expirationDateDay, expirationDateYear,
276                 expirationDateHour, expirationDateMinute, user.getTimeZone(),
277                 new QuestionExpirationDateException());
278         }
279 
280         validate(title, description, choices);
281 
282         PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
283             questionId);
284 
285         question.setModifiedDate(new Date());
286         question.setTitle(title);
287         question.setDescription(description);
288         question.setExpirationDate(expirationDate);
289 
290         pollsQuestionPersistence.update(question, false);
291 
292         // Choices
293 
294         int oldChoicesCount = pollsChoicePersistence.countByQuestionId(
295             questionId);
296 
297         if (oldChoicesCount > choices.size()) {
298             throw new QuestionChoiceException();
299         }
300 
301         for (PollsChoice choice : choices) {
302             String choiceName = choice.getName();
303             String choiceDescription = choice.getDescription();
304 
305             choice = pollsChoicePersistence.fetchByQ_N(questionId, choiceName);
306 
307             if (choice == null) {
308                 pollsChoiceLocalService.addChoice(
309                     questionId, choiceName, choiceDescription);
310             }
311             else {
312                 pollsChoiceLocalService.updateChoice(
313                     choice.getChoiceId(), questionId, choiceName,
314                     choiceDescription);
315             }
316         }
317 
318         return question;
319     }
320 
321     protected void validate(
322             String title, String description, List<PollsChoice> choices)
323         throws PortalException {
324 
325         if (Validator.isNull(title)) {
326             throw new QuestionTitleException();
327         }
328         else if (Validator.isNull(description)) {
329             throw new QuestionDescriptionException();
330         }
331 
332         if (choices != null && choices.size() < 2) {
333             throw new QuestionChoiceException();
334         }
335     }
336 
337 }