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.journalcontent.action;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.journal.model.JournalArticle;
31  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
32  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
33  import com.liferay.portlet.journal.util.JournalUtil;
34  import com.liferay.util.servlet.ServletResponseUtil;
35  
36  import java.io.StringReader;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  import org.apache.struts.action.Action;
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionForward;
46  import org.apache.struts.action.ActionMapping;
47  
48  import org.dom4j.Document;
49  import org.dom4j.Node;
50  import org.dom4j.io.SAXReader;
51  
52  /**
53   * <a href="UpdateArticleFieldAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class UpdateArticleFieldAction extends Action {
59  
60      public ActionForward execute(
61              ActionMapping mapping, ActionForm form, HttpServletRequest request,
62              HttpServletResponse response)
63          throws Exception {
64  
65          try {
66              updateArticleField(request, response);
67  
68              return null;
69          }
70          catch (Exception e) {
71              PortalUtil.sendError(e, request, response);
72  
73              return null;
74          }
75      }
76  
77      protected void updateArticleField(
78              HttpServletRequest request, HttpServletResponse response)
79          throws Exception {
80  
81          long groupId = ParamUtil.getLong(request, "groupId");
82          String articleId = ParamUtil.getString(request, "articleId");
83          double version = ParamUtil.getDouble(request, "version");
84  
85          String containerId = ParamUtil.getString(request, "containerId");
86  
87          if (Validator.isNotNull(containerId)) {
88              int x = containerId.indexOf("_");
89              int y = containerId.lastIndexOf("_");
90  
91              if ((x != -1) && (y != -1)) {
92                  groupId = GetterUtil.getLong(containerId.substring(0, x));
93                  articleId = containerId.substring(x + 1, y);
94                  version = GetterUtil.getDouble(
95                      containerId.substring(y, containerId.length()));
96              }
97          }
98  
99          String languageId = LanguageUtil.getLanguageId(request);
100 
101         String fieldName = ParamUtil.getString(request, "fieldName");
102         String fieldData = ParamUtil.getString(request, "fieldData");
103 
104         if (fieldName.startsWith("journal-content-field-name-")) {
105             fieldName = fieldName.substring(27, fieldName.length());
106         }
107 
108         JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
109             groupId, articleId, version);
110 
111         String content = article.getContent();
112 
113         SAXReader reader = new SAXReader();
114 
115         Document doc = reader.read(new StringReader(content));
116 
117         if (_log.isDebugEnabled()) {
118             _log.debug("Before\n" + content);
119         }
120 
121         String path =
122             "/root/dynamic-element[@name='" + fieldName +
123                 "']/dynamic-content[@language-id='" + languageId + "']";
124 
125         Node node = doc.selectSingleNode(path);
126 
127         if (node == null) {
128             path =
129                 "/root/dynamic-element[@name='" + fieldName +
130                     "']/dynamic-content";
131 
132             node = doc.selectSingleNode(path);
133         }
134 
135         node.setText(fieldData);
136 
137         content = JournalUtil.formatXML(doc);
138 
139         if (_log.isDebugEnabled()) {
140             _log.debug("After\n" + content);
141         }
142 
143         JournalArticleServiceUtil.updateContent(
144             groupId, articleId, version, content);
145 
146         ServletResponseUtil.write(response, fieldData);
147     }
148 
149     private static Log _log = LogFactory.getLog(UpdateArticleFieldAction.class);
150 
151 }