1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.asset.action;
16  
17  import com.liferay.portal.kernel.dao.search.SearchContainer;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.model.Group;
23  import com.liferay.portal.service.GroupLocalServiceUtil;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.asset.service.AssetEntryServiceUtil;
28  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
29  import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
30  import com.liferay.util.RSSUtil;
31  import com.liferay.util.servlet.ServletResponseUtil;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.apache.struts.action.Action;
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class RSSAction extends Action {
47  
48      public ActionForward execute(
49              ActionMapping mapping, ActionForm form, HttpServletRequest request,
50              HttpServletResponse response)
51          throws Exception {
52  
53          try {
54              ServletResponseUtil.sendFile(
55                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
56  
57              return null;
58          }
59          catch (Exception e) {
60              PortalUtil.sendError(e, request, response);
61  
62              return null;
63          }
64      }
65  
66      protected byte[] getRSS(HttpServletRequest request) throws Exception {
67          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
68              WebKeys.THEME_DISPLAY);
69  
70          long companyId = ParamUtil.getLong(request, "companyId");
71          long groupId = ParamUtil.getLong(request, "groupId");
72          int max = ParamUtil.getInteger(
73              request, "max", SearchContainer.DEFAULT_DELTA);
74          String type = ParamUtil.getString(
75              request, "type", RSSUtil.DEFAULT_TYPE);
76          double version = ParamUtil.getDouble(
77              request, "version", RSSUtil.DEFAULT_VERSION);
78          String displayStyle = ParamUtil.getString(
79              request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
80  
81          String feedURL = StringPool.BLANK;
82  
83          String entryURL =
84              themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
85                  "/asset/find_asset?";
86  
87          String rss = StringPool.BLANK;
88  
89          if (companyId > 0) {
90              rss = AssetEntryServiceUtil.getCompanyEntriesRSS(
91                  companyId, max, type, version, displayStyle, feedURL, entryURL);
92          }
93          else if (groupId > 0) {
94              Group group = GroupLocalServiceUtil.getGroup(groupId);
95  
96              companyId = group.getCompanyId();
97  
98              String[] allTagNames = StringUtil.split(
99                  ParamUtil.getString(request, "tags"));
100 
101             long[] tagIds = AssetTagLocalServiceUtil.getTagIds(
102                 companyId, allTagNames);
103 
104             String[] notTagNames = StringUtil.split(
105                 ParamUtil.getString(request, "noTags"));
106 
107             long[] notTagIds = AssetTagLocalServiceUtil.getTagIds(
108                 companyId, notTagNames);
109 
110             AssetEntryQuery entryQuery = new AssetEntryQuery();
111 
112             entryQuery.setAnyTagIds(tagIds);
113             entryQuery.setGroupIds(new long[] {groupId});
114             entryQuery.setEnd(max);
115             entryQuery.setExcludeZeroViewCount(false);
116             entryQuery.setExpirationDate(null);
117             entryQuery.setNotAnyTagIds(notTagIds);
118             entryQuery.setPublishDate(null);
119             entryQuery.setStart(0);
120 
121             rss = AssetEntryServiceUtil.getEntriesRSS(
122                 entryQuery, type, version, displayStyle, feedURL, entryURL);
123         }
124 
125         return rss.getBytes(StringPool.UTF8);
126     }
127 
128 }