001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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.util.GetterUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Node;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.journal.model.JournalArticle;
028    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
029    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
030    import com.liferay.portlet.journal.util.JournalUtil;
031    import com.liferay.util.servlet.ServletResponseUtil;
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    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class UpdateArticleFieldAction extends Action {
045    
046            public ActionForward execute(
047                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
048                            HttpServletResponse response)
049                    throws Exception {
050    
051                    try {
052                            updateArticleField(request, response);
053    
054                            return null;
055                    }
056                    catch (Exception e) {
057                            PortalUtil.sendError(e, request, response);
058    
059                            return null;
060                    }
061            }
062    
063            protected void updateArticleField(
064                            HttpServletRequest request, HttpServletResponse response)
065                    throws Exception {
066    
067                    long groupId = ParamUtil.getLong(request, "groupId");
068                    String articleId = ParamUtil.getString(request, "articleId");
069                    double version = ParamUtil.getDouble(request, "version");
070    
071                    String containerId = ParamUtil.getString(request, "containerId");
072    
073                    if (Validator.isNotNull(containerId)) {
074                            int x = containerId.indexOf("_");
075                            int y = containerId.lastIndexOf("_");
076    
077                            if ((x != -1) && (y != -1)) {
078                                    groupId = GetterUtil.getLong(containerId.substring(0, x));
079                                    articleId = containerId.substring(x + 1, y);
080                                    version = GetterUtil.getDouble(
081                                            containerId.substring(y, containerId.length()));
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, fieldName.length());
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 = JournalUtil.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    }