001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.ContentTypes;
022    import com.liferay.portal.kernel.util.DateUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.kernel.xml.Document;
031    import com.liferay.portal.kernel.xml.Element;
032    import com.liferay.portal.kernel.xml.SAXReaderUtil;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
037    import com.liferay.portlet.journal.model.JournalArticle;
038    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
039    import com.liferay.portlet.journal.util.JournalUtil;
040    import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
041    import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
042    
043    import java.text.DateFormat;
044    
045    import java.util.Date;
046    import java.util.List;
047    import java.util.Map;
048    
049    import javax.servlet.http.HttpServletRequest;
050    import javax.servlet.http.HttpServletResponse;
051    
052    import org.apache.struts.action.Action;
053    import org.apache.struts.action.ActionForm;
054    import org.apache.struts.action.ActionForward;
055    import org.apache.struts.action.ActionMapping;
056    
057    /**
058     * @author Raymond Aug??
059     * @author Brian Wing Shun Chan
060     */
061    public class GetArticlesAction extends Action {
062    
063            @Override
064            public ActionForward execute(
065                            ActionMapping actionMapping, ActionForm actionForm,
066                            HttpServletRequest request, HttpServletResponse response)
067                    throws Exception {
068    
069                    try {
070                            List<JournalArticle> articles = getArticles(request);
071    
072                            String fileName = null;
073                            byte[] bytes = getContent(request, articles);
074    
075                            ServletResponseUtil.sendFile(
076                                    request, response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
077    
078                            return null;
079                    }
080                    catch (Exception e) {
081                            PortalUtil.sendError(e, request, response);
082    
083                            return null;
084                    }
085            }
086    
087            protected List<JournalArticle> getArticles(HttpServletRequest request)
088                    throws Exception {
089    
090                    long companyId = PortalUtil.getCompanyId(request);
091                    long groupId = ParamUtil.getLong(request, "groupId");
092                    String articleId = null;
093                    Double version = null;
094                    String title = null;
095                    String description = null;
096                    String content = null;
097                    String type = ParamUtil.getString(request, "type");
098                    String[] structureIds = StringUtil.split(
099                            ParamUtil.getString(request, "structureId"));
100                    String[] templateIds = StringUtil.split(
101                            ParamUtil.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                    int status = WorkflowConstants.STATUS_APPROVED;
142                    Date reviewDate = null;
143                    boolean andOperator = true;
144                    int start = 0;
145                    int end = ParamUtil.getInteger(request, "delta", 5);
146                    String orderBy = ParamUtil.getString(request, "orderBy");
147                    String orderByCol = ParamUtil.getString(request, "orderByCol", orderBy);
148                    String orderByType = ParamUtil.getString(request, "orderByType");
149                    boolean orderByAsc = orderByType.equals("asc");
150    
151                    OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
152    
153                    if (orderByCol.equals("display-date")) {
154                            obc = new ArticleDisplayDateComparator(orderByAsc);
155                    }
156    
157                    return JournalArticleServiceUtil.search(
158                            companyId, groupId, 0, articleId, version, title, description,
159                            content, type, structureIds, templateIds, displayDateGT,
160                            displayDateLT, status, reviewDate, andOperator, start, end, obc);
161            }
162    
163            protected byte[] getContent(
164                            HttpServletRequest request, List<JournalArticle> articles)
165                    throws Exception {
166    
167                    long groupId = ParamUtil.getLong(request, "groupId");
168    
169                    String languageId = LanguageUtil.getLanguageId(request);
170    
171                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
172                            WebKeys.THEME_DISPLAY);
173    
174                    Map<String, String> tokens = JournalUtil.getTokens(
175                            groupId, themeDisplay);
176    
177                    Document resultsDoc = SAXReaderUtil.createDocument(StringPool.UTF8);
178    
179                    Element resultSetEl = resultsDoc.addElement("result-set");
180    
181                    for (JournalArticle article : articles) {
182                            Element resultEl = resultSetEl.addElement("result");
183    
184                            Document articleDoc = SAXReaderUtil.read(
185                                    article.getContentByLocale(languageId));
186    
187                            resultEl.content().add(articleDoc.getRootElement().createCopy());
188    
189                            resultEl = resultEl.element("root");
190    
191                            JournalUtil.addAllReservedEls(
192                                    resultEl, tokens, article, languageId, themeDisplay);
193                    }
194    
195                    return DDMXMLUtil.formatXML(resultsDoc).getBytes(StringPool.UTF8);
196            }
197    
198            private static Log _log = LogFactoryUtil.getLog(GetArticlesAction.class);
199    
200    }