001    /**
002     * Copyright (c) 2000-2012 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.upload.UploadServletRequest;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.service.ImageLocalServiceUtil;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portal.webserver.WebServerServletTokenUtil;
030    import com.liferay.portlet.journal.model.JournalArticle;
031    import com.liferay.portlet.journal.model.JournalArticleConstants;
032    import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
033    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
034    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
035    
036    import java.io.File;
037    
038    import java.util.Iterator;
039    
040    import javax.servlet.http.HttpServletRequest;
041    import javax.servlet.http.HttpServletResponse;
042    
043    import org.apache.struts.action.Action;
044    import org.apache.struts.action.ActionForm;
045    import org.apache.struts.action.ActionForward;
046    import org.apache.struts.action.ActionMapping;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Raymond Augé
051     */
052    public class ViewArticleContentAction extends Action {
053    
054            @Override
055            public ActionForward execute(
056                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
057                            HttpServletResponse response)
058                    throws Exception {
059    
060                    try {
061                            String cmd = ParamUtil.getString(request, Constants.CMD);
062    
063                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
064                                    WebKeys.THEME_DISPLAY);
065    
066                            long groupId = ParamUtil.getLong(request, "groupId");
067                            String articleId = ParamUtil.getString(request, "articleId");
068                            double version = ParamUtil.getDouble(
069                                    request, "version", JournalArticleConstants.VERSION_DEFAULT);
070    
071                            String languageId = LanguageUtil.getLanguageId(request);
072    
073                            String output = null;
074    
075                            if (cmd.equals(Constants.PREVIEW)) {
076                                    JournalArticle article = JournalArticleServiceUtil.getArticle(
077                                            groupId, articleId, version);
078    
079                                    output = JournalArticleLocalServiceUtil.getArticleContent(
080                                            article, article.getTemplateId(), null, languageId,
081                                            themeDisplay);
082                            }
083                            else {
084                                    output = JournalArticleServiceUtil.getArticleContent(
085                                            groupId, articleId, version, languageId, themeDisplay);
086                            }
087    
088                            request.setAttribute(WebKeys.JOURNAL_ARTICLE_CONTENT, output);
089    
090                            if (output.startsWith("<?xml ")) {
091                                    return mapping.findForward(
092                                            "portlet.journal.raw_article_content");
093                            }
094                            else {
095                                    return mapping.findForward(
096                                            "portlet.journal.view_article_content");
097                            }
098                    }
099                    catch (Exception e) {
100                            PortalUtil.sendError(e, request, response);
101    
102                            return null;
103                    }
104            }
105    
106            protected void format(
107                            long groupId, String articleId, double version,
108                            String previewArticleId, Element root,
109                            UploadServletRequest uploadServletRequest)
110                    throws Exception {
111    
112                    Iterator<Element> itr = root.elements().iterator();
113    
114                    while (itr.hasNext()) {
115                            Element el = itr.next();
116    
117                            Element dynamicContent = el.element("dynamic-content");
118    
119                            String elInstanceId = el.attributeValue(
120                                    "instance-id", StringPool.BLANK);
121                            String elName = el.attributeValue("name", StringPool.BLANK);
122                            String elType = el.attributeValue("type", StringPool.BLANK);
123                            String elContent = StringPool.BLANK;
124                            String elLanguage = StringPool.BLANK;
125    
126                            if (dynamicContent != null) {
127                                    elContent = dynamicContent.getTextTrim();
128    
129                                    elLanguage = dynamicContent.attributeValue(
130                                            "language-id", StringPool.BLANK);
131    
132                                    if (!elLanguage.equals(StringPool.BLANK)) {
133                                            elLanguage = "_" + elLanguage;
134                                    }
135                            }
136    
137                            if (elType.equals("image") && Validator.isNull(elContent)) {
138                                    File file = uploadServletRequest.getFile(
139                                            "structure_image_" + elName + elLanguage);
140                                    byte[] bytes = FileUtil.getBytes(file);
141    
142                                    if ((bytes != null) && (bytes.length > 0)) {
143                                            long imageId =
144                                                    JournalArticleImageLocalServiceUtil.getArticleImageId(
145                                                            groupId, previewArticleId, version, elInstanceId,
146                                                            elName, elLanguage, true);
147    
148                                            String token = WebServerServletTokenUtil.getToken(imageId);
149    
150                                            dynamicContent.setText(
151                                                    "/image/journal/article?img_id=" + imageId + "&t=" +
152                                                            token);
153    
154                                            ImageLocalServiceUtil.updateImage(imageId, bytes);
155                                    }
156                                    else {
157                                            if (Validator.isNotNull(articleId)) {
158                                                    long imageId = JournalArticleImageLocalServiceUtil.
159                                                            getArticleImageId(
160                                                                    groupId, articleId, version, elInstanceId,
161                                                                    elName, elLanguage);
162    
163                                                    String token = WebServerServletTokenUtil.getToken(
164                                                            imageId);
165    
166                                                    dynamicContent.setText(
167                                                            "/image/journal/article?img_id=" + imageId +
168                                                                    "&t=" + token);
169                                            }
170                                    }
171                            }
172    
173                            format(
174                                    groupId, articleId, version, previewArticleId, el,
175                                    uploadServletRequest);
176                    }
177            }
178    
179    }