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