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.upload.UploadServletRequest;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.kernel.xml.Document;
34  import com.liferay.portal.kernel.xml.Element;
35  import com.liferay.portal.kernel.xml.SAXReaderUtil;
36  import com.liferay.portal.model.User;
37  import com.liferay.portal.service.ImageLocalServiceUtil;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.upload.UploadServletRequestImpl;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.WebKeys;
42  import com.liferay.portlet.journal.model.JournalArticle;
43  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
44  import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
45  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
46  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
47  import com.liferay.portlet.journal.util.JournalUtil;
48  import com.liferay.util.PwdGenerator;
49  
50  import java.io.File;
51  
52  import java.util.Date;
53  import java.util.Iterator;
54  import java.util.Map;
55  
56  import javax.servlet.http.HttpServletRequest;
57  import javax.servlet.http.HttpServletResponse;
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  /**
65   * <a href="ViewArticleContentAction.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Brian Wing Shun Chan
68   * @author Raymond Augé
69   *
70   */
71  public class ViewArticleContentAction extends Action {
72  
73      public ActionForward execute(
74              ActionMapping mapping, ActionForm form, HttpServletRequest request,
75              HttpServletResponse response)
76          throws Exception {
77  
78          UploadServletRequest uploadRequest = null;
79  
80          try {
81              String cmd = ParamUtil.getString(request, Constants.CMD);
82  
83              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
84                  WebKeys.THEME_DISPLAY);
85  
86              long groupId = ParamUtil.getLong(request, "groupId");
87              String articleId = ParamUtil.getString(request, "articleId");
88              double version = ParamUtil.getDouble(
89                  request, "version", JournalArticleImpl.DEFAULT_VERSION);
90  
91              String languageId = LanguageUtil.getLanguageId(request);
92  
93              String output = null;
94  
95              if (cmd.equals(Constants.PREVIEW)) {
96                  uploadRequest = new UploadServletRequestImpl(request);
97  
98                  String title = ParamUtil.getString(uploadRequest, "title");
99                  String description = ParamUtil.getString(
100                     uploadRequest, "description");
101                 String type = ParamUtil.getString(uploadRequest, "type");
102                 String structureId = ParamUtil.getString(
103                     uploadRequest, "structureId");
104                 String templateId = ParamUtil.getString(
105                     uploadRequest, "templateId");
106 
107                 Date now = new Date();
108 
109                 Date createDate = now;
110                 Date modifiedDate = now;
111                 Date displayDate = now;
112 
113                 User user = PortalUtil.getUser(uploadRequest);
114 
115                 String xml = ParamUtil.getString(uploadRequest, "xml");
116 
117                 Document doc = SAXReaderUtil.read(xml);
118 
119                 Element root = doc.getRootElement();
120 
121                 String previewArticleId =
122                     "PREVIEW_" +
123                     PwdGenerator.getPassword(PwdGenerator.KEY3, 10);
124 
125                 format(
126                     groupId, articleId, version, previewArticleId, root,
127                     uploadRequest);
128 
129                 Map<String, String> tokens = JournalUtil.getTokens(
130                     groupId, themeDisplay);
131 
132                 tokens.put("article_resource_pk", "-1");
133 
134                 JournalArticle article = new JournalArticleImpl();
135 
136                 article.setGroupId(groupId);
137                 article.setCompanyId(user.getCompanyId());
138                 article.setUserId(user.getUserId());
139                 article.setUserName(user.getFullName());
140                 article.setCreateDate(createDate);
141                 article.setModifiedDate(modifiedDate);
142                 article.setArticleId(articleId);
143                 article.setVersion(version);
144                 article.setTitle(title);
145                 article.setDescription(description);
146                 article.setContent(xml);
147                 article.setType(type);
148                 article.setStructureId(structureId);
149                 article.setTemplateId(templateId);
150                 article.setDisplayDate(displayDate);
151 
152                 output = JournalArticleLocalServiceUtil.getArticleContent(
153                     article, templateId, languageId, 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 (uploadRequest != null) {
178                 uploadRequest.cleanUp();
179             }
180         }
181     }
182 
183     protected void format(
184             long groupId, String articleId, double version,
185             String previewArticleId, Element root,
186             UploadServletRequest uploadRequest)
187         throws Exception {
188 
189         Iterator<Element> itr = root.elements().iterator();
190 
191         while (itr.hasNext()) {
192             Element el = itr.next();
193 
194             Element dynamicContent = el.element("dynamic-content");
195 
196             String elName = el.attributeValue("name", StringPool.BLANK);
197             String elType = el.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 = uploadRequest.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, elName,
221                             elLanguage, true);
222 
223                     String token = ImageServletTokenUtil.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, elName,
236                                 elLanguage);
237 
238                         String token = ImageServletTokenUtil.getToken(imageId);
239 
240                         dynamicContent.setText(
241                             "/image/journal/article?img_id=" + imageId +
242                                 "&t=" + token);
243                     }
244                 }
245             }
246 
247             format(
248                 groupId, articleId, version, previewArticleId, el,
249                 uploadRequest);
250         }
251     }
252 
253 }