1   /**
2    * Copyright (c) 2000-2008 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.commons.logging.Log;
60  import org.apache.commons.logging.LogFactory;
61  import org.apache.struts.action.Action;
62  import org.apache.struts.action.ActionForm;
63  import org.apache.struts.action.ActionForward;
64  import org.apache.struts.action.ActionMapping;
65  
66  import org.dom4j.Document;
67  import org.dom4j.Element;
68  import org.dom4j.io.SAXReader;
69  
70  /**
71   * <a href="ViewArticleContentAction.java.html"><b><i>View Source</i></b></a>
72   *
73   * @author Brian Wing Shun Chan
74   * @author Raymond Augé
75   *
76   */
77  public class ViewArticleContentAction extends Action {
78  
79      public ActionForward execute(
80              ActionMapping mapping, ActionForm form, HttpServletRequest req,
81              HttpServletResponse res)
82          throws Exception {
83  
84          UploadServletRequest uploadReq = null;
85  
86          try {
87              String cmd = ParamUtil.getString(req, Constants.CMD);
88  
89              ThemeDisplay themeDisplay =
90                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
91  
92              long groupId = ParamUtil.getLong(req, "groupId");
93              String articleId = ParamUtil.getString(req, "articleId");
94              double version = ParamUtil.getDouble(
95                  req, "version", JournalArticleImpl.DEFAULT_VERSION);
96  
97              String languageId = LanguageUtil.getLanguageId(req);
98  
99              String output = null;
100 
101             if (cmd.equals(Constants.PREVIEW)) {
102                 uploadReq = new UploadServletRequest(req);
103 
104                 String title = ParamUtil.getString(uploadReq, "title");
105                 String description = ParamUtil.getString(
106                     uploadReq, "description");
107 
108                 Date now = new Date();
109 
110                 Date createDate = now;
111                 Date modifiedDate = now;
112                 Date displayDate = now;
113 
114                 User user = PortalUtil.getUser(uploadReq);
115 
116                 String xml = ParamUtil.getString(uploadReq, "xml");
117 
118                 SAXReader reader = new SAXReader();
119 
120                 Document doc = reader.read(new StringReader(xml));
121 
122                 Element root = doc.getRootElement();
123 
124                 String previewArticleId =
125                     "PREVIEW_" +
126                     PwdGenerator.getPassword(PwdGenerator.KEY3, 10);
127 
128                 format(
129                     groupId, articleId, version, previewArticleId, root,
130                     uploadReq);
131 
132                 Map tokens = JournalUtil.getTokens(groupId, themeDisplay);
133 
134                 tokens.put("article_resource_pk", "-1");
135 
136                 JournalArticle article = new JournalArticleImpl();
137 
138                 article.setUserId(user.getUserId());
139                 article.setCreateDate(createDate);
140                 article.setModifiedDate(modifiedDate);
141                 article.setArticleId(articleId);
142                 article.setVersion(version);
143                 article.setTitle(title);
144                 article.setDescription(description);
145                 article.setDisplayDate(displayDate);
146 
147                 JournalUtil.addAllReservedEls(root, tokens, article);
148 
149                 xml = JournalUtil.formatXML(doc);
150 
151                 String templateId = ParamUtil.getString(
152                     uploadReq, "templateId");
153 
154                 JournalTemplate template =
155                     JournalTemplateLocalServiceUtil.getTemplate(
156                         groupId, templateId);
157 
158                 String langType = template.getLangType();
159                 String script = template.getXsl();
160 
161                 try {
162                     output = JournalUtil.transform(
163                         tokens, languageId, xml, script, langType);
164                 }
165                 catch (Exception e1) {
166                     _log.error(e1, e1);
167 
168                     PortalUtil.sendError(
169                         HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1, req,
170                         res);
171                 }
172             }
173             else {
174                 output = JournalArticleServiceUtil.getArticleContent(
175                     groupId, articleId, version, languageId, themeDisplay);
176             }
177 
178             req.setAttribute(WebKeys.JOURNAL_ARTICLE_CONTENT, output);
179 
180             if (output.startsWith("<?xml ")) {
181                 return mapping.findForward(
182                     "portlet.journal.raw_article_content");
183             }
184             else {
185                 return mapping.findForward(
186                     "portlet.journal.view_article_content");
187             }
188         }
189         catch (Exception e2) {
190             req.setAttribute(PageContext.EXCEPTION, e2);
191 
192             return mapping.findForward(ActionConstants.COMMON_ERROR);
193         }
194         finally {
195             if (uploadReq != null) {
196                 uploadReq.cleanUp();
197             }
198         }
199     }
200 
201     protected void format(
202             long groupId, String articleId, double version,
203             String previewArticleId, Element root, UploadServletRequest req)
204         throws Exception {
205 
206         Iterator itr = root.elements().iterator();
207 
208         while (itr.hasNext()) {
209             Element el = (Element)itr.next();
210 
211             Element dynamicContent = el.element("dynamic-content");
212 
213             String elName = el.attributeValue("name", StringPool.BLANK);
214             String elType = el.attributeValue("type", StringPool.BLANK);
215             String elLanguage = "";
216 
217             if (dynamicContent != null) {
218                 elLanguage = dynamicContent.attributeValue(
219                     "language-id", StringPool.BLANK);
220 
221                 if (!elLanguage.equals(StringPool.BLANK)) {
222                     elLanguage = "_" + elLanguage;
223                 }
224             }
225 
226             if (elType.equals("image")) {
227                 File file = req.getFile(
228                     "structure_image_" + elName + elLanguage);
229                 byte[] bytes = FileUtil.getBytes(file);
230 
231                 if ((bytes != null) && (bytes.length > 0)) {
232                     long imageId =
233                         JournalArticleImageLocalServiceUtil.getArticleImageId(
234                             groupId, previewArticleId, version, elName,
235                             elLanguage, true);
236 
237                     dynamicContent.setText(
238                         "/image/journal/article?img_id=" + imageId + "&t=" +
239                             ImageServletTokenUtil.getToken(imageId));
240 
241                     ImageLocalUtil.updateImage(imageId, bytes);
242                 }
243                 else {
244                     if (Validator.isNotNull(articleId)) {
245                         long imageId = JournalArticleImageLocalServiceUtil.
246                             getArticleImageId(
247                                 groupId, articleId, version, elName,
248                                 elLanguage);
249 
250                         dynamicContent.setText(
251                             "/image/journal/article?img_id=" + imageId + "&t=" +
252                                 ImageServletTokenUtil.getToken(imageId));
253                     }
254                 }
255             }
256 
257             format(
258                 groupId, articleId, version, previewArticleId, el, req);
259         }
260     }
261 
262     private static Log _log = LogFactory.getLog(ViewArticleContentAction.class);
263 
264 }