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.language.LanguageUtil;
024: import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
025: import com.liferay.portal.kernel.util.Constants;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.User;
030: import com.liferay.portal.service.impl.ImageLocalUtil;
031: import com.liferay.portal.struts.ActionConstants;
032: import com.liferay.portal.theme.ThemeDisplay;
033: import com.liferay.portal.util.PortalUtil;
034: import com.liferay.portal.util.WebKeys;
035: import com.liferay.portlet.journal.model.JournalArticle;
036: import com.liferay.portlet.journal.model.JournalTemplate;
037: import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
038: import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
039: import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
040: import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
041: import com.liferay.portlet.journal.util.JournalUtil;
042: import com.liferay.util.FileUtil;
043: import com.liferay.util.PwdGenerator;
044: import com.liferay.util.servlet.UploadServletRequest;
045:
046: import java.io.File;
047: import java.io.StringReader;
048:
049: import java.util.Date;
050: import java.util.Iterator;
051: import java.util.Map;
052:
053: import javax.servlet.http.HttpServletRequest;
054: import javax.servlet.http.HttpServletResponse;
055: import javax.servlet.jsp.PageContext;
056:
057: import org.apache.commons.logging.Log;
058: import org.apache.commons.logging.LogFactory;
059: import org.apache.struts.action.Action;
060: import org.apache.struts.action.ActionForm;
061: import org.apache.struts.action.ActionForward;
062: import org.apache.struts.action.ActionMapping;
063:
064: import org.dom4j.Document;
065: import org.dom4j.Element;
066: import org.dom4j.io.SAXReader;
067:
068: /**
069: * <a href="ViewArticleContentAction.java.html"><b><i>View Source</i></b></a>
070: *
071: * @author Brian Wing Shun Chan
072: * @author Raymond Augé
073: *
074: */
075: public class ViewArticleContentAction extends Action {
076:
077: public ActionForward execute(ActionMapping mapping,
078: ActionForm form, HttpServletRequest req,
079: HttpServletResponse res) throws Exception {
080:
081: UploadServletRequest uploadReq = null;
082:
083: try {
084: String cmd = ParamUtil.getString(req, Constants.CMD);
085:
086: ThemeDisplay themeDisplay = (ThemeDisplay) req
087: .getAttribute(WebKeys.THEME_DISPLAY);
088:
089: long groupId = ParamUtil.getLong(req, "groupId");
090: String articleId = ParamUtil.getString(req, "articleId");
091: double version = ParamUtil.getDouble(req, "version",
092: JournalArticleImpl.DEFAULT_VERSION);
093:
094: String languageId = LanguageUtil.getLanguageId(req);
095:
096: String output = null;
097:
098: if (cmd.equals(Constants.PREVIEW)) {
099: uploadReq = new UploadServletRequest(req);
100:
101: String title = ParamUtil.getString(uploadReq, "title");
102: String description = ParamUtil.getString(uploadReq,
103: "description");
104:
105: Date now = new Date();
106:
107: Date createDate = now;
108: Date modifiedDate = now;
109: Date displayDate = now;
110:
111: User user = PortalUtil.getUser(uploadReq);
112:
113: String xml = ParamUtil.getString(uploadReq, "xml");
114:
115: SAXReader reader = new SAXReader();
116:
117: Document doc = reader.read(new StringReader(xml));
118:
119: Element root = doc.getRootElement();
120:
121: String previewArticleId = "PREVIEW_"
122: + PwdGenerator.getPassword(PwdGenerator.KEY3,
123: 10);
124:
125: format(groupId, articleId, version, previewArticleId,
126: root, uploadReq);
127:
128: Map tokens = JournalUtil.getTokens(groupId,
129: themeDisplay);
130:
131: tokens.put("article_resource_pk", "-1");
132:
133: JournalArticle article = new JournalArticleImpl();
134:
135: article.setUserId(user.getUserId());
136: article.setCreateDate(createDate);
137: article.setModifiedDate(modifiedDate);
138: article.setArticleId(articleId);
139: article.setVersion(version);
140: article.setTitle(title);
141: article.setDescription(description);
142: article.setDisplayDate(displayDate);
143:
144: JournalUtil.addAllReservedEls(root, tokens, article);
145:
146: xml = JournalUtil.formatXML(doc);
147:
148: String templateId = ParamUtil.getString(uploadReq,
149: "templateId");
150:
151: JournalTemplate template = JournalTemplateLocalServiceUtil
152: .getTemplate(groupId, templateId);
153:
154: String langType = template.getLangType();
155: String script = template.getXsl();
156:
157: try {
158: output = JournalUtil.transform(tokens, languageId,
159: xml, script, langType);
160: } catch (Exception e1) {
161: _log.error(e1, e1);
162:
163: PortalUtil
164: .sendError(
165: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
166: e1, req, res);
167: }
168: } else {
169: output = JournalArticleServiceUtil.getArticleContent(
170: groupId, articleId, version, languageId,
171: themeDisplay);
172: }
173:
174: req.setAttribute(WebKeys.JOURNAL_ARTICLE_CONTENT, output);
175:
176: if (output.startsWith("<?xml ")) {
177: return mapping
178: .findForward("portlet.journal.raw_article_content");
179: } else {
180: return mapping
181: .findForward("portlet.journal.view_article_content");
182: }
183: } catch (Exception e2) {
184: req.setAttribute(PageContext.EXCEPTION, e2);
185:
186: return mapping.findForward(ActionConstants.COMMON_ERROR);
187: } finally {
188: if (uploadReq != null) {
189: uploadReq.cleanUp();
190: }
191: }
192: }
193:
194: protected void format(long groupId, String articleId,
195: double version, String previewArticleId, Element root,
196: UploadServletRequest req) throws Exception {
197:
198: Iterator itr = root.elements().iterator();
199:
200: while (itr.hasNext()) {
201: Element el = (Element) itr.next();
202:
203: Element dynamicContent = el.element("dynamic-content");
204:
205: String elName = el.attributeValue("name", StringPool.BLANK);
206: String elType = el.attributeValue("type", StringPool.BLANK);
207: String elContent = StringPool.BLANK;
208: String elLanguage = StringPool.BLANK;
209:
210: if (dynamicContent != null) {
211: elContent = dynamicContent.getTextTrim();
212:
213: elLanguage = dynamicContent.attributeValue(
214: "language-id", StringPool.BLANK);
215:
216: if (!elLanguage.equals(StringPool.BLANK)) {
217: elLanguage = "_" + elLanguage;
218: }
219: }
220:
221: if (elType.equals("image") && Validator.isNull(elContent)) {
222: File file = req.getFile("structure_image_" + elName
223: + elLanguage);
224: byte[] bytes = FileUtil.getBytes(file);
225:
226: if ((bytes != null) && (bytes.length > 0)) {
227: long imageId = JournalArticleImageLocalServiceUtil
228: .getArticleImageId(groupId,
229: previewArticleId, version, elName,
230: elLanguage, true);
231:
232: dynamicContent
233: .setText("/image/journal/article?img_id="
234: + imageId
235: + "&t="
236: + ImageServletTokenUtil
237: .getToken(imageId));
238:
239: ImageLocalUtil.updateImage(imageId, bytes);
240: } else {
241: if (Validator.isNotNull(articleId)) {
242: long imageId = JournalArticleImageLocalServiceUtil
243: .getArticleImageId(groupId, articleId,
244: version, elName, elLanguage);
245:
246: dynamicContent
247: .setText("/image/journal/article?img_id="
248: + imageId
249: + "&t="
250: + ImageServletTokenUtil
251: .getToken(imageId));
252: }
253: }
254: }
255:
256: format(groupId, articleId, version, previewArticleId, el,
257: req);
258: }
259: }
260:
261: private static Log _log = LogFactory
262: .getLog(ViewArticleContentAction.class);
263:
264: }
|