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