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                  request, response, null, getRSS(request),
56                  ContentTypes.TEXT_XML_UTF8);
57  
58              return null;
59          }
60          catch (Exception e) {
61              PortalUtil.sendError(e, request, response);
62  
63              return null;
64          }
65      }
66  
67      protected byte[] getRSS(HttpServletRequest request) throws Exception {
68          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
69              WebKeys.THEME_DISPLAY);
70  
71          long companyId = ParamUtil.getLong(request, "companyId");
72          long groupId = ParamUtil.getLong(request, "groupId");
73          int max = ParamUtil.getInteger(
74              request, "max", SearchContainer.DEFAULT_DELTA);
75          String type = ParamUtil.getString(
76              request, "type", RSSUtil.DEFAULT_TYPE);
77          double version = ParamUtil.getDouble(
78              request, "version", RSSUtil.DEFAULT_VERSION);
79          String displayStyle = ParamUtil.getString(
80              request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
81  
82          String feedURL = StringPool.BLANK;
83  
84          String entryURL =
85              themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
86                  "/asset/find_asset?";
87  
88          String rss = StringPool.BLANK;
89  
90          if (companyId > 0) {
91              rss = AssetEntryServiceUtil.getCompanyEntriesRSS(
92                  companyId, max, type, version, displayStyle, feedURL, entryURL);
93          }
94          else if (groupId > 0) {
95              Group group = GroupLocalServiceUtil.getGroup(groupId);
96  
97              companyId = group.getCompanyId();
98  
99              String[] allTagNames = StringUtil.split(
100                 ParamUtil.getString(request, "tags"));
101 
102             long[] tagIds = AssetTagLocalServiceUtil.getTagIds(
103                 companyId, allTagNames);
104 
105             String[] notTagNames = StringUtil.split(
106                 ParamUtil.getString(request, "noTags"));
107 
108             long[] notTagIds = AssetTagLocalServiceUtil.getTagIds(
109                 companyId, notTagNames);
110 
111             AssetEntryQuery entryQuery = new AssetEntryQuery();
112 
113             entryQuery.setAnyTagIds(tagIds);
114             entryQuery.setGroupIds(new long[] {groupId});
115             entryQuery.setEnd(max);
116             entryQuery.setExcludeZeroViewCount(false);
117             entryQuery.setExpirationDate(null);
118             entryQuery.setNotAnyTagIds(notTagIds);
119             entryQuery.setPublishDate(null);
120             entryQuery.setStart(0);
121 
122             rss = AssetEntryServiceUtil.getEntriesRSS(
123                 entryQuery, type, version, displayStyle, feedURL, entryURL);
124         }
125 
126         return rss.getBytes(StringPool.UTF8);
127     }
128 
129 }