Search in sources :

Example 6 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.

the class BaseCustomModelApiTest method compareCustomTypesAspects.

protected void compareCustomTypesAspects(AbstractClassModel expectedDetails, AbstractClassModel actualDetails, String... excludeFields) {
    List<CustomModelProperty> expectedProps = expectedDetails.getProperties();
    List<CustomModelProperty> actualProps = actualDetails.getProperties();
    // Sort them
    sortIfnotNull(expectedProps);
    sortIfnotNull(actualProps);
    boolean propEqualResult = true;
    if (expectedProps.size() == actualProps.size()) {
        for (int i = 0, size = expectedProps.size(); i < size; i++) {
            boolean equalProp = EqualsBuilder.reflectionEquals(expectedProps.get(i), actualProps.get(i), excludeFields);
            if (!equalProp) {
                propEqualResult = false;
                break;
            }
        }
    } else {
        propEqualResult = false;
    }
    if (excludeFields.length > 0) {
        int size = excludeFields.length;
        excludeFields = Arrays.copyOf(excludeFields, size + 1);
        excludeFields[size] = "properties";
    }
    boolean result = EqualsBuilder.reflectionEquals(expectedDetails, actualDetails, excludeFields);
    String typesAspects = (expectedDetails instanceof CustomAspect) ? "aspects" : "types";
    assertTrue("Two " + typesAspects + " are not equal. Expected:<" + expectedDetails.toString() + "> but was:<" + actualDetails.toString() + ">", (result && propEqualResult));
}
Also used : CustomAspect(org.alfresco.rest.api.model.CustomAspect) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint)

Example 7 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty 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 8 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method replacePrefix.

private void replacePrefix(List<? extends AbstractClassModel> existingTypesOrAspects, String modelOldNamespacePrefix, String modelNewNamespacePrefix) {
    for (AbstractClassModel classModel : existingTypesOrAspects) {
        // Type/Aspect's parent name
        String parentName = classModel.getParentName();
        if (parentName != null) {
            Pair<String, String> prefixLocalNamePair = splitPrefixedQName(parentName);
            // As we don't want to modify the parent name of the imported models.
            if (modelOldNamespacePrefix.equals(prefixLocalNamePair.getFirst())) {
                // Change the parent name prefix, to a new model namespace prefix.
                String newParentName = constructName(prefixLocalNamePair.getSecond(), modelNewNamespacePrefix);
                classModel.setParentName(newParentName);
            }
        }
        // Change the property constraint ref
        List<CustomModelProperty> properties = classModel.getProperties();
        for (CustomModelProperty prop : properties) {
            List<String> constraintRefs = prop.getConstraintRefs();
            if (constraintRefs.size() > 0) {
                List<String> modifiedRefs = new ArrayList<>(constraintRefs.size());
                for (String ref : constraintRefs) {
                    // We don't need to check if the prefix is equal to the model prefix here, as it was
                    // done upon adding the constraint refs in the setM2Properties method.
                    Pair<String, String> prefixLocalNamePair = splitPrefixedQName(ref);
                    // Change the constraint ref prefix, to a new model namespace prefix.
                    String newRef = constructName(prefixLocalNamePair.getSecond(), modelNewNamespacePrefix);
                    modifiedRefs.add(newRef);
                }
                prop.setConstraintRefs(modifiedRefs);
            }
        }
    }
}
Also used : AbstractClassModel(org.alfresco.rest.api.model.AbstractClassModel) ArrayList(java.util.ArrayList) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty)

Example 9 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method mergeProperties.

private void mergeProperties(AbstractClassModel existingDetails, AbstractClassModel newDetails, Parameters parameters, boolean isModelActive) {
    validateList(newDetails.getProperties(), "cmm.rest_api.properties_empty_null");
    // Transform existing properties into a map
    Map<String, CustomModelProperty> existingProperties = transformToMap(existingDetails.getProperties(), toNameFunction());
    // Transform new properties into a map
    Map<String, CustomModelProperty> newProperties = transformToMap(newDetails.getProperties(), toNameFunction());
    String propName = parameters.getParameter(PARAM_UPDATE_PROP);
    // (propName == null) => property create request
    if (propName == null) {
        // As this is a create request, check for duplicate properties
        for (String name : newProperties.keySet()) {
            if (existingProperties.containsKey(name)) {
                throw new ConstraintViolatedException(I18NUtil.getMessage("cmm.rest_api.property_create_name_already_in_use", name));
            }
        }
    } else {
        // Update request
        CustomModelProperty existingProp = existingProperties.get(propName);
        if (existingProp == null) {
            throw new EntityNotFoundException(propName);
        }
        CustomModelProperty modifiedProp = newProperties.get(propName);
        if (modifiedProp == null) {
            throw new InvalidArgumentException("cmm.rest_api.property_update_prop_not_found", new Object[] { propName });
        }
        existingProp.setTitle(modifiedProp.getTitle());
        existingProp.setDescription(modifiedProp.getDescription());
        existingProp.setDefaultValue(modifiedProp.getDefaultValue());
        existingProp.setConstraintRefs(modifiedProp.getConstraintRefs());
        existingProp.setConstraints(modifiedProp.getConstraints());
        if (isModelActive) {
            validateActivePropertyUpdate(existingProp, modifiedProp);
        }
        existingProp.setDataType(modifiedProp.getDataType());
        existingProp.setMandatory(modifiedProp.isMandatory());
        existingProp.setMandatoryEnforced(modifiedProp.isMandatoryEnforced());
        existingProp.setMultiValued(modifiedProp.isMultiValued());
    }
    // Override properties
    existingProperties.putAll(newProperties);
    existingDetails.setProperties(new ArrayList<>(existingProperties.values()));
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty)

Example 10 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method convertToCustomModelProperty.

private List<CustomModelProperty> convertToCustomModelProperty(ClassDefinition classDefinition, boolean includeInherited) {
    Collection<PropertyDefinition> ownProperties = null;
    ClassDefinition parentDef = classDefinition.getParentClassDefinition();
    if (!includeInherited && parentDef != null) {
        // Remove inherited properties
        ownProperties = removeRightEntries(classDefinition.getProperties(), parentDef.getProperties()).values();
    } else {
        ownProperties = classDefinition.getProperties().values();
    }
    List<CustomModelProperty> customProperties = new ArrayList<>(ownProperties.size());
    for (PropertyDefinition propDef : ownProperties) {
        customProperties.add(new CustomModelProperty(propDef, dictionaryService));
    }
    return customProperties;
}
Also used : ArrayList(java.util.ArrayList) ClassDefinition(org.alfresco.service.cmr.dictionary.ClassDefinition) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty)

Aggregations

CustomModelProperty (org.alfresco.rest.api.model.CustomModelProperty)18 ArrayList (java.util.ArrayList)15 CustomAspect (org.alfresco.rest.api.model.CustomAspect)14 Test (org.junit.Test)13 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)10 CustomType (org.alfresco.rest.api.model.CustomType)9 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)9 List (java.util.List)7 CustomModel (org.alfresco.rest.api.model.CustomModel)6 CustomModelNamedValue (org.alfresco.rest.api.model.CustomModelNamedValue)5 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)1 M2Property (org.alfresco.repo.dictionary.M2Property)1 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)1 AbstractClassModel (org.alfresco.rest.api.model.AbstractClassModel)1 SiteInformation (org.alfresco.rest.api.tests.RepoService.SiteInformation)1 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)1