cmsresourceutil.java

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

JAVA
1,285
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/explorer/CmsResourceUtil.java,v $
 * Date   : $Date: 2007-09-05 11:19:35 $
 * Version: $Revision: 1.8 $
 *
 * 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.explorer;

import org.opencms.db.CmsResourceState;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsRequestContext;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.file.types.I_CmsResourceType;
import org.opencms.i18n.CmsMessages;
import org.opencms.lock.CmsLock;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsOrganizationalUnit;
import org.opencms.security.CmsPermissionSet;
import org.opencms.security.CmsPermissionSetCustom;
import org.opencms.security.CmsPrincipal;
import org.opencms.util.A_CmsModeIntEnumeration;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import org.opencms.workplace.CmsWorkplace;
import org.opencms.workplace.commons.CmsTouch;

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

import org.apache.commons.logging.Log;

/**
 * Provides {@link CmsResource} utility functions.<p>
 * 
 * This class provides in java all resource information used by the explorer view,
 * mostly generated in javascript (see explorer.js)<p>
 * 
 * @author Michael Moossen
 * 
 * @version $Revision: 1.8 $ 
 * 
 * @since 6.0.0 
 */
public final class CmsResourceUtil {

    /**
     * Enumeration class for defining the resource project state.<p>
     */
    public static class CmsResourceProjectState extends A_CmsModeIntEnumeration {

        /** Constant for the project state unlocked. */
        protected static final CmsResourceProjectState CLEAN = new CmsResourceProjectState(0);

        /** Constant for the project state locked for publishing. */
        protected static final CmsResourceProjectState LOCKED_FOR_PUBLISHING = new CmsResourceProjectState(5);

        /** Constant for the project state locked in current project. */
        protected static final CmsResourceProjectState MODIFIED_IN_CURRENT_PROJECT = new CmsResourceProjectState(1);

        /** Constant for the project state locked in other project. */
        protected static final CmsResourceProjectState MODIFIED_IN_OTHER_PROJECT = new CmsResourceProjectState(2);

        private static final long serialVersionUID = 4580450220255428716L;

        /**
         * Default constructor.<p>
         * 
         * @param mode the mode descriptor
         */
        protected CmsResourceProjectState(int mode) {

            super(mode);
        }

        /**
         * Checks if this is a {@link #LOCKED_FOR_PUBLISHING} state.<p>
         * 
         * @return <code>true</code> if this is a {@link #LOCKED_FOR_PUBLISHING} state
         */
        public boolean isLockedForPublishing() {

            return (this == LOCKED_FOR_PUBLISHING);
        }

        /**
         * Checks if this is a {@link #MODIFIED_IN_CURRENT_PROJECT} state.<p>
         * 
         * @return <code>true</code> if this is a {@link #MODIFIED_IN_CURRENT_PROJECT} state
         */
        public boolean isModifiedInCurrentProject() {

            return (this == MODIFIED_IN_CURRENT_PROJECT);
        }

        /**
         * Checks if this is a {@link #MODIFIED_IN_OTHER_PROJECT} state.<p>
         * 
         * @return <code>true</code> if this is a {@link #MODIFIED_IN_OTHER_PROJECT} state
         */
        public boolean isModifiedInOtherProject() {

            return (this == MODIFIED_IN_OTHER_PROJECT);
        }

        /**
         * Checks if this is a {@link #CLEAN} state.<p>
         * 
         * @return <code>true</code> if this is a {@link #CLEAN} state
         */
        public boolean isUnlocked() {

            return (this == CLEAN);
        }
    }

    /**
     * Enumeration class for defining the site modes.<p>
     */
    private static class CmsResourceUtilSiteMode {

        /**
         * Default constructor.<p>
         */
        protected CmsResourceUtilSiteMode() {

            // noop
        }
    }

    /** Layoutstyle for resources after expire date. */
    public static final int LAYOUTSTYLE_AFTEREXPIRE = 2;

    /** Layoutstyle for resources before release date. */
    public static final int LAYOUTSTYLE_BEFORERELEASE = 1;

    /** Layoutstyle for resources after release date and before expire date. */
    public static final int LAYOUTSTYLE_INRANGE = 0;

    /** Constant that signalizes that all path operations will be based on the current site. */
    public static final CmsResourceUtilSiteMode SITE_MODE_CURRENT = new CmsResourceUtilSiteMode();

    /** Constant that signalizes that all path operations will be based on the best matching site. */
    public static final CmsResourceUtilSiteMode SITE_MODE_MATCHING = new CmsResourceUtilSiteMode();

    /** Constant that signalizes that all path operations will be based on the root path. */
    public static final CmsResourceUtilSiteMode SITE_MODE_ROOT = new CmsResourceUtilSiteMode();

    /** Constant for the project state locked for publishing. */
    public static final CmsResourceProjectState STATE_LOCKED_FOR_PUBLISHING = CmsResourceProjectState.LOCKED_FOR_PUBLISHING;

    /** Constant for the project state locked in current project. */
    public static final CmsResourceProjectState STATE_MODIFIED_IN_CURRENT_PROJECT = CmsResourceProjectState.MODIFIED_IN_CURRENT_PROJECT;

    /** Constant for the project state locked in other project. */
    public static final CmsResourceProjectState STATE_MODIFIED_IN_OTHER_PROJECT = CmsResourceProjectState.MODIFIED_IN_OTHER_PROJECT;

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

    /** The folder size display string constant. */
    private static final String SIZE_DIR = "-";

    /** Constant for the project state unlocked. */
    private static final CmsResourceProjectState STATE_CLEAN = CmsResourceProjectState.CLEAN;

    /** If greater than zero, the path will be formatted to this number of chars. */
    private int m_abbrevLength;

    /** The current cms context. */
    private CmsObject m_cms;

    /** The current resource lock. */
    private CmsLock m_lock;

    /** The message bundle for formatting dates, depends on the request locale. */
    private CmsMessages m_messages;

    /** Reference project resources cache. */
    private List m_projectResources;

    /** The project to use to check project state, if <code>null</code> the current project will be used. */
    private CmsProject m_referenceProject;

    /** The 'relative to' path. */
    private String m_relativeTo;

    /** The current request context. */
    private CmsRequestContext m_request;

    /** The current resource. */
    private CmsResource m_resource;

    /** The current resource type. */
    private I_CmsResourceType m_resourceType;

    /** The current site mode. */
    private CmsResourceUtilSiteMode m_siteMode = SITE_MODE_CURRENT;

    /**
     * Creates a new {@link CmsResourceUtil} object.<p> 
     * 
     * @param cms the cms context
     */
    public CmsResourceUtil(CmsObject cms) {

        setCms(cms);
    }

    /**
     * Creates a new {@link CmsResourceUtil} object.<p> 
     * 
     * @param cms the cms context
     * @param resource the resource
     */
    public CmsResourceUtil(CmsObject cms, CmsResource resource) {

        setCms(cms);
        setResource(resource);
    }

    /**
     * Creates a new {@link CmsResourceUtil} object.<p> 
     * 
     * @param resource the resource
     */
    public CmsResourceUtil(CmsResource resource) {

        setResource(resource);
    }

    /**
     * Returns the path abbreviation length.<p>
     *
     * If greater than zero, the path will be formatted to this number of chars.<p>
     * 
     * This only affects the generation of the path for the current resource.<p> 
     *
     * @return the path abbreviation Length
     */
    public int getAbbrevLength() {

        return m_abbrevLength;
    }

    /**
     * Returns the cms context.<p>
     *
     * @return the cms context
     */
    public CmsObject getCms() {

        return m_cms;
    }

    /**
     * Returns the formatted date of expiration.<p>
     * 
     * @return the formatted date of expiration
     */
    public String getDateExpired() {

        long release = m_resource.getDateExpired();
        if (release != CmsResource.DATE_EXPIRED_DEFAULT) {
            return getMessages().getDateTime(release);
        } else {
            return CmsTouch.DEFAULT_DATE_STRING;
        }
    }

    /**
     * Returns the formatted date of release.<p>
     * 
     * @return the formatted date of release
     */
    public String getDateReleased() {

        long release = m_resource.getDateReleased();
        if (release != CmsResource.DATE_RELEASED_DEFAULT) {
            return getMessages().getDateTime(release);
        } else {
            return CmsTouch.DEFAULT_DATE_STRING;
        }
    }

    /**
     * Returns the path of the current resource, taking into account just the site mode.<p>
     * 
     * @return the full path
     */
    public String getFullPath() {

        String path = m_resource.getRootPath();
        if ((m_siteMode != SITE_MODE_ROOT) && (m_cms != null)) {
            String site = getSite();
            if (path.startsWith(site)) {
                path = path.substring(site.length());
            }
        }
        return path;
    }

    /**
     * Returns the resource icon path displayed in the explorer view for the given resource.<p>
     * 
     * Relative to <code>/system/workplace/resources/</code>.<p>
     * 
     * If the resource has no sibling it is the same as {@link #getIconPathResourceType()}.<p>
     * 
     * @return the resource icon path displayed in the explorer view for the given resource
     * 
     * @see #getStyleSiblings()
     */
    public String getIconPathExplorer() {

        if (m_resource.getSiblingCount() > 1) {
            // links are present
            if (m_resource.isLabeled()) {
                // there is at least one link in a marked site
                return "explorer/link_labeled.gif";
            } else {
                // common links are present
                return "explorer/link.gif";
            }
        } else {
            return getIconPathResourceType();
        }
    }

    /**
     * Returns the lock icon path for the given resource.<p>
     * 
     * Relative to <code>/system/workplace/resources/</code>.<p>
     * 
     * Returns <code>explorer/project_none.gif</code> if request context is <code>null</code>.<p>
     * 
     * @return the lock icon path for the given resource
     */
    public String getIconPathLock() {

        CmsLock lock = getLock();
        String iconPath = null;
        if (!lock.isUnlocked() && (m_request != null) && isInsideProject()) {
            if (getLock().isOwnedBy(m_request.currentUser())
                && (getLockedInProjectId().equals(getReferenceProject().getUuid()))) {
                if (lock.isShared()) {
                    iconPath = "shared";
                } else {
                    iconPath = "user";
                }
            } else {
                iconPath = "other";
            }
        }
        if (iconPath == null) {
            iconPath = "project_none";
        } else {
            iconPath = "lock_" + iconPath;
        }
        return "explorer/" + iconPath + ".gif";
    }

    /**
     * Returns the project state icon path for the given resource.<p>
     * 
     * Relative to <code>/system/workplace/resources/</code>.<p>
     * 
     * @return the project state icon path for the given resource
     */
    public String getIconPathProjectState() {

        String iconPath;
        if (getProjectState() == STATE_MODIFIED_IN_CURRENT_PROJECT) {
            iconPath = "this.png";
        } else if (getProjectState() == STATE_MODIFIED_IN_OTHER_PROJECT) {
            iconPath = "other.png";
        } else if (getProjectState() == STATE_LOCKED_FOR_PUBLISHING) {
            iconPath = "publish.png";
        } else {
            // STATE_UNLOCKED
            iconPath = "none.gif";
        }
        return "explorer/project_" + iconPath;
    }

    /**
     * Returns the resource type icon path for the given resource.<p>
     * 
     * Relative to <code>/system/workplace/resources/</code>.<p>
     * 
     * @return the resource type icon path for the given resource
     */
    public String getIconPathResourceType() {

        if (!isEditable()) {
            return "filetypes/"
                + OpenCms.getWorkplaceManager().getExplorerTypeSetting(CmsResourceTypePlain.getStaticTypeName()).getIcon();
        }
        return "filetypes/" + OpenCms.getWorkplaceManager().getExplorerTypeSetting(getResourceTypeName()).getIcon();

⌨️ 快捷键说明

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