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.ArrayList;
046    import java.util.Date;
047    import java.util.List;
048    import java.util.Map;
049    
050    import javax.servlet.http.HttpServletRequest;
051    import javax.servlet.http.HttpServletResponse;
052    
053    import org.apache.struts.action.Action;
054    import org.apache.struts.action.ActionForm;
055    import org.apache.struts.action.ActionForward;
056    import org.apache.struts.action.ActionMapping;
057    
058    /**
059     * @author Raymond Aug??
060     * @author Brian Wing Shun Chan
061     */
062    public class GetArticlesAction extends Action {
063    
064            @Override
065            public ActionForward execute(
066                            ActionMapping actionMapping, ActionForm actionForm,
067                            HttpServletRequest request, HttpServletResponse response)
068                    throws Exception {
069    
070                    try {
071                            List<JournalArticle> articles = getArticles(request);
072    
073                            String fileName = null;
074                            byte[] bytes = getContent(request, articles);
075    
076                            ServletResponseUtil.sendFile(
077                                    request, response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
078    
079                            return null;
080                    }
081                    catch (Exception e) {
082                            PortalUtil.sendError(e, request, response);
083    
084                            return null;
085                    }
086            }
087    
088            protected List<JournalArticle> getArticles(HttpServletRequest request)
089                    throws Exception {
090    
091                    long companyId = PortalUtil.getCompanyId(request);
092                    long groupId = ParamUtil.getLong(request, "groupId");
093    
094                    long folderId = ParamUtil.getLong(request, "folderId");
095    
096                    List<Long> folderIds = new ArrayList<Long>(1);
097    
098                    folderIds.add(folderId);
099    
100                    String articleId = null;
101                    Double version = null;
102                    String title = null;
103                    String description = null;
104                    String content = null;
105                    String type = ParamUtil.getString(request, "type");
106                    String[] structureIds = StringUtil.split(
107                            ParamUtil.getString(request, "structureId"));
108                    String[] templateIds = StringUtil.split(
109                            ParamUtil.getString(request, "templateId"));
110    
111                    Date displayDateGT = null;
112    
113                    String displayDateGTParam = ParamUtil.getString(
114                            request, "displayDateGT");
115    
116                    if (Validator.isNotNull(displayDateGTParam)) {
117                            DateFormat displayDateGTFormat = DateUtil.getISOFormat(
118                                    displayDateGTParam);
119    
120                            displayDateGT = GetterUtil.getDate(
121                                    displayDateGTParam, displayDateGTFormat);
122                    }
123    
124                    if (_log.isDebugEnabled()) {
125                            _log.debug("displayDateGT is " + displayDateGT);
126                    }
127    
128                    Date displayDateLT = null;
129    
130                    String displayDateLTParam = ParamUtil.getString(
131                            request, "displayDateLT");
132    
133                    if (Validator.isNotNull(displayDateLTParam)) {
134                            DateFormat displayDateLTFormat = DateUtil.getISOFormat(
135                                    displayDateLTParam);
136    
137                            displayDateLT = GetterUtil.getDate(
138                                    displayDateLTParam, displayDateLTFormat);
139                    }
140    
141                    if (displayDateLT == null) {
142                            displayDateLT = new Date();
143                    }
144    
145                    if (_log.isDebugEnabled()) {
146                            _log.debug("displayDateLT is " + displayDateLT);
147                    }
148    
149                    int status = WorkflowConstants.STATUS_APPROVED;
150                    Date reviewDate = null;
151                    boolean andOperator = true;
152                    int start = 0;
153                    int end = ParamUtil.getInteger(request, "delta", 5);
154                    String orderBy = ParamUtil.getString(request, "orderBy");
155                    String orderByCol = ParamUtil.getString(request, "orderByCol", orderBy);
156                    String orderByType = ParamUtil.getString(request, "orderByType");
157                    boolean orderByAsc = orderByType.equals("asc");
158    
159                    OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
160    
161                    if (orderByCol.equals("display-date")) {
162                            obc = new ArticleDisplayDateComparator(orderByAsc);
163                    }
164    
165                    return JournalArticleServiceUtil.search(
166                            companyId, groupId, folderIds, 0, articleId, version, title,
167                            description, content, type, structureIds, templateIds,
168                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
169                            start, end, obc);
170            }
171    
172            protected byte[] getContent(
173                            HttpServletRequest request, List<JournalArticle> articles)
174                    throws Exception {
175    
176                    long groupId = ParamUtil.getLong(request, "groupId");
177    
178                    String languageId = LanguageUtil.getLanguageId(request);
179    
180                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
181                            WebKeys.THEME_DISPLAY);
182    
183                    Map<String, String> tokens = JournalUtil.getTokens(
184                            groupId, themeDisplay);
185    
186                    Document resultsDoc = SAXReaderUtil.createDocument(StringPool.UTF8);
187    
188                    Element resultSetEl = resultsDoc.addElement("result-set");
189    
190                    for (JournalArticle article : articles) {
191                            Element resultEl = resultSetEl.addElement("result");
192    
193                            Document articleDoc = SAXReaderUtil.read(
194                                    article.getContentByLocale(languageId));
195    
196                            resultEl.content().add(articleDoc.getRootElement().createCopy());
197    
198                            resultEl = resultEl.element("root");
199    
200                            JournalUtil.addAllReservedEls(
201                                    resultEl, tokens, article, languageId, themeDisplay);
202                    }
203    
204                    return DDMXMLUtil.formatXML(resultsDoc).getBytes(StringPool.UTF8);
205            }
206    
207            private static Log _log = LogFactoryUtil.getLog(GetArticlesAction.class);
208    
209    }