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