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.messageboards.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.HtmlUtil;
31  import com.liferay.portal.kernel.util.ObjectValuePair;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.model.Company;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.security.auth.PrincipalException;
37  import com.liferay.portal.security.permission.ActionKeys;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.PortalUtil;
40  import com.liferay.portal.util.PropsKeys;
41  import com.liferay.portal.util.PropsUtil;
42  import com.liferay.portlet.messageboards.model.MBCategory;
43  import com.liferay.portlet.messageboards.model.MBMessage;
44  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
45  import com.liferay.portlet.messageboards.model.MBThread;
46  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
47  import com.liferay.portlet.messageboards.service.base.MBMessageServiceBaseImpl;
48  import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
49  import com.liferay.portlet.messageboards.service.permission.MBDiscussionPermission;
50  import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
51  import com.liferay.portlet.messageboards.util.BBCodeUtil;
52  import com.liferay.portlet.messageboards.util.comparator.MessageCreateDateComparator;
53  import com.liferay.util.RSSUtil;
54  
55  import com.sun.syndication.feed.synd.SyndContent;
56  import com.sun.syndication.feed.synd.SyndContentImpl;
57  import com.sun.syndication.feed.synd.SyndEntry;
58  import com.sun.syndication.feed.synd.SyndEntryImpl;
59  import com.sun.syndication.feed.synd.SyndFeed;
60  import com.sun.syndication.feed.synd.SyndFeedImpl;
61  import com.sun.syndication.io.FeedException;
62  
63  import java.util.ArrayList;
64  import java.util.Iterator;
65  import java.util.List;
66  
67  import javax.portlet.PortletPreferences;
68  
69  /**
70   * <a href="MBMessageServiceImpl.java.html"><b><i>View Source</i></b></a>
71   *
72   * @author Brian Wing Shun Chan
73   *
74   */
75  public class MBMessageServiceImpl extends MBMessageServiceBaseImpl {
76  
77      public MBMessage addDiscussionMessage(
78              long groupId, String className, long classPK, long threadId,
79              long parentMessageId, String subject, String body,
80              ThemeDisplay themeDisplay)
81          throws PortalException, SystemException {
82  
83          User user = getUser();
84  
85          MBDiscussionPermission.check(
86              getPermissionChecker(), user.getCompanyId(), groupId, className,
87              classPK, user.getUserId(), ActionKeys.ADD_DISCUSSION);
88  
89          return mbMessageLocalService.addDiscussionMessage(
90              getUserId(), null, groupId, className, classPK, threadId,
91              parentMessageId, subject, body, themeDisplay);
92      }
93  
94      public MBMessage addMessage(
95              long categoryId, String subject, String body,
96              List<ObjectValuePair<String, byte[]>> files, boolean anonymous,
97              double priority, String[] tagsEntries,
98              boolean addCommunityPermissions, boolean addGuestPermissions)
99          throws PortalException, SystemException {
100 
101         MBCategoryPermission.check(
102             getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
103 
104         if (!MBCategoryPermission.contains(
105                 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
106 
107             files.clear();
108         }
109 
110         if (!MBCategoryPermission.contains(
111                 getPermissionChecker(), categoryId,
112                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
113 
114             priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
115         }
116 
117         return mbMessageLocalService.addMessage(
118             getGuestOrUserId(), null, categoryId, subject, body, files,
119             anonymous, priority, tagsEntries, null, addCommunityPermissions,
120             addGuestPermissions, null);
121     }
122 
123     public MBMessage addMessage(
124             long categoryId, String subject, String body,
125             List<ObjectValuePair<String, byte[]>> files, boolean anonymous,
126             double priority, String[] tagsEntries,
127             String[] communityPermissions, String[] guestPermissions)
128         throws PortalException, SystemException {
129 
130         MBCategoryPermission.check(
131             getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
132 
133         if (!MBCategoryPermission.contains(
134                 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
135 
136             files.clear();
137         }
138 
139         if (!MBCategoryPermission.contains(
140                 getPermissionChecker(), categoryId,
141                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
142 
143             priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
144         }
145 
146         return mbMessageLocalService.addMessage(
147             getGuestOrUserId(), null, categoryId, subject, body, files,
148             anonymous, priority, tagsEntries, null, communityPermissions,
149             guestPermissions, null);
150     }
151 
152     public MBMessage addMessage(
153             long categoryId, String subject, String body,
154             List<ObjectValuePair<String, byte[]>> files, boolean anonymous,
155             double priority, String[] tagsEntries, PortletPreferences prefs,
156             boolean addCommunityPermissions, boolean addGuestPermissions,
157             ThemeDisplay themeDisplay)
158         throws PortalException, SystemException {
159 
160         MBCategoryPermission.check(
161             getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
162 
163         if (!MBCategoryPermission.contains(
164                 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
165 
166             files.clear();
167         }
168 
169         if (!MBCategoryPermission.contains(
170                 getPermissionChecker(), categoryId,
171                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
172 
173             priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
174         }
175 
176         return mbMessageLocalService.addMessage(
177             getGuestOrUserId(), null, categoryId, subject, body, files,
178             anonymous, priority, tagsEntries, prefs, addCommunityPermissions,
179             addGuestPermissions, themeDisplay);
180     }
181 
182     public MBMessage addMessage(
183             long categoryId, String subject, String body,
184             List<ObjectValuePair<String, byte[]>> files, boolean anonymous,
185             double priority, String[] tagsEntries, PortletPreferences prefs,
186             String[] communityPermissions, String[] guestPermissions,
187             ThemeDisplay themeDisplay)
188         throws PortalException, SystemException {
189 
190         MBCategoryPermission.check(
191             getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
192 
193         if (!MBCategoryPermission.contains(
194                 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
195 
196             files.clear();
197         }
198 
199         if (!MBCategoryPermission.contains(
200                 getPermissionChecker(), categoryId,
201                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
202 
203             priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
204         }
205 
206         return mbMessageLocalService.addMessage(
207             getGuestOrUserId(), null, categoryId, subject, body, files,
208             anonymous, priority, tagsEntries, prefs, communityPermissions,
209             guestPermissions, themeDisplay);
210     }
211 
212     public MBMessage addMessage(
213             long categoryId, long threadId, long parentMessageId,
214             String subject, String body,
215             List<ObjectValuePair<String, byte[]>> files, boolean anonymous,
216             double priority, String[] tagsEntries,
217             boolean addCommunityPermissions, boolean addGuestPermissions)
218         throws PortalException, SystemException {
219 
220         checkReplyToPermission(categoryId, parentMessageId);
221 
222         if (!MBCategoryPermission.contains(
223                 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
224 
225             files.clear();
226         }
227 
228         if (!MBCategoryPermission.contains(
229                 getPermissionChecker(), categoryId,
230                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
231 
232             priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
233         }
234 
235         return mbMessageLocalService.addMessage(
236             getGuestOrUserId(), null, categoryId, threadId, parentMessageId,
237             subject, body, files, anonymous, priority, tagsEntries, null,
238             addCommunityPermissions, addGuestPermissions, null);
239     }
240 
241     public MBMessage addMessage(
242             long categoryId, long threadId, long parentMessageId,
243             String subject, String body,
244             List<ObjectValuePair<String, byte[]>> files, boolean anonymous,
245             double priority, String[] tagsEntries,
246             String[] communityPermissions, String[] guestPermissions)
247         throws PortalException, SystemException {
248 
249         checkReplyToPermission(categoryId, parentMessageId);
250 
251         if (!MBCategoryPermission.contains(
252                 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
253 
254             files.clear();
255         }
256 
257         if (!MBCategoryPermission.contains(
258                 getPermissionChecker(), categoryId,
259                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
260 
261             priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
262         }
263 
264         return mbMessageLocalService.addMessage(
265             getGuestOrUserId(), null, categoryId, threadId, parentMessageId,
266             subject, body, files, anonymous, priority, tagsEntries, null,
267             communityPermissions, guestPermissions, null);
268     }
269 
270     public MBMessage addMessage(
271             long categoryId, long threadId, long parentMessageId,
272             String subject, String body,
273             List<ObjectValuePair<String, byte[]>> files, boolean anonymous,
274             double priority, String[] tagsEntries, PortletPreferences prefs,
275             boolean addCommunityPermissions, boolean addGuestPermissions,
276             ThemeDisplay themeDisplay)
277         throws PortalException, SystemException {
278 
279         checkReplyToPermission(categoryId, parentMessageId);
280 
281         if (!MBCategoryPermission.contains(
282                 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
283 
284             files.clear();
285         }
286 
287         if (!MBCategoryPermission.contains(
288                 getPermissionChecker(), categoryId,
289                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
290 
291             priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
292         }
293 
294         return mbMessageLocalService.addMessage(
295             getGuestOrUserId(), null, categoryId, threadId, parentMessageId,
296             subject, body, files, anonymous, priority, tagsEntries, prefs,
297             addCommunityPermissions, addGuestPermissions, themeDisplay);
298     }
299 
300     public MBMessage addMessage(
301             long categoryId, long threadId, long parentMessageId,
302             String subject, String body,
303             List<ObjectValuePair<String, byte[]>> files, boolean anonymous,
304             double priority, String[] tagsEntries, PortletPreferences prefs,
305             String[] communityPermissions, String[] guestPermissions,
306             ThemeDisplay themeDisplay)
307         throws PortalException, SystemException {
308 
309         checkReplyToPermission(categoryId, parentMessageId);
310 
311         if (!MBCategoryPermission.contains(
312                 getPermissionChecker(), categoryId, ActionKeys.ADD_FILE)) {
313 
314             files.clear();
315         }
316 
317         if (!MBCategoryPermission.contains(
318                 getPermissionChecker(), categoryId,
319                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
320 
321             priority = MBThreadImpl.PRIORITY_NOT_GIVEN;
322         }
323 
324         return mbMessageLocalService.addMessage(
325             getGuestOrUserId(), null, categoryId, threadId, parentMessageId,
326             subject, body, files, anonymous, priority, tagsEntries, prefs,
327             communityPermissions, guestPermissions, themeDisplay);
328     }
329 
330     public void deleteDiscussionMessage(
331             long groupId, String className, long classPK, long messageId)
332         throws PortalException, SystemException {
333 
334         User user = getUser();
335 
336         MBDiscussionPermission.check(
337             getPermissionChecker(), user.getCompanyId(), groupId, className,
338             classPK, messageId, user.getUserId(), ActionKeys.DELETE_DISCUSSION);
339 
340         mbMessageLocalService.deleteDiscussionMessage(messageId);
341     }
342 
343     public void deleteMessage(long messageId)
344         throws PortalException, SystemException {
345 
346         MBMessagePermission.check(
347             getPermissionChecker(), messageId, ActionKeys.DELETE);
348 
349         mbMessageLocalService.deleteMessage(messageId);
350     }
351 
352     public List<MBMessage> getCategoryMessages(
353             long categoryId, int start, int end)
354         throws PortalException, SystemException {
355 
356         List<MBMessage> messages = new ArrayList<MBMessage>();
357 
358         Iterator<MBMessage> itr = mbMessageLocalService.getCategoryMessages(
359             categoryId, start, end).iterator();
360 
361         while (itr.hasNext()) {
362             MBMessage message = itr.next();
363 
364             if (MBMessagePermission.contains(
365                     getPermissionChecker(), message, ActionKeys.VIEW)) {
366 
367                 messages.add(message);
368             }
369         }
370 
371         return messages;
372     }
373 
374     public int getCategoryMessagesCount(long categoryId)
375         throws SystemException {
376 
377         return mbMessageLocalService.getCategoryMessagesCount(categoryId);
378     }
379 
380     public String getCategoryMessagesRSS(
381             long categoryId, int max, String type, double version,
382             String displayStyle, String feedURL, String entryURL,
383             ThemeDisplay themeDisplay)
384         throws PortalException, SystemException {
385 
386         MBCategory category = mbCategoryLocalService.getCategory(
387             categoryId);
388 
389         String name = category.getName();
390         String description = category.getDescription();
391 
392         List<MBMessage> messages = new ArrayList<MBMessage>();
393 
394         int lastIntervalStart = 0;
395         boolean listNotExhausted = true;
396         MessageCreateDateComparator comparator =
397             new MessageCreateDateComparator(false);
398 
399         while ((messages.size() < max) && listNotExhausted) {
400             List<MBMessage> messageList =
401                 mbMessageLocalService.getCategoryMessages(
402                     categoryId, lastIntervalStart, lastIntervalStart + max,
403                     comparator);
404 
405             Iterator<MBMessage> itr = messageList.iterator();
406 
407             lastIntervalStart += max;
408             listNotExhausted = (messageList.size() == max);
409 
410             while (itr.hasNext() && (messages.size() < max)) {
411                 MBMessage message = itr.next();
412 
413                 if (MBMessagePermission.contains(
414                         getPermissionChecker(), message, ActionKeys.VIEW)) {
415 
416                     messages.add(message);
417                 }
418             }
419         }
420 
421         return exportToRSS(
422             name, description, type, version, displayStyle, feedURL, entryURL,
423             messages, themeDisplay);
424     }
425 
426     public String getCompanyMessagesRSS(
427             long companyId, int max, String type, double version,
428             String displayStyle, String feedURL, String entryURL,
429             ThemeDisplay themeDisplay)
430         throws PortalException, SystemException {
431 
432         Company company = companyPersistence.findByPrimaryKey(companyId);
433 
434         String name = company.getName();
435         String description = company.getName();
436 
437         List<MBMessage> messages = new ArrayList<MBMessage>();
438 
439         int lastIntervalStart = 0;
440         boolean listNotExhausted = true;
441         MessageCreateDateComparator comparator =
442             new MessageCreateDateComparator(false);
443 
444         while ((messages.size() < max) && listNotExhausted) {
445             List<MBMessage> messageList =
446                 mbMessageLocalService.getCompanyMessages(
447                     companyId, lastIntervalStart, lastIntervalStart + max,
448                     comparator);
449 
450             Iterator<MBMessage> itr = messageList.iterator();
451 
452             lastIntervalStart += max;
453             listNotExhausted = (messageList.size() == max);
454 
455             while (itr.hasNext() && (messages.size() < max)) {
456                 MBMessage message = itr.next();
457 
458                 if (MBMessagePermission.contains(
459                         getPermissionChecker(), message, ActionKeys.VIEW)) {
460 
461                     messages.add(message);
462                 }
463             }
464         }
465 
466         return exportToRSS(
467             name, description, type, version, displayStyle, feedURL, entryURL,
468             messages, themeDisplay);
469     }
470 
471     public String getGroupMessagesRSS(
472             long groupId, int max, String type, double version,
473             String displayStyle, String feedURL, String entryURL,
474             ThemeDisplay themeDisplay)
475         throws PortalException, SystemException {
476 
477         String name = StringPool.BLANK;
478         String description = StringPool.BLANK;
479 
480         List<MBMessage> messages = new ArrayList<MBMessage>();
481 
482         int lastIntervalStart = 0;
483         boolean listNotExhausted = true;
484         MessageCreateDateComparator comparator =
485             new MessageCreateDateComparator(false);
486 
487         while ((messages.size() < max) && listNotExhausted) {
488             List<MBMessage> messageList =
489                 mbMessageLocalService.getGroupMessages(
490                     groupId, lastIntervalStart, lastIntervalStart + max,
491                     comparator);
492 
493             Iterator<MBMessage> itr = messageList.iterator();
494 
495             lastIntervalStart += max;
496             listNotExhausted = (messageList.size() == max);
497 
498             while (itr.hasNext() && (messages.size() < max)) {
499                 MBMessage message = itr.next();
500 
501                 if (MBMessagePermission.contains(
502                         getPermissionChecker(), message, ActionKeys.VIEW)) {
503 
504                     messages.add(message);
505                 }
506             }
507         }
508 
509         if (messages.size() > 0) {
510             MBMessage message = messages.get(messages.size() - 1);
511 
512             name = message.getSubject();
513             description = message.getSubject();
514         }
515 
516         return exportToRSS(
517             name, description, type, version, displayStyle, feedURL, entryURL,
518             messages, themeDisplay);
519     }
520 
521     public String getGroupMessagesRSS(
522             long groupId, long userId, int max, String type, double version,
523             String displayStyle, String feedURL, String entryURL,
524             ThemeDisplay themeDisplay)
525         throws PortalException, SystemException {
526 
527         String name = StringPool.BLANK;
528         String description = StringPool.BLANK;
529 
530         List<MBMessage> messages = new ArrayList<MBMessage>();
531 
532         int lastIntervalStart = 0;
533         boolean listNotExhausted = true;
534         MessageCreateDateComparator comparator =
535             new MessageCreateDateComparator(false);
536 
537         while ((messages.size() < max) && listNotExhausted) {
538             List<MBMessage> messageList =
539                 mbMessageLocalService.getGroupMessages(
540                     groupId, userId, lastIntervalStart, lastIntervalStart + max,
541                     comparator);
542 
543             Iterator<MBMessage> itr = messageList.iterator();
544 
545             lastIntervalStart += max;
546             listNotExhausted = (messageList.size() == max);
547 
548             while (itr.hasNext() && (messages.size() < max)) {
549                 MBMessage message = itr.next();
550 
551                 if (MBMessagePermission.contains(
552                         getPermissionChecker(), message, ActionKeys.VIEW)) {
553 
554                     messages.add(message);
555                 }
556             }
557         }
558 
559         if (messages.size() > 0) {
560             MBMessage message = messages.get(messages.size() - 1);
561 
562             name = message.getSubject();
563             description = message.getSubject();
564         }
565 
566         return exportToRSS(
567             name, description, type, version, displayStyle, feedURL, entryURL,
568             messages, themeDisplay);
569     }
570 
571     public MBMessage getMessage(long messageId)
572         throws PortalException, SystemException {
573 
574         MBMessagePermission.check(
575             getPermissionChecker(), messageId, ActionKeys.VIEW);
576 
577         return mbMessageLocalService.getMessage(messageId);
578     }
579 
580     /**
581      * @deprecated
582      */
583     public MBMessageDisplay getMessageDisplay(long messageId)
584         throws PortalException, SystemException {
585 
586         MBMessagePermission.check(
587             getPermissionChecker(), messageId, ActionKeys.VIEW);
588 
589         return mbMessageLocalService.getMessageDisplay(messageId);
590     }
591 
592     public MBMessageDisplay getMessageDisplay(long messageId, String threadView)
593         throws PortalException, SystemException {
594 
595         MBMessagePermission.check(
596             getPermissionChecker(), messageId, ActionKeys.VIEW);
597 
598         return mbMessageLocalService.getMessageDisplay(messageId, threadView);
599     }
600 
601     public String getThreadMessagesRSS(
602             long threadId, int max, String type, double version,
603             String displayStyle, String feedURL, String entryURL,
604             ThemeDisplay themeDisplay)
605         throws PortalException, SystemException {
606 
607         String name = StringPool.BLANK;
608         String description = StringPool.BLANK;
609 
610         List<MBMessage> messages = new ArrayList<MBMessage>();
611 
612         MessageCreateDateComparator comparator =
613             new MessageCreateDateComparator(false);
614 
615         Iterator<MBMessage> itr = mbMessageLocalService.getThreadMessages(
616             threadId, comparator).iterator();
617 
618         while (itr.hasNext() && (messages.size() < max)) {
619             MBMessage message = itr.next();
620 
621             if (MBMessagePermission.contains(
622                     getPermissionChecker(), message, ActionKeys.VIEW)) {
623 
624                 messages.add(message);
625             }
626         }
627 
628         if (messages.size() > 0) {
629             MBMessage message = messages.get(messages.size() - 1);
630 
631             name = message.getSubject();
632             description = message.getSubject();
633         }
634 
635         return exportToRSS(
636             name, description, type, version, displayStyle, feedURL, entryURL,
637             messages, themeDisplay);
638     }
639 
640     public void subscribeMessage(long messageId)
641         throws PortalException, SystemException {
642 
643         MBMessagePermission.check(
644             getPermissionChecker(), messageId, ActionKeys.SUBSCRIBE);
645 
646         mbMessageLocalService.subscribeMessage(getUserId(), messageId);
647     }
648 
649     public void unsubscribeMessage(long messageId)
650         throws PortalException, SystemException {
651 
652         MBMessagePermission.check(
653             getPermissionChecker(), messageId, ActionKeys.SUBSCRIBE);
654 
655         mbMessageLocalService.unsubscribeMessage(getUserId(), messageId);
656     }
657 
658     public MBMessage updateDiscussionMessage(
659             long groupId, String className, long classPK, long messageId,
660             String subject, String body)
661         throws PortalException, SystemException {
662 
663         User user = getUser();
664 
665         MBDiscussionPermission.check(
666             getPermissionChecker(), user.getCompanyId(), groupId, className,
667             classPK, messageId, user.getUserId(), ActionKeys.UPDATE_DISCUSSION);
668 
669         return mbMessageLocalService.updateDiscussionMessage(
670             getUserId(), messageId, subject, body);
671     }
672 
673     public MBMessage updateMessage(
674             long messageId, String subject, String body,
675             List<ObjectValuePair<String, byte[]>> files,
676             List<String> existingFiles, double priority, String[] tagsEntries)
677         throws PortalException, SystemException {
678 
679         MBMessage message = mbMessageLocalService.getMessage(messageId);
680 
681         MBMessagePermission.check(
682             getPermissionChecker(), messageId, ActionKeys.UPDATE);
683 
684         if (!MBCategoryPermission.contains(
685                 getPermissionChecker(), message.getCategoryId(),
686                 ActionKeys.ADD_FILE)) {
687 
688             files.clear();
689         }
690 
691         if (!MBCategoryPermission.contains(
692                 getPermissionChecker(), message.getCategoryId(),
693                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
694 
695             MBThread thread = mbThreadLocalService.getThread(
696                 message.getThreadId());
697 
698             priority = thread.getPriority();
699         }
700 
701         return mbMessageLocalService.updateMessage(
702             getUserId(), messageId, subject, body, files, existingFiles,
703             priority, tagsEntries, null, null);
704     }
705 
706     public MBMessage updateMessage(
707             long messageId, String subject, String body,
708             List<ObjectValuePair<String, byte[]>> files,
709             List<String> existingFiles, double priority, String[] tagsEntries,
710             PortletPreferences prefs, ThemeDisplay themeDisplay)
711         throws PortalException, SystemException {
712 
713         MBMessage message = mbMessageLocalService.getMessage(messageId);
714 
715         MBMessagePermission.check(
716             getPermissionChecker(), messageId, ActionKeys.UPDATE);
717 
718         if (!MBCategoryPermission.contains(
719                 getPermissionChecker(), message.getCategoryId(),
720                 ActionKeys.ADD_FILE)) {
721 
722             files.clear();
723         }
724 
725         if (!MBCategoryPermission.contains(
726                 getPermissionChecker(), message.getCategoryId(),
727                 ActionKeys.UPDATE_THREAD_PRIORITY)) {
728 
729             MBThread thread = mbThreadLocalService.getThread(
730                 message.getThreadId());
731 
732             priority = thread.getPriority();
733         }
734 
735         return mbMessageLocalService.updateMessage(
736             getUserId(), messageId, subject, body, files, existingFiles,
737             priority, tagsEntries, prefs, themeDisplay);
738     }
739 
740     protected void checkReplyToPermission(long categoryId, long parentMessageId)
741         throws PortalException, SystemException {
742 
743         if (parentMessageId > 0) {
744             if (MBCategoryPermission.contains(
745                     getPermissionChecker(), categoryId,
746                     ActionKeys.ADD_MESSAGE)) {
747 
748                 return;
749             }
750 
751             MBMessage parentMessage = mbMessagePersistence.fetchByPrimaryKey(
752                 parentMessageId);
753 
754             if ((parentMessage == null) ||
755                 !MBCategoryPermission.contains(
756                     getPermissionChecker(), categoryId,
757                     ActionKeys.REPLY_TO_MESSAGE)) {
758 
759                 throw new PrincipalException();
760             }
761         }
762         else {
763             MBCategoryPermission.check(
764                 getPermissionChecker(), categoryId, ActionKeys.ADD_MESSAGE);
765         }
766     }
767 
768     protected String exportToRSS(
769             String name, String description, String type, double version,
770             String displayStyle, String feedURL, String entryURL,
771             List<MBMessage> messages, ThemeDisplay themeDisplay)
772         throws SystemException {
773 
774         SyndFeed syndFeed = new SyndFeedImpl();
775 
776         syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
777         syndFeed.setTitle(name);
778         syndFeed.setLink(feedURL);
779         syndFeed.setDescription(description);
780 
781         List<SyndEntry> entries = new ArrayList<SyndEntry>();
782 
783         syndFeed.setEntries(entries);
784 
785         Iterator<MBMessage> itr = messages.iterator();
786 
787         while (itr.hasNext()) {
788             MBMessage message = itr.next();
789 
790             String author = PortalUtil.getUserName(
791                 message.getUserId(), message.getUserName());
792 
793             String value = null;
794 
795             if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_ABSTRACT)) {
796                 value = StringUtil.shorten(
797                     HtmlUtil.extractText(message.getBody()),
798                     _RSS_ABSTRACT_LENGTH, StringPool.BLANK);
799             }
800             else if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
801                 value = StringPool.BLANK;
802             }
803             else {
804                 value = BBCodeUtil.getHTML(message);
805 
806                 value = StringUtil.replace(
807                     value,
808                     new String[] {
809                         "@theme_images_path@",
810                         "href=\"/",
811                         "src=\"/"
812                     },
813                     new String[] {
814                         themeDisplay.getURLPortal() +
815                             themeDisplay.getPathThemeImages(),
816                         "href=\"" + themeDisplay.getURLPortal() + "/",
817                         "src=\"" + themeDisplay.getURLPortal() + "/"
818                     });
819             }
820 
821             SyndEntry syndEntry = new SyndEntryImpl();
822 
823             if (!message.isAnonymous()) {
824                 syndEntry.setAuthor(author);
825             }
826 
827             syndEntry.setTitle(message.getSubject());
828             syndEntry.setLink(
829                 entryURL + "&messageId=" + message.getMessageId());
830             syndEntry.setUri(syndEntry.getLink());
831             syndEntry.setPublishedDate(message.getCreateDate());
832             syndEntry.setUpdatedDate(message.getModifiedDate());
833 
834             SyndContent syndContent = new SyndContentImpl();
835 
836             syndContent.setType(RSSUtil.DEFAULT_ENTRY_TYPE);
837             syndContent.setValue(value);
838 
839             syndEntry.setDescription(syndContent);
840 
841             entries.add(syndEntry);
842         }
843 
844         try {
845             return RSSUtil.export(syndFeed);
846         }
847         catch (FeedException fe) {
848             throw new SystemException(fe);
849         }
850     }
851 
852     private static final int _RSS_ABSTRACT_LENGTH = GetterUtil.getInteger(
853         PropsUtil.get(PropsKeys.MESSAGE_BOARDS_RSS_ABSTRACT_LENGTH));
854 
855     private static Log _log = LogFactoryUtil.getLog(MBMessageServiceImpl.class);
856 
857 }