001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.action;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.repository.model.FileEntry;
019    import com.liferay.portal.kernel.repository.model.FileVersion;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.DiffResult;
022    import com.liferay.portal.kernel.util.DiffUtil;
023    import com.liferay.portal.kernel.util.HtmlUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
031    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
032    import com.liferay.portlet.documentlibrary.util.DLUtil;
033    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
034    
035    import java.io.FileInputStream;
036    import java.io.InputStream;
037    import java.io.InputStreamReader;
038    
039    import java.util.List;
040    
041    import javax.portlet.PortletConfig;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
044    
045    import org.apache.struts.action.ActionForm;
046    import org.apache.struts.action.ActionForward;
047    import org.apache.struts.action.ActionMapping;
048    
049    /**
050     * @author Bruno Farache
051     */
052    public class CompareVersionsAction extends PortletAction {
053    
054            @Override
055            public ActionForward render(
056                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
057                            RenderRequest renderRequest, RenderResponse renderResponse)
058                    throws Exception {
059    
060                    try {
061                            compareVersions(renderRequest);
062                    }
063                    catch (Exception e) {
064                            if (e instanceof NoSuchFileEntryException ||
065                                    e instanceof PrincipalException) {
066    
067                                    SessionErrors.add(renderRequest, e.getClass());
068    
069                                    setForward(renderRequest, "portlet.document_library.error");
070                            }
071                            else {
072                                    throw e;
073                            }
074                    }
075    
076                    return mapping.findForward("portlet.document_library.compare_versions");
077            }
078    
079            protected void compareVersions(RenderRequest renderRequest)
080                    throws Exception {
081    
082                    long fileEntryId = ParamUtil.getLong(renderRequest, "fileEntryId");
083    
084                    String sourceVersion = ParamUtil.getString(
085                            renderRequest, "sourceVersion");
086                    String targetVersion = ParamUtil.getString(
087                            renderRequest, "targetVersion");
088    
089                    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);
090    
091                    FileVersion sourceFileVersion = fileEntry.getFileVersion(sourceVersion);
092    
093                    InputStream sourceIs = sourceFileVersion.getContentStream(false);
094    
095                    String sourceExtension = sourceFileVersion.getExtension();
096    
097                    if (sourceExtension.equals("htm") || sourceExtension.equals("html") ||
098                            sourceExtension.equals("xml")) {
099    
100                            String sourceContent = HtmlUtil.escape(StringUtil.read(sourceIs));
101    
102                            sourceIs = new UnsyncByteArrayInputStream(
103                                    sourceContent.getBytes(StringPool.UTF8));
104                    }
105    
106                    FileVersion targetFileVersion = fileEntry.getFileVersion(targetVersion);
107    
108                    InputStream targetIs = targetFileVersion.getContentStream(false);
109    
110                    String targetExtension = targetFileVersion.getExtension();
111    
112                    if (targetExtension.equals("htm") || targetExtension.equals("html") ||
113                            targetExtension.equals("xml")) {
114    
115                            String targetContent = HtmlUtil.escape(StringUtil.read(targetIs));
116    
117                            targetIs = new UnsyncByteArrayInputStream(
118                                    targetContent.getBytes(StringPool.UTF8));
119                    }
120    
121                    if (DocumentConversionUtil.isEnabled()) {
122                            if (DocumentConversionUtil.isConvertBeforeCompare(
123                                            sourceExtension)) {
124    
125                                    String sourceTempFileId = DLUtil.getTempFileId(
126                                            fileEntryId, sourceVersion);
127    
128                                    sourceIs = new FileInputStream(
129                                            DocumentConversionUtil.convert(
130                                                    sourceTempFileId, sourceIs, sourceExtension, "txt"));
131                            }
132    
133                            if (DocumentConversionUtil.isConvertBeforeCompare(
134                                            targetExtension)) {
135    
136                                    String targetTempFileId = DLUtil.getTempFileId(
137                                            fileEntryId, targetVersion);
138    
139                                    targetIs = new FileInputStream(
140                                            DocumentConversionUtil.convert(
141                                                    targetTempFileId, targetIs, targetExtension, "txt"));
142                            }
143                    }
144    
145                    List<DiffResult>[] diffResults = DiffUtil.diff(
146                            new InputStreamReader(sourceIs), new InputStreamReader(targetIs));
147    
148                    renderRequest.setAttribute(
149                            WebKeys.SOURCE_NAME,
150                            sourceFileVersion.getTitle() + StringPool.SPACE + sourceVersion);
151                    renderRequest.setAttribute(
152                            WebKeys.TARGET_NAME,
153                            targetFileVersion.getTitle() + StringPool.SPACE + targetVersion);
154                    renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
155            }
156    
157    }