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