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