1   /**
2    * Copyright (c) 2000-2009 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.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.FileUtil;
29  import com.liferay.portal.kernel.util.HtmlUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.security.permission.ActionKeys;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PrefsPropsUtil;
38  import com.liferay.portal.util.PropsKeys;
39  import com.liferay.portal.util.PropsValues;
40  import com.liferay.portal.util.WebKeys;
41  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
42  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
43  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
44  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
45  
46  import java.io.ByteArrayInputStream;
47  import java.io.InputStream;
48  import java.io.InputStreamReader;
49  
50  import java.util.List;
51  
52  import javax.portlet.PortletConfig;
53  import javax.portlet.RenderRequest;
54  import javax.portlet.RenderResponse;
55  
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionForward;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Bruno Farache
64   *
65   */
66  public class CompareVersionsAction extends PortletAction {
67  
68      public ActionForward render(
69              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
70              RenderRequest renderRequest, RenderResponse renderResponse)
71          throws Exception {
72  
73          try {
74              compareVersions(renderRequest);
75          }
76          catch (Exception e) {
77              if (e instanceof NoSuchFileEntryException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(renderRequest, e.getClass().getName());
81  
82                  setForward(renderRequest, "portlet.document_library.error");
83              }
84              else {
85                  throw e;
86              }
87          }
88  
89          return mapping.findForward("portlet.document_library.compare_versions");
90      }
91  
92      protected void compareVersions(RenderRequest renderRequest)
93          throws Exception {
94  
95          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
96              WebKeys.THEME_DISPLAY);
97  
98          long companyId = themeDisplay.getCompanyId();
99          long userId = themeDisplay.getUserId();
100 
101         long fileEntryId = ParamUtil.getLong(renderRequest, "fileEntryId");
102 
103         long folderId = ParamUtil.getLong(renderRequest, "folderId");
104         String name = ParamUtil.getString(renderRequest, "name");
105 
106         DLFileEntryPermission.check(
107             themeDisplay.getPermissionChecker(), folderId, name,
108             ActionKeys.VIEW);
109 
110         String extension = FileUtil.getExtension(name);
111 
112         String titleWithExtension = ParamUtil.getString(
113             renderRequest, "titleWithExtension");
114 
115         double sourceVersion = ParamUtil.getDouble(
116             renderRequest, "sourceVersion");
117         double targetVersion = ParamUtil.getDouble(
118             renderRequest, "targetVersion");
119 
120         InputStream sourceIs = DLFileEntryLocalServiceUtil.getFileAsStream(
121             companyId, userId, folderId, name, sourceVersion);
122         InputStream targetIs = DLFileEntryLocalServiceUtil.getFileAsStream(
123             companyId, userId, folderId, name, targetVersion);
124 
125         if (extension.equals("htm") || extension.equals("html") ||
126             extension.equals("xml")) {
127 
128             String escapedSource = HtmlUtil.escape(StringUtil.read(sourceIs));
129             String escapedTarget = HtmlUtil.escape(StringUtil.read(targetIs));
130 
131             sourceIs = new ByteArrayInputStream(
132                 escapedSource.getBytes(StringPool.UTF8));
133             targetIs = new ByteArrayInputStream(
134                 escapedTarget.getBytes(StringPool.UTF8));
135         }
136 
137         if (PrefsPropsUtil.getBoolean(
138                 PropsKeys.OPENOFFICE_SERVER_ENABLED,
139                 PropsValues.OPENOFFICE_SERVER_ENABLED) &&
140             isConvertBeforeCompare(extension)) {
141 
142             String sourceTempFileId = DocumentConversionUtil.getTempFileId(
143                 fileEntryId, sourceVersion);
144             String targetTempFileId = DocumentConversionUtil.getTempFileId(
145                 fileEntryId, targetVersion);
146 
147             sourceIs = DocumentConversionUtil.convert(
148                 sourceTempFileId, sourceIs, extension, "txt");
149             targetIs = DocumentConversionUtil.convert(
150                 targetTempFileId, targetIs, extension, "txt");
151         }
152 
153         List<DiffResult>[] diffResults = DiffUtil.diff(
154             new InputStreamReader(sourceIs), new InputStreamReader(targetIs));
155 
156         renderRequest.setAttribute(
157             WebKeys.SOURCE_NAME,
158             titleWithExtension + StringPool.SPACE + sourceVersion);
159         renderRequest.setAttribute(
160             WebKeys.TARGET_NAME,
161             titleWithExtension + StringPool.SPACE + targetVersion);
162         renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
163     }
164 
165     protected boolean isConvertBeforeCompare(String extension) {
166         if (extension.equals("txt")) {
167             return false;
168         }
169 
170         String[] conversions = DocumentConversionUtil.getConversions(extension);
171 
172         for (int i = 0; i < conversions.length; i++) {
173             if (conversions[i].equals("txt")) {
174                 return true;
175             }
176         }
177 
178         return false;
179     }
180 
181 }