1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.wiki.action;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.DiffResult;
20  import com.liferay.portal.kernel.util.DiffUtil;
21  import com.liferay.portal.kernel.util.HtmlUtil;
22  import com.liferay.portal.kernel.util.HttpUtil;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.wiki.NoSuchPageException;
28  import com.liferay.portlet.wiki.model.WikiNode;
29  import com.liferay.portlet.wiki.model.WikiPage;
30  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
31  import com.liferay.portlet.wiki.util.WikiUtil;
32  
33  import java.util.List;
34  
35  import javax.portlet.PortletConfig;
36  import javax.portlet.PortletURL;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Bruno Farache
48   * @author Julio Camarero
49   */
50  public class CompareVersionsAction extends PortletAction {
51  
52      public ActionForward render(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              RenderRequest renderRequest, RenderResponse renderResponse)
55          throws Exception {
56  
57          try {
58              ActionUtil.getNode(renderRequest);
59              ActionUtil.getPage(renderRequest);
60  
61              compareVersions(renderRequest, renderResponse);
62          }
63          catch (Exception e) {
64              if (e instanceof NoSuchPageException) {
65  
66                  SessionErrors.add(renderRequest, e.getClass().getName());
67  
68                  return mapping.findForward("portlet.wiki.error");
69              }
70              else {
71                  throw e;
72              }
73          }
74  
75          return mapping.findForward("portlet.wiki.compare_versions");
76      }
77  
78      protected void compareVersions(
79              RenderRequest renderRequest, RenderResponse renderResponse)
80          throws Exception {
81  
82          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
83              WebKeys.THEME_DISPLAY);
84  
85          long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
86  
87          String title = ParamUtil.getString(renderRequest, "title");
88  
89          double sourceVersion = ParamUtil.getDouble(
90              renderRequest, "sourceVersion");
91          double targetVersion = ParamUtil.getDouble(
92              renderRequest, "targetVersion");
93          String type = ParamUtil.getString(renderRequest, "type", "escape");
94  
95          WikiPage sourcePage = WikiPageServiceUtil.getPage(
96              nodeId, title, sourceVersion);
97          WikiPage targetPage = WikiPageServiceUtil.getPage(
98              nodeId, title, targetVersion);
99  
100         if (type.equals("html")) {
101             WikiNode sourceNode = sourcePage.getNode();
102 
103             PortletURL viewPageURL = renderResponse.createRenderURL();
104 
105             viewPageURL.setParameter("struts_action", "/wiki/view");
106             viewPageURL.setParameter("nodeName", sourceNode.getName());
107 
108             PortletURL editPageURL = renderResponse.createRenderURL();
109 
110             editPageURL.setParameter("struts_action", "/wiki/edit_page");
111             editPageURL.setParameter("nodeId", String.valueOf(nodeId));
112             editPageURL.setParameter("title", title);
113 
114             String attachmentURLPrefix =
115                 themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
116                     "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
117                         "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
118 
119             String htmlDiffResult = WikiUtil.diffHtml(
120                 sourcePage, targetPage, viewPageURL, editPageURL,
121                 attachmentURLPrefix);
122 
123             renderRequest.setAttribute(
124                 WebKeys.DIFF_HTML_RESULTS, htmlDiffResult);
125         }
126         else {
127             String sourceContent = sourcePage.getContent();
128             String targetContent = targetPage.getContent();
129 
130             sourceContent = WikiUtil.processContent(sourceContent);
131             targetContent = WikiUtil.processContent(targetContent);
132 
133             if (type.equals("escape")) {
134                 sourceContent = HtmlUtil.escape(sourceContent);
135                 targetContent = HtmlUtil.escape(targetContent);
136             }
137             else if (type.equals("strip")) {
138                 sourceContent = HtmlUtil.extractText(sourceContent);
139                 targetContent = HtmlUtil.extractText(targetContent);
140             }
141 
142             List<DiffResult>[] diffResults = DiffUtil.diff(
143                 new UnsyncStringReader(sourceContent),
144                 new UnsyncStringReader(targetContent));
145 
146             renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
147         }
148 
149         renderRequest.setAttribute(WebKeys.WIKI_NODE_ID, nodeId);
150         renderRequest.setAttribute(WebKeys.TITLE, title);
151         renderRequest.setAttribute(WebKeys.SOURCE_VERSION, sourceVersion);
152         renderRequest.setAttribute(WebKeys.TARGET_VERSION, targetVersion);
153     }
154 
155 }