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.journal.action;
24  
25  import com.liferay.portal.kernel.dao.search.DAOParamUtil;
26  import com.liferay.portal.kernel.language.LanguageUtil;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.ContentTypes;
30  import com.liferay.portal.kernel.util.DateUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.kernel.xml.Document;
38  import com.liferay.portal.kernel.xml.Element;
39  import com.liferay.portal.kernel.xml.SAXReaderUtil;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.WebKeys;
43  import com.liferay.portlet.journal.model.JournalArticle;
44  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
45  import com.liferay.portlet.journal.util.JournalUtil;
46  import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
47  import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
48  import com.liferay.util.servlet.ServletResponseUtil;
49  
50  import java.text.DateFormat;
51  
52  import java.util.Date;
53  import java.util.List;
54  import java.util.Map;
55  
56  import javax.servlet.http.HttpServletRequest;
57  import javax.servlet.http.HttpServletResponse;
58  
59  import org.apache.struts.action.Action;
60  import org.apache.struts.action.ActionForm;
61  import org.apache.struts.action.ActionForward;
62  import org.apache.struts.action.ActionMapping;
63  
64  /**
65   * <a href="GetArticlesAction.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Raymond Augé
68   * @author Brian Wing Shun Chan
69   *
70   */
71  public class GetArticlesAction extends Action {
72  
73      public ActionForward execute(
74              ActionMapping mapping, ActionForm form, HttpServletRequest request,
75              HttpServletResponse response)
76          throws Exception {
77  
78          try {
79              List<JournalArticle> articles = getArticles(request);
80  
81              String fileName = null;
82              byte[] bytes = getContent(request, articles);
83  
84              ServletResponseUtil.sendFile(
85                  response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
86  
87              return null;
88          }
89          catch (Exception e) {
90              PortalUtil.sendError(e, request, response);
91  
92              return null;
93          }
94      }
95  
96      protected List<JournalArticle> getArticles(HttpServletRequest request)
97          throws Exception {
98  
99          long companyId = PortalUtil.getCompanyId(request);
100         long groupId = DAOParamUtil.getLong(request, "groupId");
101         String articleId = null;
102         Double version = null;
103         String title = null;
104         String description = null;
105         String content = null;
106         String type = DAOParamUtil.getString(request, "type");
107         String[] structureIds = StringUtil.split(
108             DAOParamUtil.getString(request, "structureId"));
109         String[] templateIds = StringUtil.split(
110             DAOParamUtil.getString(request, "templateId"));
111 
112         Date displayDateGT = null;
113 
114         String displayDateGTParam = ParamUtil.getString(
115             request, "displayDateGT");
116 
117         if (Validator.isNotNull(displayDateGTParam)) {
118             DateFormat displayDateGTFormat = DateUtil.getISOFormat(
119                 displayDateGTParam);
120 
121             displayDateGT = GetterUtil.getDate(
122                 displayDateGTParam, displayDateGTFormat);
123         }
124 
125         if (_log.isDebugEnabled()) {
126             _log.debug("displayDateGT is " + displayDateGT);
127         }
128 
129         Date displayDateLT = null;
130 
131         String displayDateLTParam = ParamUtil.getString(
132             request, "displayDateLT");
133 
134         if (Validator.isNotNull(displayDateLTParam)) {
135             DateFormat displayDateLTFormat = DateUtil.getISOFormat(
136                 displayDateLTParam);
137 
138             displayDateLT = GetterUtil.getDate(
139                 displayDateLTParam, displayDateLTFormat);
140         }
141 
142         if (displayDateLT == null) {
143             displayDateLT = new Date();
144         }
145 
146         if (_log.isDebugEnabled()) {
147             _log.debug("displayDateLT is " + displayDateLT);
148         }
149 
150         Boolean approved = Boolean.TRUE;
151         Boolean expired = Boolean.FALSE;
152         Date reviewDate = null;
153         boolean andOperator = true;
154         int start = 0;
155         int end = ParamUtil.getInteger(request, "delta", 5);
156         String orderBy = ParamUtil.getString(request, "orderBy");
157         String orderByCol = ParamUtil.getString(request, "orderByCol", orderBy);
158         String orderByType = ParamUtil.getString(request, "orderByType");
159         boolean orderByAsc = orderByType.equals("asc");
160 
161         OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
162 
163         if (orderByCol.equals("display-date")) {
164             obc = new ArticleDisplayDateComparator(orderByAsc);
165         }
166 
167         return JournalArticleLocalServiceUtil.search(
168             companyId, groupId, articleId, version, title, description, content,
169             type, structureIds, templateIds, displayDateGT, displayDateLT,
170             approved, expired, reviewDate, andOperator, start, end, obc);
171     }
172 
173     protected byte[] getContent(
174             HttpServletRequest request, List<JournalArticle> articles)
175         throws Exception {
176 
177         long groupId = ParamUtil.getLong(request, "groupId");
178 
179         String languageId = LanguageUtil.getLanguageId(request);
180 
181         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
182             WebKeys.THEME_DISPLAY);
183 
184         Map<String, String> tokens = JournalUtil.getTokens(
185             groupId, themeDisplay);
186 
187         Document resultsDoc = SAXReaderUtil.createDocument(StringPool.UTF8);
188 
189         Element resultSetEl = resultsDoc.addElement("result-set");
190 
191         for (JournalArticle article : articles) {
192             Element resultEl = resultSetEl.addElement("result");
193 
194             Document articleDoc = SAXReaderUtil.read(
195                 article.getContentByLocale(languageId));
196 
197             resultEl.content().add(
198                 articleDoc.getRootElement().createCopy());
199 
200             resultEl = resultEl.element("root");
201 
202             JournalUtil.addAllReservedEls(resultEl, tokens, article);
203         }
204 
205         return JournalUtil.formatXML(resultsDoc).getBytes(StringPool.UTF8);
206     }
207 
208     private static Log _log = LogFactoryUtil.getLog(GetArticlesAction.class);
209 
210 }