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

📄 cmstoolmanager.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/CmsToolManager.java,v $
 * Date   : $Date: 2006/03/28 13:10:02 $
 * Version: $Revision: 1.44 $
 *
 * 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.tools;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsResource;
import org.opencms.i18n.CmsEncoder;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.util.CmsIdentifiableObjectContainer;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.workplace.CmsDialog;
import org.opencms.workplace.CmsWorkplace;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;

/**
 * Manages the registered tools, actualizing its state every time the workplace is reinitialize.<p>
 * 
 * Manages also the configuration settings for the administration view, and provides
 * several tool related methods.<p>
 *
 * @author Michael Moossen  
 * 
 * @version $Revision: 1.44 $ 
 * 
 * @since 6.0.0 
 */
public class CmsToolManager {

    /**  Root location of the administration view. */
    public static final String ADMINVIEW_ROOT_LOCATION = CmsWorkplace.PATH_WORKPLACE + "views/admin";

    /**  Property definition name to look for. */
    public static final String HANDLERCLASS_PROPERTY = "admintoolhandler-class";

    /**  Navegation bar separator (html code). */
    public static final String NAVBAR_SEPARATOR = "\n&nbsp;&gt;&nbsp;\n";

    /**  Tool root separator. */
    public static final String ROOT_SEPARATOR = ":";

    /**  Key for the default tool root, if there is no configured root with this a key, a new one will be configured. */
    public static final String ROOTKEY_DEFAULT = "admin";

    /**  Tool path separator. */
    public static final String TOOLPATH_SEPARATOR = "/";

    /** Location of the default admin view jsp page. */
    public static final String VIEW_JSPPAGE_LOCATION = ADMINVIEW_ROOT_LOCATION + "/admin-main.jsp";

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

    /** List of all available roots. */
    private final CmsIdentifiableObjectContainer m_roots;

    /** List of all available tools. */
    private final CmsIdentifiableObjectContainer m_tools;

    /** List of all available urls and related tool paths. */
    private final CmsIdentifiableObjectContainer m_urls;

    /**
     * Default constructor.<p>
     */
    public CmsToolManager() {

        m_roots = new CmsIdentifiableObjectContainer(true, false);
        m_tools = new CmsIdentifiableObjectContainer(true, false);
        m_urls = new CmsIdentifiableObjectContainer(false, false);
    }

    /**
     * Returns the OpenCms link for the given tool path which requires no parameters.<p>
     * 
     * @param jsp the jsp action element
     * @param toolPath the tool path
     * 
     * @return the OpenCms link for the given tool path which requires parameters
     */
    public static String linkForToolPath(CmsJspActionElement jsp, String toolPath) {

        StringBuffer result = new StringBuffer();
        result.append(jsp.link(VIEW_JSPPAGE_LOCATION));
        result.append('?');
        result.append(CmsToolDialog.PARAM_PATH);
        result.append('=');
        result.append(CmsEncoder.encode(toolPath));
        return result.toString();
    }

    /**
     * Returns the OpenCms link for the given tool path which requires parameters.<p>
     * 
     * Please note: Don't overuse the parameter map because this will likely introduce issues 
     * with encoding. If possible, don't pass parameters at all, or only very simple parameters
     * with no special chars that can easily be parsed.<p>
     * 
     * @param jsp the jsp action element
     * @param toolPath the tool path
     * @param params the map of required tool parameters
     * 
     * @return the OpenCms link for the given tool path which requires parameters
     */
    public static String linkForToolPath(CmsJspActionElement jsp, String toolPath, Map params) {

        if (params == null) {
            // no parameters - take the shortcut
            return linkForToolPath(jsp, toolPath);
        }
        params.put(CmsToolDialog.PARAM_PATH, toolPath);
        return CmsRequestUtil.appendParameters(jsp.link(VIEW_JSPPAGE_LOCATION), params, true);
    }

    /**
     * Adds a new tool root to the tool manager.<p>
     * 
     * @param toolRoot the tool root to add
     */
    public void addToolRoot(CmsToolRootHandler toolRoot) {

        m_roots.addIdentifiableObject(toolRoot.getKey(), toolRoot);
    }

    /**
     * Called by the <code>{@link org.opencms.workplace.CmsWorkplaceManager#initialize(CmsObject)}</code> method.<p>
     * 
     * @param cms the admin cms context
     */
    public void configure(CmsObject cms) {

        if (CmsLog.INIT.isInfoEnabled()) {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_TOOLMANAGER_CREATED_0));
        }
        if (m_roots.getObject(ROOTKEY_DEFAULT) == null) {
            CmsToolRootHandler defToolRoot = new CmsToolRootHandler();
            defToolRoot.setKey(ROOTKEY_DEFAULT);
            defToolRoot.setUri(CmsWorkplace.PATH_WORKPLACE + "admin/");
            defToolRoot.setName("${key." + Messages.GUI_ADMIN_VIEW_ROOT_NAME_0 + "}");
            defToolRoot.setHelpText("${key." + Messages.GUI_ADMIN_VIEW_ROOT_HELP_0 + "}");
            addToolRoot(defToolRoot);
        }
        m_tools.clear();
        m_urls.clear();
        Iterator it = getToolRoots().iterator();
        while (it.hasNext()) {
            CmsToolRootHandler toolRoot = (CmsToolRootHandler)it.next();
            if (!cms.existsResource(toolRoot.getUri())) {
                if (CmsLog.INIT.isInfoEnabled()) {
                    CmsLog.INIT.info(Messages.get().getBundle().key(
                        Messages.INIT_TOOLMANAGER_ROOT_SKIPPED_2,
                        toolRoot.getKey(),
                        toolRoot.getUri()));
                }
                continue;
            }
            try {
                toolRoot.setup(cms, null, toolRoot.getUri());
                configureToolRoot(cms, toolRoot);
                // log info
                if (CmsLog.INIT.isInfoEnabled()) {
                    CmsLog.INIT.info(Messages.get().getBundle().key(
                        Messages.INIT_TOOLMANAGER_SETUP_1,
                        toolRoot.getKey()));
                }
            } catch (CmsException e) {
                // log failure
                if (CmsLog.INIT.isWarnEnabled()) {
                    CmsLog.INIT.warn(Messages.get().getBundle().key(
                        Messages.INIT_TOOLMANAGER_SETUP_ERROR_1,
                        toolRoot.getKey()), e);
                }
            }
        }
    }

    /**
     * Returns the navegation bar html code for the given tool path.<p>
     * 
     * @param toolPath the path
     * @param wp the jsp page
     * 
     * @return the html code
     */
    public String generateNavBar(String toolPath, CmsWorkplace wp) {

        if (toolPath.equals(getBaseToolPath(wp))) {
            return "<div class='pathbar'>&nbsp;</div>\n";
        }
        CmsTool adminTool = resolveAdminTool(getCurrentRoot(wp).getKey(), toolPath);
        String html = A_CmsHtmlIconButton.defaultButtonHtml(
            wp.getJsp(),
            CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
            "nav" + adminTool.getId(),
            adminTool.getHandler().getShortName(),
            null,
            false,
            null,
            null,
            null);
        String parent = toolPath;
        while (!parent.equals(getBaseToolPath(wp))) {
            parent = getParent(wp, parent);
            adminTool = resolveAdminTool(getCurrentRoot(wp).getKey(), parent);

            String id = "nav" + adminTool.getId();
            String link = linkForToolPath(wp.getJsp(), parent, adminTool.getHandler().getParameters(wp));
            String onClic = "openPage('" + link + "');";
            String buttonHtml = A_CmsHtmlIconButton.defaultButtonHtml(
                wp.getJsp(),
                CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
                id,
                adminTool.getHandler().getName(),
                adminTool.getHandler().getHelpText(),
                true,
                null,
                null,
                onClic);
            html = buttonHtml + NAVBAR_SEPARATOR + html;
        }

        return CmsToolMacroResolver.resolveMacros("<div class='pathbar'>\n" + html + "&nbsp;</div>\n", wp);
    }

    /**
     * Returns the base tool path for the active user.<p>
     * 
     * @param wp the workplace object
     * 
     * @return the base tool path for the active user
     */
    public String getBaseToolPath(CmsWorkplace wp) {

        CmsToolUserData userData = getUserData(wp);
        String path = TOOLPATH_SEPARATOR;
        if (userData != null) {
            path = userData.getBaseTool(getCurrentRoot(wp).getKey());
        }
        return path;
    }

    /**
     * Returns the current user's root handler.<p>
     * 
     * @param wp the workplace context
     * 
     * @return the current user's root handler
     */
    public CmsToolRootHandler getCurrentRoot(CmsWorkplace wp) {

        CmsToolUserData userData = getUserData(wp);
        String root = ROOTKEY_DEFAULT;
        if (userData != null && m_roots.getObject(userData.getRootKey()) != null) {
            root = userData.getRootKey();
        }
        return (CmsToolRootHandler)m_roots.getObject(root);
    }

    /**
     * Returns the current tool.<p>
     * 
     * @param wp the workplace object
     *
     * @return the current tool 
     */
    public CmsTool getCurrentTool(CmsWorkplace wp) {

        return resolveAdminTool(getCurrentRoot(wp).getKey(), getCurrentToolPath(wp));
    }

    /**
     * Returns the current tool path.<p>
     *
     * @param wp the workplace object
     *
     * @return the current tool path
     */
    public String getCurrentToolPath(CmsWorkplace wp) {

        CmsToolUserData userData = getUserData(wp);
        String path = getBaseToolPath(wp);
        if (userData != null) {
            path = userData.getCurrentToolPath(getCurrentRoot(wp).getKey());
        }
        return path;
    }

    /**
     * Returns the path to the parent of the tool identified by the given tool path.<p>
     * 
     * The parent of the root is the same root.<p>
     * 
     * @param wp the workplace object
     * @param toolPath the abstract tool path
     * 
     * @return his parent
     */
    public String getParent(CmsWorkplace wp, String toolPath) {

        if (toolPath.equals(getBaseToolPath(wp))) {
            return toolPath;
        }
        int pos = toolPath.lastIndexOf(TOOLPATH_SEPARATOR);
        return pos <= 0 ? TOOLPATH_SEPARATOR : toolPath.substring(0, pos);
    }

    /**
     * Returns a list with all registered tools.<p>
     * 
     * @return list if <code>{@link CmsTool}</code>
     */
    public List getToolHandlers() {

        return m_tools.elementList();
    }

    /**
     * Returns a list of tool roots.<p>
     * 
     * @return a list of {@link CmsToolRootHandler} objects 
     */
    public List getToolRoots() {

        return m_roots.elementList();
    }

    /**
     * Returns a list of all tools in the given path.<p>
     * 
     * @param wp the workplace context
     * @param baseTool the path
     * @param includeSubtools if the tools in subfolders should be also returned
     * 
     * @return a list of {@link CmsTool} objects 
     */
    public List getToolsForPath(CmsWorkplace wp, String baseTool, boolean includeSubtools) {

        List toolList = new ArrayList();
        String rootKey = getCurrentRoot(wp).getKey();
        Iterator itTools = m_tools.elementList().iterator();
        while (itTools.hasNext()) {
            CmsTool tool = (CmsTool)itTools.next();
            String path = tool.getHandler().getPath();
            if (resolveAdminTool(rootKey, path) != tool) {
                continue;
            }
            if (path.equals(TOOLPATH_SEPARATOR)) {
                continue;
            }
            // leave out everything above the base
            if (!path.startsWith(baseTool)) {
                continue;
            }
            // filter for path
            if (baseTool.equals(TOOLPATH_SEPARATOR) || path.startsWith(baseTool + TOOLPATH_SEPARATOR)) {
                // filter sub tree
                if (includeSubtools || path.indexOf(TOOLPATH_SEPARATOR, baseTool.length() + 1) < 0) {
                    toolList.add(tool);
                }
            }
        }
        return toolList;
    }

    /**
     * Returns the <code>{@link CmsToolUserData}</code> object for a given user.<p>
     *
     * @param wp the workplace object
     *
     * @return the current user data
     */

⌨️ 快捷键说明

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