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