1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.action;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.service.impl.ImageLocalUtil;
33  import com.liferay.portal.struts.ActionConstants;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.journal.model.JournalArticle;
38  import com.liferay.portlet.journal.model.JournalTemplate;
39  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
40  import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
41  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
42  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
43  import com.liferay.portlet.journal.util.JournalUtil;
44  import com.liferay.util.FileUtil;
45  import com.liferay.util.PwdGenerator;
46  import com.liferay.util.servlet.UploadServletRequest;
47  
48  import java.io.File;
49  import java.io.StringReader;
50  
51  import java.util.Date;
52  import java.util.Iterator;
53  import java.util.Map;
54  
55  import javax.servlet.http.HttpServletRequest;
56  import javax.servlet.http.HttpServletResponse;
57  import javax.servlet.jsp.PageContext;
58  
59  import org.apache.struts.action.Action;
60  import org.apache.struts.action.ActionForm;
61  import org.apache.struts.action.ActionForward;
62  import org.apache.struts.action.ActionMapping;
63  
64  import org.dom4j.Document;
65  import org.dom4j.Element;
66  import org.dom4j.io.SAXReader;
67  
68  /**
69   * <a href="ViewArticleContentAction.java.html"><b><i>View Source</i></b></a>
70   *
71   * @author Brian Wing Shun Chan
72   * @author Raymond Augé
73   *
74   */
75  public class ViewArticleContentAction extends Action {
76  
77      public ActionForward execute(
78              ActionMapping mapping, ActionForm form, HttpServletRequest req,
79              HttpServletResponse res)
80          throws Exception {
81  
82          UploadServletRequest uploadReq = null;
83  
84          try {
85              String cmd = ParamUtil.getString(req, Constants.CMD);
86  
87              ThemeDisplay themeDisplay =
88                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
89  
90              long groupId = ParamUtil.getLong(req, "groupId");
91              String articleId = ParamUtil.getString(req, "articleId");
92              double version = ParamUtil.getDouble(
93                  req, "version", JournalArticleImpl.DEFAULT_VERSION);
94  
95              String languageId = LanguageUtil.getLanguageId(req);
96  
97              String output = null;
98  
99              if (cmd.equals(Constants.PREVIEW)) {
100                 uploadReq = new UploadServletRequest(req);
101 
102                 String title = ParamUtil.getString(uploadReq, "title");
103                 String description = ParamUtil.getString(
104                     uploadReq, "description");
105 
106                 Date now = new Date();
107 
108                 Date createDate = now;
109                 Date modifiedDate = now;
110                 Date displayDate = now;
111 
112                 User user = PortalUtil.getUser(uploadReq);
113 
114                 String xml = ParamUtil.getString(uploadReq, "xml");
115 
116                 SAXReader reader = new SAXReader();
117 
118                 Document doc = reader.read(new StringReader(xml));
119 
120                 Element root = doc.getRootElement();
121 
122                 String previewArticleId =
123                     "PREVIEW_" +
124                     PwdGenerator.getPassword(PwdGenerator.KEY3, 10);
125 
126                 format(
127                     groupId, articleId, version, previewArticleId, root,
128                     uploadReq);
129 
130                 Map tokens = JournalUtil.getTokens(groupId, themeDisplay);
131 
132                 JournalArticle article = new JournalArticleImpl();
133 
134                 article.setUserId(user.getUserId());
135                 article.setCreateDate(createDate);
136                 article.setModifiedDate(modifiedDate);
137                 article.setArticleId(articleId);
138                 article.setVersion(version);
139                 article.setTitle(title);
140                 article.setDescription(description);
141                 article.setDisplayDate(displayDate);
142 
143                 JournalUtil.addAllReservedEls(root, tokens, article);
144 
145                 xml = JournalUtil.formatXML(doc);
146 
147                 String templateId = ParamUtil.getString(
148                     uploadReq, "templateId");
149 
150                 JournalTemplate template =
151                     JournalTemplateLocalServiceUtil.getTemplate(
152                         groupId, templateId);
153 
154                 String langType = template.getLangType();
155                 String script = template.getXsl();
156 
157                 output = JournalUtil.transform(
158                     tokens, languageId, xml, script, langType);
159             }
160             else {
161                 output = JournalArticleServiceUtil.getArticleContent(
162                     groupId, articleId, version, languageId, themeDisplay);
163             }
164 
165             req.setAttribute(WebKeys.JOURNAL_ARTICLE_CONTENT, output);
166 
167             if (output.startsWith("<?xml ")) {
168                 return mapping.findForward(
169                     "portlet.journal.raw_article_content");
170             }
171             else {
172                 return mapping.findForward(
173                     "portlet.journal.view_article_content");
174             }
175         }
176         catch (Exception e) {
177             req.setAttribute(PageContext.EXCEPTION, e);
178 
179             return mapping.findForward(ActionConstants.COMMON_ERROR);
180         }
181         finally {
182             if (uploadReq != null) {
183                 uploadReq.cleanUp();
184             }
185         }
186     }
187 
188     protected void format(
189             long groupId, String articleId, double version,
190             String previewArticleId, Element root, UploadServletRequest req)
191         throws Exception {
192 
193         Iterator itr = root.elements().iterator();
194 
195         while (itr.hasNext()) {
196             Element el = (Element)itr.next();
197 
198             Element dynamicContent = el.element("dynamic-content");
199 
200             String elName = el.attributeValue("name", StringPool.BLANK);
201             String elType = el.attributeValue("type", StringPool.BLANK);
202             String elLanguage = "";
203 
204             if (dynamicContent != null) {
205                 elLanguage = dynamicContent.attributeValue(
206                     "language-id", StringPool.BLANK);
207 
208                 if (!elLanguage.equals(StringPool.BLANK)) {
209                     elLanguage = "_" + elLanguage;
210                 }
211             }
212 
213             if (elType.equals("image")) {
214                 File file = req.getFile(
215                     "structure_image_" + elName + elLanguage);
216                 byte[] bytes = FileUtil.getBytes(file);
217 
218                 if ((bytes != null) && (bytes.length > 0)) {
219                     long imageId =
220                         JournalArticleImageLocalServiceUtil.getArticleImageId(
221                             groupId, previewArticleId, version, elName,
222                             elLanguage, true);
223 
224                     dynamicContent.setText(
225                         "/image/journal/article?img_id=" + imageId + "&t=" +
226                             ImageServletTokenUtil.getToken(imageId));
227 
228                     ImageLocalUtil.updateImage(imageId, bytes);
229                 }
230                 else {
231                     if (Validator.isNotNull(articleId)) {
232                         long imageId = JournalArticleImageLocalServiceUtil.
233                             getArticleImageId(
234                                 groupId, articleId, version, elName,
235                                 elLanguage);
236 
237                         dynamicContent.setText(
238                             "/image/journal/article?img_id=" + imageId + "&t=" +
239                                 ImageServletTokenUtil.getToken(imageId));
240                     }
241                 }
242             }
243 
244             format(
245                 groupId, articleId, version, previewArticleId, el, req);
246         }
247     }
248 
249 }