001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.action;
022:
023: import com.liferay.portal.kernel.dao.DAOParamUtil;
024: import com.liferay.portal.kernel.language.LanguageUtil;
025: import com.liferay.portal.kernel.util.ContentTypes;
026: import com.liferay.portal.kernel.util.DateUtil;
027: import com.liferay.portal.kernel.util.GetterUtil;
028: import com.liferay.portal.kernel.util.OrderByComparator;
029: import com.liferay.portal.kernel.util.ParamUtil;
030: import com.liferay.portal.kernel.util.StringUtil;
031: import com.liferay.portal.kernel.util.Validator;
032: import com.liferay.portal.struts.ActionConstants;
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.io.StringReader;
044:
045: import java.text.DateFormat;
046:
047: import java.util.Date;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Map;
051:
052: import javax.servlet.http.HttpServletRequest;
053: import javax.servlet.http.HttpServletResponse;
054: import javax.servlet.jsp.PageContext;
055:
056: import org.apache.commons.logging.Log;
057: import org.apache.commons.logging.LogFactory;
058: import org.apache.struts.action.Action;
059: import org.apache.struts.action.ActionForm;
060: import org.apache.struts.action.ActionForward;
061: import org.apache.struts.action.ActionMapping;
062:
063: import org.dom4j.Document;
064: import org.dom4j.DocumentFactory;
065: import org.dom4j.Element;
066: import org.dom4j.io.SAXReader;
067:
068: /**
069: * <a href="GetArticlesAction.java.html"><b><i>View Source</i></b></a>
070: *
071: * @author Raymond Augé
072: * @author Brian Wing Shun Chan
073: *
074: */
075: public class GetArticlesAction extends Action {
076:
077: public ActionForward execute(ActionMapping mapping,
078: ActionForm form, HttpServletRequest req,
079: HttpServletResponse res) throws Exception {
080:
081: try {
082: List articles = getArticles(req);
083:
084: String fileName = null;
085: byte[] byteArray = getContent(req, articles);
086:
087: ServletResponseUtil.sendFile(res, fileName, byteArray,
088: ContentTypes.TEXT_XML_UTF8);
089:
090: return null;
091: } catch (Exception e) {
092: req.setAttribute(PageContext.EXCEPTION, e);
093:
094: return mapping.findForward(ActionConstants.COMMON_ERROR);
095: }
096: }
097:
098: protected List getArticles(HttpServletRequest req) throws Exception {
099: long companyId = PortalUtil.getCompanyId(req);
100: long groupId = DAOParamUtil.getLong(req, "groupId");
101: String articleId = null;
102: Double version = null;
103: String title = null;
104: String description = null;
105: String content = null;
106: String type = DAOParamUtil.getString(req, "type");
107: String[] structureIds = StringUtil.split(DAOParamUtil
108: .getString(req, "structureId"));
109: String[] templateIds = StringUtil.split(DAOParamUtil.getString(
110: req, "templateId"));
111:
112: Date displayDateGT = null;
113:
114: String displayDateGTParam = ParamUtil.getString(req,
115: "displayDateGT");
116:
117: if (Validator.isNotNull(displayDateGTParam)) {
118: DateFormat displayDateGTFormat = DateUtil
119: .getISOFormat(displayDateGTParam);
120:
121: displayDateGT = GetterUtil.getDate(displayDateGTParam,
122: displayDateGTFormat);
123: }
124:
125: if (_log.isDebugEnabled()) {
126: _log.debug("displayDateGT is " + displayDateGT);
127: }
128:
129: Date displayDateLT = null;
130:
131: String displayDateLTParam = ParamUtil.getString(req,
132: "displayDateLT");
133:
134: if (Validator.isNotNull(displayDateLTParam)) {
135: DateFormat displayDateLTFormat = DateUtil
136: .getISOFormat(displayDateLTParam);
137:
138: displayDateLT = GetterUtil.getDate(displayDateLTParam,
139: displayDateLTFormat);
140: }
141:
142: if (_log.isDebugEnabled()) {
143: _log.debug("displayDateLT is " + displayDateLT);
144: }
145:
146: Boolean approved = Boolean.TRUE;
147: Boolean expired = Boolean.FALSE;
148: Date reviewDate = null;
149: boolean andOperator = true;
150: int begin = 0;
151: int end = ParamUtil.getInteger(req, "delta", 5);
152: String orderBy = ParamUtil.getString(req, "orderBy");
153: String orderByCol = ParamUtil.getString(req, "orderByCol",
154: orderBy);
155: String orderByType = ParamUtil.getString(req, "orderByType");
156: boolean orderByAsc = orderByType.equals("asc");
157:
158: OrderByComparator obc = new ArticleModifiedDateComparator(
159: orderByAsc);
160:
161: if (orderByCol.equals("display-date")) {
162: obc = new ArticleDisplayDateComparator(orderByAsc);
163: }
164:
165: return JournalArticleLocalServiceUtil.search(companyId,
166: groupId, articleId, version, title, description,
167: content, type, structureIds, templateIds,
168: displayDateGT, displayDateLT, approved, expired,
169: reviewDate, andOperator, begin, end, obc);
170: }
171:
172: protected byte[] getContent(HttpServletRequest req, List articles)
173: throws Exception {
174:
175: long groupId = ParamUtil.getLong(req, "groupId");
176:
177: String languageId = LanguageUtil.getLanguageId(req);
178:
179: ThemeDisplay themeDisplay = (ThemeDisplay) req
180: .getAttribute(WebKeys.THEME_DISPLAY);
181:
182: Map tokens = JournalUtil.getTokens(groupId, themeDisplay);
183:
184: Document resultsDoc = DocumentFactory.getInstance()
185: .createDocument("UTF-8");
186:
187: Element resultSetEl = resultsDoc.addElement("result-set");
188:
189: SAXReader reader = new SAXReader();
190:
191: Iterator itr = articles.iterator();
192:
193: while (itr.hasNext()) {
194: JournalArticle article = (JournalArticle) itr.next();
195:
196: Element resultEl = resultSetEl.addElement("result");
197:
198: Document articleDoc = reader.read(new StringReader(article
199: .getContentByLocale(languageId)));
200:
201: resultEl.content().add(
202: articleDoc.getRootElement().createCopy());
203:
204: if (article.isTemplateDriven()) {
205: resultEl = resultEl.element("root");
206: }
207:
208: JournalUtil.addAllReservedEls(resultEl, tokens, article);
209: }
210:
211: return JournalUtil.formatXML(resultsDoc).getBytes("UTF-8");
212: }
213:
214: private static Log _log = LogFactory
215: .getLog(GetArticlesAction.class);
216:
217: }
|