1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.tags.action;
24  
25  import com.liferay.portal.kernel.dao.search.SearchContainer;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.model.Group;
31  import com.liferay.portal.service.GroupLocalServiceUtil;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.tags.service.TagsAssetServiceUtil;
36  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
37  import com.liferay.util.RSSUtil;
38  import com.liferay.util.servlet.ServletResponseUtil;
39  
40  import java.util.Date;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.struts.action.Action;
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class RSSAction extends Action {
57  
58      public ActionForward execute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest request,
60              HttpServletResponse response)
61          throws Exception {
62  
63          try {
64              ServletResponseUtil.sendFile(
65                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
66  
67              return null;
68          }
69          catch (Exception e) {
70              PortalUtil.sendError(e, request, response);
71  
72              return null;
73          }
74      }
75  
76      protected byte[] getRSS(HttpServletRequest request) throws Exception {
77          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
78              WebKeys.THEME_DISPLAY);
79  
80          long companyId = ParamUtil.getLong(request, "companyId");
81          long groupId = ParamUtil.getLong(request, "groupId");
82          int max = ParamUtil.getInteger(
83              request, "max", SearchContainer.DEFAULT_DELTA);
84          String type = ParamUtil.getString(
85              request, "type", RSSUtil.DEFAULT_TYPE);
86          double version = ParamUtil.getDouble(
87              request, "version", RSSUtil.DEFAULT_VERSION);
88          String displayStyle = ParamUtil.getString(
89              request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
90  
91          String feedURL = StringPool.BLANK;
92  
93          String entryURL =
94              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
95                  "/tags/find_asset?";
96  
97          String rss = StringPool.BLANK;
98  
99          if (companyId > 0) {
100             rss = TagsAssetServiceUtil.getCompanyAssetsRSS(
101                 companyId, max, type, version, displayStyle, feedURL, entryURL);
102         }
103         else if (groupId > 0) {
104             Group group = GroupLocalServiceUtil.getGroup(groupId);
105 
106             companyId = group.getCompanyId();
107 
108             long[] classNameIds = new long[0];
109 
110             String[] allEntries = StringUtil.split(
111                 ParamUtil.getString(request, "tags"));
112 
113             long[] entryIds = TagsEntryLocalServiceUtil.getEntryIds(
114                 companyId, allEntries);
115 
116             String[] notEntries = StringUtil.split(
117                 ParamUtil.getString(request, "noTags"));
118 
119             long[] notEntryIds = TagsEntryLocalServiceUtil.getEntryIds(
120                 companyId, notEntries);
121 
122             boolean andOperator = false;
123             String orderByCol1 = null;
124             String orderByCol2 = null;
125             String orderByType1 = null;
126             String orderByType2 = null;
127             boolean excludeZeroViewCount = false;
128             Date publishDate = null;
129             Date expirationDate = null;
130 
131             rss = TagsAssetServiceUtil.getAssetsRSS(
132                 groupId, classNameIds, entryIds, notEntryIds, andOperator,
133                 orderByCol1, orderByCol2, orderByType1, orderByType2,
134                 excludeZeroViewCount, publishDate, expirationDate, max, type,
135                 version, displayStyle, feedURL, entryURL);
136         }
137 
138         return rss.getBytes(StringPool.UTF8);
139     }
140 
141 }