Search in sources :

Example 1 with PropertyAlreadyExistsMetadataException

use of org.alfresco.module.org_alfresco_module_rm.admin.PropertyAlreadyExistsMetadataException in project records-management by Alfresco.

the class CustomPropertyDefinitionPut method updatePropertyDefinition.

/**
 * If label has a non-null value, it is set on the property def.
 * If constraintRef has a non-null value, it is set on this propDef.
 * If constraintRef has a null value, all constraints for that propDef are removed.
 *
 * @param params
 * @return
 * @throws CustomMetadataException
 */
protected QName updatePropertyDefinition(Map<String, Serializable> params) throws CustomMetadataException {
    QName result = null;
    boolean updated = false;
    String propId = (String) params.get(PROP_ID);
    ParameterCheck.mandatoryString("propId", propId);
    QName propQName = rmAdminService.getQNameForClientId(propId);
    if (propQName == null) {
        propQName = rmAdminService.getQNameForClientId(URLEncoder.encode(propId));
    }
    if (propQName == null) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Could not find property definition for: " + propId);
    }
    if (params.containsKey(PARAM_CONSTRAINT_REF)) {
        String constraintRef = (String) params.get(PARAM_CONSTRAINT_REF);
        List<ConstraintDefinition> constraints = rmAdminService.getCustomPropertyDefinitions().get(propQName).getConstraints();
        if (constraintRef == null) {
            result = rmAdminService.removeCustomPropertyDefinitionConstraints(propQName);
            updated = constraints.isEmpty() ? false : true;
        } else {
            boolean exists = false;
            for (ConstraintDefinition constraintDefinition : constraints) {
                if (constraintDefinition.getConstraint().getShortName().equalsIgnoreCase(constraintRef)) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                QName constraintRefQName = QName.createQName(constraintRef, getNamespaceService());
                result = rmAdminService.setCustomPropertyDefinitionConstraint(propQName, constraintRefQName);
                updated = true;
            }
        }
    }
    if (params.containsKey(PARAM_LABEL)) {
        String label = (String) params.get(PARAM_LABEL);
        try {
            result = rmAdminService.updateCustomPropertyDefinitionName(propQName, label);
        } catch (PropertyAlreadyExistsMetadataException ex) {
            if (!updated) {
                String propIdAsString = rmAdminService.getQNameForClientId(label).toPrefixString(getNamespaceService());
                throw new PropertyAlreadyExistsMetadataException(propIdAsString);
            }
        }
    }
    return result;
}
Also used : PropertyAlreadyExistsMetadataException(org.alfresco.module.org_alfresco_module_rm.admin.PropertyAlreadyExistsMetadataException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) QName(org.alfresco.service.namespace.QName) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Aggregations

PropertyAlreadyExistsMetadataException (org.alfresco.module.org_alfresco_module_rm.admin.PropertyAlreadyExistsMetadataException)1 ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)1 QName (org.alfresco.service.namespace.QName)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1