1
22
23 package com.liferay.portlet.messageboards.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.security.permission.ActionKeys;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.model.Company;
30 import com.liferay.portal.security.auth.PrincipalException;
31 import com.liferay.portal.service.impl.PrincipalBean;
32 import com.liferay.portal.service.persistence.CompanyUtil;
33 import com.liferay.portal.theme.ThemeDisplay;
34 import com.liferay.portlet.messageboards.model.MBCategory;
35 import com.liferay.portlet.messageboards.model.MBMessage;
36 import com.liferay.portlet.messageboards.model.MBMessageDisplay;
37 import com.liferay.portlet.messageboards.model.MBThread;
38 import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
39 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
40 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
41 import com.liferay.portlet.messageboards.service.MBMessageService;
42 import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
43 import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
44 import com.liferay.portlet.messageboards.service.permission.MBDiscussionPermission;
45 import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
46 import com.liferay.portlet.messageboards.service.persistence.MBMessageUtil;
47 import com.liferay.portlet.messageboards.util.MBUtil;
48 import com.liferay.portlet.messageboards.util.comparator.MessageCreateDateComparator;
49 import com.liferay.util.RSSUtil;
50
51 import com.sun.syndication.feed.synd.SyndContent;
52 import com.sun.syndication.feed.synd.SyndContentImpl;
53 import com.sun.syndication.feed.synd.SyndEntry;
54 import com.sun.syndication.feed.synd.SyndEntryImpl;
55 import com.sun.syndication.feed.synd.SyndFeed;
56 import com.sun.syndication.feed.synd.SyndFeedImpl;
57 import com.sun.syndication.io.FeedException;
58
59 import java.io.IOException;
60
61 import java.util.ArrayList;
62 import java.util.Iterator;
63 import java.util.List;
64
65 import javax.portlet.PortletPreferences;
66
67
73 public class MBMessageServiceImpl
74 extends PrincipalBean implements MBMessageService {
75
76 public MBMessage addDiscussionMessage(
77 long groupId, String className, long classPK, long threadId,
78 long parentMessageId, String subject, String body,
79 ThemeDisplay themeDisplay)
80 throws PortalException, SystemException {
81
82 MBDiscussionPermission.check(
83 getPermissionChecker(), groupId, className, classPK,
84 ActionKeys.ADD_DISCUSSION);
85
86 return MBMessageLocalServiceUtil.addDiscussionMessage(
87 getUserId(), groupId, className, classPK, threadId, parentMessageId,
88 subject, body, themeDisplay);
89 }
90
91 public MBMessage addMessage(
92 long categoryId, String subject, String body, List files,
93 boolean anonymous, double priority, String[] tagsEntries,
94 boolean addCommunityPermissions, boolean addGuestPermissions)
95 throws PortalException, SystemException {
96
97 MBCategoryPermission.check(
98 getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
99
100 if (!MBCategoryPermission.contains(
101 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
102
103 files.clear();
104 }
105
106 if (!MBCategoryPermission.contains(
107 getPermissionChecker(), categoryId,
108 ActionKeys.UPDATE_THREAD_PRIORITY)) {
109
110 priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
111 }
112
113 return MBMessageLocalServiceUtil.addMessage(
114 getGuestOrUserId(), categoryId, subject, body, files, anonymous,
115 priority, tagsEntries, null, addCommunityPermissions,
116 addGuestPermissions);
117 }
118
119 public MBMessage addMessage(
120 long categoryId, String subject, String body, List files,
121 boolean anonymous, double priority, String[] tagsEntries,
122 PortletPreferences prefs, boolean addCommunityPermissions,
123 boolean addGuestPermissions)
124 throws PortalException, SystemException {
125
126 MBCategoryPermission.check(
127 getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
128
129 if (!MBCategoryPermission.contains(
130 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
131
132 files.clear();
133 }
134
135 if (!MBCategoryPermission.contains(
136 getPermissionChecker(), categoryId,
137 ActionKeys.UPDATE_THREAD_PRIORITY)) {
138
139 priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
140 }
141
142 return MBMessageLocalServiceUtil.addMessage(
143 getGuestOrUserId(), categoryId, subject, body, files, anonymous,
144 priority, tagsEntries, prefs, addCommunityPermissions,
145 addGuestPermissions);
146 }
147
148 public MBMessage addMessage(
149 long categoryId, long threadId, long parentMessageId,
150 String subject, String body, List files, boolean anonymous,
151 double priority, String[] tagsEntries,
152 boolean addCommunityPermissions, boolean addGuestPermissions)
153 throws PortalException, SystemException {
154
155 checkReplyToPermission(categoryId, parentMessageId);
156
157 if (!MBCategoryPermission.contains(
158 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
159
160 files.clear();
161 }
162
163 if (!MBCategoryPermission.contains(
164 getPermissionChecker(), categoryId,
165 ActionKeys.UPDATE_THREAD_PRIORITY)) {
166
167 priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
168 }
169
170 return MBMessageLocalServiceUtil.addMessage(
171 getGuestOrUserId(), categoryId, threadId, parentMessageId, subject,
172 body, files, anonymous, priority, tagsEntries, null,
173 addCommunityPermissions, addGuestPermissions);
174 }
175
176 public MBMessage addMessage(
177 long categoryId, long threadId, long parentMessageId,
178 String subject, String body, List files, boolean anonymous,
179 double priority, String[] tagsEntries, PortletPreferences prefs,
180 boolean addCommunityPermissions, boolean addGuestPermissions)
181 throws PortalException, SystemException {
182
183 checkReplyToPermission(categoryId, parentMessageId);
184
185 if (!MBCategoryPermission.contains(
186 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
187
188 files.clear();
189 }
190
191 if (!MBCategoryPermission.contains(
192 getPermissionChecker(), categoryId,
193 ActionKeys.UPDATE_THREAD_PRIORITY)) {
194
195 priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
196 }
197
198 return MBMessageLocalServiceUtil.addMessage(
199 getGuestOrUserId(), categoryId, threadId, parentMessageId, subject,
200 body, files, anonymous, priority, tagsEntries, prefs,
201 addCommunityPermissions, addGuestPermissions);
202 }
203
204 public MBMessage addMessage(
205 long categoryId, String subject, String body, List files,
206 boolean anonymous, double priority, String[] tagsEntries,
207 String[] communityPermissions, String[] guestPermissions)
208 throws PortalException, SystemException {
209
210 MBCategoryPermission.check(
211 getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
212
213 if (!MBCategoryPermission.contains(
214 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
215
216 files.clear();
217 }
218
219 if (!MBCategoryPermission.contains(
220 getPermissionChecker(), categoryId,
221 ActionKeys.UPDATE_THREAD_PRIORITY)) {
222
223 priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
224 }
225
226 return MBMessageLocalServiceUtil.addMessage(
227 getGuestOrUserId(), categoryId, subject, body, files, anonymous,
228 priority, tagsEntries, null, communityPermissions,
229 guestPermissions);
230 }
231
232 public MBMessage addMessage(
233 long categoryId, String subject, String body, List files,
234 boolean anonymous, double priority, String[] tagsEntries,
235 PortletPreferences prefs, String[] communityPermissions,
236 String[] guestPermissions)
237 throws PortalException, SystemException {
238
239 MBCategoryPermission.check(
240 getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
241
242 if (!MBCategoryPermission.contains(
243 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
244
245 files.clear();
246 }
247
248 if (!MBCategoryPermission.contains(
249 getPermissionChecker(), categoryId,
250 ActionKeys.UPDATE_THREAD_PRIORITY)) {
251
252 priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
253 }
254
255 return MBMessageLocalServiceUtil.addMessage(
256 getGuestOrUserId(), categoryId, subject, body, files, anonymous,
257 priority, tagsEntries, prefs, communityPermissions,
258 guestPermissions);
259 }
260
261 public MBMessage addMessage(
262 long categoryId, long threadId, long parentMessageId,
263 String subject, String body, List files, boolean anonymous,
264 double priority, String[] tagsEntries,
265 String[] communityPermissions, String[] guestPermissions)
266 throws PortalException, SystemException {
267
268 checkReplyToPermission(categoryId, parentMessageId);
269
270 if (!MBCategoryPermission.contains(
271 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
272
273 files.clear();
274 }
275
276 if (!MBCategoryPermission.contains(
277 getPermissionChecker(), categoryId,
278 ActionKeys.UPDATE_THREAD_PRIORITY)) {
279
280 priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
281 }
282
283 return MBMessageLocalServiceUtil.addMessage(
284 getGuestOrUserId(), categoryId, threadId, parentMessageId, subject,
285 body, files, anonymous, priority, tagsEntries, null,
286 communityPermissions, guestPermissions);
287 }
288
289 public MBMessage addMessage(
290 long categoryId, long threadId, long parentMessageId,
291 String subject, String body, List files, boolean anonymous,
292 double priority, String[] tagsEntries, PortletPreferences prefs,
293 String[] communityPermissions, String[] guestPermissions)
294 throws PortalException, SystemException {
295
296 checkReplyToPermission(categoryId, parentMessageId);
297
298 if (!MBCategoryPermission.contains(
299 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
300
301 files.clear();
302 }
303
304 if (!MBCategoryPermission.contains(
305 getPermissionChecker(), categoryId,
306 ActionKeys.UPDATE_THREAD_PRIORITY)) {
307
308 priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
309 }
310
311 return MBMessageLocalServiceUtil.addMessage(
312 getGuestOrUserId(), categoryId, threadId, parentMessageId, subject,
313 body, files, anonymous, priority, tagsEntries, prefs,
314 communityPermissions, guestPermissions);
315 }
316
317 public void deleteDiscussionMessage(
318 long groupId, String className, long classPK, long messageId)
319 throws PortalException, SystemException {
320
321 MBDiscussionPermission.check(
322 getPermissionChecker(), groupId, className, classPK,
323 ActionKeys.DELETE_DISCUSSION);
324
325 MBMessageLocalServiceUtil.deleteDiscussionMessage(messageId);
326 }
327
328 public void deleteMessage(long messageId)
329 throws PortalException, SystemException {
330
331 MBMessagePermission.check(
332 getPermissionChecker(), messageId, ActionKeys.DELETE);
333
334 MBMessageLocalServiceUtil.deleteMessage(messageId);
335 }
336
337 public String getCategoryMessagesRSS(
338 long categoryId, int max, String type, double version,
339 String feedURL, String entryURL, PortletPreferences prefs)
340 throws PortalException, SystemException {
341
342 MBCategory category = MBCategoryLocalServiceUtil.getCategory(
343 categoryId);
344
345 String name = category.getName();
346 String description = category.getDescription();
347
348 List messages = new ArrayList();
349
350 MessageCreateDateComparator comparator =
351 new MessageCreateDateComparator(false, false);
352
353 Iterator itr = MBMessageLocalServiceUtil.getCategoryMessages(
354 categoryId, 0, _MAX_END, comparator).iterator();
355
356 while (itr.hasNext() && (messages.size() < max)) {
357 MBMessage message = (MBMessage)itr.next();
358
359 if (MBMessagePermission.contains(
360 getPermissionChecker(), message, ActionKeys.VIEW)) {
361
362 messages.add(message);
363 }
364 }
365
366 return exportToRSS(
367 name, description, type, version, feedURL, entryURL, messages,
368 prefs);
369 }
370
371 public String getCompanyMessagesRSS(
372 long companyId, int max, String type, double version,
373 String feedURL, String entryURL, PortletPreferences prefs)
374 throws PortalException, SystemException {
375
376 Company company = CompanyUtil.findByPrimaryKey(companyId);
377
378 String name = company.getName();
379 String description = company.getName();
380
381 List messages = new ArrayList();
382
383 MessageCreateDateComparator comparator =
384 new MessageCreateDateComparator(false, false);
385
386 Iterator itr = MBMessageLocalServiceUtil.getCompanyMessages(
387 companyId, 0, _MAX_END, comparator).iterator();
388
389 while (itr.hasNext() && (messages.size() < max)) {
390 MBMessage message = (MBMessage)itr.next();
391
392 if (MBMessagePermission.contains(
393 getPermissionChecker(), message, ActionKeys.VIEW)) {
394
395 messages.add(message);
396 }
397 }
398
399 return exportToRSS(
400 name, description, type, version, feedURL, entryURL, messages,
401 prefs);
402 }
403
404 public String getGroupMessagesRSS(
405 long groupId, int max, String type, double version,
406 String feedURL, String entryURL, PortletPreferences prefs)
407 throws PortalException, SystemException {
408
409 String name = StringPool.BLANK;
410 String description = StringPool.BLANK;
411
412 List messages = new ArrayList();
413
414 MessageCreateDateComparator comparator =
415 new MessageCreateDateComparator(false, true);
416
417 Iterator itr = MBMessageLocalServiceUtil.getGroupMessages(
418 groupId, 0, _MAX_END, comparator).iterator();
419
420 while (itr.hasNext() && (messages.size() < max)) {
421 MBMessage message = (MBMessage)itr.next();
422
423 if (MBMessagePermission.contains(
424 getPermissionChecker(), message, ActionKeys.VIEW)) {
425
426 messages.add(message);
427 }
428 }
429
430 if (messages.size() > 0) {
431 MBMessage message = (MBMessage)messages.get(messages.size() - 1);
432
433 name = message.getSubject();
434 description = message.getSubject();
435 }
436
437 return exportToRSS(
438 name, description, type, version, feedURL, entryURL, messages,
439 prefs);
440 }
441
442 public MBMessage getMessage(long messageId)
443 throws PortalException, SystemException {
444
445 MBMessagePermission.check(
446 getPermissionChecker(), messageId, ActionKeys.VIEW);
447
448 return MBMessageLocalServiceUtil.getMessage(messageId);
449 }
450
451 public MBMessageDisplay getMessageDisplay(long messageId)
452 throws PortalException, SystemException {
453
454 MBMessagePermission.check(
455 getPermissionChecker(), messageId, ActionKeys.VIEW);
456
457 return MBMessageLocalServiceUtil.getMessageDisplay(messageId);
458 }
459
460 public String getThreadMessagesRSS(
461 long threadId, int max, String type, double version,
462 String feedURL, String entryURL, PortletPreferences prefs)
463 throws PortalException, SystemException {
464
465 String name = StringPool.BLANK;
466 String description = StringPool.BLANK;
467
468 List messages = new ArrayList();
469
470 MessageCreateDateComparator comparator =
471 new MessageCreateDateComparator(false, true);
472
473 Iterator itr = MBMessageLocalServiceUtil.getThreadMessages(
474 threadId, comparator).iterator();
475
476 while (itr.hasNext() && (messages.size() < max)) {
477 MBMessage message = (MBMessage)itr.next();
478
479 if (MBMessagePermission.contains(
480 getPermissionChecker(), message, ActionKeys.VIEW)) {
481
482 messages.add(message);
483 }
484 }
485
486 if (messages.size() > 0) {
487 MBMessage message = (MBMessage)messages.get(messages.size() - 1);
488
489 name = message.getSubject();
490 description = message.getSubject();
491 }
492
493 return exportToRSS(
494 name, description, type, version, feedURL, entryURL, messages,
495 prefs);
496 }
497
498 public void subscribeMessage(long messageId)
499 throws PortalException, SystemException {
500
501 MBMessagePermission.check(
502 getPermissionChecker(), messageId, ActionKeys.SUBSCRIBE);
503
504 MBMessageLocalServiceUtil.subscribeMessage(getUserId(), messageId);
505 }
506
507 public void unsubscribeMessage(long messageId)
508 throws PortalException, SystemException {
509
510 MBMessagePermission.check(
511 getPermissionChecker(), messageId, ActionKeys.SUBSCRIBE);
512
513 MBMessageLocalServiceUtil.unsubscribeMessage(getUserId(), messageId);
514 }
515
516 public MBMessage updateDiscussionMessage(
517 long groupId, String className, long classPK, long messageId,
518 String subject, String body)
519 throws PortalException, SystemException {
520
521 MBDiscussionPermission.check(
522 getPermissionChecker(), groupId, className, classPK,
523 ActionKeys.UPDATE_DISCUSSION);
524
525 return MBMessageLocalServiceUtil.updateDiscussionMessage(
526 messageId, subject, body);
527 }
528
529 public MBMessage updateMessage(
530 long messageId, long categoryId, String subject, String body,
531 List files, double priority, String[] tagsEntries)
532 throws PortalException, SystemException {
533
534 MBMessagePermission.check(
535 getPermissionChecker(), messageId, ActionKeys.UPDATE);
536
537 if (!MBCategoryPermission.contains(
538 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
539
540 files.clear();
541 }
542
543 if (!MBCategoryPermission.contains(
544 getPermissionChecker(), categoryId,
545 ActionKeys.UPDATE_THREAD_PRIORITY)) {
546
547 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
548
549 MBThread thread = MBThreadLocalServiceUtil.getThread(
550 message.getThreadId());
551
552 priority = thread.getPriority();
553 }
554
555 return MBMessageLocalServiceUtil.updateMessage(
556 messageId, categoryId, subject, body, files, priority, tagsEntries,
557 null);
558 }
559
560 public MBMessage updateMessage(
561 long messageId, long categoryId, String subject, String body,
562 List files, double priority, String[] tagsEntries,
563 PortletPreferences prefs)
564 throws PortalException, SystemException {
565
566 MBMessagePermission.check(
567 getPermissionChecker(), messageId, ActionKeys.UPDATE);
568
569 if (!MBCategoryPermission.contains(
570 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
571
572 files.clear();
573 }
574
575 if (!MBCategoryPermission.contains(
576 getPermissionChecker(), categoryId,
577 ActionKeys.UPDATE_THREAD_PRIORITY)) {
578
579 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
580
581 MBThread thread = MBThreadLocalServiceUtil.getThread(
582 message.getThreadId());
583
584 priority = thread.getPriority();
585 }
586
587 return MBMessageLocalServiceUtil.updateMessage(
588 messageId, categoryId, subject, body, files, priority, tagsEntries,
589 prefs);
590 }
591
592 protected void checkReplyToPermission(long categoryId, long parentMessageId)
593 throws PortalException, SystemException {
594
595 if (parentMessageId > 0) {
596 if (MBCategoryPermission.contains(
597 getPermissionChecker(), categoryId,
598 ActionKeys.ADD_MESSAGE)) {
599
600 return;
601 }
602
603 MBMessage parentMessage = MBMessageUtil.fetchByPrimaryKey(
604 parentMessageId);
605
606 if ((parentMessage == null) ||
607 !MBCategoryPermission.contains(
608 getPermissionChecker(), categoryId,
609 ActionKeys.REPLY_TO_MESSAGE)) {
610
611 throw new PrincipalException();
612 }
613 }
614 else {
615 MBCategoryPermission.check(
616 getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
617 }
618 }
619
620 protected String exportToRSS(
621 String name, String description, String type, double version,
622 String feedURL, String entryURL, List messages,
623 PortletPreferences prefs)
624 throws SystemException {
625
626 SyndFeed syndFeed = new SyndFeedImpl();
627
628 syndFeed.setFeedType(type + "_" + version);
629
630 syndFeed.setTitle(name);
631 syndFeed.setLink(feedURL);
632 syndFeed.setDescription(description);
633
634 List entries = new ArrayList();
635
636 syndFeed.setEntries(entries);
637
638 int rssContentLength = MBUtil.getRSSContentLength(prefs);
639
640 Iterator itr = messages.iterator();
641
642 while (itr.hasNext()) {
643 MBMessage message = (MBMessage)itr.next();
644
645 SyndEntry syndEntry = new SyndEntryImpl();
646
647 if (!message.isAnonymous()) {
648 String userName = MBUtil.getUserName(
649 message.getUserId(), message.getUserName(), prefs);
650
651 syndEntry.setAuthor(userName);
652 }
653
654 syndEntry.setTitle(message.getSubject());
655 syndEntry.setLink(
656 entryURL + "&messageId=" + message.getMessageId());
657 syndEntry.setPublishedDate(message.getCreateDate());
658
659 SyndContent syndContent = new SyndContentImpl();
660
661 syndContent.setType("html");
662 syndContent.setValue(message.getBody());
663
664 syndEntry.setDescription(syndContent);
665
666 entries.add(syndEntry);
667 }
668
669 try {
670 return RSSUtil.export(syndFeed);
671 }
672 catch (FeedException fe) {
673 throw new SystemException(fe);
674 }
675 catch (IOException ioe) {
676 throw new SystemException(ioe);
677 }
678 }
679
680 private static final int _MAX_END = 200;
681
682 }