001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.documentlibrary.action;
022:
023: import com.liferay.portal.kernel.security.permission.ActionKeys;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.security.auth.PrincipalException;
027: import com.liferay.portal.struts.PortletAction;
028: import com.liferay.portal.theme.ThemeDisplay;
029: import com.liferay.portal.util.WebKeys;
030: import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
031: import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
032: import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
033: import com.liferay.util.diff.DiffUtil;
034: import com.liferay.util.servlet.SessionErrors;
035:
036: import java.io.InputStream;
037: import java.io.InputStreamReader;
038:
039: import java.util.List;
040:
041: import javax.portlet.ActionRequest;
042: import javax.portlet.ActionResponse;
043: import javax.portlet.PortletConfig;
044: import javax.portlet.RenderRequest;
045: import javax.portlet.RenderResponse;
046:
047: import org.apache.struts.action.ActionForm;
048: import org.apache.struts.action.ActionForward;
049: import org.apache.struts.action.ActionMapping;
050:
051: /**
052: * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
053: *
054: * @author Bruno Farache
055: *
056: */
057: public class CompareVersionsAction extends PortletAction {
058:
059: public void processAction(ActionMapping mapping, ActionForm form,
060: PortletConfig config, ActionRequest req, ActionResponse res)
061: throws Exception {
062:
063: try {
064: compareVersions(req);
065: } catch (Exception e) {
066: if (e instanceof NoSuchFileEntryException
067: || e instanceof PrincipalException) {
068:
069: SessionErrors.add(req, e.getClass().getName());
070:
071: setForward(req, "portlet.document_library.error");
072: } else {
073: throw e;
074: }
075: }
076: }
077:
078: public ActionForward render(ActionMapping mapping, ActionForm form,
079: PortletConfig config, RenderRequest req, RenderResponse res)
080: throws Exception {
081:
082: return mapping
083: .findForward("portlet.document_library.compare_versions");
084: }
085:
086: protected void compareVersions(ActionRequest req) throws Exception {
087: ThemeDisplay themeDisplay = (ThemeDisplay) req
088: .getAttribute(WebKeys.THEME_DISPLAY);
089:
090: long companyId = themeDisplay.getCompanyId();
091: long userId = themeDisplay.getUserId();
092:
093: long folderId = ParamUtil.getLong(req, "folderId");
094: String name = ParamUtil.getString(req, "name");
095:
096: DLFileEntryPermission.check(
097: themeDisplay.getPermissionChecker(), folderId, name,
098: ActionKeys.VIEW);
099:
100: String titleWithExtension = ParamUtil.getString(req,
101: "titleWithExtension");
102:
103: double sourceVersion = ParamUtil
104: .getDouble(req, "sourceVersion");
105: double targetVersion = ParamUtil
106: .getDouble(req, "targetVersion");
107:
108: InputStream sourceIs = DLFileEntryLocalServiceUtil
109: .getFileAsStream(companyId, userId, folderId, name,
110: sourceVersion);
111: InputStream targetIs = DLFileEntryLocalServiceUtil
112: .getFileAsStream(companyId, userId, folderId, name,
113: targetVersion);
114:
115: List[] diffResults = DiffUtil.diff(new InputStreamReader(
116: sourceIs), new InputStreamReader(targetIs));
117:
118: req.setAttribute(WebKeys.SOURCE_NAME, titleWithExtension
119: + StringPool.SPACE + sourceVersion);
120: req.setAttribute(WebKeys.TARGET_NAME, titleWithExtension
121: + StringPool.SPACE + targetVersion);
122: req.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
123: }
124:
125: }
|