cmsworkplace.java

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

JAVA
1,722
字号
     * 
     * @param resourceName the name of the resource to get the resource path for
     * 
     * @return the full Workplace resource path to the selected resource
     */
    public static String getResourceUri(String resourceName) {

        StringBuffer result = new StringBuffer(256);
        result.append(getSkinUri());
        result.append(resourceName);
        return result.toString();
    }

    /**
     * Returns the path to the skin resources.<p>
     * 
     * @return the path to the skin resources
     */
    public static String getSkinUri() {

        if (m_skinUri == null) {
            m_skinUri = OpenCms.getSystemInfo().getContextPath() + RFS_PATH_RESOURCES;
        }
        return m_skinUri;
    }

    /**
     * Returns the path to the cascading stylesheets.<p>
     * 
     * @param jsp the JSP context
     * @return the path to the cascading stylesheets
     */
    public static String getStyleUri(CmsJspActionElement jsp) {

        if (m_styleUri == null) {

            CmsProject project = jsp.getCmsObject().getRequestContext().currentProject();
            try {
                jsp.getCmsObject().getRequestContext().setCurrentProject(
                    jsp.getCmsObject().readProject(CmsProject.ONLINE_PROJECT_ID));
                m_styleUri = jsp.link("/system/workplace/commons/style/");
            } catch (CmsException e) {
                LOG.error(e.getLocalizedMessage());
            } finally {
                jsp.getCmsObject().getRequestContext().setCurrentProject(project);
            }
        }
        return m_styleUri;
    }

    /**
     * Returns the path to the cascading stylesheets.<p>
     * 
     * @param jsp the JSP context
     * @param filename the name of the stylesheet
     * @return the path to the cascading stylesheets
     */
    public static String getStyleUri(CmsJspActionElement jsp, String filename) {

        if (m_styleUri == null) {
            CmsProject project = jsp.getCmsObject().getRequestContext().currentProject();
            try {
                jsp.getCmsObject().getRequestContext().setCurrentProject(
                    jsp.getCmsObject().readProject(CmsProject.ONLINE_PROJECT_ID));
                m_styleUri = jsp.link("/system/workplace/commons/style/");
            } catch (CmsException e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error(e.getLocalizedMessage(), e);
                }
            } finally {
                jsp.getCmsObject().getRequestContext().setCurrentProject(project);
            }
        }
        return m_styleUri + filename;
    }

    /**
     * Returns the temporary file name for the given resource name.<p>
     * 
     * To create a temporary file name of a resource name, the prefix char <code>'~'</code> (tilde)
     * is added to the file name after all parent folder names have been removed.<p>
     * 
     * @param resourceName the resource name to return the temporary file name for
     * 
     * @return the temporary file name for the given resource name
     * 
     * @see #isTemporaryFileName(String)
     * @see #isTemporaryFile(CmsResource)
     */
    public static String getTemporaryFileName(String resourceName) {

        if (resourceName == null) {
            return null;
        }
        StringBuffer result = new StringBuffer(resourceName.length() + 2);
        result.append(CmsResource.getFolderPath(resourceName));
        result.append(TEMP_FILE_PREFIX);
        result.append(CmsResource.getName(resourceName));
        return result.toString();
    }

    /**
     * Updates the user settings in the given workplace settings for the current user, reading the user settings
     * from the database if required.<p>
     * 
     * @param cms the cms object for the current user
     * @param settings the workplace settings to update (if <code>null</code> a new instance is created)
     * @param update flag indicating if settings are only updated (user preferences)
     * 
     * @return the current users workplace settings
     * 
     * @see #initWorkplaceSettings(CmsObject, CmsWorkplaceSettings, boolean)
     */
    public static CmsWorkplaceSettings initUserSettings(CmsObject cms, CmsWorkplaceSettings settings, boolean update) {

        if (settings == null) {
            settings = new CmsWorkplaceSettings();
        }

        // save current workplace user & user settings object
        CmsUser user;
        if (update) {
            try {
                // read the user from db to get the latest user information if required
                user = cms.readUser(cms.getRequestContext().currentUser().getId());
            } catch (CmsException e) {
                // can usually be ignored
                if (LOG.isInfoEnabled()) {
                    LOG.info(e.getLocalizedMessage());
                }
                user = cms.getRequestContext().currentUser();
            }
        } else {
            user = cms.getRequestContext().currentUser();
        }
        // store the user and it's settings in the Workplace settings
        settings.setUser(user);
        settings.setUserSettings(new CmsUserSettings(user));

        // return the result settings
        return settings;
    }

    /**
     * Updates the given workplace settings, also re-initializing
     * the state of the Workplace to the users preferences (for example setting the startup site and project).
     * 
     * The user settings will also be updated by calling <code>{@link #initUserSettings(CmsObject, CmsWorkplaceSettings, boolean)}</code>
     * before updating the workplace project, selected site etc.<p>
     * 
     * @param cms the cms object for the current user
     * @param settings the workplace settings to update (if <code>null</code> a new instance is created)
     * @param update flag indicating if settings are only updated (user preferences)
     * 
     * @return the current users initialized workplace settings
     * 
     * @see #initUserSettings(CmsObject, CmsWorkplaceSettings, boolean) 
     */
    public static synchronized CmsWorkplaceSettings initWorkplaceSettings(
        CmsObject cms,
        CmsWorkplaceSettings settings,
        boolean update) {

        // init the workplace user settings 
        settings = initUserSettings(cms, settings, update);

        // save current project
        settings.setProject(cms.getRequestContext().currentProject().getUuid());

        // switch to users preferred site      
        String siteRoot = settings.getUserSettings().getStartSite();
        if (siteRoot.endsWith("/")) {
            // remove trailing slash
            siteRoot = siteRoot.substring(0, siteRoot.length() - 1);
        }
        if (CmsStringUtil.isNotEmpty(siteRoot) && (OpenCms.getSiteManager().getSiteForSiteRoot(siteRoot) == null)) {
            // this is not the root site and the site is not in the list
            siteRoot = OpenCms.getWorkplaceManager().getDefaultUserSettings().getStartSite();
            if (siteRoot.endsWith("/")) {
                // remove trailing slash
                siteRoot = siteRoot.substring(0, siteRoot.length() - 1);
            }
        }
        boolean access = false;
        CmsResource res = null;
        try {
            // check access to the site
            res = cms.readResource("/");
            access = cms.hasPermissions(res, CmsPermissionSet.ACCESS_VIEW);
        } catch (CmsException e) {
            // error reading site root, in this case we will use a readable default
            if (LOG.isInfoEnabled()) {
                LOG.info(e.getLocalizedMessage(), e);
            }

        }
        if ((res == null) || !access) {
            List sites = OpenCms.getSiteManager().getAvailableSites(cms, true);
            if (sites.size() > 0) {
                siteRoot = ((CmsSite)sites.get(0)).getSiteRoot();
                cms.getRequestContext().setSiteRoot(siteRoot);
            }
        }
        // set the current site
        settings.setSite(siteRoot);

        // set the preferred folder to display
        settings.setExplorerResource(settings.getUserSettings().getStartFolder());

        // get the default view from the user settings
        settings.setViewUri(OpenCms.getLinkManager().substituteLink(cms, settings.getUserSettings().getStartView()));

        return settings;
    }

    /**
     * Returns <code>true</code> if the given resource is a temporary file.<p>
     * 
     * A resource is considered a temporary file it is a file where the
     * {@link CmsResource#FLAG_TEMPFILE} flag has been set, or if the file name (without parent folders)
     * starts with the prefix char <code>'~'</code> (tilde).<p>
     * 
     * @param resource the resource name to check
     * 
     * @return <code>true</code> if the given resource name is a temporary file
     * 
     * @see #getTemporaryFileName(String)
     * @see #isTemporaryFileName(String)
     */
    public static boolean isTemporaryFile(CmsResource resource) {

        return (resource != null)
            && ((resource.isFile() && (((resource.getFlags() & CmsResource.FLAG_TEMPFILE) > 0) || (isTemporaryFileName(resource.getName())))));
    }

    /**
     * Returns <code>true</code> if the given resource name is a temporary file name.<p>
     * 
     * A resource name is considered a temporary file name if the name of the file 
     * (without parent folders) starts with the prefix char <code>'~'</code> (tilde).<p>
     * 
     * @param resourceName the resource name to check
     * 
     * @return <code>true</code> if the given resource name is a temporary file name
     * 
     * @see #getTemporaryFileName(String)
     * @see #isTemporaryFile(CmsResource)
     */
    public static boolean isTemporaryFileName(String resourceName) {

        return (resourceName != null) && (CmsResource.getName(resourceName).startsWith(TEMP_FILE_PREFIX));
    }

    /**
     * Stores the settings in the given session.<p>
     * 
     * @param session the session to store the settings in
     * @param settings the settings
     */
    static void storeSettings(HttpSession session, CmsWorkplaceSettings settings) {

        // save the workplace settings in the session
        session.setAttribute(CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS, settings);
    }

    /**
     * Returns all parameters of the current workplace class 
     * as hidden field tags that can be inserted in a form.<p>
     * 
     * @return all parameters of the current workplace class
     * as hidden field tags that can be inserted in a html form
     */
    public String allParamsAsHidden() {

        StringBuffer result = new StringBuffer(512);
        Map params = allParamValues();
        Iterator i = params.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry entry = (Map.Entry)i.next();
            result.append("<input type=\"hidden\" name=\"");
            result.append(entry.getKey());
            result.append("\" value=\"");
            String encoded = CmsEncoder.encode(entry.getValue().toString(), getCms().getRequestContext().getEncoding());
            result.append(encoded);
            result.append("\">\n");
        }
        return result.toString();
    }

    /**
     * Returns all present request parameters as String.<p>
     * 
     * The String is formatted as a parameter String (<code>param1=val1&amp;param2=val2</code>) with UTF-8 encoded values.<p>
     * 
     * @return all present request parameters as String
     */
    public String allParamsAsRequest() {

        StringBuffer retValue = new StringBuffer(512);
        HttpServletRequest request = getJsp().getRequest();
        Iterator paramNames = request.getParameterMap().keySet().iterator();
        while (paramNames.hasNext()) {
            String paramName = (String)paramNames.next();
            String paramValue = request.getParameter(paramName);
            retValue.append(paramName + "=" + CmsEncoder.encode(paramValue, getCms().getRequestContext().getEncoding()));
            if (paramNames.hasNext()) {
                retValue.append("&");
            }
        }
        return retValue.toString();
    }

    /**
     * Builds the end html of the body.<p>
     * 
     * @return the end html of the body
     */
    public String bodyEnd() {

        return pageBody(HTML_END, null, null);
    }

    /**
     * Builds the start html of the body.<p>
     * 
     * @param className optional class attribute to add to the body tag
     * @return the start html of the body
     */
    public String bodyStart(String className) {

        return pageBody(HTML_START, className, null);
    }

    /**
     * Builds the start html of the body.<p>
     * 
     * @param className optional class attribute to add to the body tag
     * @param parameters optional parameters to add to the body tag
     * @return the start html of the body
     */
    public String bodyStart(String className, String parameters) {

        return pageBody(HTML_START, className, parameters);
    }

⌨️ 快捷键说明

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