1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.util.PortalUtil;
40  import com.liferay.portal.util.WebKeys;
41  import com.liferay.portlet.journal.model.JournalArticle;
42  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
43  import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
44  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
45  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
46  import com.liferay.portlet.journal.util.JournalUtil;
47  import com.liferay.util.PwdGenerator;
48  
49  import java.io.File;
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  
58  import org.apache.struts.action.Action;
59  import org.apache.struts.action.ActionForm;
60  import org.apache.struts.action.ActionForward;
61  import org.apache.struts.action.ActionMapping;
62  
63  /**
64   * <a href="ViewArticleContentAction.java.html"><b><i>View Source</i></b></a>
65   *
66   * @author Brian Wing Shun Chan
67   * @author Raymond Augé
68   */
69  public class ViewArticleContentAction extends Action {
70  
71      public ActionForward execute(
72              ActionMapping mapping, ActionForm form, HttpServletRequest request,
73              HttpServletResponse response)
74          throws Exception {
75  
76          UploadServletRequest uploadRequest = null;
77  
78          try {
79              String cmd = ParamUtil.getString(request, Constants.CMD);
80  
81              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
82                  WebKeys.THEME_DISPLAY);
83  
84              long groupId = ParamUtil.getLong(request, "groupId");
85              String articleId = ParamUtil.getString(request, "articleId");
86              double version = ParamUtil.getDouble(
87                  request, "version", JournalArticleImpl.DEFAULT_VERSION);
88  
89              String languageId = LanguageUtil.getLanguageId(request);
90  
91              String output = null;
92  
93              if (cmd.equals(Constants.PREVIEW)) {
94                  uploadRequest = PortalUtil.getUploadServletRequest(request);
95  
96                  String title = ParamUtil.getString(uploadRequest, "title");
97                  String description = ParamUtil.getString(
98                      uploadRequest, "description");
99                  String type = ParamUtil.getString(uploadRequest, "type");
100                 String structureId = ParamUtil.getString(
101                     uploadRequest, "structureId");
102                 String templateId = ParamUtil.getString(
103                     uploadRequest, "templateId");
104 
105                 Date now = new Date();
106 
107                 Date createDate = now;
108                 Date modifiedDate = now;
109                 Date displayDate = now;
110 
111                 User user = PortalUtil.getUser(uploadRequest);
112 
113                 String xml = ParamUtil.getString(uploadRequest, "xml");
114 
115                 Document doc = SAXReaderUtil.read(xml);
116 
117                 Element root = doc.getRootElement();
118 
119                 String previewArticleId =
120                     "PREVIEW_" +
121                     PwdGenerator.getPassword(PwdGenerator.KEY3, 10);
122 
123                 format(
124                     groupId, articleId, version, previewArticleId, root,
125                     uploadRequest);
126 
127                 Map<String, String> tokens = JournalUtil.getTokens(
128                     groupId, themeDisplay);
129 
130                 tokens.put("article_resource_pk", "-1");
131 
132                 JournalArticle article = new JournalArticleImpl();
133 
134                 article.setGroupId(groupId);
135                 article.setCompanyId(user.getCompanyId());
136                 article.setUserId(user.getUserId());
137                 article.setUserName(user.getFullName());
138                 article.setCreateDate(createDate);
139                 article.setModifiedDate(modifiedDate);
140                 article.setArticleId(articleId);
141                 article.setVersion(version);
142                 article.setTitle(title);
143                 article.setDescription(description);
144                 article.setContent(xml);
145                 article.setType(type);
146                 article.setStructureId(structureId);
147                 article.setTemplateId(templateId);
148                 article.setDisplayDate(displayDate);
149 
150                 output = JournalArticleLocalServiceUtil.getArticleContent(
151                     article, templateId, languageId, themeDisplay);
152             }
153             else {
154                 output = JournalArticleServiceUtil.getArticleContent(
155                     groupId, articleId, version, languageId, themeDisplay);
156             }
157 
158             request.setAttribute(WebKeys.JOURNAL_ARTICLE_CONTENT, output);
159 
160             if (output.startsWith("<?xml ")) {
161                 return mapping.findForward(
162                     "portlet.journal.raw_article_content");
163             }
164             else {
165                 return mapping.findForward(
166                     "portlet.journal.view_article_content");
167             }
168         }
169         catch (Exception e) {
170             PortalUtil.sendError(e, request, response);
171 
172             return null;
173         }
174         finally {
175             if (uploadRequest != null) {
176                 uploadRequest.cleanUp();
177             }
178         }
179     }
180 
181     protected void format(
182             long groupId, String articleId, double version,
183             String previewArticleId, Element root,
184             UploadServletRequest uploadRequest)
185         throws Exception {
186 
187         Iterator<Element> itr = root.elements().iterator();
188 
189         while (itr.hasNext()) {
190             Element el = itr.next();
191 
192             Element dynamicContent = el.element("dynamic-content");
193 
194             String elName = el.attributeValue("name", StringPool.BLANK);
195             String elType = el.attributeValue("type", StringPool.BLANK);
196             String elContent = StringPool.BLANK;
197             String elLanguage = StringPool.BLANK;
198 
199             if (dynamicContent != null) {
200                 elContent = dynamicContent.getTextTrim();
201 
202                 elLanguage = dynamicContent.attributeValue(
203                     "language-id", StringPool.BLANK);
204 
205                 if (!elLanguage.equals(StringPool.BLANK)) {
206                     elLanguage = "_" + elLanguage;
207                 }
208             }
209 
210             if (elType.equals("image") && Validator.isNull(elContent)) {
211                 File file = uploadRequest.getFile(
212                     "structure_image_" + elName + elLanguage);
213                 byte[] bytes = FileUtil.getBytes(file);
214 
215                 if ((bytes != null) && (bytes.length > 0)) {
216                     long imageId =
217                         JournalArticleImageLocalServiceUtil.getArticleImageId(
218                             groupId, previewArticleId, version, elName,
219                             elLanguage, true);
220 
221                     String token = ImageServletTokenUtil.getToken(imageId);
222 
223                     dynamicContent.setText(
224                         "/image/journal/article?img_id=" + imageId + "&t=" +
225                             token);
226 
227                     ImageLocalServiceUtil.updateImage(imageId, bytes);
228                 }
229                 else {
230                     if (Validator.isNotNull(articleId)) {
231                         long imageId = JournalArticleImageLocalServiceUtil.
232                             getArticleImageId(
233                                 groupId, articleId, version, elName,
234                                 elLanguage);
235 
236                         String token = ImageServletTokenUtil.getToken(imageId);
237 
238                         dynamicContent.setText(
239                             "/image/journal/article?img_id=" + imageId +
240                                 "&t=" + token);
241                     }
242                 }
243             }
244 
245             format(
246                 groupId, articleId, version, previewArticleId, el,
247                 uploadRequest);
248         }
249     }
250 
251 }