cmsundochanges.java

来自「找了很久才找到到源代码」· Java 代码 · 共 478 行 · 第 1/2 页

JAVA
478
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsUndoChanges.java,v $
 * Date   : $Date: 2007-08-13 16:29:43 $
 * Version: $Revision: 1.18 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.workplace.commons;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsResource.CmsResourceUndoMode;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.security.CmsPermissionSet;
import org.opencms.util.CmsUUID;
import org.opencms.workplace.CmsMultiDialog;
import org.opencms.workplace.CmsWorkplaceSettings;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.commons.logging.Log;

/**
 * Provides methods for the undo changes on a resource dialog.<p> 
 * 
 * The following files use this class:
 * <ul>
 * <li>/commons/undochanges.jsp
 * </ul>
 * <p>
 *
 * @author  Andreas Zahner 
 * 
 * @version $Revision: 1.18 $ 
 * 
 * @since 6.0.0 
 */
public class CmsUndoChanges extends CmsMultiDialog {

    /** Value for the action: undo changes. */
    public static final int ACTION_UNDOCHANGES = 100;

    /** The dialog type. */
    public static final String DIALOG_TYPE = "undochanges";

    /** Request parameter name for the recursive flag.<p> */
    public static final String PARAM_RECURSIVE = "recursive";

    /** Request parameter name for the move flag.<p> */
    public static final String PARAM_MOVE = "move";

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsUndoChanges.class);

    /** The single current resource. */
    private CmsResource m_currentResource;

    /** The undo move operation flag parameter value. */
    private String m_paramMove;

    /** The undo recursively flag parameter value. */
    private String m_paramRecursive;

    /**
     * Returns the original path of given resource, that is the online path for the resource. 
     * If it differs from the offline path, the resource has been moved.<p>
     * 
     * @param cms the cms context 
     * @param resourceName a site relative resource name
     * 
     * @return the online path, or <code>null</code> if resource has not been published
     */
    public static String resourceOriginalPath(CmsObject cms, String resourceName) {

        CmsProject proj = cms.getRequestContext().currentProject();
        try {
            CmsResource resource = cms.readResource(resourceName, CmsResourceFilter.ALL);
            String result = cms.getSitePath(resource);
            cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
            result = cms.getSitePath(cms.readResource(resource.getStructureId()));
            // remove '/' if needed
            if (result.charAt(result.length() - 1) == '/') {
                if (resourceName.charAt(resourceName.length() - 1) != '/') {
                    result = result.substring(0, result.length() - 1);
                }
            }
            return result;
        } catch (CmsException e) {
            return null;
        } finally {
            cms.getRequestContext().setCurrentProject(proj);
        }
    }

    /**
     * Public constructor with JSP action element.<p>
     * 
     * @param jsp an initialized JSP action element
     */
    public CmsUndoChanges(CmsJspActionElement jsp) {

        super(jsp);
    }

    /**
     * Public constructor with JSP variables.<p>
     * 
     * @param context the JSP page context
     * @param req the JSP request
     * @param res the JSP response
     */
    public CmsUndoChanges(PageContext context, HttpServletRequest req, HttpServletResponse res) {

        this(new CmsJspActionElement(context, req, res));
    }

    /**
     * Performs the undo changes action, will be called by the JSP page.<p>
     * 
     * @throws JspException if problems including sub-elements occur
     */
    public void actionUndoChanges() throws JspException {

        // save initialized instance of this class in request attribute for included sub-elements
        getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
        try {
            boolean isFolder = false;
            String source = (String)getResourceList().get(0);
            if (!isMultiOperation()) {
                CmsResource resource = getCms().readResource(source, CmsResourceFilter.ALL);
                isFolder = resource.isFolder();
            }
            // get the folders to refresh
            List folderList = new ArrayList(1 + getResourceList().size());
            folderList.add(CmsResource.getParentFolder(source));
            Iterator it = getResourceList().iterator();
            while (it.hasNext()) {
                String res = (String)it.next();
                String target = resourceOriginalPath(getCms(), res);
                if (target != null && !target.equals(res)) {
                    CmsResource resource = getCms().readResource(res, CmsResourceFilter.ALL);
                    if (resource.isFolder()) {
                        folderList.add(CmsResource.getParentFolder(target));
                    }
                }
            }
            if (performDialogOperation()) {
                // if no exception is caused and "true" is returned move operation was successful
                if (isMultiOperation() || isFolder) {
                    // set request attribute to reload the explorer tree view
                    getJsp().getRequest().setAttribute(REQUEST_ATTRIBUTE_RELOADTREE, folderList);
                }
                actionCloseDialog();
            } else {
                // "false" returned, display "please wait" screen
                getJsp().include(FILE_DIALOG_SCREEN_WAIT);
            }
        } catch (Throwable e) {
            // error during deletion, show error dialog
            includeErrorpage(this, e);
        }
    }

    /**
     * Checks if the resource operation is an operation on at least one moved resource.<p>
     * 
     * @return true if the operation an operation on at least one moved resource, otherwise false
     */
    protected boolean isOperationOnMovedResource() {

        Iterator i = getResourceList().iterator();
        while (i.hasNext()) {
            String resName = (String)i.next();
            String target = resourceOriginalPath(getCms(), resName);
            if (target != null && !target.equals(resName)) {
                // found a moved resource
                return true;
            }
        }
        return false;
    }

    /**
     * Returns the HTML for the undo changes options and detailed output for single resource operations.<p>
     * 
     * @return the HTML for the undo changes options
     */
    public String buildDialogOptions() {

        StringBuffer result = new StringBuffer(256);

        boolean isMoved = isOperationOnMovedResource();
        if (!isMultiOperation()) {
            result.append(dialogSpacer());
            result.append(key(Messages.GUI_UNDO_LASTMODIFIED_INFO_3, new Object[] {
                getFileName(),
                getLastModifiedDate(),
                getLastModifiedUser()}));
            if (isMoved) {
                result.append(dialogSpacer());
                result.append(key(Messages.GUI_UNDO_MOVE_OPERATION_INFO_2, new Object[] {
                    getFileName(),
                    resourceOriginalPath(getCms(), getParamResource())}));
            }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?