001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.messageboards.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.parsers.bbcode.BBCodeTranslatorUtil;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.ObjectValuePair;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.model.Company;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.security.permission.ActionKeys;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.PropsValues;
035    import com.liferay.portlet.messageboards.LockedThreadException;
036    import com.liferay.portlet.messageboards.NoSuchCategoryException;
037    import com.liferay.portlet.messageboards.model.MBCategory;
038    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
039    import com.liferay.portlet.messageboards.model.MBMessage;
040    import com.liferay.portlet.messageboards.model.MBMessageDisplay;
041    import com.liferay.portlet.messageboards.model.MBThread;
042    import com.liferay.portlet.messageboards.model.MBThreadConstants;
043    import com.liferay.portlet.messageboards.service.base.MBMessageServiceBaseImpl;
044    import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
045    import com.liferay.portlet.messageboards.service.permission.MBDiscussionPermission;
046    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
047    import com.liferay.portlet.messageboards.util.comparator.MessageCreateDateComparator;
048    import com.liferay.util.RSSUtil;
049    
050    import com.sun.syndication.feed.synd.SyndContent;
051    import com.sun.syndication.feed.synd.SyndContentImpl;
052    import com.sun.syndication.feed.synd.SyndEntry;
053    import com.sun.syndication.feed.synd.SyndEntryImpl;
054    import com.sun.syndication.feed.synd.SyndFeed;
055    import com.sun.syndication.feed.synd.SyndFeedImpl;
056    import com.sun.syndication.io.FeedException;
057    
058    import java.io.InputStream;
059    
060    import java.util.ArrayList;
061    import java.util.Collections;
062    import java.util.Iterator;
063    import java.util.List;
064    
065    /**
066     * @author Brian Wing Shun Chan
067     * @author Mika Koivisto
068     * @author Shuyang Zhou
069     */
070    public class MBMessageServiceImpl extends MBMessageServiceBaseImpl {
071    
072            public MBMessage addDiscussionMessage(
073                            long groupId, String className, long classPK,
074                            String permissionClassName, long permissionClassPK,
075                            long permissionOwnerId, long threadId, long parentMessageId,
076                            String subject, String body, ServiceContext serviceContext)
077                    throws PortalException, SystemException {
078    
079                    User user = getGuestOrUser();
080    
081                    MBDiscussionPermission.check(
082                            getPermissionChecker(), user.getCompanyId(),
083                            serviceContext.getScopeGroupId(), permissionClassName,
084                            permissionClassPK, permissionOwnerId, ActionKeys.ADD_DISCUSSION);
085    
086                    return mbMessageLocalService.addDiscussionMessage(
087                            user.getUserId(), null, groupId, className, classPK, threadId,
088                            parentMessageId, subject, body, serviceContext);
089            }
090    
091            public MBMessage addMessage(
092                            long groupId, long categoryId, long threadId, long parentMessageId,
093                            String subject, String body, String format,
094                            List<ObjectValuePair<String, InputStream>> inputStreamOVPs,
095                            boolean anonymous, double priority, boolean allowPingbacks,
096                            ServiceContext serviceContext)
097                    throws PortalException, SystemException {
098    
099                    checkReplyToPermission(groupId, categoryId, parentMessageId);
100    
101                    if (lockLocalService.isLocked(MBThread.class.getName(), threadId)) {
102                            throw new LockedThreadException();
103                    }
104    
105                    if (!MBCategoryPermission.contains(
106                                    getPermissionChecker(), groupId, categoryId,
107                                    ActionKeys.ADD_FILE)) {
108    
109                            inputStreamOVPs = Collections.emptyList();
110                    }
111    
112                    boolean preview = ParamUtil.getBoolean(serviceContext, "preview");
113    
114                    int workFlowAction = serviceContext.getWorkflowAction();
115    
116                    if ((workFlowAction == WorkflowConstants.STATUS_DRAFT) && !preview) {
117                            MBMessagePermission.check(
118                                    getPermissionChecker(), parentMessageId, ActionKeys.UPDATE);
119                    }
120    
121                    if (!MBCategoryPermission.contains(
122                                    getPermissionChecker(), groupId, categoryId,
123                                    ActionKeys.UPDATE_THREAD_PRIORITY)) {
124    
125                            priority = MBThreadConstants.PRIORITY_NOT_GIVEN;
126                    }
127    
128                    return mbMessageLocalService.addMessage(
129                            getGuestOrUserId(), null, groupId, categoryId, threadId,
130                            parentMessageId, subject, body, format, inputStreamOVPs, anonymous,
131                            priority, allowPingbacks, serviceContext);
132            }
133    
134            public MBMessage addMessage(
135                            long groupId, long categoryId, String subject, String body,
136                            String format,
137                            List<ObjectValuePair<String, InputStream>> inputStreamOVPs,
138                            boolean anonymous, double priority, boolean allowPingbacks,
139                            ServiceContext serviceContext)
140                    throws PortalException, SystemException {
141    
142                    MBCategoryPermission.check(
143                            getPermissionChecker(), groupId, categoryId,
144                            ActionKeys.ADD_MESSAGE);
145    
146                    if (!MBCategoryPermission.contains(
147                                    getPermissionChecker(), groupId, categoryId,
148                                    ActionKeys.ADD_FILE)) {
149    
150                            inputStreamOVPs = Collections.emptyList();
151                    }
152    
153                    if (!MBCategoryPermission.contains(
154                                    getPermissionChecker(), groupId, categoryId,
155                                    ActionKeys.UPDATE_THREAD_PRIORITY)) {
156    
157                            priority = MBThreadConstants.PRIORITY_NOT_GIVEN;
158                    }
159    
160                    return mbMessageLocalService.addMessage(
161                            getGuestOrUserId(), null, groupId, categoryId, subject, body,
162                            format, inputStreamOVPs, anonymous, priority, allowPingbacks,
163                            serviceContext);
164            }
165    
166            public void deleteDiscussionMessage(
167                            long groupId, String className, long classPK,
168                            String permissionClassName, long permissionClassPK,
169                            long permissionOwnerId, long messageId)
170                    throws PortalException, SystemException {
171    
172                    User user = getUser();
173    
174                    MBDiscussionPermission.check(
175                            getPermissionChecker(), user.getCompanyId(), groupId,
176                            permissionClassName, permissionClassPK, messageId,
177                            permissionOwnerId, ActionKeys.DELETE_DISCUSSION);
178    
179                    mbMessageLocalService.deleteDiscussionMessage(messageId);
180            }
181    
182            public void deleteMessage(long messageId)
183                    throws PortalException, SystemException {
184    
185                    MBMessagePermission.check(
186                            getPermissionChecker(), messageId, ActionKeys.DELETE);
187    
188                    mbMessageLocalService.deleteMessage(messageId);
189            }
190    
191            public List<MBMessage> getCategoryMessages(
192                            long groupId, long categoryId, int status, int start, int end)
193                    throws PortalException, SystemException {
194    
195                    List<MBMessage> messages = new ArrayList<MBMessage>();
196    
197                    Iterator<MBMessage> itr = mbMessageLocalService.getCategoryMessages(
198                            groupId, categoryId, status, start, end).iterator();
199    
200                    while (itr.hasNext()) {
201                            MBMessage message = itr.next();
202    
203                            if (MBMessagePermission.contains(
204                                            getPermissionChecker(), message, ActionKeys.VIEW)) {
205    
206                                    messages.add(message);
207                            }
208                    }
209    
210                    return messages;
211            }
212    
213            public int getCategoryMessagesCount(
214                            long groupId, long categoryId, int status)
215                    throws SystemException {
216    
217                    return mbMessageLocalService.getCategoryMessagesCount(
218                            groupId, categoryId, status);
219            }
220    
221            public String getCategoryMessagesRSS(
222                            long groupId, long categoryId, int status, int max, String type,
223                            double version, String displayStyle, String feedURL,
224                            String entryURL, ThemeDisplay themeDisplay)
225                    throws PortalException, SystemException {
226    
227                    String name = StringPool.BLANK;
228                    String description = StringPool.BLANK;
229    
230                    try {
231                            MBCategory category = mbCategoryLocalService.getCategory(
232                                    categoryId);
233    
234                            groupId = category.getGroupId();
235                            name = category.getName();
236                            description = category.getDescription();
237                    }
238                    catch (NoSuchCategoryException nsce) {
239                            Group group = groupLocalService.getGroup(categoryId);
240    
241                            groupId = group.getGroupId();
242                            categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
243                            name = group.getDescriptiveName();
244                            description = group.getDescription();
245                    }
246    
247                    List<MBMessage> messages = new ArrayList<MBMessage>();
248    
249                    int lastIntervalStart = 0;
250                    boolean listNotExhausted = true;
251                    MessageCreateDateComparator comparator =
252                            new MessageCreateDateComparator(false);
253    
254                    while ((messages.size() < max) && listNotExhausted) {
255                            List<MBMessage> messageList =
256                                    mbMessageLocalService.getCategoryMessages(
257                                            groupId, categoryId, status, lastIntervalStart,
258                                            lastIntervalStart + max, comparator);
259    
260                            Iterator<MBMessage> itr = messageList.iterator();
261    
262                            lastIntervalStart += max;
263                            listNotExhausted = (messageList.size() == max);
264    
265                            while (itr.hasNext() && (messages.size() < max)) {
266                                    MBMessage message = itr.next();
267    
268                                    if (MBMessagePermission.contains(
269                                                    getPermissionChecker(), message, ActionKeys.VIEW)) {
270    
271                                            messages.add(message);
272                                    }
273                            }
274                    }
275    
276                    return exportToRSS(
277                            name, description, type, version, displayStyle, feedURL, entryURL,
278                            messages, themeDisplay);
279            }
280    
281            public String getCompanyMessagesRSS(
282                            long companyId, int status, int max, String type, double version,
283                            String displayStyle, String feedURL, String entryURL,
284                            ThemeDisplay themeDisplay)
285                    throws PortalException, SystemException {
286    
287                    Company company = companyPersistence.findByPrimaryKey(companyId);
288    
289                    String name = company.getName();
290                    String description = company.getName();
291    
292                    List<MBMessage> messages = new ArrayList<MBMessage>();
293    
294                    int lastIntervalStart = 0;
295                    boolean listNotExhausted = true;
296                    MessageCreateDateComparator comparator =
297                            new MessageCreateDateComparator(false);
298    
299                    while ((messages.size() < max) && listNotExhausted) {
300                            List<MBMessage> messageList =
301                                    mbMessageLocalService.getCompanyMessages(
302                                            companyId, status, lastIntervalStart,
303                                            lastIntervalStart + max, comparator);
304    
305                            Iterator<MBMessage> itr = messageList.iterator();
306    
307                            lastIntervalStart += max;
308                            listNotExhausted = (messageList.size() == max);
309    
310                            while (itr.hasNext() && (messages.size() < max)) {
311                                    MBMessage message = itr.next();
312    
313                                    if (MBMessagePermission.contains(
314                                                    getPermissionChecker(), message, ActionKeys.VIEW)) {
315    
316                                            messages.add(message);
317                                    }
318                            }
319                    }
320    
321                    return exportToRSS(
322                            name, description, type, version, displayStyle, feedURL, entryURL,
323                            messages, themeDisplay);
324            }
325    
326            public int getGroupMessagesCount(long groupId, int status)
327                    throws SystemException {
328    
329                    if (status == WorkflowConstants.STATUS_ANY) {
330                            return mbMessagePersistence.filterCountByGroupId(groupId);
331                    }
332                    else {
333                            return mbMessagePersistence.filterCountByG_S(groupId, status);
334                    }
335            }
336    
337            public String getGroupMessagesRSS(
338                            long groupId, int status, int max, String type, double version,
339                            String displayStyle, String feedURL, String entryURL,
340                            ThemeDisplay themeDisplay)
341                    throws PortalException, SystemException {
342    
343                    String name = StringPool.BLANK;
344                    String description = StringPool.BLANK;
345    
346                    List<MBMessage> messages = new ArrayList<MBMessage>();
347    
348                    int lastIntervalStart = 0;
349                    boolean listNotExhausted = true;
350                    MessageCreateDateComparator comparator =
351                            new MessageCreateDateComparator(false);
352    
353                    while ((messages.size() < max) && listNotExhausted) {
354                            List<MBMessage> messageList =
355                                    mbMessageLocalService.getGroupMessages(
356                                            groupId, status, lastIntervalStart, lastIntervalStart + max,
357                                            comparator);
358    
359                            Iterator<MBMessage> itr = messageList.iterator();
360    
361                            lastIntervalStart += max;
362                            listNotExhausted = (messageList.size() == max);
363    
364                            while (itr.hasNext() && (messages.size() < max)) {
365                                    MBMessage message = itr.next();
366    
367                                    if (MBMessagePermission.contains(
368                                                    getPermissionChecker(), message, ActionKeys.VIEW)) {
369    
370                                            messages.add(message);
371                                    }
372                            }
373                    }
374    
375                    if (messages.size() > 0) {
376                            MBMessage message = messages.get(messages.size() - 1);
377    
378                            name = message.getSubject();
379                            description = message.getSubject();
380                    }
381    
382                    return exportToRSS(
383                            name, description, type, version, displayStyle, feedURL, entryURL,
384                            messages, themeDisplay);
385            }
386    
387            public String getGroupMessagesRSS(
388                            long groupId, long userId, int status, int max, String type,
389                            double version, String displayStyle, String feedURL,
390                            String entryURL, ThemeDisplay themeDisplay)
391                    throws PortalException, SystemException {
392    
393                    String name = StringPool.BLANK;
394                    String description = StringPool.BLANK;
395    
396                    List<MBMessage> messages = new ArrayList<MBMessage>();
397    
398                    int lastIntervalStart = 0;
399                    boolean listNotExhausted = true;
400                    MessageCreateDateComparator comparator =
401                            new MessageCreateDateComparator(false);
402    
403                    while ((messages.size() < max) && listNotExhausted) {
404                            List<MBMessage> messageList =
405                                    mbMessageLocalService.getGroupMessages(
406                                            groupId, userId, status, lastIntervalStart,
407                                            lastIntervalStart + max, comparator);
408    
409                            Iterator<MBMessage> itr = messageList.iterator();
410    
411                            lastIntervalStart += max;
412                            listNotExhausted = (messageList.size() == max);
413    
414                            while (itr.hasNext() && (messages.size() < max)) {
415                                    MBMessage message = itr.next();
416    
417                                    if (MBMessagePermission.contains(
418                                                    getPermissionChecker(), message, ActionKeys.VIEW)) {
419    
420                                            messages.add(message);
421                                    }
422                            }
423                    }
424    
425                    if (messages.size() > 0) {
426                            MBMessage message = messages.get(messages.size() - 1);
427    
428                            name = message.getSubject();
429                            description = message.getSubject();
430                    }
431    
432                    return exportToRSS(
433                            name, description, type, version, displayStyle, feedURL, entryURL,
434                            messages, themeDisplay);
435            }
436    
437            public MBMessage getMessage(long messageId)
438                    throws PortalException, SystemException {
439    
440                    MBMessagePermission.check(
441                            getPermissionChecker(), messageId, ActionKeys.VIEW);
442    
443                    return mbMessageLocalService.getMessage(messageId);
444            }
445    
446            public MBMessageDisplay getMessageDisplay(
447                            long messageId, int status, String threadView,
448                            boolean includePrevAndNext)
449                    throws PortalException, SystemException {
450    
451                    MBMessagePermission.check(
452                            getPermissionChecker(), messageId, ActionKeys.VIEW);
453    
454                    return mbMessageLocalService.getMessageDisplay(
455                            getGuestOrUserId(), messageId, status, threadView,
456                            includePrevAndNext);
457            }
458    
459            public int getThreadAnswersCount(
460                            long groupId, long categoryId, long threadId)
461                    throws SystemException {
462    
463                    return mbMessagePersistence.filterCountByG_C_T_A(
464                            groupId, categoryId, threadId, true);
465            }
466    
467            public List<MBMessage> getThreadMessages(
468                            long groupId, long categoryId, long threadId, int status, int start,
469                            int end)
470                    throws SystemException {
471    
472                    if (status == WorkflowConstants.STATUS_ANY) {
473                            return mbMessagePersistence.filterFindByG_C_T(
474                                    groupId, categoryId, threadId, start, end);
475                    }
476                    else {
477                            return mbMessagePersistence.filterFindByG_C_T_S(
478                                    groupId, categoryId, threadId, status, start, end);
479                    }
480            }
481    
482            public int getThreadMessagesCount(
483                            long groupId, long categoryId, long threadId, int status)
484                    throws SystemException {
485    
486                    if (status == WorkflowConstants.STATUS_ANY) {
487                            return mbMessagePersistence.filterCountByG_C_T(
488                                    groupId, categoryId, threadId);
489                    }
490                    else {
491                            return mbMessagePersistence.filterCountByG_C_T_S(
492                                    groupId, categoryId, threadId, status);
493                    }
494            }
495    
496            public String getThreadMessagesRSS(
497                            long threadId, int status, int max, String type, double version,
498                            String displayStyle, String feedURL, String entryURL,
499                            ThemeDisplay themeDisplay)
500                    throws PortalException, SystemException {
501    
502                    String name = StringPool.BLANK;
503                    String description = StringPool.BLANK;
504    
505                    List<MBMessage> messages = new ArrayList<MBMessage>();
506    
507                    MBThread thread = mbThreadLocalService.getThread(threadId);
508    
509                    if (MBMessagePermission.contains(
510                                    getPermissionChecker(), thread.getRootMessageId(),
511                                    ActionKeys.VIEW)) {
512    
513                            MessageCreateDateComparator comparator =
514                                    new MessageCreateDateComparator(false);
515    
516                            Iterator<MBMessage> itr = mbMessageLocalService.getThreadMessages(
517                                    threadId, status, comparator).iterator();
518    
519                            while (itr.hasNext() && (messages.size() < max)) {
520                                    MBMessage message = itr.next();
521    
522                                    if (MBMessagePermission.contains(
523                                                    getPermissionChecker(), message, ActionKeys.VIEW)) {
524    
525                                            messages.add(message);
526                                    }
527                            }
528    
529                            if (messages.size() > 0) {
530                                    MBMessage message = messages.get(messages.size() - 1);
531    
532                                    name = message.getSubject();
533                                    description = message.getSubject();
534                            }
535                    }
536    
537                    return exportToRSS(
538                            name, description, type, version, displayStyle, feedURL, entryURL,
539                            messages, themeDisplay);
540            }
541    
542            public void subscribeMessage(long messageId)
543                    throws PortalException, SystemException {
544    
545                    MBMessagePermission.check(
546                            getPermissionChecker(), messageId, ActionKeys.SUBSCRIBE);
547    
548                    mbMessageLocalService.subscribeMessage(getUserId(), messageId);
549            }
550    
551            public void unsubscribeMessage(long messageId)
552                    throws PortalException, SystemException {
553    
554                    MBMessagePermission.check(
555                            getPermissionChecker(), messageId, ActionKeys.SUBSCRIBE);
556    
557                    mbMessageLocalService.unsubscribeMessage(getUserId(), messageId);
558            }
559    
560            public void updateAnswer(long messageId, boolean answer, boolean cascade)
561                    throws PortalException, SystemException {
562    
563                    mbMessageLocalService.updateAnswer(messageId, answer, cascade);
564            }
565    
566            public MBMessage updateDiscussionMessage(
567                            String className, long classPK, String permissionClassName,
568                            long permissionClassPK, long permissionOwnerId, long messageId,
569                            String subject, String body, ServiceContext serviceContext)
570                    throws PortalException, SystemException {
571    
572                    User user = getUser();
573    
574                    MBDiscussionPermission.check(
575                            getPermissionChecker(), user.getCompanyId(),
576                            serviceContext.getScopeGroupId(), permissionClassName,
577                            permissionClassPK, messageId, permissionOwnerId,
578                            ActionKeys.UPDATE_DISCUSSION);
579    
580                    return mbMessageLocalService.updateDiscussionMessage(
581                            getUserId(), messageId, className, classPK, subject, body,
582                            serviceContext);
583            }
584    
585            public MBMessage updateMessage(
586                            long messageId, String subject, String body,
587                            List<ObjectValuePair<String, InputStream>> inputStreamOVPs,
588                            List<String> existingFiles, double priority, boolean allowPingbacks,
589                            ServiceContext serviceContext)
590                    throws PortalException, SystemException {
591    
592                    MBMessage message = mbMessageLocalService.getMessage(messageId);
593    
594                    boolean preview = ParamUtil.getBoolean(serviceContext, "preview");
595    
596                    if (preview &&
597                            MBMessagePermission.contains(
598                                    getPermissionChecker(), message, ActionKeys.UPDATE)) {
599    
600                            checkReplyToPermission(
601                                    message.getGroupId(), message.getCategoryId(),
602                                    message.getParentMessageId());
603                    }
604                    else {
605                            MBMessagePermission.check(
606                                    getPermissionChecker(), messageId, ActionKeys.UPDATE);
607                    }
608    
609                    if (lockLocalService.isLocked(
610                                    MBThread.class.getName(), message.getThreadId())) {
611    
612                            throw new LockedThreadException();
613                    }
614    
615                    if (!MBCategoryPermission.contains(
616                                    getPermissionChecker(), message.getGroupId(),
617                                    message.getCategoryId(), ActionKeys.ADD_FILE)) {
618    
619                            inputStreamOVPs = Collections.emptyList();
620                    }
621    
622                    if (!MBCategoryPermission.contains(
623                                    getPermissionChecker(), message.getGroupId(),
624                                    message.getCategoryId(), ActionKeys.UPDATE_THREAD_PRIORITY)) {
625    
626                            MBThread thread = mbThreadLocalService.getThread(
627                                    message.getThreadId());
628    
629                            priority = thread.getPriority();
630                    }
631    
632                    return mbMessageLocalService.updateMessage(
633                            getGuestOrUserId(), messageId, subject, body, inputStreamOVPs,
634                            existingFiles, priority, allowPingbacks, serviceContext);
635            }
636    
637            protected void checkReplyToPermission(
638                            long groupId, long categoryId, long parentMessageId)
639                    throws PortalException, SystemException {
640    
641                    if (parentMessageId > 0) {
642                            if (MBCategoryPermission.contains(
643                                            getPermissionChecker(), groupId, categoryId,
644                                            ActionKeys.ADD_MESSAGE)) {
645    
646                                    return;
647                            }
648    
649                            MBMessage parentMessage = mbMessagePersistence.fetchByPrimaryKey(
650                                    parentMessageId);
651    
652                            if ((parentMessage == null) ||
653                                    !MBCategoryPermission.contains(
654                                            getPermissionChecker(), groupId, categoryId,
655                                            ActionKeys.REPLY_TO_MESSAGE)) {
656    
657                                    throw new PrincipalException();
658                            }
659                    }
660                    else {
661                            MBCategoryPermission.check(
662                                    getPermissionChecker(), groupId, categoryId,
663                                    ActionKeys.ADD_MESSAGE);
664                    }
665            }
666    
667            protected String exportToRSS(
668                            String name, String description, String type, double version,
669                            String displayStyle, String feedURL, String entryURL,
670                            List<MBMessage> messages, ThemeDisplay themeDisplay)
671                    throws SystemException {
672    
673                    SyndFeed syndFeed = new SyndFeedImpl();
674    
675                    syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
676                    syndFeed.setTitle(name);
677                    syndFeed.setLink(feedURL);
678                    syndFeed.setDescription(description);
679    
680                    List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
681    
682                    syndFeed.setEntries(syndEntries);
683    
684                    Iterator<MBMessage> itr = messages.iterator();
685    
686                    while (itr.hasNext()) {
687                            MBMessage message = itr.next();
688    
689                            String author = HtmlUtil.escape(
690                                    PortalUtil.getUserName(
691                                            message.getUserId(), message.getUserName()));
692    
693                            String value = null;
694    
695                            if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_ABSTRACT)) {
696                                    value = StringUtil.shorten(
697                                            HtmlUtil.extractText(message.getBody()),
698                                            PropsValues.MESSAGE_BOARDS_RSS_ABSTRACT_LENGTH,
699                                            StringPool.BLANK);
700                            }
701                            else if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
702                                    value = StringPool.BLANK;
703                            }
704                            else {
705                                    value = BBCodeTranslatorUtil.getHTML(message.getBody());
706    
707                                    value = StringUtil.replace(
708                                            value,
709                                            new String[] {
710                                                    "@theme_images_path@", "href=\"/", "src=\"/"
711                                            },
712                                            new String[] {
713                                                    themeDisplay.getURLPortal() +
714                                                            themeDisplay.getPathThemeImages(),
715                                                    "href=\"" + themeDisplay.getURLPortal() + "/",
716                                                    "src=\"" + themeDisplay.getURLPortal() + "/"
717                                            });
718                            }
719    
720                            SyndEntry syndEntry = new SyndEntryImpl();
721    
722                            if (!message.isAnonymous()) {
723                                    syndEntry.setAuthor(author);
724                            }
725    
726                            syndEntry.setTitle(message.getSubject());
727                            syndEntry.setLink(
728                                    entryURL + "&messageId=" + message.getMessageId());
729                            syndEntry.setUri(syndEntry.getLink());
730                            syndEntry.setPublishedDate(message.getCreateDate());
731                            syndEntry.setUpdatedDate(message.getModifiedDate());
732    
733                            SyndContent syndContent = new SyndContentImpl();
734    
735                            syndContent.setType(RSSUtil.ENTRY_TYPE_DEFAULT);
736                            syndContent.setValue(value);
737    
738                            syndEntry.setDescription(syndContent);
739    
740                            syndEntries.add(syndEntry);
741                    }
742    
743                    try {
744                            return RSSUtil.export(syndFeed);
745                    }
746                    catch (FeedException fe) {
747                            throw new SystemException(fe);
748                    }
749            }
750    
751    }