001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journalcontent.action;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.struts.ActionConstants;
028: import com.liferay.portlet.journal.model.JournalArticle;
029: import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
030: import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
031: import com.liferay.portlet.journal.util.JournalUtil;
032: import com.liferay.util.servlet.ServletResponseUtil;
033:
034: import java.io.StringReader;
035:
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038: import javax.servlet.jsp.PageContext;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.apache.struts.action.Action;
043: import org.apache.struts.action.ActionForm;
044: import org.apache.struts.action.ActionForward;
045: import org.apache.struts.action.ActionMapping;
046:
047: import org.dom4j.Document;
048: import org.dom4j.Node;
049: import org.dom4j.io.SAXReader;
050:
051: /**
052: * <a href="UpdateArticleFieldAction.java.html"><b><i>View Source</i></b></a>
053: *
054: * @author Brian Wing Shun Chan
055: *
056: */
057: public class UpdateArticleFieldAction extends Action {
058:
059: public ActionForward execute(ActionMapping mapping,
060: ActionForm form, HttpServletRequest req,
061: HttpServletResponse res) throws Exception {
062:
063: try {
064: updateArticleField(req, res);
065:
066: return null;
067: } catch (Exception e) {
068: req.setAttribute(PageContext.EXCEPTION, e);
069:
070: return mapping.findForward(ActionConstants.COMMON_ERROR);
071: }
072: }
073:
074: protected void updateArticleField(HttpServletRequest req,
075: HttpServletResponse res) throws Exception {
076:
077: long groupId = ParamUtil.getLong(req, "groupId");
078: String articleId = ParamUtil.getString(req, "articleId");
079: double version = ParamUtil.getDouble(req, "version");
080:
081: String containerId = ParamUtil.getString(req, "containerId");
082:
083: if (Validator.isNotNull(containerId)) {
084: int x = containerId.indexOf("_");
085: int y = containerId.lastIndexOf("_");
086:
087: if ((x != -1) && (y != -1)) {
088: groupId = GetterUtil.getLong(containerId
089: .substring(0, x));
090: articleId = containerId.substring(x + 1, y);
091: version = GetterUtil.getDouble(containerId.substring(y,
092: containerId.length()));
093: }
094: }
095:
096: String languageId = LanguageUtil.getLanguageId(req);
097:
098: String fieldName = ParamUtil.getString(req, "fieldName");
099: String fieldData = ParamUtil.getString(req, "fieldData");
100:
101: if (fieldName.startsWith("journal-content-field-name-")) {
102: fieldName = fieldName.substring(27, fieldName.length());
103: }
104:
105: JournalArticle article = JournalArticleLocalServiceUtil
106: .getArticle(groupId, articleId, version);
107:
108: String content = article.getContent();
109:
110: SAXReader reader = new SAXReader();
111:
112: Document doc = reader.read(new StringReader(content));
113:
114: if (_log.isDebugEnabled()) {
115: _log.debug("Before\n" + content);
116: }
117:
118: String path = "/root/dynamic-element[@name='" + fieldName
119: + "']/dynamic-content[@language-id='" + languageId
120: + "']";
121:
122: Node node = doc.selectSingleNode(path);
123:
124: if (node == null) {
125: path = "/root/dynamic-element[@name='" + fieldName
126: + "']/dynamic-content";
127:
128: node = doc.selectSingleNode(path);
129: }
130:
131: node.setText(fieldData);
132:
133: content = JournalUtil.formatXML(doc);
134:
135: if (_log.isDebugEnabled()) {
136: _log.debug("After\n" + content);
137: }
138:
139: JournalArticleServiceUtil.updateContent(groupId, articleId,
140: version, content);
141:
142: ServletResponseUtil.write(res, fieldData);
143: }
144:
145: private static Log _log = LogFactory
146: .getLog(UpdateArticleFieldAction.class);
147:
148: }
|