1
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
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 }