1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.xml.Document;
30  import com.liferay.portal.kernel.xml.Element;
31  import com.liferay.portal.kernel.xml.SAXReaderUtil;
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.journal.model.JournalArticle;
36  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
37  import com.liferay.portlet.journal.util.JournalUtil;
38  import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
39  import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
40  import com.liferay.util.servlet.ServletResponseUtil;
41  
42  import java.text.DateFormat;
43  
44  import java.util.Date;
45  import java.util.List;
46  import java.util.Map;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  
51  import org.apache.struts.action.Action;
52  import org.apache.struts.action.ActionForm;
53  import org.apache.struts.action.ActionForward;
54  import org.apache.struts.action.ActionMapping;
55  
56  /**
57   * <a href="GetArticlesAction.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Raymond Augé
60   * @author Brian Wing Shun Chan
61   */
62  public class GetArticlesAction extends Action {
63  
64      public ActionForward execute(
65              ActionMapping mapping, ActionForm form, HttpServletRequest request,
66              HttpServletResponse response)
67          throws Exception {
68  
69          try {
70              List<JournalArticle> articles = getArticles(request);
71  
72              String fileName = null;
73              byte[] bytes = getContent(request, articles);
74  
75              ServletResponseUtil.sendFile(
76                  request, response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
77  
78              return null;
79          }
80          catch (Exception e) {
81              PortalUtil.sendError(e, request, response);
82  
83              return null;
84          }
85      }
86  
87      protected List<JournalArticle> getArticles(HttpServletRequest request)
88          throws Exception {
89  
90          long companyId = PortalUtil.getCompanyId(request);
91          long groupId = DAOParamUtil.getLong(request, "groupId");
92          String articleId = null;
93          Double version = null;
94          String title = null;
95          String description = null;
96          String content = null;
97          String type = DAOParamUtil.getString(request, "type");
98          String[] structureIds = StringUtil.split(
99              DAOParamUtil.getString(request, "structureId"));
100         String[] templateIds = StringUtil.split(
101             DAOParamUtil.getString(request, "templateId"));
102 
103         Date displayDateGT = null;
104 
105         String displayDateGTParam = ParamUtil.getString(
106             request, "displayDateGT");
107 
108         if (Validator.isNotNull(displayDateGTParam)) {
109             DateFormat displayDateGTFormat = DateUtil.getISOFormat(
110                 displayDateGTParam);
111 
112             displayDateGT = GetterUtil.getDate(
113                 displayDateGTParam, displayDateGTFormat);
114         }
115 
116         if (_log.isDebugEnabled()) {
117             _log.debug("displayDateGT is " + displayDateGT);
118         }
119 
120         Date displayDateLT = null;
121 
122         String displayDateLTParam = ParamUtil.getString(
123             request, "displayDateLT");
124 
125         if (Validator.isNotNull(displayDateLTParam)) {
126             DateFormat displayDateLTFormat = DateUtil.getISOFormat(
127                 displayDateLTParam);
128 
129             displayDateLT = GetterUtil.getDate(
130                 displayDateLTParam, displayDateLTFormat);
131         }
132 
133         if (displayDateLT == null) {
134             displayDateLT = new Date();
135         }
136 
137         if (_log.isDebugEnabled()) {
138             _log.debug("displayDateLT is " + displayDateLT);
139         }
140 
141         Boolean approved = Boolean.TRUE;
142         Boolean expired = Boolean.FALSE;
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             approved, expired, 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 }