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

📄 cmspropertycustom.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * 
     * The html does not include the value of the created property,
     * the values are set delayed (see buildSetFormValues() for details).<p>
     * 
     * @param propertyName the name of the property
     * @param propertyTitle the nice name of the property
     * @param editable indicates if the properties are editable
     * @return the html for a single text input property row
     */
    protected StringBuffer buildPropertyEntry(String propertyName, String propertyTitle, boolean editable) {

        StringBuffer result = new StringBuffer(256);
        // create "disabled" attribute if properties are not editable
        String disabled = "";
        if (!editable) {
            disabled = " disabled=\"disabled\"";
        }
        result.append(buildTableRowStart(propertyTitle));
        if (getActiveProperties().containsKey(propertyName)) {
            // the property is used, so create text field with checkbox and hidden field
            CmsProperty currentProperty = (CmsProperty)getActiveProperties().get(propertyName);
            String propValue = currentProperty.getValue();
            if (propValue != null) {
                propValue = propValue.trim();
            }
            propValue = CmsEncoder.escapeXml(propValue);
            
            // create text input field
            result.append("<input type=\"text\" class=\"maxwidth\"");
            result.append(" name=\"");
            result.append(PREFIX_VALUE);
            result.append(propertyName);
            result.append("\" id=\"");
            result.append(PREFIX_VALUE);
            result.append(propertyName);
            result.append("\"");
            if (editable) {
                result.append(" onKeyup=\"checkValue('");
                result.append(propertyName);
                result.append("');\"");
            }
            result.append(disabled);
            result.append(">");
            
            // create hidden field for value
            result.append("<input type=\"hidden\" name=\"");
            result.append(PREFIX_HIDDEN);
            result.append(propertyName);
            result.append("\" id=\"");
            result.append(PREFIX_HIDDEN);
            result.append(propertyName);
            result.append("\" value=\"");
            result.append(propValue);
            result.append("\">");
            result.append("</td>\n");
            result.append("\t<td class=\"textcenter\">");
            
            // create activate/deactivate checkbox
            result.append("<input type=\"checkbox\" name=\"");
            result.append(PREFIX_USEPROPERTY);
            result.append(propertyName);
            result.append("\" id=\"");
            result.append(PREFIX_USEPROPERTY);
            result.append(propertyName);
            result.append("\" value=\"true\"");
            result.append(" checked=\"checked\"");
            if (editable) {
                result.append(" onClick=\"toggleDelete('");
                result.append(propertyName);
                result.append("');\"");
            }
            result.append(disabled + ">");
        } else {
            // property is not used, create an empty text input field
            result.append("<input type=\"text\" class=\"maxwidth\" ");
            result.append("name=\"");
            result.append(PREFIX_VALUE);
            result.append(propertyName);
            result.append("\" id=\"");
            result.append(PREFIX_VALUE);
            result.append(propertyName);
            result.append("\"");
            result.append(disabled);
            result.append("></td>\n");
            result.append("\t<td class=\"textcenter\">&nbsp;");
        }
        result.append(buildTableRowEnd());
        return result;
    }

    /**
     * Builds the HTML for the end of a table row for a single property.<p>
     * 
     * @return the HTML code for a table row end
     */
    protected String buildTableRowEnd() {

        return "</td>\n</tr>\n";
    }

    /**
     * Builds the HTML for the start of a table row for a single property.<p>
     * 
     * @param propertyName the name of the current property
     * @return the HTML code for the start of a table row
     */
    protected StringBuffer buildTableRowStart(String propertyName) {

        StringBuffer result = new StringBuffer(96);
        result.append("<tr>\n");
        result.append("\t<td style=\"white-space: nowrap;\" unselectable=\"on\">");
        result.append(propertyName);
        result.append("</td>\n");
        result.append("\t<td class=\"maxwidth\">");
        return result;
    }

    /**
     * Builds the HTML for the common text input property values stored in the String array "PROPERTIES".<p>
     * 
     * @param editable indicates if the properties are editable
     * @return the HTML code for the common text input fields
     */
    protected StringBuffer buildTextInput(boolean editable) {

        StringBuffer result = new StringBuffer(256);
        Iterator i = getExplorerTypeSettings().getProperties().iterator();
        // iterate over the properties
        while (i.hasNext()) {
            String curProperty = (String)i.next();
            result.append(buildPropertyEntry(curProperty, curProperty, editable));
        }
        return result;
    }

    /**
     * Returns a map with CmsProperty object values keyed by property keys.<p>
     * 
     * @return a map with CmsProperty object values
     */
    protected Map getActiveProperties() {

        // get all used properties for the resource
        if (m_activeProperties == null) {
            try {
                m_activeProperties = CmsPropertyAdvanced.getPropertyMap(getCms().readPropertyObjects(
                    getParamResource(),
                    false));
            } catch (CmsException e) {
                // create an empty list
                if (LOG.isInfoEnabled()) {
                    LOG.info(e.getLocalizedMessage());
                }
                m_activeProperties = new HashMap();
            }
        }
        return m_activeProperties;
    }

    /**
     * Initializes the explorer type settings for the current resource type.<p>
     */
    protected void initExplorerTypeSettings() {

        try {
            CmsResource res = getCms().readResource(getParamResource(), CmsResourceFilter.ALL);
            String resTypeName = OpenCms.getResourceManager().getResourceType(res.getTypeId()).getTypeName();
            // get settings for resource type
            setExplorerTypeSettings(getSettingsForType(resTypeName));
            setShowNavigation(getExplorerTypeSettings().isShowNavigation());
        } catch (Throwable e) {
            // error reading file, show error dialog
            try {
                includeErrorpage(this, e);
            } catch (JspException exc) {
                LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_INCLUDE_FAILED_1, FILE_DIALOG_SCREEN_ERRORPAGE));
            }
        }
    }

    /**
     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
     */
    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {

        // fill the parameter values in the get/set methods
        fillParamValues(request);
        // get the explorer type settings for the current resource
        initExplorerTypeSettings();
        // set the dialog type
        setParamDialogtype(DIALOG_TYPE);
        boolean isPopup = Boolean.valueOf(getParamIsPopup()).booleanValue();
        // set the action for the JSP switch 
        if (DIALOG_SHOW_DEFAULT.equals(getParamAction())) {
            // save changed properties and redirect to the default OpenCms dialog
            setAction(ACTION_DEFAULT);
            try {
                actionEdit(request);
                sendForward(CmsPropertyAdvanced.URI_PROPERTY_DIALOG, paramsAsParameterMap());
            } catch (Exception e) {
                // should usually never happen
                if (LOG.isInfoEnabled()) {
                    LOG.info(e.getLocalizedMessage());
                }
            }
        } else if (DIALOG_SAVE_EDIT.equals(getParamAction())) {
            // save the edited properties
            if (isPopup) {
                setAction(ACTION_CLOSEPOPUP_SAVE);
            } else {
                setAction(ACTION_SAVE_EDIT);
            }
        } else if (DIALOG_CANCEL.equals(getParamAction())) {
            // save the edited properties
            if (isPopup) {
                setAction(ACTION_CLOSEPOPUP);
            } else {
                setAction(ACTION_CANCEL);
            }
        } else {
            setAction(ACTION_EDIT);
            String resName = CmsResource.getName(getParamResource());
            if (resName.startsWith(CmsWorkplace.TEMP_FILE_PREFIX)) {
                resName = resName.substring(1);
            }
            setParamTitle(key(Messages.GUI_PROPERTIES_1, new Object[] {resName}));
        }
    }

    /**
     * Performs the editing of the resources properties.<p>
     * 
     * @param request the HttpServletRequest
     * @return true, if the properties were successfully changed, otherwise false
     * @throws CmsException if editing is not successful
     */
    protected boolean performEditOperation(HttpServletRequest request) throws CmsException {

        boolean useTempfileProject = Boolean.valueOf(getParamUsetempfileproject()).booleanValue();
        try {
            if (useTempfileProject) {
                switchToTempProject();
            }
            // write the common properties defined in the explorer type settings
            Iterator i = getExplorerTypeSettings().getProperties().iterator();
            // iterate over the properties
            while (i.hasNext()) {
                String curProperty = (String)i.next();
                String paramValue = request.getParameter(PREFIX_VALUE + curProperty);
                String oldValue = request.getParameter(PREFIX_HIDDEN + curProperty);
                writeProperty(curProperty, paramValue, oldValue);
            }

            // write the navigation properties if enabled
            if (showNavigation()) {
                // get the navigation enabled parameter
                String paramValue = request.getParameter("enablenav");
                String oldValue = null;
                if (Boolean.valueOf(paramValue).booleanValue()) {
                    // navigation enabled, update params
                    paramValue = request.getParameter("navpos");
                    if (!"-1".equals(paramValue) && !String.valueOf(Float.MAX_VALUE).equals(paramValue)) {
                        // update the property only when it is different from "-1" (meaning no change)
                        oldValue = request.getParameter(PREFIX_HIDDEN + CmsPropertyDefinition.PROPERTY_NAVPOS);
                        writeProperty(CmsPropertyDefinition.PROPERTY_NAVPOS, paramValue, oldValue);
                    }
                    paramValue = request.getParameter(PREFIX_VALUE + CmsPropertyDefinition.PROPERTY_NAVTEXT);
                    oldValue = request.getParameter(PREFIX_HIDDEN + CmsPropertyDefinition.PROPERTY_NAVTEXT);
                    writeProperty(CmsPropertyDefinition.PROPERTY_NAVTEXT, paramValue, oldValue);
                } else {
                    // navigation disabled, delete property values
                    writeProperty(CmsPropertyDefinition.PROPERTY_NAVPOS, null, null);
                    writeProperty(CmsPropertyDefinition.PROPERTY_NAVTEXT, null, null);
                }
            }
        } finally {
            if (useTempfileProject) {
                switchToCurrentProject();
            }
        }
        return true;
    }

    /**
     * Writes a property value for a resource, if the value was changed.<p>
     * 
     * If a property definition for the resource does not exist,
     * it is automatically created by this method.<p>
     * 
     * @param propName the name of the property
     * @param propValue the new value of the property
     * @param oldValue the old value of the property
     * @throws CmsException if something goes wrong
     */
    protected void writeProperty(String propName, String propValue, String oldValue) throws CmsException {

        // get the current property object
        CmsProperty currentProperty = (CmsProperty)getActiveProperties().get(propName);
        if (currentProperty == null) {
            // new property, create new property object
            currentProperty = new CmsProperty();
            currentProperty.setName(propName);
        } else {
            // clone the property, because the original property is frozen
            currentProperty = currentProperty.cloneAsProperty();
        }

        // check if there is a parameter value for the current property
        boolean emptyParam = true;
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(propValue)) {
            emptyParam = false;
        }

        if (emptyParam) {
            // parameter is empty, check if the property has to be deleted
            if (getActiveProperties().containsKey(propName)) {
                // lock resource if autolock is enabled
                checkLock(getParamResource());
                // determine the value to delete
                if (currentProperty.getStructureValue() != null) {
                    currentProperty.setStructureValue(CmsProperty.DELETE_VALUE);
                    currentProperty.setResourceValue(null);
                } else {
                    currentProperty.setResourceValue(CmsProperty.DELETE_VALUE);
                    currentProperty.setStructureValue(null);
                }
                // write the updated property object
                getCms().writePropertyObject(getParamResource(), currentProperty);
            }
        } else {
            // parameter is not empty, check if the value has changed
            if (!propValue.equals(oldValue)) {
                // lock resource if autolock is enabled
                checkLock(getParamResource());
                if (currentProperty.getStructureValue() == null && currentProperty.getResourceValue() == null) {
                    // new property, determine setting from OpenCms workplace configuration
                    if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) {
                        currentProperty.setStructureValue(propValue);
                        currentProperty.setResourceValue(null);
                    } else {
                        currentProperty.setResourceValue(propValue);
                        currentProperty.setStructureValue(null);
                    }
                } else if (currentProperty.getStructureValue() != null) {
                    // structure value has to be updated
                    currentProperty.setStructureValue(propValue);
                    currentProperty.setResourceValue(null);
                } else {
                    // resource value has to be updated
                    currentProperty.setResourceValue(propValue);
                    currentProperty.setStructureValue(null);
                }
                // set auto-creation of the property to true
                currentProperty.setAutoCreatePropertyDefinition(true);
                // write the updated property object
                getCms().writePropertyObject(getParamResource(), currentProperty);
            }
        }
    }

}

⌨️ 快捷键说明

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