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 mapping, ActionForm form, HttpServletRequest request,
049 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(
082 containerId.substring(y, containerId.length()));
083 }
084 }
085
086 String languageId = LanguageUtil.getLanguageId(request);
087
088 String fieldName = ParamUtil.getString(request, "fieldName");
089 String fieldData = ParamUtil.getString(request, "fieldData");
090
091 if (fieldName.startsWith("journal-content-field-name-")) {
092 fieldName = fieldName.substring(27, fieldName.length());
093 }
094
095 JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
096 groupId, articleId, version);
097
098 String content = article.getContent();
099
100 Document doc = SAXReaderUtil.read(content);
101
102 if (_log.isDebugEnabled()) {
103 _log.debug("Before\n" + content);
104 }
105
106 String path =
107 "/root/dynamic-element[@name='" + fieldName +
108 "']/dynamic-content[@language-id='" + languageId + "']";
109
110 Node node = doc.selectSingleNode(path);
111
112 if (node == null) {
113 path =
114 "/root/dynamic-element[@name='" + fieldName +
115 "']/dynamic-content";
116
117 node = doc.selectSingleNode(path);
118 }
119
120 node.setText(fieldData);
121
122 content = DDMXMLUtil.formatXML(doc);
123
124 if (_log.isDebugEnabled()) {
125 _log.debug("After\n" + content);
126 }
127
128 JournalArticleServiceUtil.updateContent(
129 groupId, articleId, version, content);
130
131 ServletResponseUtil.write(response, fieldData);
132 }
133
134 private static Log _log = LogFactoryUtil.getLog(
135 UpdateArticleFieldAction.class);
136
137 }