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