Search in sources :

Example 1 with M2Constraint

use of org.alfresco.repo.dictionary.M2Constraint in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method convertToM2Model.

/**
 * Converts the given {@code org.alfresco.rest.api.model.CustomModel}
 * object, a collection of {@code org.alfresco.rest.api.model.CustomType}
 * objects, a collection of
 * {@code org.alfresco.rest.api.model.CustomAspect} objects, and a collection of
 * {@code org.alfresco.rest.api.model.CustomModelConstraint} objects into a {@link M2Model} object
 *
 * @param customModel the custom model
 * @param types the custom types
 * @param aspects the custom aspects
 * @param constraints the custom constraints
 * @return {@link M2Model} object
 */
private M2Model convertToM2Model(CustomModel customModel, Collection<CustomType> types, Collection<CustomAspect> aspects, Collection<CustomModelConstraint> constraints) {
    validateBasicModelInput(customModel);
    Set<Pair<String, String>> namespacesToImport = new LinkedHashSet<>();
    final String namespacePrefix = customModel.getNamespacePrefix();
    final String namespaceURI = customModel.getNamespaceUri();
    // Construct the model name
    final String name = constructName(customModel.getName(), namespacePrefix);
    M2Model model = M2Model.createModel(name);
    model.createNamespace(namespaceURI, namespacePrefix);
    model.setDescription(customModel.getDescription());
    String author = customModel.getAuthor();
    if (author == null) {
        author = getCurrentUserFullName();
    }
    model.setAuthor(author);
    // Types
    if (types != null) {
        for (CustomType type : types) {
            validateName(type.getName(), TYPE_NAME_NULL_ERR);
            M2Type m2Type = model.createType(constructName(type.getName(), namespacePrefix));
            m2Type.setDescription(type.getDescription());
            m2Type.setTitle(type.getTitle());
            setParentName(m2Type, type.getParentName(), namespacesToImport, namespacePrefix);
            setM2Properties(m2Type, type.getProperties(), namespacePrefix, namespacesToImport);
        }
    }
    // Aspects
    if (aspects != null) {
        for (CustomAspect aspect : aspects) {
            validateName(aspect.getName(), ASPECT_NAME_NULL_ERR);
            M2Aspect m2Aspect = model.createAspect(constructName(aspect.getName(), namespacePrefix));
            m2Aspect.setDescription(aspect.getDescription());
            m2Aspect.setTitle(aspect.getTitle());
            setParentName(m2Aspect, aspect.getParentName(), namespacesToImport, namespacePrefix);
            setM2Properties(m2Aspect, aspect.getProperties(), namespacePrefix, namespacesToImport);
        }
    }
    // Constraints
    if (constraints != null) {
        for (CustomModelConstraint constraint : constraints) {
            validateName(constraint.getName(), CONSTRAINT_NAME_NULL_ERR);
            final String constraintName = constructName(constraint.getName(), namespacePrefix);
            M2Constraint m2Constraint = model.createConstraint(constraintName, constraint.getType());
            // Set title, desc and parameters
            setConstraintOtherData(constraint, m2Constraint, null);
        }
    }
    // Add imports
    for (Pair<String, String> uriPrefix : namespacesToImport) {
        // Don't import the already defined namespace
        if (!namespaceURI.equals(uriPrefix.getFirst())) {
            model.createImport(uriPrefix.getFirst(), uriPrefix.getSecond());
        }
    }
    return model;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CustomType(org.alfresco.rest.api.model.CustomType) M2Constraint(org.alfresco.repo.dictionary.M2Constraint) M2Type(org.alfresco.repo.dictionary.M2Type) M2Model(org.alfresco.repo.dictionary.M2Model) CustomAspect(org.alfresco.rest.api.model.CustomAspect) M2Aspect(org.alfresco.repo.dictionary.M2Aspect) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) Pair(org.alfresco.util.Pair)

Example 2 with M2Constraint

use of org.alfresco.repo.dictionary.M2Constraint in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method setM2Properties.

private void setM2Properties(M2Class m2Class, List<CustomModelProperty> properties, String namespacePrefix, Set<Pair<String, String>> namespacesToImport) {
    if (properties != null) {
        for (CustomModelProperty prop : properties) {
            validateName(prop.getName(), "cmm.rest_api.property_name_null");
            M2Property m2Property = m2Class.createProperty(constructName(prop.getName(), namespacePrefix));
            m2Property.setTitle(prop.getTitle());
            m2Property.setDescription(prop.getDescription());
            m2Property.setMandatory(prop.isMandatory());
            m2Property.setMandatoryEnforced(prop.isMandatoryEnforced());
            m2Property.setMultiValued(prop.isMultiValued());
            String dataType = prop.getDataType();
            // Default type is d:text
            if (StringUtils.isBlank(dataType)) {
                dataType = DEFAULT_DATA_TYPE;
            } else {
                if (!dataType.contains(":")) {
                    throw new InvalidArgumentException("cmm.rest_api.property_datatype_invalid", new Object[] { dataType });
                }
            }
            namespacesToImport.add(resolveToUriAndPrefix(dataType));
            try {
                // validate default values
                this.valueDataTypeValidator.validateValue(dataType, prop.getDefaultValue());
            } catch (Exception ex) {
                throw new InvalidArgumentException(ex.getMessage());
            }
            m2Property.setType(dataType);
            m2Property.setDefaultValue(prop.getDefaultValue());
            // Set indexing options
            m2Property.setIndexed(prop.isIndexed());
            // so it can support boolean data type.
            if (!BOOLEAN_DATA_TYPE.equals(dataType)) {
                if (Facetable.TRUE == prop.getFacetable()) {
                    m2Property.setFacetable(true);
                } else if (Facetable.FALSE == prop.getFacetable()) {
                    m2Property.setFacetable(false);
                }
            }
            m2Property.setIndexTokenisationMode(prop.getIndexTokenisationMode());
            // Check for constraints
            List<String> constraintRefs = prop.getConstraintRefs();
            List<CustomModelConstraint> constraints = prop.getConstraints();
            if (constraintRefs.size() > 0) {
                for (String ref : constraintRefs) {
                    Pair<String, String> prefixLocalName = splitPrefixedQName(ref);
                    if (!namespacePrefix.equals(prefixLocalName.getFirst())) {
                        throw new ConstraintViolatedException(I18NUtil.getMessage("cmm.rest_api.constraint_ref_not_defined", ref));
                    }
                    m2Property.addConstraintRef(ref);
                }
            }
            if (constraints.size() > 0) {
                for (CustomModelConstraint modelConstraint : constraints) {
                    String constraintName = null;
                    if (modelConstraint.getName() != null) {
                        validateName(modelConstraint.getName(), CONSTRAINT_NAME_NULL_ERR);
                        constraintName = constructName(modelConstraint.getName(), namespacePrefix);
                    }
                    M2Constraint m2Constraint = m2Property.addConstraint(constraintName, modelConstraint.getType());
                    // Set title, desc and parameters
                    setConstraintOtherData(modelConstraint, m2Constraint, dataType);
                }
            }
        }
    }
}
Also used : M2Constraint(org.alfresco.repo.dictionary.M2Constraint) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) M2Property(org.alfresco.repo.dictionary.M2Property) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ActiveModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException) ModelDoesNotExistException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException) CustomModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException) InvalidCustomModelException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) CustomModelException(org.alfresco.service.cmr.dictionary.CustomModelException) ModelExistsException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelExistsException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)

Example 3 with M2Constraint

use of org.alfresco.repo.dictionary.M2Constraint in project records-management by Alfresco.

the class RecordsManagementAdminServiceImpl method addCustomConstraintDefinition.

/**
 * @see org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService#addCustomConstraintDefinition(org.alfresco.service.namespace.QName, java.lang.String, boolean, java.util.List, org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint.MatchLogic)
 */
public void addCustomConstraintDefinition(QName constraintName, String title, boolean caseSensitive, List<String> allowedValues, MatchLogic matchLogic) {
    mandatory("constraintName", constraintName);
    mandatoryString("title", title);
    mandatory("allowedValues", allowedValues);
    mandatory("matchLogic", matchLogic);
    NodeRef modelRef = getCustomModelRef(constraintName.getNamespaceURI());
    M2Model deserializedModel = readCustomContentModel(modelRef);
    String constraintNameAsPrefixString = constraintName.toPrefixString(getNamespaceService());
    M2Constraint customConstraint = deserializedModel.getConstraint(constraintNameAsPrefixString);
    if (customConstraint != null) {
        throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_CONSTRAINT_EXISTS, constraintNameAsPrefixString));
    }
    M2Constraint newCon = deserializedModel.createConstraint(constraintNameAsPrefixString, CUSTOM_CONSTRAINT_TYPE);
    newCon.setTitle(title);
    newCon.createParameter(PARAM_ALLOWED_VALUES, allowedValues);
    newCon.createParameter(PARAM_CASE_SENSITIVE, caseSensitive ? "true" : "false");
    newCon.createParameter(PARAM_MATCH_LOGIC, matchLogic.toString());
    writeCustomContentModel(modelRef, deserializedModel);
    if (logger.isInfoEnabled()) {
        logger.info("addCustomConstraintDefinition: " + constraintNameAsPrefixString + " (valueCnt: " + allowedValues.size() + ")");
    }
}
Also used : M2Constraint(org.alfresco.repo.dictionary.M2Constraint) NodeRef(org.alfresco.service.cmr.repository.NodeRef) M2Model(org.alfresco.repo.dictionary.M2Model) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ParameterCheck.mandatoryString(org.springframework.extensions.surf.util.ParameterCheck.mandatoryString)

Example 4 with M2Constraint

use of org.alfresco.repo.dictionary.M2Constraint in project records-management by Alfresco.

the class RecordsManagementAdminServiceImpl method changeCustomConstraintValues.

/**
 * @see org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService#changeCustomConstraintValues(org.alfresco.service.namespace.QName, java.util.List)
 */
public void changeCustomConstraintValues(QName constraintName, List<String> newAllowedValues) {
    mandatory("constraintName", constraintName);
    mandatory("newAllowedValues", newAllowedValues);
    NodeRef modelRef = getCustomModelRef(constraintName.getNamespaceURI());
    M2Model deserializedModel = readCustomContentModel(modelRef);
    String constraintNameAsPrefixString = constraintName.toPrefixString(getNamespaceService());
    M2Constraint customConstraint = deserializedModel.getConstraint(constraintNameAsPrefixString);
    if (customConstraint == null) {
        throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_CANNOT_FIND_CONSTRAINT, constraintNameAsPrefixString));
    }
    String type = customConstraint.getType();
    if (type == null || (!type.equals(CUSTOM_CONSTRAINT_TYPE) && !type.equals(CAPATIBILITY_CUSTOM_CONTRAINT_TYPE))) {
        throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_UNEXPECTED_TYPE_CONSTRAINT, type, constraintNameAsPrefixString, CUSTOM_CONSTRAINT_TYPE));
    }
    customConstraint.removeParameter(PARAM_ALLOWED_VALUES);
    customConstraint.createParameter(PARAM_ALLOWED_VALUES, newAllowedValues);
    writeCustomContentModel(modelRef, deserializedModel);
    if (logger.isInfoEnabled()) {
        logger.info("changeCustomConstraintValues: " + constraintNameAsPrefixString + " (valueCnt: " + newAllowedValues.size() + ")");
    }
}
Also used : M2Constraint(org.alfresco.repo.dictionary.M2Constraint) NodeRef(org.alfresco.service.cmr.repository.NodeRef) M2Model(org.alfresco.repo.dictionary.M2Model) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ParameterCheck.mandatoryString(org.springframework.extensions.surf.util.ParameterCheck.mandatoryString)

Example 5 with M2Constraint

use of org.alfresco.repo.dictionary.M2Constraint in project records-management by Alfresco.

the class RecordsManagementAdminServiceImpl method changeCustomConstraintTitle.

/**
 * @see org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService#changeCustomConstraintTitle(org.alfresco.service.namespace.QName, java.lang.String)
 */
public void changeCustomConstraintTitle(QName constraintName, String title) {
    mandatory("constraintName", constraintName);
    mandatoryString("title", title);
    NodeRef modelRef = getCustomModelRef(constraintName.getNamespaceURI());
    M2Model deserializedModel = readCustomContentModel(modelRef);
    String constraintNameAsPrefixString = constraintName.toPrefixString(getNamespaceService());
    M2Constraint customConstraint = deserializedModel.getConstraint(constraintNameAsPrefixString);
    if (customConstraint == null) {
        throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_CANNOT_FIND_CONSTRAINT, constraintNameAsPrefixString));
    }
    String type = customConstraint.getType();
    if ((type == null) || (!type.equals(CUSTOM_CONSTRAINT_TYPE))) {
        throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_UNEXPECTED_TYPE_CONSTRAINT, type, constraintNameAsPrefixString, CUSTOM_CONSTRAINT_TYPE));
    }
    customConstraint.setTitle(title);
    writeCustomContentModel(modelRef, deserializedModel);
    if (logger.isInfoEnabled()) {
        logger.info("changeCustomConstraintTitle: " + constraintNameAsPrefixString + " (title: " + title + ")");
    }
}
Also used : M2Constraint(org.alfresco.repo.dictionary.M2Constraint) NodeRef(org.alfresco.service.cmr.repository.NodeRef) M2Model(org.alfresco.repo.dictionary.M2Model) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ParameterCheck.mandatoryString(org.springframework.extensions.surf.util.ParameterCheck.mandatoryString)

Aggregations

M2Constraint (org.alfresco.repo.dictionary.M2Constraint)6 M2Model (org.alfresco.repo.dictionary.M2Model)5 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)4 NodeRef (org.alfresco.service.cmr.repository.NodeRef)4 ParameterCheck.mandatoryString (org.springframework.extensions.surf.util.ParameterCheck.mandatoryString)4 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)2 LinkedHashSet (java.util.LinkedHashSet)1 M2Aspect (org.alfresco.repo.dictionary.M2Aspect)1 M2Property (org.alfresco.repo.dictionary.M2Property)1 M2Type (org.alfresco.repo.dictionary.M2Type)1 CustomAspect (org.alfresco.rest.api.model.CustomAspect)1 CustomModelProperty (org.alfresco.rest.api.model.CustomModelProperty)1 CustomType (org.alfresco.rest.api.model.CustomType)1 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)1 CustomModelException (org.alfresco.service.cmr.dictionary.CustomModelException)1 ActiveModelConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException)1