⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsmultidialog.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsMultiDialog.java,v $
 * Date   : $Date: 2006/03/27 14:52:43 $
 * Version: $Revision: 1.2 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 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;

import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.i18n.CmsMessageContainer;
import org.opencms.i18n.I_CmsMessageBundle;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.CmsMultiException;
import org.opencms.security.CmsPermissionSet;
import org.opencms.util.CmsStringUtil;

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

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

import org.apache.commons.logging.Log;

/**
 * The base class to build a dialog capable of multiple file operations.<p>
 * 
 * Extend this class for workplace dialogs that can perform operations on more than one 
 * VFS resource like copy, move, touch etc.<p>
 * 
 * Provides methods to determine if a multi-resource operation has to be done and helper methods,
 * e.g. to get the list of resources to work with.<p>
 *
 * @author  Andreas Zahner 
 * 
 * @version $Revision: 1.2 $ 
 * 
 * @since 6.2.0 
 */
public abstract class CmsMultiDialog extends CmsDialog {

    /** The delimiter that is used in the resource list request parameter. */
    public static final String DELIMITER_RESOURCES = "|";

    /** Request parameter name for the resource list. */
    public static final String PARAM_RESOURCELIST = "resourcelist";

    /** Collects all eventually thrown exceptions during a multi operation. */
    private CmsMultiException m_multiOperationException;

    /** The resourcelist parameter value. */
    private String m_paramResourcelist;

    /** The list of resource names for the multi operation. */
    private List m_resourceList;
    
    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsMultiDialog.class);

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

        super(jsp);
        m_multiOperationException = new CmsMultiException();
    }

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

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

    /**
     * Adds an exception thrown during a multi resource operation to the multi exception.<p>
     * 
     * After iterating the dialog resources, use {@link #checkMultiOperationException(I_CmsMessageBundle, String)} to
     * display the multi exception depending on collected exceptions.<p>
     * 
     * @param exc the exception that was thrown
     */
    public void addMultiOperationException(CmsException exc) {

        m_multiOperationException.addException(exc);
    }

    /**
     * Builds the HTML for the resource list that is affected by the multi operation.<p>
     * 
     * @return the HTML for the resource list that is affected by the multi operation
     */
    public String buildResourceList() {

        StringBuffer result = new StringBuffer(1024);
        result.append("<table border=\"0\">\n");
        Iterator i = getResourceList().iterator();
        while (i.hasNext()) {
            String resName = (String)i.next();
            result.append("\t<tr>\n");
            result.append("\t\t<td style=\"vertical-align:top;\">./");
            result.append(CmsResource.getName(resName));
            result.append("&nbsp;</td>\n\t\t<td style=\"vertical-align:top;\">");
            String title = null;
            try {
                // get the title property value
                title = getCms().readPropertyObject(resName, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue(null);
            } catch (CmsException e) {
                // ignore this exception, title not found
            }
            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(title)) {
                // show the title information
                result.append(title);
            }
            result.append("</td>\n\t</tr>\n");
        }
        result.append("</table>");
        return result.toString();
    }

    /**
     * Checks if an exception occured during a multi resource operation, and throws a new exception if necessary.<p>
     * 
     * @param messages the message bundle to use for the exception message generation
     * @param key the key for the exception to throw with one parameter
     * @throws CmsException the exception that is thrown when the multi operation was not successful
     */
    public void checkMultiOperationException(I_CmsMessageBundle messages, String key) throws CmsException {

        if (m_multiOperationException.hasExceptions()) {
            m_multiOperationException.setMessage(new CmsMessageContainer(
                messages,
                key,
                new Object[] {m_multiOperationException}));
            throw m_multiOperationException;
        }
    }

    /**
     * Returns the value of the resourcelist parameter, or null if the parameter is not provided.<p>
     * 
     * This parameter selects the resources to perform operations on.<p>
     *  
     * @return the value of the resourcelist parameter or null, if the parameter is not provided
     */
    public String getParamResourcelist() {

        if (CmsStringUtil.isNotEmpty(m_paramResourcelist) && !"null".equals(m_paramResourcelist)) {
            return m_paramResourcelist;
        } else {
            return null;
        }
    }

    /**
     * Returns the resources that are defined for the dialog operation.<p>
     * 
     * For single resource operations, the list contains one item: the resource name found 
     * in the request parameter value of the "resource" parameter.<p>
     * 
     * @return the resources that are defined for the dialog operation
     */
    public List getResourceList() {

        if (m_resourceList == null) {
            // use lazy initializing
            if (getParamResourcelist() != null) {
                // found the resourcelist parameter
                m_resourceList = CmsStringUtil.splitAsList(getParamResourcelist(), DELIMITER_RESOURCES, true);
                Collections.sort(m_resourceList);
            } else {
                // this is a single resource operation, create list containing the resource name
                m_resourceList = new ArrayList(1);
                m_resourceList.add(getParamResource());
            }
        }
        return m_resourceList;
    }

    /**
     * Returns true if the dialog operation has to be performed on multiple resources.<p>
     * 
     * @return true if the dialog operation has to be performed on multiple resources, otherwise false
     */
    public boolean isMultiOperation() {

        return (getResourceList().size() > 1);
    }

    /**
     * Sets the title of the dialog depending on the operation type, multiple or single operation.<p>
     * 
     * @param singleKey the key for the single operation
     * @param multiKey the key for the multiple operation
     */
    public void setDialogTitle(String singleKey, String multiKey) {

        if (isMultiOperation()) {
            // generate title with number of selected resources and parent folder parameters
            String resCount = String.valueOf(getResourceList().size());
            String currentFolder = CmsResource.getFolderPath(getSettings().getExplorerResource());
            currentFolder = CmsStringUtil.formatResourceName(currentFolder, 40);
            Object[] params = new Object[] {resCount, currentFolder};
            setParamTitle(key(multiKey, params));
        } else {
            // generate title using the resource name as parameter for the key
            String resourceName = CmsStringUtil.formatResourceName(getParamResource(), 50);
            setParamTitle(key(singleKey, new Object[] {resourceName}));
        }
    }

    /**
     * Sets the value of the resourcelist parameter.<p>
     * 
     * @param paramResourcelist the value of the resourcelist parameter
     */
    public void setParamResourcelist(String paramResourcelist) {

        m_paramResourcelist = paramResourcelist;
    }
    
    /**
     * Checks if the permissions of the current user on the single resource to use in the dialog are sufficient.<p>
     * 
     * For a multi resource operation, this returns always true, checks only for single resource operations.<p>
     * 
     * @see CmsDialog#checkResourcePermissions(CmsPermissionSet, boolean)
     * 
     * @param required the required permissions for the dialog
     * @param neededForFolder if true, the permissions are required for the parent folder of the resource (e.g. for editors)
     * @return true if the permissions are sufficient, otherwise false
     */
    protected boolean checkResourcePermissions(CmsPermissionSet required, boolean neededForFolder) {

        if (isMultiOperation()) {
            // for multi resource operation, return always true
            return true;
        } else {
            // check for single resource operation
            return super.checkResourcePermissions(required, neededForFolder);
        }
    }
    
    /**
     * Checks if the resource operation is an operation on at least one folder.<p>
     * 
     * @return true if the operation an operation on at least one folder, otherwise false
     */
    protected boolean isOperationOnFolder() {

        Iterator i = getResourceList().iterator();
        while (i.hasNext()) {
            String resName = (String)i.next();
            try {
                CmsResource curRes = getCms().readResource(resName, CmsResourceFilter.ALL);
                if (curRes.isFolder()) {
                    // found a folder
                    return true;
                }
            } catch (CmsException e) {
                // can usually be ignored
                if (LOG.isInfoEnabled()) {
                    LOG.info(e.getLocalizedMessage());
                }
            }
        }
        return false;
    }

    /**
     * Performs the dialog operation for the selected resources.<p>
     * 
     * @return true, if the operation was successful, otherwise false
     * @throws CmsException if operation was not successful
     */
    protected abstract boolean performDialogOperation() throws CmsException;

}

⌨️ 快捷键说明

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