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