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