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