1   /**
2    * Copyright (c) 2000-2008 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.NoSuchCompanyException;
26  import com.liferay.portal.NoSuchGroupException;
27  import com.liferay.portal.kernel.dao.search.SearchContainer;
28  import com.liferay.portal.kernel.util.ContentTypes;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.model.Group;
33  import com.liferay.portal.service.GroupLocalServiceUtil;
34  import com.liferay.portal.struts.ActionConstants;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.tags.service.TagsAssetServiceUtil;
38  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
39  import com.liferay.util.RSSUtil;
40  import com.liferay.util.servlet.ServletResponseUtil;
41  
42  import java.util.Date;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  import javax.servlet.jsp.PageContext;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  import org.apache.struts.action.Action;
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class RSSAction extends Action {
62  
63      public ActionForward execute(
64              ActionMapping mapping, ActionForm form, HttpServletRequest req,
65              HttpServletResponse res)
66          throws Exception {
67  
68          try {
69              ServletResponseUtil.sendFile(
70                  res, null, getRSS(req), ContentTypes.TEXT_XML_UTF8);
71  
72              return null;
73          }
74          catch (Exception e) {
75              req.setAttribute(PageContext.EXCEPTION, e);
76  
77              return mapping.findForward(ActionConstants.COMMON_ERROR);
78          }
79      }
80  
81      protected byte[] getRSS(HttpServletRequest req) throws Exception {
82          ThemeDisplay themeDisplay =
83              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
84  
85          long companyId = ParamUtil.getLong(req, "companyId");
86          long groupId = ParamUtil.getLong(req, "groupId");
87          int max = ParamUtil.getInteger(
88              req, "max", SearchContainer.DEFAULT_DELTA);
89          String type = ParamUtil.getString(req, "type", RSSUtil.DEFAULT_TYPE);
90          double version = ParamUtil.getDouble(
91              req, "version", RSSUtil.DEFAULT_VERSION);
92          String displayStyle = ParamUtil.getString(
93              req, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
94  
95          String feedURL = StringPool.BLANK;
96  
97          String entryURL =
98              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
99                  "/tags/find_asset?";
100 
101         String rss = StringPool.BLANK;
102 
103         if (companyId > 0) {
104             try {
105                 rss = TagsAssetServiceUtil.getCompanyAssetsRSS(
106                     companyId, max, type, version, displayStyle, feedURL,
107                     entryURL);
108             }
109             catch (NoSuchCompanyException nsce) {
110                 if (_log.isWarnEnabled()) {
111                     _log.warn(nsce);
112                 }
113             }
114         }
115         else if (groupId > 0) {
116             try {
117                 Group group = GroupLocalServiceUtil.getGroup(groupId);
118 
119                 companyId = group.getCompanyId();
120 
121                 long[] classNameIds = new long[0];
122 
123                 String[] allEntries = StringUtil.split(
124                     ParamUtil.getString(req, "tags"));
125 
126                 long[] entryIds = TagsEntryLocalServiceUtil.getEntryIds(
127                     companyId, allEntries);
128 
129                 String[] notEntries = StringUtil.split(
130                     ParamUtil.getString(req, "noTags"));
131 
132                 long[] notEntryIds = TagsEntryLocalServiceUtil.getEntryIds(
133                     companyId, notEntries);
134 
135                 boolean andOperator = false;
136                 String orderByCol1 = null;
137                 String orderByCol2 = null;
138                 String orderByType1 = null;
139                 String orderByType2 = null;
140                 boolean excludeZeroViewCount = false;
141                 Date publishDate = null;
142                 Date expirationDate = null;
143 
144                 rss = TagsAssetServiceUtil.getAssetsRSS(
145                     groupId, classNameIds, entryIds, notEntryIds, andOperator,
146                     orderByCol1, orderByCol2, orderByType1, orderByType2,
147                     excludeZeroViewCount, publishDate, expirationDate,
148                     max, type, version, displayStyle, feedURL, entryURL);
149             }
150             catch (NoSuchGroupException nsge) {
151                 if (_log.isWarnEnabled()) {
152                     _log.warn(nsge);
153                 }
154             }
155         }
156 
157         return rss.getBytes(StringPool.UTF8);
158     }
159 
160     private static Log _log = LogFactory.getLog(RSSAction.class);
161 
162 }