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