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.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.struts.ActionConstants;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.journal.model.JournalArticle;
40  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
41  import com.liferay.portlet.journal.util.JournalUtil;
42  import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
43  import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
44  import com.liferay.util.servlet.ServletResponseUtil;
45  
46  import java.io.StringReader;
47  
48  import java.text.DateFormat;
49  
50  import java.util.Date;
51  import java.util.List;
52  import java.util.Map;
53  
54  import javax.servlet.http.HttpServletRequest;
55  import javax.servlet.http.HttpServletResponse;
56  import javax.servlet.jsp.PageContext;
57  
58  import org.apache.commons.logging.Log;
59  import org.apache.commons.logging.LogFactory;
60  import org.apache.struts.action.Action;
61  import org.apache.struts.action.ActionForm;
62  import org.apache.struts.action.ActionForward;
63  import org.apache.struts.action.ActionMapping;
64  
65  import org.dom4j.Document;
66  import org.dom4j.DocumentFactory;
67  import org.dom4j.Element;
68  import org.dom4j.io.SAXReader;
69  
70  /**
71   * <a href="GetArticlesAction.java.html"><b><i>View Source</i></b></a>
72   *
73   * @author Raymond Augé
74   * @author Brian Wing Shun Chan
75   *
76   */
77  public class GetArticlesAction extends Action {
78  
79      public ActionForward execute(
80              ActionMapping mapping, ActionForm form, HttpServletRequest req,
81              HttpServletResponse res)
82          throws Exception {
83  
84          try {
85              List<JournalArticle> articles = getArticles(req);
86  
87              String fileName = null;
88              byte[] byteArray = getContent(req, articles);
89  
90              ServletResponseUtil.sendFile(
91                  res, fileName, byteArray, ContentTypes.TEXT_XML_UTF8);
92  
93              return null;
94          }
95          catch (Exception e) {
96              req.setAttribute(PageContext.EXCEPTION, e);
97  
98              return mapping.findForward(ActionConstants.COMMON_ERROR);
99          }
100     }
101 
102     protected List<JournalArticle> getArticles(HttpServletRequest req)
103         throws Exception {
104 
105         long companyId = PortalUtil.getCompanyId(req);
106         long groupId = DAOParamUtil.getLong(req, "groupId");
107         String articleId = null;
108         Double version = null;
109         String title = null;
110         String description = null;
111         String content = null;
112         String type = DAOParamUtil.getString(req, "type");
113         String[] structureIds = StringUtil.split(
114             DAOParamUtil.getString(req, "structureId"));
115         String[] templateIds = StringUtil.split(
116             DAOParamUtil.getString(req, "templateId"));
117 
118         Date displayDateGT = null;
119 
120         String displayDateGTParam = ParamUtil.getString(req, "displayDateGT");
121 
122         if (Validator.isNotNull(displayDateGTParam)) {
123             DateFormat displayDateGTFormat = DateUtil.getISOFormat(
124                 displayDateGTParam);
125 
126             displayDateGT = GetterUtil.getDate(
127                 displayDateGTParam, displayDateGTFormat);
128         }
129 
130         if (_log.isDebugEnabled()) {
131             _log.debug("displayDateGT is " + displayDateGT);
132         }
133 
134         Date displayDateLT = null;
135 
136         String displayDateLTParam = ParamUtil.getString(req, "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 begin = 0;
159         int end = ParamUtil.getInteger(req, "delta", 5);
160         String orderBy = ParamUtil.getString(req, "orderBy");
161         String orderByCol = ParamUtil.getString(req, "orderByCol", orderBy);
162         String orderByType = ParamUtil.getString(req, "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, begin, end, obc);
175     }
176 
177     protected byte[] getContent(
178             HttpServletRequest req, List<JournalArticle> articles)
179         throws Exception {
180 
181         long groupId = ParamUtil.getLong(req, "groupId");
182 
183         String languageId = LanguageUtil.getLanguageId(req);
184 
185         ThemeDisplay themeDisplay =
186             (ThemeDisplay)req.getAttribute(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 }