cmsresourceutil.java

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

JAVA
1,285
字号
        } finally {
            getCms().getRequestContext().setSiteRoot(rootSite);
        }
        return title;
    }

    /**
     * Returns the size of the given resource as a String.<p>
     * 
     * For directories it returns <code>SIZE_DIR</code>.<p>
     * 
     * @return the size of the given resource as a String
     */
    public String getSizeString() {

        return m_resource.getLength() == -1 ? SIZE_DIR : "" + m_resource.getLength();
    }

    /**
     * Returns resource state abbreviation.<p>
     * 
     * @return resource state abbreviation
     */
    public char getStateAbbreviation() {

        return getResource().getState().getAbbreviation();
    }

    /**
     * Returns the state name for a resource.<p>
     * 
     * Uses default locale if request context is <code>null</code>.<p>
     * 
     * @return the state name for that resource
     */
    public String getStateName() {

        CmsResourceState state = m_resource.getState();
        String name;
        if (m_request == null) {
            name = org.opencms.workplace.explorer.Messages.get().getBundle().key(
                org.opencms.workplace.explorer.Messages.getStateKey(state));
        } else {
            name = org.opencms.workplace.explorer.Messages.get().getBundle(m_request.getLocale()).key(
                org.opencms.workplace.explorer.Messages.getStateKey(state));
        }
        return name;
    }

    /**
     * Returns the style class to use for the given resource.<p>
     * 
     * @return style class name
     * 
     * @see org.opencms.workplace.list.CmsListExplorerColumn#getExplorerStyleDef()
     */
    public String getStyleClassName() {

        if (isInsideProject() && isEditable()) {
            if (m_resource.getState().isChanged()) {
                return "fc";
            } else if (m_resource.getState().isNew()) {
                return "fn";
            } else if (m_resource.getState().isDeleted()) {
                return "fd";
            } else {
                return "nf";
            }
        }
        return "fp";
    }

    /**
     * Returns additional style sheets for the resource type icon depending on siblings.<p>
     * 
     * That is, depending on {@link CmsResource#getSiblingCount()}
     * 
     * Use it with the {@link #getIconPathExplorer} method.<p>
     * 
     * @return additional style sheets depending on siblings
     */
    public String getStyleSiblings() {

        StringBuffer style = new StringBuffer(128);
        if (m_resource.getSiblingCount() > 1) {
            style.append("background-image:url(");
            style.append(CmsWorkplace.getSkinUri());
            style.append(getIconPathResourceType());
            style.append("); background-position: 0px 0px; background-repeat: no-repeat; ");
        }
        return style.toString();
    }

    /**
     * Returns the system lock information tooltip for the explorer view.<p>
     * 
     * @param forExplorer if the tool tip should be generated for the explorer view
     * 
     * @return the system lock information tooltip
     */
    public String getSystemLockInfo(boolean forExplorer) {

        if (getLock().getSystemLock().isPublish()) {
            if (!forExplorer) {
                return getMessages().key(Messages.GUI_PUBLISH_TOOLTIP_0);
            } else {
                // see explorer.js(sysLockInfo) and top_js.jsp(publishlock)
                return "p"; // should have length == 1
            }
        }
        return "";
    }

    /**
     * Returns additional style sheets depending on publication constraints.<p>
     * 
     * That is, depending on {@link CmsResource#getDateReleased()} and 
     * {@link CmsResource#getDateExpired()}.<p>
     * 
     * @return additional style sheets depending on publication constraints
     * 
     * @see #getTimeWindowLayoutType()
     */
    public String getTimeWindowLayoutStyle() {

        return getTimeWindowLayoutType() == CmsResourceUtil.LAYOUTSTYLE_INRANGE ? "" : "font-style:italic;";
    }

    /**
     * Returns the layout style for the current time window state.<p>
     * 
     * <ul>
     *   <li><code>{@link CmsResourceUtil#LAYOUTSTYLE_INRANGE}</code>: The time window is in range
     *   <li><code>{@link CmsResourceUtil#LAYOUTSTYLE_BEFORERELEASE}</code>: The resource is not yet released
     *   <li><code>{@link CmsResourceUtil#LAYOUTSTYLE_AFTEREXPIRE}</code>: The resource has already expired
     * </ul>
     * 
     * @return the layout style for the current time window state
     * 
     * @see #getTimeWindowLayoutStyle()
     */
    public int getTimeWindowLayoutType() {

        int layoutstyle = CmsResourceUtil.LAYOUTSTYLE_INRANGE;
        if (!m_resource.isReleased(getCms().getRequestContext().getRequestTime())) {
            layoutstyle = CmsResourceUtil.LAYOUTSTYLE_BEFORERELEASE;
        } else if (m_resource.isExpired(getCms().getRequestContext().getRequestTime())) {
            layoutstyle = CmsResourceUtil.LAYOUTSTYLE_AFTEREXPIRE;
        }
        return layoutstyle;
    }

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

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

    /**
     * Returns the name of the user who created the given resource.<p>
     * 
     * @return the name of the user who created the given resource
     */
    public String getUserCreated() {

        String user = m_resource.getUserCreated().toString();
        try {
            user = getCurrentOuRelativeName(CmsPrincipal.readPrincipalIncludingHistory(
                getCms(),
                m_resource.getUserCreated()).getName());
        } catch (Throwable e) {
            LOG.error(e.getLocalizedMessage());
        }
        return user;
    }

    /**
     * Returns the name of the user who last modified the given resource.<p>
     * 
     * @return the name of the user who last modified the given resource
     */
    public String getUserLastModified() {

        String user = m_resource.getUserLastModified().toString();
        try {
            user = getCurrentOuRelativeName(CmsPrincipal.readPrincipalIncludingHistory(
                getCms(),
                m_resource.getUserLastModified()).getName());
        } catch (Throwable e) {
            LOG.error(e.getLocalizedMessage());
        }
        return user;
    }

    /**
     * Returns <code>true</code> if the given resource is editable by the current user.<p>
     * 
     * Returns <code>false</code> if no request context is set.<p>
     * 
     * @return <code>true</code> if the given resource is editable by the current user
     */
    public boolean isEditable() {

        if (m_request == null) {
            return false;
        }
        CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(getResourceTypeName());
        if (settings != null) {
            String rightSite = OpenCms.getSiteManager().getSiteRoot(getResource().getRootPath());
            if (rightSite == null) {
                rightSite = "";
            }
            String currentSite = getCms().getRequestContext().getSiteRoot();
            try {
                getCms().getRequestContext().setSiteRoot(rightSite);
                return settings.isEditable(getCms(), getResource());
            } finally {
                getCms().getRequestContext().setSiteRoot(currentSite);
            }
        }
        return false;
    }

    /**
     * Returns <code>true</code> if the given resource is in the reference project.<p>
     * 
     * Returns <code>false</code> if the request context is <code>null</code>.<p>
     * 
     * @return <code>true</code> if the given resource is in the reference project
     * 
     * @see #getReferenceProject()
     */
    public boolean isInsideProject() {

        return CmsProject.isInsideProject(getProjectResources(), getResource());
    }

    /**
     * Returns <code>true</code> if the stored resource has been released and has not expired.<p>
     * 
     * If no request context is available, the current time is used for the validation check.<p>
     * 
     * @return <code>true</code> if the stored resource has been released and has not expired
     * 
     * @see CmsResource#isReleasedAndNotExpired(long)
     */
    public boolean isReleasedAndNotExpired() {

        long requestTime;
        if (m_request == null) {
            requestTime = System.currentTimeMillis();
        } else {
            requestTime = m_request.getRequestTime();
        }
        return m_resource.isReleasedAndNotExpired(requestTime);
    }

    /**
     * Sets 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> 
     *
     * @param abbrevLength the path abbreviation length to set
     */
    public void setAbbrevLength(int abbrevLength) {

        m_abbrevLength = abbrevLength;
    }

    /**
     * Sets the cms context.<p>
     *
     * @param cms the cms context to set
     */
    public void setCms(CmsObject cms) {

        m_cms = cms;
        m_request = cms.getRequestContext();
        m_referenceProject = null;
        m_projectResources = null;
        m_messages = null;
    }

    /**
     * Sets the project to use to check project state.<p>
     *
     * @param project the project to set
     */
    public void setReferenceProject(CmsProject project) {

        m_referenceProject = project;
        m_projectResources = null;
    }

    /**
     * Sets the 'relative to' path.<p>
     *
     * This only affects the generation of the path for the current resource.<p> 
     *
     * @param relativeTo the 'relative to' path to set
     */
    public void setRelativeTo(String relativeTo) {

        m_relativeTo = relativeTo;
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_relativeTo)) {
            m_relativeTo = null;
        } else {
            if (!m_relativeTo.startsWith("/")) {
                m_relativeTo = "/" + m_relativeTo;
            }
            if (!m_relativeTo.endsWith("/")) {
                m_relativeTo += "/";
            }
        }
    }

    /**
     * Sets the resource.<p>
     *
     * @param resource the resource to set
     */
    public void setResource(CmsResource resource) {

        m_resource = resource;
        m_lock = null;
        m_resourceType = null;
    }

    /**
     * Sets the site mode.<p>
     *
     * This only affects the generation of the path for the current resource.<p> 
     *
     * @param siteMode the site mode to set
     */
    public void setSiteMode(CmsResourceUtilSiteMode siteMode) {

        m_siteMode = siteMode;
    }

    /**
     * Returns the simple name if the ou is the same as the current user's ou.<p>
     * 
     * @param name the fully qualified name to check
     * 
     * @return the simple name if the ou is the same as the current user's ou
     */
    private String getCurrentOuRelativeName(String name) {

        if (m_request == null) {
            return CmsOrganizationalUnit.SEPARATOR + name;
        }
        String ou = CmsOrganizationalUnit.getParentFqn(name);
        if (ou.equals(m_request.currentUser().getOuFqn())) {
            return CmsOrganizationalUnit.getSimpleName(name);
        }
        return CmsOrganizationalUnit.SEPARATOR + name;
    }

    /**
     * Returns the message bundle for formatting dates, depends on the request locale.<p>
     * 
     * @return the message bundle for formatting dates
     */
    private CmsMessages getMessages() {

        if (m_messages == null) {
            if (m_request != null) {
                m_messages = Messages.get().getBundle(m_request.getLocale());
            } else {
                m_messages = Messages.get().getBundle();
            }
        }
        return m_messages;
    }

    /**
     * Returns the reference project resources.<p>
     * 
     * @return the reference project resources
     */
    private List getProjectResources() {

        if (m_projectResources == null) {
            try {
                m_projectResources = getCms().readProjectResources(getReferenceProject());
            } catch (Throwable e) {
                LOG.error(e.getLocalizedMessage(), e);
                // use an empty list (all resources are "outside")
                m_projectResources = new ArrayList();
            }
        }
        return m_projectResources;
    }
}

⌨️ 快捷键说明

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