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