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.activities.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.HtmlUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.Portal;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.social.model.SocialActivity;
032    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
033    import com.liferay.portlet.social.service.SocialActivityInterpreterLocalServiceUtil;
034    import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
035    import com.liferay.util.RSSUtil;
036    
037    import com.sun.syndication.feed.synd.SyndContent;
038    import com.sun.syndication.feed.synd.SyndContentImpl;
039    import com.sun.syndication.feed.synd.SyndEntry;
040    import com.sun.syndication.feed.synd.SyndEntryImpl;
041    import com.sun.syndication.feed.synd.SyndFeed;
042    import com.sun.syndication.feed.synd.SyndFeedImpl;
043    import com.sun.syndication.feed.synd.SyndLink;
044    import com.sun.syndication.feed.synd.SyndLinkImpl;
045    
046    import java.util.ArrayList;
047    import java.util.Collections;
048    import java.util.Date;
049    import java.util.List;
050    
051    import javax.portlet.PortletPreferences;
052    import javax.portlet.PortletRequest;
053    import javax.portlet.ResourceRequest;
054    import javax.portlet.ResourceResponse;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     * @author Vilmos Papp
059     * @author Eduardo Garcia
060     */
061    public class RSSAction extends com.liferay.portal.struts.RSSAction {
062    
063            protected String exportToRSS(
064                            PortletRequest portletRequest, String title, String description,
065                            String format, double version, String displayStyle,
066                            List<SocialActivity> activities, ServiceContext serviceContext)
067                    throws Exception {
068    
069                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
070                            WebKeys.THEME_DISPLAY);
071    
072                    SyndFeed syndFeed = new SyndFeedImpl();
073    
074                    syndFeed.setDescription(GetterUtil.getString(description, title));
075    
076                    List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
077    
078                    syndFeed.setEntries(syndEntries);
079    
080                    for (SocialActivity activity : activities) {
081                            SocialActivityFeedEntry activityFeedEntry =
082                                    SocialActivityInterpreterLocalServiceUtil.interpret(
083                                            StringPool.BLANK, activity, serviceContext);
084    
085                            if (activityFeedEntry == null) {
086                                    continue;
087                            }
088    
089                            SyndEntry syndEntry = new SyndEntryImpl();
090    
091                            SyndContent syndContent = new SyndContentImpl();
092    
093                            syndContent.setType(RSSUtil.ENTRY_TYPE_DEFAULT);
094    
095                            String value = null;
096    
097                            if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
098                                    value = StringPool.BLANK;
099                            }
100                            else {
101                                    value = activityFeedEntry.getBody();
102                            }
103    
104                            syndContent.setValue(value);
105    
106                            syndEntry.setDescription(syndContent);
107    
108                            if (Validator.isNotNull(activityFeedEntry.getLink())) {
109                                    syndEntry.setLink(activityFeedEntry.getLink());
110                            }
111    
112                            syndEntry.setPublishedDate(new Date(activity.getCreateDate()));
113                            syndEntry.setTitle(
114                                    HtmlUtil.extractText(activityFeedEntry.getTitle()));
115                            syndEntry.setUri(syndEntry.getLink());
116    
117                            syndEntries.add(syndEntry);
118                    }
119    
120                    syndFeed.setFeedType(RSSUtil.getFeedType(format, version));
121    
122                    List<SyndLink> syndLinks = new ArrayList<SyndLink>();
123    
124                    syndFeed.setLinks(syndLinks);
125    
126                    SyndLink selfSyndLink = new SyndLinkImpl();
127    
128                    syndLinks.add(selfSyndLink);
129    
130                    String link =
131                            PortalUtil.getLayoutFullURL(themeDisplay) +
132                                    Portal.FRIENDLY_URL_SEPARATOR + "activities/rss";
133    
134                    selfSyndLink.setHref(link);
135    
136                    selfSyndLink.setRel("self");
137    
138                    SyndLink alternateSyndLink = new SyndLinkImpl();
139    
140                    syndLinks.add(alternateSyndLink);
141    
142                    alternateSyndLink.setHref(PortalUtil.getLayoutFullURL(themeDisplay));
143                    alternateSyndLink.setRel("alternate");
144    
145                    syndFeed.setPublishedDate(new Date());
146                    syndFeed.setTitle(title);
147                    syndFeed.setUri(link);
148    
149                    return RSSUtil.export(syndFeed);
150            }
151    
152            protected List<SocialActivity> getActivities(
153                            PortletRequest portletRequest, int max)
154                    throws Exception {
155    
156                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
157                            WebKeys.THEME_DISPLAY);
158    
159                    Group group = GroupLocalServiceUtil.getGroup(
160                            themeDisplay.getScopeGroupId());
161    
162                    int start = 0;
163    
164                    if (group.isOrganization()) {
165                            return SocialActivityLocalServiceUtil.getOrganizationActivities(
166                                    group.getOrganizationId(), start, max);
167                    }
168                    else if (group.isRegularSite()) {
169                            return SocialActivityLocalServiceUtil.getGroupActivities(
170                                    group.getGroupId(), start, max);
171                    }
172                    else if (group.isUser()) {
173                            return SocialActivityLocalServiceUtil.getUserActivities(
174                                    group.getClassPK(), start, max);
175                    }
176    
177                    return Collections.emptyList();
178            }
179    
180            @Override
181            protected byte[] getRSS(
182                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
183                    throws Exception {
184    
185                    String feedTitle = ParamUtil.getString(resourceRequest, "feedTitle");
186                    String format = ParamUtil.getString(
187                            resourceRequest, "type", RSSUtil.FORMAT_DEFAULT);
188                    double version = ParamUtil.getDouble(
189                            resourceRequest, "version", RSSUtil.VERSION_DEFAULT);
190                    String displayStyle = ParamUtil.getString(
191                            resourceRequest, "displayStyle", RSSUtil.DISPLAY_STYLE_DEFAULT);
192    
193                    int max = ParamUtil.getInteger(
194                            resourceRequest, "max", SearchContainer.DEFAULT_DELTA);
195    
196                    List<SocialActivity> activities = getActivities(resourceRequest, max);
197    
198                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
199                            resourceRequest);
200    
201                    String rss = exportToRSS(
202                            resourceRequest, feedTitle, null, format, version, displayStyle,
203                            activities, serviceContext);
204    
205                    return rss.getBytes(StringPool.UTF8);
206            }
207    
208            @Override
209            protected boolean isRSSFeedsEnabled(PortletRequest portletRequest)
210                    throws Exception {
211    
212                    if (!super.isRSSFeedsEnabled(portletRequest)) {
213                            return false;
214                    }
215    
216                    PortletPreferences portletPreferences = portletRequest.getPreferences();
217    
218                    return GetterUtil.getBoolean(
219                            portletPreferences.getValue("enableRss", null), true);
220            }
221    
222    }