001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.action;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.DiffResult;
020    import com.liferay.portal.kernel.util.DiffUtil;
021    import com.liferay.portal.kernel.util.HtmlUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.wiki.NoSuchPageException;
027    import com.liferay.portlet.wiki.model.WikiNode;
028    import com.liferay.portlet.wiki.model.WikiPage;
029    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
030    import com.liferay.portlet.wiki.util.WikiUtil;
031    
032    import java.util.List;
033    
034    import javax.portlet.PortletConfig;
035    import javax.portlet.PortletURL;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Bruno Farache
045     * @author Julio Camarero
046     */
047    public class CompareVersionsAction extends PortletAction {
048    
049            @Override
050            public ActionForward render(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, RenderRequest renderRequest,
053                            RenderResponse renderResponse)
054                    throws Exception {
055    
056                    try {
057                            ActionUtil.getNode(renderRequest);
058                            ActionUtil.getPage(renderRequest);
059    
060                            compareVersions(renderRequest, renderResponse);
061                    }
062                    catch (Exception e) {
063                            if (e instanceof NoSuchPageException) {
064                                    SessionErrors.add(renderRequest, e.getClass());
065    
066                                    return actionMapping.findForward("portlet.wiki.error");
067                            }
068                            else {
069                                    throw e;
070                            }
071                    }
072    
073                    return actionMapping.findForward("portlet.wiki.compare_versions");
074            }
075    
076            protected void compareVersions(
077                            RenderRequest renderRequest, RenderResponse renderResponse)
078                    throws Exception {
079    
080                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
081                            WebKeys.THEME_DISPLAY);
082    
083                    long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
084    
085                    String title = ParamUtil.getString(renderRequest, "title");
086    
087                    double sourceVersion = ParamUtil.getDouble(
088                            renderRequest, "sourceVersion");
089                    double targetVersion = ParamUtil.getDouble(
090                            renderRequest, "targetVersion");
091                    String type = ParamUtil.getString(renderRequest, "type", "escape");
092    
093                    WikiPage sourcePage = WikiPageServiceUtil.getPage(
094                            nodeId, title, sourceVersion);
095                    WikiPage targetPage = WikiPageServiceUtil.getPage(
096                            nodeId, title, targetVersion);
097    
098                    if (type.equals("html")) {
099                            WikiNode sourceNode = sourcePage.getNode();
100    
101                            PortletURL viewPageURL = renderResponse.createRenderURL();
102    
103                            viewPageURL.setParameter("struts_action", "/wiki/view");
104                            viewPageURL.setParameter("nodeName", sourceNode.getName());
105    
106                            PortletURL editPageURL = renderResponse.createRenderURL();
107    
108                            editPageURL.setParameter("struts_action", "/wiki/edit_page");
109                            editPageURL.setParameter("nodeId", String.valueOf(nodeId));
110                            editPageURL.setParameter("title", title);
111    
112                            String attachmentURLPrefix = WikiUtil.getAttachmentURLPrefix(
113                                    themeDisplay.getPathMain(), themeDisplay.getPlid(), nodeId,
114                                    title);
115    
116                            String htmlDiffResult = WikiUtil.diffHtml(
117                                    sourcePage, targetPage, viewPageURL, editPageURL,
118                                    attachmentURLPrefix);
119    
120                            renderRequest.setAttribute(
121                                    WebKeys.DIFF_HTML_RESULTS, htmlDiffResult);
122                    }
123                    else {
124                            String sourceContent = sourcePage.getContent();
125                            String targetContent = targetPage.getContent();
126    
127                            sourceContent = WikiUtil.processContent(sourceContent);
128                            targetContent = WikiUtil.processContent(targetContent);
129    
130                            if (type.equals("strip")) {
131                                    sourceContent = HtmlUtil.extractText(sourceContent);
132                                    targetContent = HtmlUtil.extractText(targetContent);
133                            }
134                            else {
135                                    sourceContent = HtmlUtil.escape(sourceContent);
136                                    targetContent = HtmlUtil.escape(targetContent);
137                            }
138    
139                            List<DiffResult>[] diffResults = DiffUtil.diff(
140                                    new UnsyncStringReader(sourceContent),
141                                    new UnsyncStringReader(targetContent));
142    
143                            renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
144                    }
145    
146                    renderRequest.setAttribute(WebKeys.WIKI_NODE_ID, nodeId);
147                    renderRequest.setAttribute(WebKeys.TITLE, title);
148                    renderRequest.setAttribute(WebKeys.SOURCE_VERSION, sourceVersion);
149                    renderRequest.setAttribute(WebKeys.TARGET_VERSION, targetVersion);
150            }
151    
152    }