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.io.unsync.UnsyncStringReader;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.DiffResult;
28  import com.liferay.portal.kernel.util.DiffUtil;
29  import com.liferay.portal.kernel.util.HtmlUtil;
30  import com.liferay.portal.kernel.util.HttpUtil;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.wiki.NoSuchPageException;
36  import com.liferay.portlet.wiki.model.WikiNode;
37  import com.liferay.portlet.wiki.model.WikiPage;
38  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
39  import com.liferay.portlet.wiki.util.WikiUtil;
40  
41  import java.util.List;
42  
43  import javax.portlet.PortletConfig;
44  import javax.portlet.PortletURL;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Bruno Farache
56   * @author Julio Camarero
57   */
58  public class CompareVersionsAction extends PortletAction {
59  
60      public ActionForward render(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              RenderRequest renderRequest, RenderResponse renderResponse)
63          throws Exception {
64  
65          try {
66              ActionUtil.getNode(renderRequest);
67              ActionUtil.getPage(renderRequest);
68  
69              compareVersions(renderRequest, renderResponse);
70          }
71          catch (Exception e) {
72              if (e instanceof NoSuchPageException) {
73  
74                  SessionErrors.add(renderRequest, e.getClass().getName());
75  
76                  return mapping.findForward("portlet.wiki.error");
77              }
78              else {
79                  throw e;
80              }
81          }
82  
83          return mapping.findForward("portlet.wiki.compare_versions");
84      }
85  
86      protected void compareVersions(
87              RenderRequest renderRequest, RenderResponse renderResponse)
88          throws Exception {
89  
90          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
91              WebKeys.THEME_DISPLAY);
92  
93          long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
94  
95          String title = ParamUtil.getString(renderRequest, "title");
96  
97          double sourceVersion = ParamUtil.getDouble(
98              renderRequest, "sourceVersion");
99          double targetVersion = ParamUtil.getDouble(
100             renderRequest, "targetVersion");
101         String type = ParamUtil.getString(renderRequest, "type", "escape");
102 
103         WikiPage sourcePage = WikiPageServiceUtil.getPage(
104             nodeId, title, sourceVersion);
105         WikiPage targetPage = WikiPageServiceUtil.getPage(
106             nodeId, title, targetVersion);
107 
108         if (type.equals("html")) {
109             WikiNode sourceNode = sourcePage.getNode();
110 
111             PortletURL viewPageURL = renderResponse.createRenderURL();
112 
113             viewPageURL.setParameter("struts_action", "/wiki/view");
114             viewPageURL.setParameter("nodeName", sourceNode.getName());
115 
116             PortletURL editPageURL = renderResponse.createRenderURL();
117 
118             editPageURL.setParameter("struts_action", "/wiki/edit_page");
119             editPageURL.setParameter("nodeId", String.valueOf(nodeId));
120             editPageURL.setParameter("title", title);
121 
122             String attachmentURLPrefix =
123                 themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
124                     "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
125                         "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
126 
127             String htmlDiffResult = WikiUtil.diffHtml(
128                 sourcePage, targetPage, viewPageURL, editPageURL,
129                 attachmentURLPrefix);
130 
131             renderRequest.setAttribute(
132                 WebKeys.DIFF_HTML_RESULTS, htmlDiffResult);
133         }
134         else {
135             String sourceContent = sourcePage.getContent();
136             String targetContent = targetPage.getContent();
137 
138             sourceContent = WikiUtil.processContent(sourceContent);
139             targetContent = WikiUtil.processContent(targetContent);
140 
141             if (type.equals("escape")) {
142                 sourceContent = HtmlUtil.escape(sourceContent);
143                 targetContent = HtmlUtil.escape(targetContent);
144             }
145             else if (type.equals("strip")) {
146                 sourceContent = HtmlUtil.extractText(sourceContent);
147                 targetContent = HtmlUtil.extractText(targetContent);
148             }
149 
150             List<DiffResult>[] diffResults = DiffUtil.diff(
151                 new UnsyncStringReader(sourceContent),
152                 new UnsyncStringReader(targetContent));
153 
154             renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
155         }
156 
157         renderRequest.setAttribute(WebKeys.WIKI_NODE_ID, nodeId);
158         renderRequest.setAttribute(WebKeys.TITLE, title);
159         renderRequest.setAttribute(WebKeys.SOURCE_VERSION, sourceVersion);
160         renderRequest.setAttribute(WebKeys.TARGET_VERSION, targetVersion);
161     }
162 
163 }