Search in sources :

Example 6 with CustomModelConstraint

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

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

the class CustomModelsImpl method getCustomModelConstraints.

@Override
public CollectionWithPagingInfo<CustomModelConstraint> getCustomModelConstraints(String modelName, Parameters parameters) {
    CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    Collection<ConstraintDefinition> constraintDefinitions = modelDef.getModelDefinedConstraints();
    // TODO Should we support paging?
    Paging paging = Paging.DEFAULT;
    List<CustomModelConstraint> customModelConstraints = convertToCustomModelConstraints(constraintDefinitions);
    return CollectionWithPagingInfo.asPaged(paging, customModelConstraints, false, constraintDefinitions.size());
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) Paging(org.alfresco.rest.framework.resource.parameters.Paging) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Example 8 with CustomModelConstraint

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

the class CustomModelsImpl method createCustomModel.

@Override
public CustomModel createCustomModel(M2Model m2Model) {
    // Check the current user is authorised to import the custom model
    validateCurrentUser();
    validateImportedM2Model(m2Model);
    CompiledModel compiledModel = null;
    try {
        compiledModel = customModelService.compileModel(m2Model);
    } catch (CustomModelConstraintException mce) {
        throw new ConstraintViolatedException(mce.getMessage());
    } catch (InvalidCustomModelException iex) {
        throw new InvalidArgumentException(iex.getMessage());
    }
    ModelDefinition modelDefinition = compiledModel.getModelDefinition();
    CustomModel customModel = new CustomModel();
    customModel.setName(modelDefinition.getName().getLocalName());
    customModel.setAuthor(modelDefinition.getAuthor());
    customModel.setDescription(modelDefinition.getDescription(dictionaryService));
    customModel.setStatus(ModelStatus.DRAFT);
    NamespaceDefinition nsd = modelDefinition.getNamespaces().iterator().next();
    customModel.setNamespaceUri(nsd.getUri());
    customModel.setNamespacePrefix(nsd.getPrefix());
    List<CustomType> customTypes = convertToCustomTypes(compiledModel.getTypes(), false);
    List<CustomAspect> customAspects = convertToCustomAspects(compiledModel.getAspects(), false);
    List<ConstraintDefinition> constraintDefinitions = CustomModelDefinitionImpl.removeInlineConstraints(compiledModel);
    List<CustomModelConstraint> customModelConstraints = convertToCustomModelConstraints(constraintDefinitions);
    customModel.setTypes(customTypes);
    customModel.setAspects(customAspects);
    customModel.setConstraints(customModelConstraints);
    return createCustomModelImpl(customModel, false);
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) InvalidCustomModelException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) CustomModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException) CustomModel(org.alfresco.rest.api.model.CustomModel) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) CompiledModel(org.alfresco.repo.dictionary.CompiledModel) ModelDefinition(org.alfresco.service.cmr.dictionary.ModelDefinition) CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) NamespaceDefinition(org.alfresco.service.cmr.dictionary.NamespaceDefinition) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint)

Example 9 with CustomModelConstraint

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

the class TestCustomConstraint method testCreateListConstraintInvalid.

@Test
public void testCreateListConstraintInvalid() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelConstraintInvalid" + System.currentTimeMillis();
    final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    // Create aspect
    String aspectName = "testAspect" + System.currentTimeMillis();
    createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
    // Update the Aspect by adding property
    CustomAspect aspectPayload = new CustomAspect();
    aspectPayload.setName(aspectName);
    final String aspectPropName = "testAspect1Prop" + System.currentTimeMillis();
    CustomModelProperty aspectProp = new CustomModelProperty();
    aspectProp.setName(aspectPropName);
    aspectProp.setTitle("property title");
    aspectProp.setDataType("d:int");
    // Create LIST constraint
    String inlineListConstraintName = "testListConstraint" + System.currentTimeMillis();
    CustomModelConstraint inlineListConstraint = new CustomModelConstraint();
    inlineListConstraint.setName(inlineListConstraintName);
    inlineListConstraint.setType("LIST");
    inlineListConstraint.setTitle("test List title");
    inlineListConstraint.setDescription("test List desc");
    // Create the List constraint's parameters
    List<CustomModelNamedValue> parameters = new ArrayList<>(3);
    // text list value, but the the property data type is d:int
    parameters.add(buildNamedValue("allowedValues", null, "a", "b", "c"));
    parameters.add(buildNamedValue("sorted", "false"));
    // Add the parameters into the constraint
    inlineListConstraint.setParameters(parameters);
    // Add inline constraint
    aspectProp.setConstraints(Arrays.asList(inlineListConstraint));
    List<CustomModelProperty> props = new ArrayList<>(1);
    props.add(aspectProp);
    aspectPayload.setProperties(props);
    // Try to create the property - Invalid LIST values
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 400);
    // Test d:double LIST values with d:int property data type
    parameters = new ArrayList<>(3);
    // double list value, but the the property data type is d:int
    parameters.add(buildNamedValue("allowedValues", null, "1.0", "2.0", "3.0"));
    parameters.add(buildNamedValue("sorted", "false"));
    // Add the parameters into the constraint
    inlineListConstraint.setParameters(parameters);
    // Add inline constraint
    aspectProp.setConstraints(Arrays.asList(inlineListConstraint));
    props = new ArrayList<>(1);
    props.add(aspectProp);
    aspectPayload.setProperties(props);
    // Try to create the property - Invalid LIST values
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 400);
}
Also used : CustomModelNamedValue(org.alfresco.rest.api.model.CustomModelNamedValue) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ArrayList(java.util.ArrayList) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) Test(org.junit.Test)

Example 10 with CustomModelConstraint

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

the class TestCustomConstraint method testPropDefaultValueWithConstraintRef.

@Test
public void testPropDefaultValueWithConstraintRef() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelConstraintRef" + System.currentTimeMillis();
    final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    {
        // Create List constraint
        String listConstraintName = "testListConstraint" + System.currentTimeMillis();
        CustomModelConstraint listConstraint = new CustomModelConstraint();
        listConstraint.setName(listConstraintName);
        listConstraint.setType("LIST");
        // Create the List constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(3);
        // list value
        parameters.add(buildNamedValue("allowedValues", null, "London", "Paris", "New York"));
        parameters.add(buildNamedValue("sorted", "false"));
        // Add the parameters into the constraint
        listConstraint.setParameters(parameters);
        // Create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(listConstraint), 201);
        // Retrieve the created List constraint
        HttpResponse response = getSingle("cmm/" + modelName + "/constraints", listConstraintName, 200);
        CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
        // Create aspect
        String aspectName = "testAspect" + System.currentTimeMillis();
        createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
        // Update the Aspect by adding property
        CustomAspect aspectPayload = new CustomAspect();
        aspectPayload.setName(aspectName);
        final String aspectPropName = "testAspect1Prop" + System.currentTimeMillis();
        CustomModelProperty aspectProp = new CustomModelProperty();
        aspectProp.setName(aspectPropName);
        aspectProp.setTitle("property with LIST constraint ref");
        aspectProp.setDataType("d:text");
        // Not in the list
        aspectProp.setDefaultValue("Berlin");
        // constrain ref
        aspectProp.setConstraintRefs(Arrays.asList(returnedConstraint.getPrefixedName()));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(aspectProp);
        aspectPayload.setProperties(props);
        // Try to create the property - constraint violation
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 409);
    }
    {
        // Create MINMAX constraint
        String minMaxConstraintName = "testMinMaxConstraint" + System.currentTimeMillis();
        CustomModelConstraint minMaxConstraint = new CustomModelConstraint();
        minMaxConstraint.setName(minMaxConstraintName);
        minMaxConstraint.setType("MINMAX");
        // Create the MinMax constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("maxValue", "100"));
        parameters.add(buildNamedValue("minValue", "50"));
        // Add the parameters into the constraint
        minMaxConstraint.setParameters(parameters);
        // Create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(minMaxConstraint), 201);
        // Retrieve the created MinMax constraint
        HttpResponse response = getSingle("cmm/" + modelName + "/constraints", minMaxConstraintName, 200);
        CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
        // Create type
        String typeName = "testType1" + System.currentTimeMillis();
        createTypeAspect(CustomType.class, modelName, typeName, "test type1 title", "test type1 Desc", "cm:content");
        // Update the Type by adding property
        CustomType typePayload = new CustomType();
        typePayload.setName(typeName);
        String typePropName = "testType1Prop1" + System.currentTimeMillis();
        CustomModelProperty typeProp = new CustomModelProperty();
        typeProp.setName(typePropName);
        typeProp.setTitle("property with MINMAX constraint ref");
        typeProp.setDataType("d:int");
        // Not in the defined range [50,100]
        typeProp.setDefaultValue("35");
        // constrain ref
        typeProp.setConstraintRefs(Arrays.asList(returnedConstraint.getPrefixedName()));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(typeProp);
        typePayload.setProperties(props);
        // Try to create the property - constraint violation
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 409);
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) CustomAspect(org.alfresco.rest.api.model.CustomAspect) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) Test(org.junit.Test)

Aggregations

CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)14 CustomAspect (org.alfresco.rest.api.model.CustomAspect)10 ArrayList (java.util.ArrayList)9 CustomModelProperty (org.alfresco.rest.api.model.CustomModelProperty)9 Test (org.junit.Test)9 CustomType (org.alfresco.rest.api.model.CustomType)7 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)6 List (java.util.List)5 CustomModel (org.alfresco.rest.api.model.CustomModel)5 CustomModelNamedValue (org.alfresco.rest.api.model.CustomModelNamedValue)5 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)3 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)3 ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)3 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)3 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 CustomModelConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException)2 InvalidCustomModelException (org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException)2 QName (org.alfresco.service.namespace.QName)2