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