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