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