1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PollsQuestionLocalServiceImpl
50      extends PollsQuestionLocalServiceBaseImpl {
51  
52      public PollsQuestion addQuestion(
53              long userId, String title, String description,
54              int expirationDateMonth, int expirationDateDay,
55              int expirationDateYear, int expirationDateHour,
56              int expirationDateMinute, boolean neverExpire,
57              List<PollsChoice> choices, ServiceContext serviceContext)
58          throws PortalException, SystemException {
59  
60          return addQuestion(
61              null, userId, title, description, expirationDateMonth,
62              expirationDateDay, expirationDateYear, expirationDateHour,
63              expirationDateMinute, neverExpire, choices, serviceContext);
64      }
65  
66      public PollsQuestion addQuestion(
67              String uuid, long userId, String title, String description,
68              int expirationDateMonth, int expirationDateDay,
69              int expirationDateYear, int expirationDateHour,
70              int expirationDateMinute, boolean neverExpire,
71              List<PollsChoice> choices, ServiceContext serviceContext)
72          throws PortalException, SystemException {
73  
74          // Question
75  
76          User user = userPersistence.findByPrimaryKey(userId);
77          long groupId = serviceContext.getScopeGroupId();
78  
79          Date expirationDate = null;
80  
81          if (!neverExpire) {
82              expirationDate = PortalUtil.getDate(
83                  expirationDateMonth, expirationDateDay, expirationDateYear,
84                  expirationDateHour, expirationDateMinute, user.getTimeZone(),
85                  new QuestionExpirationDateException());
86          }
87  
88          Date now = new Date();
89  
90          validate(title, description, choices);
91  
92          long questionId = counterLocalService.increment();
93  
94          PollsQuestion question = pollsQuestionPersistence.create(questionId);
95  
96          question.setUuid(uuid);
97          question.setGroupId(groupId);
98          question.setCompanyId(user.getCompanyId());
99          question.setUserId(user.getUserId());
100         question.setUserName(user.getFullName());
101         question.setCreateDate(now);
102         question.setModifiedDate(now);
103         question.setTitle(title);
104         question.setDescription(description);
105         question.setExpirationDate(expirationDate);
106 
107         pollsQuestionPersistence.update(question, false);
108 
109         // Resources
110 
111         if (serviceContext.getAddCommunityPermissions() ||
112             serviceContext.getAddGuestPermissions()) {
113 
114             addQuestionResources(
115                 question, serviceContext.getAddCommunityPermissions(),
116                 serviceContext.getAddGuestPermissions());
117         }
118         else {
119             addQuestionResources(
120                 question, serviceContext.getCommunityPermissions(),
121                 serviceContext.getGuestPermissions());
122         }
123 
124         // Choices
125 
126         if (choices != null) {
127             for (PollsChoice choice : choices) {
128                 pollsChoiceLocalService.addChoice(
129                     questionId, choice.getName(), choice.getDescription());
130             }
131         }
132 
133         return question;
134     }
135 
136     public void addQuestionResources(
137             long questionId, boolean addCommunityPermissions,
138             boolean addGuestPermissions)
139         throws PortalException, SystemException {
140 
141         PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
142             questionId);
143 
144         addQuestionResources(
145             question, addCommunityPermissions, addGuestPermissions);
146     }
147 
148     public void addQuestionResources(
149             long questionId, String[] communityPermissions,
150             String[] guestPermissions)
151         throws PortalException, SystemException {
152 
153         PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
154             questionId);
155 
156         addQuestionResources(question, communityPermissions, guestPermissions);
157     }
158 
159     public void addQuestionResources(
160             PollsQuestion question, boolean addCommunityPermissions,
161             boolean addGuestPermissions)
162         throws PortalException, SystemException {
163 
164         resourceLocalService.addResources(
165             question.getCompanyId(), question.getGroupId(),
166             question.getUserId(), PollsQuestion.class.getName(),
167             question.getQuestionId(), false, addCommunityPermissions,
168             addGuestPermissions);
169     }
170 
171     public void addQuestionResources(
172             PollsQuestion question, String[] communityPermissions,
173             String[] guestPermissions)
174         throws PortalException, SystemException {
175 
176         resourceLocalService.addModelResources(
177             question.getCompanyId(), question.getGroupId(),
178             question.getUserId(), PollsQuestion.class.getName(),
179             question.getQuestionId(), communityPermissions, guestPermissions);
180     }
181 
182     public void deleteQuestion(long questionId)
183         throws PortalException, SystemException {
184 
185         PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
186             questionId);
187 
188         deleteQuestion(question);
189     }
190 
191     public void deleteQuestion(PollsQuestion question)
192         throws PortalException, SystemException {
193 
194         // Question
195 
196         pollsQuestionPersistence.remove(question);
197 
198         // Resources
199 
200         resourceLocalService.deleteResource(
201             question.getCompanyId(), PollsQuestion.class.getName(),
202             ResourceConstants.SCOPE_INDIVIDUAL, question.getQuestionId());
203 
204         // Choices
205 
206         pollsChoicePersistence.removeByQuestionId(question.getQuestionId());
207 
208         // Votes
209 
210         pollsVotePersistence.removeByQuestionId(question.getQuestionId());
211     }
212 
213     public void deleteQuestions(long groupId)
214         throws PortalException, SystemException {
215 
216         for (PollsQuestion question :
217                 pollsQuestionPersistence.findByGroupId(groupId)) {
218 
219             deleteQuestion(question);
220         }
221     }
222 
223     public PollsQuestion getQuestion(long questionId)
224         throws PortalException, SystemException {
225 
226         return pollsQuestionPersistence.findByPrimaryKey(questionId);
227     }
228 
229     public List<PollsQuestion> getQuestions(long groupId)
230         throws SystemException {
231 
232         return pollsQuestionPersistence.findByGroupId(groupId);
233     }
234 
235     public List<PollsQuestion> getQuestions(long groupId, int start, int end)
236         throws SystemException {
237 
238         return pollsQuestionPersistence.findByGroupId(groupId, start, end);
239     }
240 
241     public int getQuestionsCount(long groupId) throws SystemException {
242         return pollsQuestionPersistence.countByGroupId(groupId);
243     }
244 
245     public PollsQuestion updateQuestion(
246             long userId, long questionId, String title, String description,
247             int expirationDateMonth, int expirationDateDay,
248             int expirationDateYear, int expirationDateHour,
249             int expirationDateMinute, boolean neverExpire)
250         throws PortalException, SystemException {
251 
252         return updateQuestion(
253             userId, questionId, title, description, expirationDateMonth,
254             expirationDateDay, expirationDateYear, expirationDateHour,
255             expirationDateMinute, neverExpire, null, null);
256     }
257 
258     public PollsQuestion updateQuestion(
259             long userId, long questionId, String title, String description,
260             int expirationDateMonth, int expirationDateDay,
261             int expirationDateYear, int expirationDateHour,
262             int expirationDateMinute, boolean neverExpire,
263             List<PollsChoice> choices, ServiceContext serviceContext)
264         throws PortalException, SystemException {
265 
266         // Question
267 
268         User user = userPersistence.findByPrimaryKey(userId);
269 
270         Date expirationDate = null;
271 
272         if (!neverExpire) {
273             expirationDate = PortalUtil.getDate(
274                 expirationDateMonth, expirationDateDay, expirationDateYear,
275                 expirationDateHour, expirationDateMinute, user.getTimeZone(),
276                 new QuestionExpirationDateException());
277         }
278 
279         validate(title, description, choices);
280 
281         PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(
282             questionId);
283 
284         question.setModifiedDate(new Date());
285         question.setTitle(title);
286         question.setDescription(description);
287         question.setExpirationDate(expirationDate);
288 
289         pollsQuestionPersistence.update(question, false);
290 
291         // Choices
292 
293         if (choices != null) {
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(
306                     questionId, choiceName);
307 
308                 if (choice == null) {
309                     pollsChoiceLocalService.addChoice(
310                         questionId, choiceName, choiceDescription);
311                 }
312                 else {
313                     pollsChoiceLocalService.updateChoice(
314                         choice.getChoiceId(), questionId, choiceName,
315                         choiceDescription);
316                 }
317             }
318         }
319 
320         return question;
321     }
322 
323     protected void validate(
324             String title, String description, List<PollsChoice> choices)
325         throws PortalException {
326 
327         if (Validator.isNull(title)) {
328             throw new QuestionTitleException();
329         }
330         else if (Validator.isNull(description)) {
331             throw new QuestionDescriptionException();
332         }
333 
334         if ((choices != null) && (choices.size() < 2)) {
335             throw new QuestionChoiceException();
336         }
337     }
338 
339 }