1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journalcontent.action;
21  
22  import com.liferay.portal.kernel.language.LanguageUtil;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Node;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.journal.model.JournalArticle;
33  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
34  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
35  import com.liferay.portlet.journal.util.JournalUtil;
36  import com.liferay.util.servlet.ServletResponseUtil;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.struts.action.Action;
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="UpdateArticleFieldAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class UpdateArticleFieldAction extends Action {
53  
54      public ActionForward execute(
55              ActionMapping mapping, ActionForm form, HttpServletRequest request,
56              HttpServletResponse response)
57          throws Exception {
58  
59          try {
60              updateArticleField(request, response);
61  
62              return null;
63          }
64          catch (Exception e) {
65              PortalUtil.sendError(e, request, response);
66  
67              return null;
68          }
69      }
70  
71      protected void updateArticleField(
72              HttpServletRequest request, HttpServletResponse response)
73          throws Exception {
74  
75          long groupId = ParamUtil.getLong(request, "groupId");
76          String articleId = ParamUtil.getString(request, "articleId");
77          double version = ParamUtil.getDouble(request, "version");
78  
79          String containerId = ParamUtil.getString(request, "containerId");
80  
81          if (Validator.isNotNull(containerId)) {
82              int x = containerId.indexOf("_");
83              int y = containerId.lastIndexOf("_");
84  
85              if ((x != -1) && (y != -1)) {
86                  groupId = GetterUtil.getLong(containerId.substring(0, x));
87                  articleId = containerId.substring(x + 1, y);
88                  version = GetterUtil.getDouble(
89                      containerId.substring(y, containerId.length()));
90              }
91          }
92  
93          String languageId = LanguageUtil.getLanguageId(request);
94  
95          String fieldName = ParamUtil.getString(request, "fieldName");
96          String fieldData = ParamUtil.getString(request, "fieldData");
97  
98          if (fieldName.startsWith("journal-content-field-name-")) {
99              fieldName = fieldName.substring(27, fieldName.length());
100         }
101 
102         JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
103             groupId, articleId, version);
104 
105         String content = article.getContent();
106 
107         Document doc = SAXReaderUtil.read(content);
108 
109         if (_log.isDebugEnabled()) {
110             _log.debug("Before\n" + content);
111         }
112 
113         String path =
114             "/root/dynamic-element[@name='" + fieldName +
115                 "']/dynamic-content[@language-id='" + languageId + "']";
116 
117         Node node = doc.selectSingleNode(path);
118 
119         if (node == null) {
120             path =
121                 "/root/dynamic-element[@name='" + fieldName +
122                     "']/dynamic-content";
123 
124             node = doc.selectSingleNode(path);
125         }
126 
127         node.setText(fieldData);
128 
129         content = JournalUtil.formatXML(doc);
130 
131         if (_log.isDebugEnabled()) {
132             _log.debug("After\n" + content);
133         }
134 
135         JournalArticleServiceUtil.updateContent(
136             groupId, articleId, version, content);
137 
138         ServletResponseUtil.write(response, fieldData);
139     }
140 
141     private static Log _log =
142         LogFactoryUtil.getLog(UpdateArticleFieldAction.class);
143 
144 }