001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.polls.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.model.User;
027: import com.liferay.portal.model.impl.ResourceImpl;
028: import com.liferay.portal.util.PortalUtil;
029: import com.liferay.portlet.polls.QuestionChoiceException;
030: import com.liferay.portlet.polls.QuestionDescriptionException;
031: import com.liferay.portlet.polls.QuestionExpirationDateException;
032: import com.liferay.portlet.polls.QuestionTitleException;
033: import com.liferay.portlet.polls.model.PollsChoice;
034: import com.liferay.portlet.polls.model.PollsQuestion;
035: import com.liferay.portlet.polls.service.base.PollsQuestionLocalServiceBaseImpl;
036:
037: import java.util.Date;
038: import java.util.Iterator;
039: import java.util.List;
040:
041: /**
042: * <a href="PollsQuestionLocalServiceImpl.java.html"><b><i>View Source</i></b>
043: * </a>
044: *
045: * @author Brian Wing Shun Chan
046: *
047: */
048: public class PollsQuestionLocalServiceImpl extends
049: PollsQuestionLocalServiceBaseImpl {
050:
051: public PollsQuestion addQuestion(long userId, long plid,
052: String title, String description, int expirationDateMonth,
053: int expirationDateDay, int expirationDateYear,
054: int expirationDateHour, int expirationDateMinute,
055: boolean neverExpire, boolean addCommunityPermissions,
056: boolean addGuestPermissions) throws PortalException,
057: SystemException {
058:
059: return addQuestion(null, userId, plid, title, description,
060: expirationDateMonth, expirationDateDay,
061: expirationDateYear, expirationDateHour,
062: expirationDateMinute, neverExpire, null, Boolean
063: .valueOf(addCommunityPermissions), Boolean
064: .valueOf(addGuestPermissions), null, null);
065: }
066:
067: public PollsQuestion addQuestion(String uuid, long userId,
068: long plid, String title, String description,
069: int expirationDateMonth, int expirationDateDay,
070: int expirationDateYear, int expirationDateHour,
071: int expirationDateMinute, boolean neverExpire,
072: boolean addCommunityPermissions, boolean addGuestPermissions)
073: throws PortalException, SystemException {
074:
075: return addQuestion(uuid, userId, plid, title, description,
076: expirationDateMonth, expirationDateDay,
077: expirationDateYear, expirationDateHour,
078: expirationDateMinute, neverExpire, null, Boolean
079: .valueOf(addCommunityPermissions), Boolean
080: .valueOf(addGuestPermissions), null, null);
081: }
082:
083: public PollsQuestion addQuestion(long userId, long plid,
084: String title, String description, int expirationDateMonth,
085: int expirationDateDay, int expirationDateYear,
086: int expirationDateHour, int expirationDateMinute,
087: boolean neverExpire, List choices,
088: boolean addCommunityPermissions, boolean addGuestPermissions)
089: throws PortalException, SystemException {
090:
091: return addQuestion(null, userId, plid, title, description,
092: expirationDateMonth, expirationDateDay,
093: expirationDateYear, expirationDateHour,
094: expirationDateMinute, neverExpire, choices, Boolean
095: .valueOf(addCommunityPermissions), Boolean
096: .valueOf(addGuestPermissions), null, null);
097: }
098:
099: public PollsQuestion addQuestion(long userId, long plid,
100: String title, String description, int expirationDateMonth,
101: int expirationDateDay, int expirationDateYear,
102: int expirationDateHour, int expirationDateMinute,
103: boolean neverExpire, List choices,
104: String[] communityPermissions, String[] guestPermissions)
105: throws PortalException, SystemException {
106:
107: return addQuestion(null, userId, plid, title, description,
108: expirationDateMonth, expirationDateDay,
109: expirationDateYear, expirationDateHour,
110: expirationDateMinute, neverExpire, choices, null, null,
111: communityPermissions, guestPermissions);
112: }
113:
114: public PollsQuestion addQuestion(String uuid, long userId,
115: long plid, String title, String description,
116: int expirationDateMonth, int expirationDateDay,
117: int expirationDateYear, int expirationDateHour,
118: int expirationDateMinute, boolean neverExpire,
119: List choices, Boolean addCommunityPermissions,
120: Boolean addGuestPermissions, String[] communityPermissions,
121: String[] guestPermissions) throws PortalException,
122: SystemException {
123:
124: // Question
125:
126: User user = userPersistence.findByPrimaryKey(userId);
127: long groupId = PortalUtil.getPortletGroupId(plid);
128: Date now = new Date();
129:
130: Date expirationDate = null;
131:
132: if (!neverExpire) {
133: expirationDate = PortalUtil.getDate(expirationDateMonth,
134: expirationDateDay, expirationDateYear,
135: expirationDateHour, expirationDateMinute, user
136: .getTimeZone(),
137: new QuestionExpirationDateException());
138: }
139:
140: validate(title, description, choices);
141:
142: long questionId = counterLocalService.increment();
143:
144: PollsQuestion question = pollsQuestionPersistence
145: .create(questionId);
146:
147: question.setUuid(uuid);
148: question.setGroupId(groupId);
149: question.setCompanyId(user.getCompanyId());
150: question.setUserId(user.getUserId());
151: question.setUserName(user.getFullName());
152: question.setCreateDate(now);
153: question.setModifiedDate(now);
154: question.setTitle(title);
155: question.setDescription(description);
156: question.setExpirationDate(expirationDate);
157:
158: pollsQuestionPersistence.update(question);
159:
160: // Resources
161:
162: if ((addCommunityPermissions != null)
163: && (addGuestPermissions != null)) {
164:
165: addQuestionResources(question, addCommunityPermissions
166: .booleanValue(), addGuestPermissions.booleanValue());
167: } else {
168: addQuestionResources(question, communityPermissions,
169: guestPermissions);
170: }
171:
172: // Choices
173:
174: if (choices != null) {
175: Iterator itr = choices.iterator();
176:
177: while (itr.hasNext()) {
178: PollsChoice choice = (PollsChoice) itr.next();
179:
180: pollsChoiceLocalService.addChoice(questionId, choice
181: .getName(), choice.getDescription());
182: }
183: }
184:
185: return question;
186: }
187:
188: public void addQuestionResources(long questionId,
189: boolean addCommunityPermissions, boolean addGuestPermissions)
190: throws PortalException, SystemException {
191:
192: PollsQuestion question = pollsQuestionPersistence
193: .findByPrimaryKey(questionId);
194:
195: addQuestionResources(question, addCommunityPermissions,
196: addGuestPermissions);
197: }
198:
199: public void addQuestionResources(PollsQuestion question,
200: boolean addCommunityPermissions, boolean addGuestPermissions)
201: throws PortalException, SystemException {
202:
203: resourceLocalService.addResources(question.getCompanyId(),
204: question.getGroupId(), question.getUserId(),
205: PollsQuestion.class.getName(),
206: question.getQuestionId(), false,
207: addCommunityPermissions, addGuestPermissions);
208: }
209:
210: public void addQuestionResources(long questionId,
211: String[] communityPermissions, String[] guestPermissions)
212: throws PortalException, SystemException {
213:
214: PollsQuestion question = pollsQuestionPersistence
215: .findByPrimaryKey(questionId);
216:
217: addQuestionResources(question, communityPermissions,
218: guestPermissions);
219: }
220:
221: public void addQuestionResources(PollsQuestion question,
222: String[] communityPermissions, String[] guestPermissions)
223: throws PortalException, SystemException {
224:
225: resourceLocalService.addModelResources(question.getCompanyId(),
226: question.getGroupId(), question.getUserId(),
227: PollsQuestion.class.getName(),
228: question.getQuestionId(), communityPermissions,
229: guestPermissions);
230: }
231:
232: public void deleteQuestion(long questionId) throws PortalException,
233: SystemException {
234:
235: PollsQuestion question = pollsQuestionPersistence
236: .findByPrimaryKey(questionId);
237:
238: deleteQuestion(question);
239: }
240:
241: public void deleteQuestion(PollsQuestion question)
242: throws PortalException, SystemException {
243:
244: // Votes
245:
246: pollsVotePersistence.removeByQuestionId(question
247: .getQuestionId());
248:
249: // Choices
250:
251: pollsChoicePersistence.removeByQuestionId(question
252: .getQuestionId());
253:
254: // Resources
255:
256: resourceLocalService
257: .deleteResource(question.getCompanyId(),
258: PollsQuestion.class.getName(),
259: ResourceImpl.SCOPE_INDIVIDUAL, question
260: .getQuestionId());
261:
262: // Question
263:
264: pollsQuestionPersistence.remove(question.getQuestionId());
265: }
266:
267: public void deleteQuestions(long groupId) throws PortalException,
268: SystemException {
269:
270: Iterator itr = pollsQuestionPersistence.findByGroupId(groupId)
271: .iterator();
272:
273: while (itr.hasNext()) {
274: PollsQuestion question = (PollsQuestion) itr.next();
275:
276: deleteQuestion(question);
277: }
278: }
279:
280: public PollsQuestion getQuestion(long questionId)
281: throws PortalException, SystemException {
282:
283: return pollsQuestionPersistence.findByPrimaryKey(questionId);
284: }
285:
286: public List getQuestions(long groupId) throws SystemException {
287: return pollsQuestionPersistence.findByGroupId(groupId);
288: }
289:
290: public List getQuestions(long groupId, int begin, int end)
291: throws SystemException {
292:
293: return pollsQuestionPersistence.findByGroupId(groupId, begin,
294: end);
295: }
296:
297: public int getQuestionsCount(long groupId) throws SystemException {
298: return pollsQuestionPersistence.countByGroupId(groupId);
299: }
300:
301: public PollsQuestion updateQuestion(long userId, long questionId,
302: String title, String description, int expirationDateMonth,
303: int expirationDateDay, int expirationDateYear,
304: int expirationDateHour, int expirationDateMinute,
305: boolean neverExpire) throws PortalException,
306: SystemException {
307:
308: return updateQuestion(userId, questionId, title, description,
309: expirationDateMonth, expirationDateDay,
310: expirationDateYear, expirationDateHour,
311: expirationDateMinute, neverExpire, null);
312: }
313:
314: public PollsQuestion updateQuestion(long userId, long questionId,
315: String title, String description, int expirationDateMonth,
316: int expirationDateDay, int expirationDateYear,
317: int expirationDateHour, int expirationDateMinute,
318: boolean neverExpire, List choices) throws PortalException,
319: SystemException {
320:
321: // Question
322:
323: User user = userPersistence.findByPrimaryKey(userId);
324:
325: Date expirationDate = null;
326:
327: if (!neverExpire) {
328: expirationDate = PortalUtil.getDate(expirationDateMonth,
329: expirationDateDay, expirationDateYear,
330: expirationDateHour, expirationDateMinute, user
331: .getTimeZone(),
332: new QuestionExpirationDateException());
333: }
334:
335: validate(title, description, choices);
336:
337: PollsQuestion question = pollsQuestionPersistence
338: .findByPrimaryKey(questionId);
339:
340: question.setModifiedDate(new Date());
341: question.setTitle(title);
342: question.setDescription(description);
343: question.setExpirationDate(expirationDate);
344:
345: pollsQuestionPersistence.update(question);
346:
347: // Choices
348:
349: if (choices != null) {
350: int oldChoicesCount = pollsChoicePersistence
351: .countByQuestionId(questionId);
352:
353: if (oldChoicesCount > choices.size()) {
354: throw new QuestionChoiceException();
355: }
356:
357: Iterator itr = choices.iterator();
358:
359: while (itr.hasNext()) {
360: PollsChoice choice = (PollsChoice) itr.next();
361:
362: String choiceName = choice.getName();
363: String choiceDescription = choice.getDescription();
364:
365: choice = pollsChoicePersistence.fetchByQ_N(questionId,
366: choiceName);
367:
368: if (choice == null) {
369: pollsChoiceLocalService.addChoice(questionId,
370: choiceName, choiceDescription);
371: } else {
372: pollsChoiceLocalService.updateChoice(choice
373: .getChoiceId(), questionId, choiceName,
374: choiceDescription);
375: }
376: }
377: }
378:
379: return question;
380: }
381:
382: protected void validate(String title, String description,
383: List choices) throws PortalException {
384:
385: if (Validator.isNull(title)) {
386: throw new QuestionTitleException();
387: } else if (Validator.isNull(description)) {
388: throw new QuestionDescriptionException();
389: }
390:
391: if ((choices != null) && (choices.size() < 2)) {
392: throw new QuestionChoiceException();
393: }
394: }
395:
396: }
|