001
014
015 package com.liferay.portlet.journalcontent.action;
016
017 import com.liferay.portal.kernel.language.LanguageUtil;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.ParamUtil;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.kernel.xml.Document;
025 import com.liferay.portal.kernel.xml.Node;
026 import com.liferay.portal.kernel.xml.SAXReaderUtil;
027 import com.liferay.portal.util.PortalUtil;
028 import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
029 import com.liferay.portlet.journal.model.JournalArticle;
030 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
031 import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
032
033 import javax.servlet.http.HttpServletRequest;
034 import javax.servlet.http.HttpServletResponse;
035
036 import org.apache.struts.action.Action;
037 import org.apache.struts.action.ActionForm;
038 import org.apache.struts.action.ActionForward;
039 import org.apache.struts.action.ActionMapping;
040
041
044 public class UpdateArticleFieldAction extends Action {
045
046 @Override
047 public ActionForward execute(
048 ActionMapping actionMapping, ActionForm actionForm,
049 HttpServletRequest request, HttpServletResponse response)
050 throws Exception {
051
052 try {
053 updateArticleField(request, response);
054
055 return null;
056 }
057 catch (Exception e) {
058 PortalUtil.sendError(e, request, response);
059
060 return null;
061 }
062 }
063
064 protected void updateArticleField(
065 HttpServletRequest request, HttpServletResponse response)
066 throws Exception {
067
068 long groupId = ParamUtil.getLong(request, "groupId");
069 String articleId = ParamUtil.getString(request, "articleId");
070 double version = ParamUtil.getDouble(request, "version");
071
072 String containerId = ParamUtil.getString(request, "containerId");
073
074 if (Validator.isNotNull(containerId)) {
075 int x = containerId.indexOf("_");
076 int y = containerId.lastIndexOf("_");
077
078 if ((x != -1) && (y != -1)) {
079 groupId = GetterUtil.getLong(containerId.substring(0, x));
080 articleId = containerId.substring(x + 1, y);
081 version = GetterUtil.getDouble(containerId.substring(y));
082 }
083 }
084
085 String languageId = LanguageUtil.getLanguageId(request);
086
087 String fieldName = ParamUtil.getString(request, "fieldName");
088 String fieldData = ParamUtil.getString(request, "fieldData");
089
090 if (fieldName.startsWith("journal-content-field-name-")) {
091 fieldName = fieldName.substring(27);
092 }
093
094 JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
095 groupId, articleId, version);
096
097 String content = article.getContent();
098
099 Document doc = SAXReaderUtil.read(content);
100
101 if (_log.isDebugEnabled()) {
102 _log.debug("Before\n" + content);
103 }
104
105 String path =
106 "/root/dynamic-element[@name='" + fieldName +
107 "']/dynamic-content[@language-id='" + languageId + "']";
108
109 Node node = doc.selectSingleNode(path);
110
111 if (node == null) {
112 path =
113 "/root/dynamic-element[@name='" + fieldName +
114 "']/dynamic-content";
115
116 node = doc.selectSingleNode(path);
117 }
118
119 node.setText(fieldData);
120
121 content = DDMXMLUtil.formatXML(doc);
122
123 if (_log.isDebugEnabled()) {
124 _log.debug("After\n" + content);
125 }
126
127 JournalArticleServiceUtil.updateContent(
128 groupId, articleId, version, content);
129
130 ServletResponseUtil.write(response, fieldData);
131 }
132
133 private static Log _log = LogFactoryUtil.getLog(
134 UpdateArticleFieldAction.class);
135
136 }