1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.documentlibrary.action;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.DiffResult;
20  import com.liferay.portal.kernel.util.DiffUtil;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.HtmlUtil;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.PropsKeys;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PrefsPropsUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
35  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
36  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
37  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
38  
39  import java.io.InputStream;
40  import java.io.InputStreamReader;
41  
42  import java.util.List;
43  
44  import javax.portlet.PortletConfig;
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   */
57  public class CompareVersionsAction extends PortletAction {
58  
59      public ActionForward render(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              RenderRequest renderRequest, RenderResponse renderResponse)
62          throws Exception {
63  
64          try {
65              compareVersions(renderRequest);
66          }
67          catch (Exception e) {
68              if (e instanceof NoSuchFileEntryException ||
69                  e instanceof PrincipalException) {
70  
71                  SessionErrors.add(renderRequest, e.getClass().getName());
72  
73                  setForward(renderRequest, "portlet.document_library.error");
74              }
75              else {
76                  throw e;
77              }
78          }
79  
80          return mapping.findForward("portlet.document_library.compare_versions");
81      }
82  
83      protected void compareVersions(RenderRequest renderRequest)
84          throws Exception {
85  
86          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
87              WebKeys.THEME_DISPLAY);
88  
89          long companyId = themeDisplay.getCompanyId();
90          long userId = themeDisplay.getUserId();
91  
92          long fileEntryId = ParamUtil.getLong(renderRequest, "fileEntryId");
93  
94          long folderId = ParamUtil.getLong(renderRequest, "folderId");
95          String name = ParamUtil.getString(renderRequest, "name");
96  
97          DLFileEntryPermission.check(
98              themeDisplay.getPermissionChecker(), folderId, name,
99              ActionKeys.VIEW);
100 
101         String extension = FileUtil.getExtension(name);
102 
103         String titleWithExtension = ParamUtil.getString(
104             renderRequest, "titleWithExtension");
105 
106         double sourceVersion = ParamUtil.getDouble(
107             renderRequest, "sourceVersion");
108         double targetVersion = ParamUtil.getDouble(
109             renderRequest, "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         if (extension.equals("htm") || extension.equals("html") ||
117             extension.equals("xml")) {
118 
119             String escapedSource = HtmlUtil.escape(StringUtil.read(sourceIs));
120             String escapedTarget = HtmlUtil.escape(StringUtil.read(targetIs));
121 
122             sourceIs = new UnsyncByteArrayInputStream(
123                 escapedSource.getBytes(StringPool.UTF8));
124             targetIs = new UnsyncByteArrayInputStream(
125                 escapedTarget.getBytes(StringPool.UTF8));
126         }
127 
128         if (PrefsPropsUtil.getBoolean(
129                 PropsKeys.OPENOFFICE_SERVER_ENABLED,
130                 PropsValues.OPENOFFICE_SERVER_ENABLED) &&
131             isConvertBeforeCompare(extension)) {
132 
133             String sourceTempFileId = DocumentConversionUtil.getTempFileId(
134                 fileEntryId, sourceVersion);
135             String targetTempFileId = DocumentConversionUtil.getTempFileId(
136                 fileEntryId, targetVersion);
137 
138             sourceIs = DocumentConversionUtil.convert(
139                 sourceTempFileId, sourceIs, extension, "txt");
140             targetIs = DocumentConversionUtil.convert(
141                 targetTempFileId, targetIs, extension, "txt");
142         }
143 
144         List<DiffResult>[] diffResults = DiffUtil.diff(
145             new InputStreamReader(sourceIs), new InputStreamReader(targetIs));
146 
147         renderRequest.setAttribute(
148             WebKeys.SOURCE_NAME,
149             titleWithExtension + StringPool.SPACE + sourceVersion);
150         renderRequest.setAttribute(
151             WebKeys.TARGET_NAME,
152             titleWithExtension + StringPool.SPACE + targetVersion);
153         renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
154     }
155 
156     protected boolean isConvertBeforeCompare(String extension) {
157         if (extension.equals("txt")) {
158             return false;
159         }
160 
161         String[] conversions = DocumentConversionUtil.getConversions(extension);
162 
163         for (int i = 0; i < conversions.length; i++) {
164             if (conversions[i].equals("txt")) {
165                 return true;
166             }
167         }
168 
169         return false;
170     }
171 
172 }