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.ParamUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.wiki.NoSuchPageException;
27  import com.liferay.portlet.wiki.model.WikiPage;
28  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
29  import com.liferay.portlet.wiki.util.WikiUtil;
30  
31  import java.util.List;
32  
33  import javax.portlet.PortletConfig;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Bruno Farache
45   */
46  public class CompareVersionsAction extends PortletAction {
47  
48      public ActionForward render(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              RenderRequest renderRequest, RenderResponse renderResponse)
51          throws Exception {
52  
53          try {
54              ActionUtil.getNode(renderRequest);
55              ActionUtil.getPage(renderRequest);
56  
57              compareVersions(renderRequest);
58          }
59          catch (Exception e) {
60              if (e instanceof NoSuchPageException) {
61  
62                  SessionErrors.add(renderRequest, e.getClass().getName());
63  
64                  return mapping.findForward("portlet.wiki.error");
65              }
66              else {
67                  throw e;
68              }
69          }
70  
71          return mapping.findForward("portlet.wiki.compare_versions");
72      }
73  
74      protected void compareVersions(RenderRequest renderRequest)
75          throws Exception {
76  
77          long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
78  
79          String title = ParamUtil.getString(renderRequest, "title");
80  
81          double sourceVersion = ParamUtil.getDouble(
82              renderRequest, "sourceVersion");
83          double targetVersion = ParamUtil.getDouble(
84              renderRequest, "targetVersion");
85          String type = ParamUtil.getString(renderRequest, "type", "escape");
86  
87          WikiPage sourcePage = WikiPageServiceUtil.getPage(
88              nodeId, title, sourceVersion);
89          WikiPage targetPage = WikiPageServiceUtil.getPage(
90              nodeId, title, targetVersion);
91  
92          String sourceContent = sourcePage.getContent();
93          String targetContent = targetPage.getContent();
94  
95          sourceContent = WikiUtil.processContent(sourceContent);
96          targetContent = WikiUtil.processContent(targetContent);
97  
98          if (type.equals("escape")) {
99              sourceContent = HtmlUtil.escape(sourceContent);
100             targetContent = HtmlUtil.escape(targetContent);
101         }
102         else if (type.equals("strip")) {
103             sourceContent = HtmlUtil.extractText(sourceContent);
104             targetContent = HtmlUtil.extractText(targetContent);
105         }
106 
107         List<DiffResult>[] diffResults = DiffUtil.diff(
108             new UnsyncStringReader(sourceContent),
109             new UnsyncStringReader(targetContent));
110 
111         renderRequest.setAttribute(
112             WebKeys.SOURCE_NAME, title + StringPool.SPACE + sourceVersion);
113         renderRequest.setAttribute(
114             WebKeys.TARGET_NAME, title + StringPool.SPACE + targetVersion);
115         renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
116     }
117 
118 }