001
014
015 package com.liferay.portlet.journal.action;
016
017 import com.liferay.portal.kernel.dao.search.DAOParamUtil;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
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.journal.model.JournalArticle;
037 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
038 import com.liferay.portlet.journal.util.JournalUtil;
039 import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
040 import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
041 import com.liferay.util.servlet.ServletResponseUtil;
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
061 public class GetArticlesAction extends Action {
062
063 public ActionForward execute(
064 ActionMapping mapping, ActionForm form, HttpServletRequest request,
065 HttpServletResponse response)
066 throws Exception {
067
068 try {
069 List<JournalArticle> articles = getArticles(request);
070
071 String fileName = null;
072 byte[] bytes = getContent(request, articles);
073
074 ServletResponseUtil.sendFile(
075 request, response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
076
077 return null;
078 }
079 catch (Exception e) {
080 PortalUtil.sendError(e, request, response);
081
082 return null;
083 }
084 }
085
086 protected List<JournalArticle> getArticles(HttpServletRequest request)
087 throws Exception {
088
089 long companyId = PortalUtil.getCompanyId(request);
090 long groupId = DAOParamUtil.getLong(request, "groupId");
091 String articleId = null;
092 Double version = null;
093 String title = null;
094 String description = null;
095 String content = null;
096 String type = DAOParamUtil.getString(request, "type");
097 String[] structureIds = StringUtil.split(
098 DAOParamUtil.getString(request, "structureId"));
099 String[] templateIds = StringUtil.split(
100 DAOParamUtil.getString(request, "templateId"));
101
102 Date displayDateGT = null;
103
104 String displayDateGTParam = ParamUtil.getString(
105 request, "displayDateGT");
106
107 if (Validator.isNotNull(displayDateGTParam)) {
108 DateFormat displayDateGTFormat = DateUtil.getISOFormat(
109 displayDateGTParam);
110
111 displayDateGT = GetterUtil.getDate(
112 displayDateGTParam, displayDateGTFormat);
113 }
114
115 if (_log.isDebugEnabled()) {
116 _log.debug("displayDateGT is " + displayDateGT);
117 }
118
119 Date displayDateLT = null;
120
121 String displayDateLTParam = ParamUtil.getString(
122 request, "displayDateLT");
123
124 if (Validator.isNotNull(displayDateLTParam)) {
125 DateFormat displayDateLTFormat = DateUtil.getISOFormat(
126 displayDateLTParam);
127
128 displayDateLT = GetterUtil.getDate(
129 displayDateLTParam, displayDateLTFormat);
130 }
131
132 if (displayDateLT == null) {
133 displayDateLT = new Date();
134 }
135
136 if (_log.isDebugEnabled()) {
137 _log.debug("displayDateLT is " + displayDateLT);
138 }
139
140 int status = WorkflowConstants.STATUS_APPROVED;
141 Date reviewDate = null;
142 boolean andOperator = true;
143 int start = 0;
144 int end = ParamUtil.getInteger(request, "delta", 5);
145 String orderBy = ParamUtil.getString(request, "orderBy");
146 String orderByCol = ParamUtil.getString(request, "orderByCol", orderBy);
147 String orderByType = ParamUtil.getString(request, "orderByType");
148 boolean orderByAsc = orderByType.equals("asc");
149
150 OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
151
152 if (orderByCol.equals("display-date")) {
153 obc = new ArticleDisplayDateComparator(orderByAsc);
154 }
155
156 return JournalArticleLocalServiceUtil.search(
157 companyId, groupId, articleId, version, title, description, content,
158 type, structureIds, templateIds, displayDateGT, displayDateLT,
159 status, reviewDate, andOperator, start, end, obc);
160 }
161
162 protected byte[] getContent(
163 HttpServletRequest request, List<JournalArticle> articles)
164 throws Exception {
165
166 long groupId = ParamUtil.getLong(request, "groupId");
167
168 String languageId = LanguageUtil.getLanguageId(request);
169
170 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
171 WebKeys.THEME_DISPLAY);
172
173 Map<String, String> tokens = JournalUtil.getTokens(
174 groupId, themeDisplay);
175
176 Document resultsDoc = SAXReaderUtil.createDocument(StringPool.UTF8);
177
178 Element resultSetEl = resultsDoc.addElement("result-set");
179
180 for (JournalArticle article : articles) {
181 Element resultEl = resultSetEl.addElement("result");
182
183 Document articleDoc = SAXReaderUtil.read(
184 article.getContentByLocale(languageId));
185
186 resultEl.content().add(
187 articleDoc.getRootElement().createCopy());
188
189 resultEl = resultEl.element("root");
190
191 JournalUtil.addAllReservedEls(resultEl, tokens, article);
192 }
193
194 return JournalUtil.formatXML(resultsDoc).getBytes(StringPool.UTF8);
195 }
196
197 private static Log _log = LogFactoryUtil.getLog(GetArticlesAction.class);
198
199 }