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