1
14
15 package com.liferay.portlet.journalcontent.action;
16
17 import com.liferay.portal.kernel.language.LanguageUtil;
18 import com.liferay.portal.kernel.log.Log;
19 import com.liferay.portal.kernel.log.LogFactoryUtil;
20 import com.liferay.portal.kernel.util.GetterUtil;
21 import com.liferay.portal.kernel.util.ParamUtil;
22 import com.liferay.portal.kernel.util.Validator;
23 import com.liferay.portal.kernel.xml.Document;
24 import com.liferay.portal.kernel.xml.Node;
25 import com.liferay.portal.kernel.xml.SAXReaderUtil;
26 import com.liferay.portal.util.PortalUtil;
27 import com.liferay.portlet.journal.model.JournalArticle;
28 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
29 import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
30 import com.liferay.portlet.journal.util.JournalUtil;
31 import com.liferay.util.servlet.ServletResponseUtil;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.apache.struts.action.Action;
37 import org.apache.struts.action.ActionForm;
38 import org.apache.struts.action.ActionForward;
39 import org.apache.struts.action.ActionMapping;
40
41
46 public class UpdateArticleFieldAction extends Action {
47
48 public ActionForward execute(
49 ActionMapping mapping, ActionForm form, HttpServletRequest request,
50 HttpServletResponse response)
51 throws Exception {
52
53 try {
54 updateArticleField(request, response);
55
56 return null;
57 }
58 catch (Exception e) {
59 PortalUtil.sendError(e, request, response);
60
61 return null;
62 }
63 }
64
65 protected void updateArticleField(
66 HttpServletRequest request, HttpServletResponse response)
67 throws Exception {
68
69 long groupId = ParamUtil.getLong(request, "groupId");
70 String articleId = ParamUtil.getString(request, "articleId");
71 double version = ParamUtil.getDouble(request, "version");
72
73 String containerId = ParamUtil.getString(request, "containerId");
74
75 if (Validator.isNotNull(containerId)) {
76 int x = containerId.indexOf("_");
77 int y = containerId.lastIndexOf("_");
78
79 if ((x != -1) && (y != -1)) {
80 groupId = GetterUtil.getLong(containerId.substring(0, x));
81 articleId = containerId.substring(x + 1, y);
82 version = GetterUtil.getDouble(
83 containerId.substring(y, containerId.length()));
84 }
85 }
86
87 String languageId = LanguageUtil.getLanguageId(request);
88
89 String fieldName = ParamUtil.getString(request, "fieldName");
90 String fieldData = ParamUtil.getString(request, "fieldData");
91
92 if (fieldName.startsWith("journal-content-field-name-")) {
93 fieldName = fieldName.substring(27, fieldName.length());
94 }
95
96 JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
97 groupId, articleId, version);
98
99 String content = article.getContent();
100
101 Document doc = SAXReaderUtil.read(content);
102
103 if (_log.isDebugEnabled()) {
104 _log.debug("Before\n" + content);
105 }
106
107 String path =
108 "/root/dynamic-element[@name='" + fieldName +
109 "']/dynamic-content[@language-id='" + languageId + "']";
110
111 Node node = doc.selectSingleNode(path);
112
113 if (node == null) {
114 path =
115 "/root/dynamic-element[@name='" + fieldName +
116 "']/dynamic-content";
117
118 node = doc.selectSingleNode(path);
119 }
120
121 node.setText(fieldData);
122
123 content = JournalUtil.formatXML(doc);
124
125 if (_log.isDebugEnabled()) {
126 _log.debug("After\n" + content);
127 }
128
129 JournalArticleServiceUtil.updateContent(
130 groupId, articleId, version, content);
131
132 ServletResponseUtil.write(response, fieldData);
133 }
134
135 private static Log _log = LogFactoryUtil.getLog(
136 UpdateArticleFieldAction.class);
137
138 }