cmsresourceutil.java

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

JAVA
1,285
字号
    }

    /**
     * Returns an integer representation for the link type.<p>
     * 
     * <ul>
     *   <li><code>0</code>: No sibling
     *   <li><code>1</code>: Sibling
     *   <li><code>2</code>: Labeled sibling
     * </ul>
     * 
     * @return an integer representation for the link type
     */
    public int getLinkType() {

        if (m_resource.getSiblingCount() > 1) {
            // links are present
            if (m_resource.isLabeled()) {
                // there is at least one link in a marked site
                return 2;
            } else {
                // common links are present
                return 1;
            }
        } else {
            // no links to the resource are in the VFS
            return 0;
        }
    }

    /**
     * Returns the the lock for the given resource.<p>
     * 
     * @return the lock the given resource
     */
    public CmsLock getLock() {

        if (m_lock == null) {
            try {
                m_lock = getCms().getLock(m_resource);
            } catch (Throwable e) {
                m_lock = CmsLock.getNullLock();
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
        return m_lock;
    }

    /**
     * Returns the user name who owns the lock for the given resource.<p>
     * 
     * @return the user name who owns the lock for the given resource
     */
    public String getLockedByName() {

        String lockedBy = "";
        if (!getLock().isNullLock()) {
            try {
                lockedBy = getCurrentOuRelativeName(getCms().readUser(getLock().getUserId()).getName());
            } catch (Throwable e) {
                lockedBy = e.getMessage();
            }
        }
        return lockedBy;
    }

    /**
     * Returns the id of the project in which the given resource is locked.<p>
     * 
     * @return the id of the project in which the given resource is locked
     */
    public CmsUUID getLockedInProjectId() {

        CmsUUID lockedInProject = null;
        if (getLock().isNullLock() && !getResource().getState().isUnchanged()) {
            // resource is unlocked and modified
            lockedInProject = getResource().getProjectLastModified();
        } else if (!getResource().getState().isUnchanged()) {
            // resource is locked and modified
            lockedInProject = getProjectId();
        } else if (!getLock().isNullLock()) {
            // resource is locked and unchanged
            lockedInProject = getLock().getProjectId();
        }
        return lockedInProject;
    }

    /**
     * Returns the project name that locked the current resource's.<p>
     * 
     * @return the the project name that locked the current resource's
     */
    public String getLockedInProjectName() {

        try {
            CmsUUID pId = getLockedInProjectId();
            if (pId == null) {
                // the resource is unlocked and unchanged
                return "";
            }
            return getCurrentOuRelativeName(getCms().readProject(pId).getName());
        } catch (Throwable e) {
            LOG.error(e.getLocalizedMessage(), e);
            return "";
        }
    }

    /**
     * Returns the lock state of the current resource.<p>
     * 
     * @return the lock state of the current resource
     */
    public int getLockState() {

        if (CmsStringUtil.isEmptyOrWhitespaceOnly(getLockedByName())) {
            // unlocked
            return 0;
        }
        if (!getLockedByName().equals(m_request.currentUser().getName())
            || !getLockedInProjectId().equals(m_request.currentProject().getUuid())) {
            // locked by other user and/or project
            return 1;
        }
        if (getLock().getType().isShared()) {
            // shared lock
            return 2;
        }
        // exclusive lock
        return 3;
    }

    /**
     * Returns the navtext of a resource.<p>
     * 
     * @return the navtext for that resource
     */
    public String getNavText() {

        String navText = "";
        try {
            navText = getCms().readPropertyObject(
                getCms().getSitePath(m_resource),
                CmsPropertyDefinition.PROPERTY_NAVTEXT,
                false).getValue();
        } catch (Throwable e) {
            String storedSiteRoot = getCms().getRequestContext().getSiteRoot();
            try {
                getCms().getRequestContext().setSiteRoot("");
                navText = getCms().readPropertyObject(
                    m_resource.getRootPath(),
                    CmsPropertyDefinition.PROPERTY_NAVTEXT,
                    false).getValue();
            } catch (Exception e1) {
                // should usually never happen
                if (LOG.isInfoEnabled()) {
                    LOG.info(e);
                }
            } finally {
                getCms().getRequestContext().setSiteRoot(storedSiteRoot);
            }
        }
        if (navText == null) {
            navText = "";
        }
        return navText;
    }

    /**
     * Returns the path of the current resource.<p>
     * 
     * Taking into account following settings:<br>
     * <ul>
     *    <li>site mode
     *    <li>abbreviation length
     *    <li>relative to
     * </ul>
     * 
     * @return the path
     */
    public String getPath() {

        String path = getFullPath();
        if (m_relativeTo != null) {
            path = getResource().getRootPath();
            if (path.startsWith(m_relativeTo)) {
                path = path.substring(m_relativeTo.length());
                if (path.length() == 0) {
                    path = ".";
                }
            } else {
                String site = getSite();
                if (path.startsWith(site + "/") || path.equals(site)) {
                    path = path.substring(site.length());
                }
            }
        }
        if (m_abbrevLength > 0) {
            boolean absolute = path.startsWith("/");
            path = CmsStringUtil.formatResourceName(path, m_abbrevLength);
            if (!absolute && path.startsWith("/")) {
                // remove leading '/'
                path = path.substring(1);
            }
        }
        return path;
    }

    /**
     * Returns the permission set for the given resource.<p>
     * 
     * @return the permission set for the given resource
     */
    public CmsPermissionSet getPermissionSet() {

        CmsPermissionSetCustom pset = new CmsPermissionSetCustom();
        CmsResource resource = getResource();
        try {
            if (getCms().hasPermissions(resource, CmsPermissionSet.ACCESS_CONTROL, false, CmsResourceFilter.ALL)) {
                pset.grantPermissions(CmsPermissionSet.PERMISSION_CONTROL);
            } else {
                pset.denyPermissions(CmsPermissionSet.PERMISSION_CONTROL);
            }
        } catch (CmsException e) {
            LOG.error(e.getLocalizedMessage());
        }
        try {
            if (getCms().hasPermissions(resource, CmsPermissionSet.ACCESS_DIRECT_PUBLISH, false, CmsResourceFilter.ALL)) {
                pset.grantPermissions(CmsPermissionSet.PERMISSION_DIRECT_PUBLISH);
            } else {
                pset.denyPermissions(CmsPermissionSet.PERMISSION_DIRECT_PUBLISH);
            }
        } catch (CmsException e) {
            LOG.error(e.getLocalizedMessage());
        }
        try {
            if (getCms().hasPermissions(resource, CmsPermissionSet.ACCESS_READ, false, CmsResourceFilter.ALL)) {
                pset.grantPermissions(CmsPermissionSet.PERMISSION_READ);
            } else {
                pset.denyPermissions(CmsPermissionSet.PERMISSION_READ);
            }
        } catch (CmsException e) {
            LOG.error(e.getLocalizedMessage());
        }
        try {
            if (getCms().hasPermissions(resource, CmsPermissionSet.ACCESS_VIEW, false, CmsResourceFilter.ALL)) {
                pset.grantPermissions(CmsPermissionSet.PERMISSION_VIEW);
            } else {
                pset.denyPermissions(CmsPermissionSet.PERMISSION_VIEW);
            }
        } catch (CmsException e) {
            LOG.error(e.getLocalizedMessage());
        }
        try {
            if (getCms().hasPermissions(resource, CmsPermissionSet.ACCESS_WRITE, false, CmsResourceFilter.ALL)) {
                pset.grantPermissions(CmsPermissionSet.PERMISSION_WRITE);
            } else {
                pset.denyPermissions(CmsPermissionSet.PERMISSION_WRITE);
            }
        } catch (CmsException e) {
            LOG.error(e.getLocalizedMessage());
        }

        return pset;
    }

    /**
     * Returns the permissions string for the given resource.<p>
     * 
     * @return the permissions string for the given resource
     */
    public String getPermissionString() {

        return getPermissionSet().getPermissionString();
    }

    /**
     * Returns the id of the project which the resource belongs to.<p>
     * 
     * @return the id of the project which the resource belongs to
     */
    public CmsUUID getProjectId() {

        CmsUUID projectId = m_resource.getProjectLastModified();
        if (!getLock().isUnlocked() && !getLock().isInherited()) {
            // use lock project ID only if lock is not inherited
            projectId = getLock().getProjectId();
        }
        return projectId;
    }

    /**
     * Returns the project state of the given resource.<p>
     * 
     * <ul>
     *   <li>null: unchanged.</li>
     *   <li>true: locked in current project.</li>
     *   <li>false: not locked in current project.</li>
     * </ul>
     * 
     * @return the project state of the given resource
     */
    public CmsResourceProjectState getProjectState() {

        if (getResource().getState().isUnchanged()) {
            return STATE_CLEAN; // STATE_CLEAN
        } else if (getLock().getSystemLock().isPublish()) {
            return STATE_LOCKED_FOR_PUBLISHING;
        } else if (getResource().getProjectLastModified().equals(getReferenceProject().getUuid())) {
            return STATE_MODIFIED_IN_CURRENT_PROJECT; // STATE_MODIFIED_CURRENT_PROJECT
        } else {
            return STATE_MODIFIED_IN_OTHER_PROJECT; // STATE_MODIFIED_OTHER_PROJECT
        }
    }

    /**
     * Returns the project to use to check project state.<p>
     *
     * @return the project to use to check project state
     */
    public CmsProject getReferenceProject() {

        if (m_referenceProject == null) {
            if (m_request != null) {
                m_referenceProject = m_request.currentProject();
            }
        }
        return m_referenceProject;
    }

    /**
     * Returns the 'relative to' path.<p>
     *
     * This only affects the generation of the path for the current resource.<p> 
     *
     * @return the 'relative to' path
     */
    public String getRelativeTo() {

        return m_relativeTo;
    }

    /**
     * Returns the resource.<p>
     *
     * @return the resource
     */
    public CmsResource getResource() {

        return m_resource;
    }

    /**
     * Returns the resource type for the given resource.<p>
     * 
     * @return the resource type for the given resource
     */
    public I_CmsResourceType getResourceType() {

        if (m_resourceType == null) {
            m_resourceType = OpenCms.getResourceManager().getResourceType(m_resource);
        }
        return m_resourceType;
    }

    /**
     * Returns the resource type id for the given resource.<p>
     * 
     * @return the resource type id for the given resource
     */
    public int getResourceTypeId() {

        return getResourceType().getTypeId();
    }

    /**
     * Returns the resource type name for the given resource.<p>
     * 
     * @return the resource type name for the given resource
     */
    public String getResourceTypeName() {

        return getResourceType().getTypeName();
    }

    /**
     * Returns the site of the current resources,
     * taking into account the set site mode.<p>
     * 
     * @return the site path
     */
    public String getSite() {

        String site = null;
        if ((m_siteMode == SITE_MODE_MATCHING) || (m_cms == null)) {
            site = OpenCms.getSiteManager().getSiteRoot(m_resource.getRootPath());
        } else if (m_siteMode == SITE_MODE_CURRENT) {
            site = m_cms.getRequestContext().getSiteRoot();
        } else if (m_siteMode == SITE_MODE_ROOT) {
            site = "";
        }
        return (site == null ? "" : site);
    }

    /**
     * Returns the site mode.<p>
     *
     * This only affects the generation of the path for the current resource.<p> 
     *
     * @return the site mode
     */
    public CmsResourceUtilSiteMode getSiteMode() {

        return m_siteMode;
    }

    /**
     * Returns the title of the site.<p>
     * 
     * @return the title of the site
     */
    public String getSiteTitle() {

        String title = getSite();
        String rootSite = getCms().getRequestContext().getSiteRoot();
        try {
            getCms().getRequestContext().setSiteRoot("");
            title = getCms().readPropertyObject(title, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue(title);
        } catch (CmsException e) {
            // ignore

⌨️ 快捷键说明

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