1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.DiffResult;
24  import com.liferay.portal.kernel.util.DiffUtil;
25  import com.liferay.portal.kernel.util.HtmlUtil;
26  import com.liferay.portal.kernel.util.HttpUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.wiki.NoSuchPageException;
32  import com.liferay.portlet.wiki.model.WikiNode;
33  import com.liferay.portlet.wiki.model.WikiPage;
34  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
35  import com.liferay.portlet.wiki.util.WikiUtil;
36  
37  import java.io.StringReader;
38  
39  import java.util.List;
40  
41  import javax.portlet.PortletConfig;
42  import javax.portlet.PortletURL;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Bruno Farache
54   * @author Julio Camarero
55   *
56   */
57  public class CompareVersionsAction extends PortletAction {
58  
59      public ActionForward render(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              RenderRequest renderRequest, RenderResponse renderResponse)
62          throws Exception {
63  
64          try {
65              ActionUtil.getNode(renderRequest);
66              ActionUtil.getPage(renderRequest);
67  
68              compareVersions(renderRequest, renderResponse);
69          }
70          catch (Exception e) {
71              if (e instanceof NoSuchPageException) {
72  
73                  SessionErrors.add(renderRequest, e.getClass().getName());
74  
75                  return mapping.findForward("portlet.wiki.error");
76              }
77              else {
78                  throw e;
79              }
80          }
81  
82          return mapping.findForward("portlet.wiki.compare_versions");
83      }
84  
85      protected void compareVersions(
86              RenderRequest renderRequest, RenderResponse renderResponse)
87          throws Exception {
88  
89          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
90              WebKeys.THEME_DISPLAY);
91  
92          long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
93  
94          String title = ParamUtil.getString(renderRequest, "title");
95  
96          double sourceVersion = ParamUtil.getDouble(
97              renderRequest, "sourceVersion");
98          double targetVersion = ParamUtil.getDouble(
99              renderRequest, "targetVersion");
100         String type = ParamUtil.getString(renderRequest, "type", "escape");
101 
102         WikiPage sourcePage = WikiPageServiceUtil.getPage(
103             nodeId, title, sourceVersion);
104         WikiPage targetPage = WikiPageServiceUtil.getPage(
105             nodeId, title, targetVersion);
106 
107         if (type.equals("html")){
108             WikiNode sourceNode = sourcePage.getNode();
109 
110             PortletURL viewPageURL = renderResponse.createRenderURL();
111 
112             viewPageURL.setParameter("struts_action", "/wiki/view");
113             viewPageURL.setParameter("nodeName", sourceNode.getName());
114 
115             PortletURL editPageURL = renderResponse.createRenderURL();
116 
117             editPageURL.setParameter("struts_action", "/wiki/edit_page");
118             editPageURL.setParameter("nodeId", String.valueOf(nodeId));
119             editPageURL.setParameter("title", title);
120 
121             String attachmentURLPrefix =
122                 themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
123                     "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
124                         "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
125 
126             String htmlDiffResult = WikiUtil.diffHtml(
127                 sourcePage, targetPage, viewPageURL, editPageURL,
128                 attachmentURLPrefix);
129 
130             renderRequest.setAttribute(
131                 WebKeys.DIFF_HTML_RESULTS, htmlDiffResult);
132         }
133         else {
134             String sourceContent = sourcePage.getContent();
135             String targetContent = targetPage.getContent();
136 
137             sourceContent = WikiUtil.processContent(sourceContent);
138             targetContent = WikiUtil.processContent(targetContent);
139 
140             if (type.equals("escape")) {
141                 sourceContent = HtmlUtil.escape(sourceContent);
142                 targetContent = HtmlUtil.escape(targetContent);
143             }
144             else if (type.equals("strip")) {
145                 sourceContent = HtmlUtil.extractText(sourceContent);
146                 targetContent = HtmlUtil.extractText(targetContent);
147             }
148 
149             List<DiffResult>[] diffResults = DiffUtil.diff(
150                 new StringReader(sourceContent),
151                 new StringReader(targetContent));
152 
153             renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
154         }
155 
156         renderRequest.setAttribute(WebKeys.WIKI_NODE_ID, nodeId);
157         renderRequest.setAttribute(WebKeys.TITLE, title);
158         renderRequest.setAttribute(WebKeys.SOURCE_VERSION, sourceVersion);
159         renderRequest.setAttribute(WebKeys.TARGET_VERSION, targetVersion);
160     }
161 
162 }