1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.documentlibrary.action;
24  
25  import com.liferay.portal.kernel.security.permission.ActionKeys;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
33  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
34  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
35  import com.liferay.util.diff.DiffUtil;
36  import com.liferay.util.servlet.SessionErrors;
37  
38  import java.io.InputStream;
39  import java.io.InputStreamReader;
40  
41  import java.util.List;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
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   *
58   */
59  public class CompareVersionsAction extends PortletAction {
60  
61      public void processAction(
62              ActionMapping mapping, ActionForm form, PortletConfig config,
63              ActionRequest req, ActionResponse res)
64          throws Exception {
65  
66          try {
67              compareVersions(req);
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchFileEntryException ||
71                  e instanceof PrincipalException) {
72  
73                  SessionErrors.add(req, e.getClass().getName());
74  
75                  setForward(req, "portlet.document_library.error");
76              }
77              else {
78                  throw e;
79              }
80          }
81      }
82  
83      public ActionForward render(
84              ActionMapping mapping, ActionForm form, PortletConfig config,
85              RenderRequest req, RenderResponse res)
86          throws Exception {
87  
88          return mapping.findForward("portlet.document_library.compare_versions");
89      }
90  
91      protected void compareVersions(ActionRequest req) throws Exception {
92          ThemeDisplay themeDisplay =
93              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
94  
95          long companyId = themeDisplay.getCompanyId();
96          long userId = themeDisplay.getUserId();
97  
98          long folderId = ParamUtil.getLong(req, "folderId");
99          String name = ParamUtil.getString(req, "name");
100 
101         DLFileEntryPermission.check(
102             themeDisplay.getPermissionChecker(), folderId, name,
103             ActionKeys.VIEW);
104 
105         String titleWithExtension = ParamUtil.getString(
106             req, "titleWithExtension");
107 
108         double sourceVersion = ParamUtil.getDouble(req, "sourceVersion");
109         double targetVersion = ParamUtil.getDouble(req, "targetVersion");
110 
111         InputStream sourceIs = DLFileEntryLocalServiceUtil.getFileAsStream(
112             companyId, userId, folderId, name, sourceVersion);
113         InputStream targetIs = DLFileEntryLocalServiceUtil.getFileAsStream(
114             companyId, userId, folderId, name, targetVersion);
115 
116         List[] diffResults = DiffUtil.diff(
117             new InputStreamReader(sourceIs), new InputStreamReader(targetIs));
118 
119         req.setAttribute(
120             WebKeys.SOURCE_NAME,
121             titleWithExtension + StringPool.SPACE + sourceVersion);
122         req.setAttribute(
123             WebKeys.TARGET_NAME,
124             titleWithExtension + StringPool.SPACE + targetVersion);
125         req.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
126     }
127 
128 }