1   /**
2    * Copyright (c) 2000-2009 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.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   */
55  public class UpdateArticleFieldAction extends Action {
56  
57      public ActionForward execute(
58              ActionMapping mapping, ActionForm form, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          try {
63              updateArticleField(request, response);
64  
65              return null;
66          }
67          catch (Exception e) {
68              PortalUtil.sendError(e, request, response);
69  
70              return null;
71          }
72      }
73  
74      protected void updateArticleField(
75              HttpServletRequest request, HttpServletResponse response)
76          throws Exception {
77  
78          long groupId = ParamUtil.getLong(request, "groupId");
79          String articleId = ParamUtil.getString(request, "articleId");
80          double version = ParamUtil.getDouble(request, "version");
81  
82          String containerId = ParamUtil.getString(request, "containerId");
83  
84          if (Validator.isNotNull(containerId)) {
85              int x = containerId.indexOf("_");
86              int y = containerId.lastIndexOf("_");
87  
88              if ((x != -1) && (y != -1)) {
89                  groupId = GetterUtil.getLong(containerId.substring(0, x));
90                  articleId = containerId.substring(x + 1, y);
91                  version = GetterUtil.getDouble(
92                      containerId.substring(y, containerId.length()));
93              }
94          }
95  
96          String languageId = LanguageUtil.getLanguageId(request);
97  
98          String fieldName = ParamUtil.getString(request, "fieldName");
99          String fieldData = ParamUtil.getString(request, "fieldData");
100 
101         if (fieldName.startsWith("journal-content-field-name-")) {
102             fieldName = fieldName.substring(27, fieldName.length());
103         }
104 
105         JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
106             groupId, articleId, version);
107 
108         String content = article.getContent();
109 
110         Document doc = SAXReaderUtil.read(content);
111 
112         if (_log.isDebugEnabled()) {
113             _log.debug("Before\n" + content);
114         }
115 
116         String path =
117             "/root/dynamic-element[@name='" + fieldName +
118                 "']/dynamic-content[@language-id='" + languageId + "']";
119 
120         Node node = doc.selectSingleNode(path);
121 
122         if (node == null) {
123             path =
124                 "/root/dynamic-element[@name='" + fieldName +
125                     "']/dynamic-content";
126 
127             node = doc.selectSingleNode(path);
128         }
129 
130         node.setText(fieldData);
131 
132         content = JournalUtil.formatXML(doc);
133 
134         if (_log.isDebugEnabled()) {
135             _log.debug("After\n" + content);
136         }
137 
138         JournalArticleServiceUtil.updateContent(
139             groupId, articleId, version, content);
140 
141         ServletResponseUtil.write(response, fieldData);
142     }
143 
144     private static Log _log =
145          LogFactoryUtil.getLog(UpdateArticleFieldAction.class);
146 
147 }