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  public class CompareVersionsAction extends PortletAction {
60  
61      public ActionForward render(
62              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
63              RenderRequest renderRequest, RenderResponse renderResponse)
64          throws Exception {
65  
66          try {
67              ActionUtil.getNode(renderRequest);
68              ActionUtil.getPage(renderRequest);
69  
70              compareVersions(renderRequest, renderResponse);
71          }
72          catch (Exception e) {
73              if (e instanceof NoSuchPageException) {
74  
75                  SessionErrors.add(renderRequest, e.getClass().getName());
76  
77                  return mapping.findForward("portlet.wiki.error");
78              }
79              else {
80                  throw e;
81              }
82          }
83  
84          return mapping.findForward("portlet.wiki.compare_versions");
85      }
86  
87      protected void compareVersions(
88              RenderRequest renderRequest, RenderResponse renderResponse)
89          throws Exception {
90  
91          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
92              WebKeys.THEME_DISPLAY);
93  
94          long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
95  
96          String title = ParamUtil.getString(renderRequest, "title");
97  
98          double sourceVersion = ParamUtil.getDouble(
99              renderRequest, "sourceVersion");
100         double targetVersion = ParamUtil.getDouble(
101             renderRequest, "targetVersion");
102         String type = ParamUtil.getString(renderRequest, "type", "escape");
103 
104         WikiPage sourcePage = WikiPageServiceUtil.getPage(
105             nodeId, title, sourceVersion);
106         WikiPage targetPage = WikiPageServiceUtil.getPage(
107             nodeId, title, targetVersion);
108 
109         if (type.equals("html")) {
110             WikiNode sourceNode = sourcePage.getNode();
111 
112             PortletURL viewPageURL = renderResponse.createRenderURL();
113 
114             viewPageURL.setParameter("struts_action", "/wiki/view");
115             viewPageURL.setParameter("nodeName", sourceNode.getName());
116 
117             PortletURL editPageURL = renderResponse.createRenderURL();
118 
119             editPageURL.setParameter("struts_action", "/wiki/edit_page");
120             editPageURL.setParameter("nodeId", String.valueOf(nodeId));
121             editPageURL.setParameter("title", title);
122 
123             String attachmentURLPrefix =
124                 themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
125                     "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
126                         "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
127 
128             String htmlDiffResult = WikiUtil.diffHtml(
129                 sourcePage, targetPage, viewPageURL, editPageURL,
130                 attachmentURLPrefix);
131 
132             renderRequest.setAttribute(
133                 WebKeys.DIFF_HTML_RESULTS, htmlDiffResult);
134         }
135         else {
136             String sourceContent = sourcePage.getContent();
137             String targetContent = targetPage.getContent();
138 
139             sourceContent = WikiUtil.processContent(sourceContent);
140             targetContent = WikiUtil.processContent(targetContent);
141 
142             if (type.equals("escape")) {
143                 sourceContent = HtmlUtil.escape(sourceContent);
144                 targetContent = HtmlUtil.escape(targetContent);
145             }
146             else if (type.equals("strip")) {
147                 sourceContent = HtmlUtil.extractText(sourceContent);
148                 targetContent = HtmlUtil.extractText(targetContent);
149             }
150 
151             List<DiffResult>[] diffResults = DiffUtil.diff(
152                 new StringReader(sourceContent),
153                 new StringReader(targetContent));
154 
155             renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
156         }
157 
158         renderRequest.setAttribute(WebKeys.WIKI_NODE_ID, nodeId);
159         renderRequest.setAttribute(WebKeys.TITLE, title);
160         renderRequest.setAttribute(WebKeys.SOURCE_VERSION, sourceVersion);
161         renderRequest.setAttribute(WebKeys.TARGET_VERSION, targetVersion);
162     }
163 
164 }