001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.action;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.WebKeys;
024    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
025    import com.liferay.util.RSSUtil;
026    
027    import javax.portlet.PortletPreferences;
028    import javax.portlet.PortletRequest;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class RSSAction extends com.liferay.portal.struts.RSSAction {
036    
037            @Override
038            protected byte[] getRSS(HttpServletRequest request) throws Exception {
039                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
040                            WebKeys.THEME_DISPLAY);
041    
042                    String plid = ParamUtil.getString(request, "p_l_id");
043                    long companyId = ParamUtil.getLong(request, "companyId");
044                    long groupId = ParamUtil.getLong(request, "groupId");
045                    long userId = ParamUtil.getLong(request, "userId");
046                    long categoryId = ParamUtil.getLong(request, "mbCategoryId");
047                    long threadId = ParamUtil.getLong(request, "threadId");
048                    int max = ParamUtil.getInteger(
049                            request, "max", SearchContainer.DEFAULT_DELTA);
050                    String type = ParamUtil.getString(
051                            request, "type", RSSUtil.FORMAT_DEFAULT);
052                    double version = ParamUtil.getDouble(
053                            request, "version", RSSUtil.VERSION_DEFAULT);
054                    String displayStyle = ParamUtil.getString(
055                            request, "displayStyle", RSSUtil.DISPLAY_STYLE_DEFAULT);
056    
057                    String entryURL =
058                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
059                                    "/message_boards/find_message?p_l_id=" + plid;
060    
061                    String rss = StringPool.BLANK;
062    
063                    if (companyId > 0) {
064                            String feedURL = StringPool.BLANK;
065    
066                            rss = MBMessageServiceUtil.getCompanyMessagesRSS(
067                                    companyId, WorkflowConstants.STATUS_APPROVED, max, type,
068                                    version, displayStyle, feedURL, entryURL, themeDisplay);
069                    }
070                    else if (groupId > 0) {
071                            String topLink = ParamUtil.getString(request, "topLink");
072    
073                            String feedURL = null;
074    
075                            if (topLink.equals("recent-posts")) {
076                                    feedURL =
077                                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
078                                                    "/message_boards/find_recent_posts?p_l_id=" + plid;
079                            }
080                            else {
081                                    feedURL =
082                                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
083                                                    "/message_boards/find_category?p_l_id=" + plid +
084                                                            "&mbCategoryId=" + categoryId;
085                            }
086    
087                            if (userId > 0) {
088                                    rss = MBMessageServiceUtil.getGroupMessagesRSS(
089                                            groupId, userId, WorkflowConstants.STATUS_APPROVED, max,
090                                            type, version, displayStyle, feedURL, entryURL,
091                                            themeDisplay);
092                            }
093                            else {
094                                    rss = MBMessageServiceUtil.getGroupMessagesRSS(
095                                            groupId, WorkflowConstants.STATUS_APPROVED, max, type,
096                                            version, displayStyle, feedURL, entryURL, themeDisplay);
097                            }
098                    }
099                    else if (categoryId > 0) {
100                            String feedURL =
101                                    themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
102                                            "/message_boards/find_category?p_l_id=" + plid +
103                                                    "&mbCategoryId=" + categoryId;
104    
105                            rss = MBMessageServiceUtil.getCategoryMessagesRSS(
106                                    groupId, categoryId, WorkflowConstants.STATUS_APPROVED, max,
107                                    type, version, displayStyle, feedURL, entryURL, themeDisplay);
108                    }
109                    else if (threadId > 0) {
110                            String feedURL =
111                                    themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
112                                            "/message_boards/find_thread?p_l_id=" + plid +
113                                                    "&threadId=" + threadId;
114    
115                            rss = MBMessageServiceUtil.getThreadMessagesRSS(
116                                    threadId, WorkflowConstants.STATUS_APPROVED, max, type, version,
117                                    displayStyle, feedURL, entryURL, themeDisplay);
118                    }
119    
120                    return rss.getBytes(StringPool.UTF8);
121            }
122    
123            @Override
124            protected boolean isRSSFeedsEnabled(PortletRequest portletRequest)
125                    throws Exception {
126    
127                    if (!super.isRSSFeedsEnabled(portletRequest)) {
128                            return false;
129                    }
130    
131                    PortletPreferences portletPreferences = portletRequest.getPreferences();
132    
133                    return GetterUtil.getBoolean(
134                            portletPreferences.getValue("enableRss", null), true);
135            }
136    
137    }