1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.action;
21  
22  import com.liferay.portal.kernel.dao.search.DAOParamUtil;
23  import com.liferay.portal.kernel.language.LanguageUtil;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
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.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.kernel.xml.Document;
35  import com.liferay.portal.kernel.xml.Element;
36  import com.liferay.portal.kernel.xml.SAXReaderUtil;
37  import com.liferay.portal.theme.ThemeDisplay;
38  import com.liferay.portal.util.PortalUtil;
39  import com.liferay.portal.util.WebKeys;
40  import com.liferay.portlet.journal.model.JournalArticle;
41  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
42  import com.liferay.portlet.journal.util.JournalUtil;
43  import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
44  import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
45  import com.liferay.util.servlet.ServletResponseUtil;
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.struts.action.Action;
57  import org.apache.struts.action.ActionForm;
58  import org.apache.struts.action.ActionForward;
59  import org.apache.struts.action.ActionMapping;
60  
61  /**
62   * <a href="GetArticlesAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Raymond Augé
65   * @author Brian Wing Shun Chan
66   *
67   */
68  public class GetArticlesAction extends Action {
69  
70      public ActionForward execute(
71              ActionMapping mapping, ActionForm form, HttpServletRequest request,
72              HttpServletResponse response)
73          throws Exception {
74  
75          try {
76              List<JournalArticle> articles = getArticles(request);
77  
78              String fileName = null;
79              byte[] bytes = getContent(request, articles);
80  
81              ServletResponseUtil.sendFile(
82                  response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
83  
84              return null;
85          }
86          catch (Exception e) {
87              PortalUtil.sendError(e, request, response);
88  
89              return null;
90          }
91      }
92  
93      protected List<JournalArticle> getArticles(HttpServletRequest request)
94          throws Exception {
95  
96          long companyId = PortalUtil.getCompanyId(request);
97          long groupId = DAOParamUtil.getLong(request, "groupId");
98          String articleId = null;
99          Double version = null;
100         String title = null;
101         String description = null;
102         String content = null;
103         String type = DAOParamUtil.getString(request, "type");
104         String[] structureIds = StringUtil.split(
105             DAOParamUtil.getString(request, "structureId"));
106         String[] templateIds = StringUtil.split(
107             DAOParamUtil.getString(request, "templateId"));
108 
109         Date displayDateGT = null;
110 
111         String displayDateGTParam = ParamUtil.getString(
112             request, "displayDateGT");
113 
114         if (Validator.isNotNull(displayDateGTParam)) {
115             DateFormat displayDateGTFormat = DateUtil.getISOFormat(
116                 displayDateGTParam);
117 
118             displayDateGT = GetterUtil.getDate(
119                 displayDateGTParam, displayDateGTFormat);
120         }
121 
122         if (_log.isDebugEnabled()) {
123             _log.debug("displayDateGT is " + displayDateGT);
124         }
125 
126         Date displayDateLT = null;
127 
128         String displayDateLTParam = ParamUtil.getString(
129             request, "displayDateLT");
130 
131         if (Validator.isNotNull(displayDateLTParam)) {
132             DateFormat displayDateLTFormat = DateUtil.getISOFormat(
133                 displayDateLTParam);
134 
135             displayDateLT = GetterUtil.getDate(
136                 displayDateLTParam, displayDateLTFormat);
137         }
138 
139         if (displayDateLT == null) {
140             displayDateLT = new Date();
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 start = 0;
152         int end = ParamUtil.getInteger(request, "delta", 5);
153         String orderBy = ParamUtil.getString(request, "orderBy");
154         String orderByCol = ParamUtil.getString(request, "orderByCol", orderBy);
155         String orderByType = ParamUtil.getString(request, "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, start, end, obc);
168     }
169 
170     protected byte[] getContent(
171             HttpServletRequest request, List<JournalArticle> articles)
172         throws Exception {
173 
174         long groupId = ParamUtil.getLong(request, "groupId");
175 
176         String languageId = LanguageUtil.getLanguageId(request);
177 
178         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
179             WebKeys.THEME_DISPLAY);
180 
181         Map<String, String> tokens = JournalUtil.getTokens(
182             groupId, themeDisplay);
183 
184         Document resultsDoc = SAXReaderUtil.createDocument(StringPool.UTF8);
185 
186         Element resultSetEl = resultsDoc.addElement("result-set");
187 
188         for (JournalArticle article : articles) {
189             Element resultEl = resultSetEl.addElement("result");
190 
191             Document articleDoc = SAXReaderUtil.read(
192                 article.getContentByLocale(languageId));
193 
194             resultEl.content().add(
195                 articleDoc.getRootElement().createCopy());
196 
197             resultEl = resultEl.element("root");
198 
199             JournalUtil.addAllReservedEls(resultEl, tokens, article);
200         }
201 
202         return JournalUtil.formatXML(resultsDoc).getBytes(StringPool.UTF8);
203     }
204 
205     private static Log _log = LogFactoryUtil.getLog(GetArticlesAction.class);
206 
207 }