Search in sources :

Example 1 with CustomModelConstraint

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

the class TestCustomConstraint method testCreateMinMaxConstraintInvalid.

@Test
public void testCreateMinMaxConstraintInvalid() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelMinMaxInvalid" + 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:text");
    String minMaxConstraintName = "testMinMaxConstraint" + System.currentTimeMillis();
    CustomModelConstraint minMaxConstraint = new CustomModelConstraint();
    minMaxConstraint.setType("MINMAX");
    minMaxConstraint.setName(minMaxConstraintName);
    minMaxConstraint.setTitle("test MinMax title");
    minMaxConstraint.setDescription("test MinMax desc");
    // Create the MinMax constraint's parameters
    List<CustomModelNamedValue> parameters = new ArrayList<>(2);
    parameters.add(buildNamedValue("maxValue", "100.0"));
    parameters.add(buildNamedValue("minValue", "0.0"));
    // Add the parameters into the constraint
    minMaxConstraint.setParameters(parameters);
    // Add inline constraint
    aspectProp.setConstraints(Arrays.asList(minMaxConstraint));
    List<CustomModelProperty> props = new ArrayList<>(1);
    props.add(aspectProp);
    aspectPayload.setProperties(props);
    // Try to create constraint as a Model Administrator
    // MINMAX constraint can only be used with numeric data type.
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 400);
    // Change type
    aspectProp.setDataType("d:datetime");
    // MINMAX constraint can only be used with numeric data type.
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 400);
    // SHA-1126
    {
        // Change type
        aspectProp.setDataType("d:double");
        parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("maxValue", "0.0"));
        parameters.add(buildNamedValue("minValue", "-5.0"));
        // Add the parameters into the constraint
        minMaxConstraint.setParameters(parameters);
        // Add inline constraint
        aspectProp.setConstraints(Arrays.asList(minMaxConstraint));
        props = new ArrayList<>(1);
        props.add(aspectProp);
        aspectPayload.setProperties(props);
        // Maximum value of the MINMAX constraint must be a positive nonzero value.
        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 2 with CustomModelConstraint

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

the class TestCustomConstraint method testCreateInlineConstraint.

@Test
public void testCreateInlineConstraint() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelInlineConstraint" + System.currentTimeMillis();
    final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    String regExConstraintName = "testInlineFileNameRegEx" + System.currentTimeMillis();
    {
        // Create RegEx constraint
        CustomModelConstraint inlineRegExConstraint = new CustomModelConstraint();
        inlineRegExConstraint.setName(regExConstraintName);
        inlineRegExConstraint.setType("REGEX");
        // Create the inline RegEx constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("expression", "(.*[\\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)"));
        parameters.add(buildNamedValue("requiresMatch", "false"));
        // Add the parameters into the constraint
        inlineRegExConstraint.setParameters(parameters);
        // Create aspect
        String aspectName = "testAspect1" + 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 = "testAspect1Prop1" + System.currentTimeMillis();
        CustomModelProperty aspectProp = new CustomModelProperty();
        aspectProp.setName(aspectPropName);
        aspectProp.setTitle("property title");
        aspectProp.setDataType("d:text");
        // Add inline constraint
        aspectProp.setConstraints(Arrays.asList(inlineRegExConstraint));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(aspectProp);
        aspectPayload.setProperties(props);
        // Create the property
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 200);
        // Retrieve all the model's constraints
        Paging paging = getPaging(0, Integer.MAX_VALUE);
        HttpResponse response = getAll("cmm/" + modelName + "/constraints", paging, 200);
        List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
        assertEquals("Inline constraints should not be included with the model defined constraints.", 0, constraints.size());
        // Retrieve the updated aspect
        response = getSingle("cmm/" + modelName + "/aspects", aspectName, 200);
        CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
        // Check the aspect's added property
        assertEquals(1, returnedAspect.getProperties().size());
        CustomModelProperty customModelProperty = returnedAspect.getProperties().get(0);
        assertEquals(aspectPropName, customModelProperty.getName());
        assertEquals(0, customModelProperty.getConstraintRefs().size());
        List<CustomModelConstraint> inlineConstraints = customModelProperty.getConstraints();
        assertEquals(1, inlineConstraints.size());
        compareCustomModelConstraints(inlineRegExConstraint, inlineConstraints.get(0), "prefixedName");
    }
    // Create inline and referenced constraint
    {
        // Create RegEx constraint
        CustomModelConstraint regExConstraint = new CustomModelConstraint();
        // duplicate name
        regExConstraint.setName(regExConstraintName);
        regExConstraint.setType("REGEX");
        regExConstraint.setTitle("test RegEx title");
        // Create the RegEx constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("expression", "(.*[\\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)"));
        parameters.add(buildNamedValue("requiresMatch", "false"));
        // Add the parameters into the constraint
        regExConstraint.setParameters(parameters);
        // Try to create constraint as a Model Administrator
        // duplicate name
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 409);
        String newRegExConstraintName = "testFileNameRegEx" + System.currentTimeMillis();
        regExConstraint.setName(newRegExConstraintName);
        // Create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 201);
        // Retrieve the created RegEx constraint
        HttpResponse response = getSingle("cmm/" + modelName + "/constraints", newRegExConstraintName, 200);
        CustomModelConstraint returnedRegExConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
        // Create inline anonymous LENGTH constraint
        CustomModelConstraint inlineAnonymousLengthConstraint = new CustomModelConstraint();
        inlineAnonymousLengthConstraint.setType("LENGTH");
        inlineAnonymousLengthConstraint.setTitle("test Length title");
        // Create the Length constraint's parameters
        parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("maxLength", "256"));
        parameters.add(buildNamedValue("minLength", "0"));
        // Add the parameters into the constraint
        inlineAnonymousLengthConstraint.setParameters(parameters);
        // Create type
        String typeName = "testType1" + System.currentTimeMillis();
        CustomType type = 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 title");
        typeProp.setDataType("d:int");
        // Constraint Ref
        typeProp.setConstraintRefs(Arrays.asList(returnedRegExConstraint.getPrefixedName()));
        // inline constraint
        typeProp.setConstraints(Arrays.asList(inlineAnonymousLengthConstraint));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(typeProp);
        typePayload.setProperties(props);
        // Try to create the property - LENGTH constraint can only be used with textual data type
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 400);
        typeProp.setDataType("d:double");
        // CTry to create the property - LENGTH constraint can only be used with textual data type
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 400);
        typeProp.setDataType("d:text");
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 200);
        // Retrieve the updated type
        response = getSingle("cmm/" + modelName + "/types", type.getName(), 200);
        CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        // Check the type's added property
        assertEquals(1, returnedType.getProperties().size());
        CustomModelProperty customModelProperty = returnedType.getProperties().get(0);
        assertEquals(typePropName, customModelProperty.getName());
        assertEquals(1, customModelProperty.getConstraintRefs().size());
        assertEquals(returnedRegExConstraint.getPrefixedName(), customModelProperty.getConstraintRefs().get(0));
        assertEquals(1, customModelProperty.getConstraints().size());
        // M2PropertyDefinition will add a name
        assertNotNull(customModelProperty.getConstraints().get(0).getName());
        compareCustomModelConstraints(inlineAnonymousLengthConstraint, customModelProperty.getConstraints().get(0), "prefixedName", "name");
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ArrayList(java.util.ArrayList) 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)

Example 3 with CustomModelConstraint

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

the class TestCustomProperty method testDeleteProperty.

@Test
public void testDeleteProperty() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelDeleteProp" + System.currentTimeMillis();
    Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    /*
         * Create aspect and update it by adding two properties
         */
    String aspectName = "testAspect1" + System.currentTimeMillis();
    createTypeAspect(CustomAspect.class, modelName, aspectName, null, null, null);
    // Update the Aspect by adding property - property one
    CustomAspect aspectPayload = new CustomAspect();
    aspectPayload.setName(aspectName);
    String aspectPropNameOne = "testAspect1Prop1" + System.currentTimeMillis();
    CustomModelProperty aspectPropOne = new CustomModelProperty();
    aspectPropOne.setName(aspectPropNameOne);
    aspectPropOne.setTitle("aspect property one title");
    aspectPropOne.setMultiValued(true);
    List<CustomModelProperty> props = new ArrayList<>(1);
    props.add(aspectPropOne);
    aspectPayload.setProperties(props);
    // create property one
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 200);
    // Update the Aspect by adding another property - property two
    aspectPayload = new CustomAspect();
    aspectPayload.setName(aspectName);
    String aspectPropNameTwo = "testAspect1Prop2" + System.currentTimeMillis();
    CustomModelProperty aspectPropTwo = new CustomModelProperty();
    aspectPropTwo.setName(aspectPropNameTwo);
    aspectPropTwo.setTitle("aspect property two title");
    aspectPropTwo.setMandatory(true);
    aspectPropTwo.setDataType("d:int");
    aspectPropTwo.setDefaultValue("1");
    props = new ArrayList<>(1);
    props.add(aspectPropTwo);
    aspectPayload.setProperties(props);
    // create property two
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 200);
    // Retrieve the updated aspect
    HttpResponse response = getSingle("cmm/" + modelName + "/aspects", aspectName, 200);
    CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
    // Check the aspect's added properties
    assertEquals(2, returnedAspect.getProperties().size());
    /*
         * Create type and update it by adding two properties
         */
    String typeName = "testType1" + System.currentTimeMillis();
    createTypeAspect(CustomType.class, modelName, typeName, "test type1 title", null, "cm:content");
    // Update the Type by adding property - property one
    CustomType typePayload = new CustomType();
    typePayload.setName(typeName);
    String typePropNameOne = "testType1Prop1" + System.currentTimeMillis();
    CustomModelProperty typePropOne = new CustomModelProperty();
    typePropOne.setName(typePropNameOne);
    typePropOne.setTitle("type property one title");
    props = new ArrayList<>(1);
    props.add(typePropOne);
    typePayload.setProperties(props);
    // create property one
    put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 200);
    // Update the Type by adding another property - property two
    typePayload = new CustomType();
    typePayload.setName(typeName);
    // Create inline MINMAX constraint
    CustomModelConstraint inlineMinMaxConstraint = new CustomModelConstraint();
    inlineMinMaxConstraint.setType("MINMAX");
    inlineMinMaxConstraint.setTitle("test MINMAX title");
    // Create the MinMax constraint's parameters
    List<CustomModelNamedValue> parameters = new ArrayList<>(2);
    parameters.add(buildNamedValue("maxValue", "100.0"));
    parameters.add(buildNamedValue("minValue", "0.0"));
    // Add the parameters into the constraint
    inlineMinMaxConstraint.setParameters(parameters);
    String typePropNameTwo = "testType1Prop2" + System.currentTimeMillis();
    CustomModelProperty typePropTwo = new CustomModelProperty();
    typePropTwo.setName(typePropNameTwo);
    typePropTwo.setTitle("type property two title");
    typePropTwo.setDataType("d:int");
    // add the inline constraint
    typePropTwo.setConstraints(Arrays.asList(inlineMinMaxConstraint));
    props = new ArrayList<>(1);
    props.add(typePropTwo);
    typePayload.setProperties(props);
    // create property one
    put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 200);
    // Retrieve the updated type
    response = getSingle("cmm/" + modelName + "/types", typeName, 200);
    CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
    // Check the type's added properties
    assertEquals(2, returnedType.getProperties().size());
    // Delete aspect's property one - model is inactive
    {
        final String deletePropOneAspectQS = getPropDeleteUpdateQS(aspectPropNameOne, true);
        // Try to delete propertyOne from aspect
        // missing payload
        put("cmm/" + modelName + "/aspects", aspectName, null, deletePropOneAspectQS, 400);
        CustomAspect deletePropAspectPayload = new CustomAspect();
        // missing aspect name
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropOneAspectQS, 400);
        setRequestContext(nonAdminUserName);
        deletePropAspectPayload.setName(aspectName);
        // unauthorised
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropOneAspectQS, 403);
        setRequestContext(customModelAdmin);
        // Delete as a Model Administrator
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropOneAspectQS, 200);
        // Check the property has been deleted
        response = getSingle("cmm/" + modelName + "/aspects", aspectName, 200);
        returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
        assertEquals(1, returnedAspect.getProperties().size());
        assertFalse("Property one should have been deleted.", aspectPropNameOne.equals(returnedAspect.getProperties().get(0).getName()));
        // Not found
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropOneAspectQS, 404);
    }
    // Delete type's property two - model is inactive
    {
        final String deletePropTwoTypeQS = getPropDeleteUpdateQS(typePropNameTwo, true);
        // Try to delete propertyOne from type
        // missing payload
        put("cmm/" + modelName + "/types", typeName, null, deletePropTwoTypeQS, 400);
        CustomType deletePropTypePayload = new CustomType();
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropTwoTypeQS, // missing type name
        400);
        setRequestContext(nonAdminUserName);
        deletePropTypePayload.setName(typeName);
        // unauthorised
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropTwoTypeQS, 403);
        setRequestContext(customModelAdmin);
        // Delete as a Model Administrator
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropTwoTypeQS, 200);
        // Check the property has been deleted
        response = getSingle("cmm/" + modelName + "/types", typeName, 200);
        returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        assertEquals(1, returnedType.getProperties().size());
        assertFalse("Property two should have been deleted.", typePropNameTwo.equals(returnedType.getProperties().get(0).getName()));
        // Not found
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropTwoTypeQS, 404);
    }
    /*
         * APPSREPO-59: delete the last property of a Type / Aspect for an active model
         */
    // Activate the model
    CustomModel statusPayload = new CustomModel();
    statusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
    // Delete aspect's property two - model is active
    {
        setRequestContext(customModelAdmin);
        final String deletePropTwoAspectQS = getPropDeleteUpdateQS(aspectPropNameTwo, true);
        CustomAspect deletePropAspectPayload = new CustomAspect();
        deletePropAspectPayload.setName(aspectName);
        // Delete as a Model Administrator
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropTwoAspectQS, 200);
        // Check the property has been deleted
        response = getSingle("cmm/" + modelName + "/aspects", aspectName, 200);
        returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
        assertEquals("Property two should have been deleted.", 0, returnedAspect.getProperties().size());
        // Not found
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropTwoAspectQS, 404);
    }
    // Delete type's property one - model is active
    {
        final String deletePropOneTypeQS = getPropDeleteUpdateQS(typePropNameOne, true);
        CustomType deletePropTypePayload = new CustomType();
        deletePropTypePayload.setName(typeName);
        // Delete as a Model Administrator
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropOneTypeQS, 200);
        // Check the property has been deleted
        response = getSingle("cmm/" + modelName + "/types", typeName, 200);
        returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        assertEquals("Property one should have been deleted.", 0, returnedType.getProperties().size());
        // Not found
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropOneTypeQS, 404);
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) CustomModel(org.alfresco.rest.api.model.CustomModel) CustomModelNamedValue(org.alfresco.rest.api.model.CustomModelNamedValue) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) Test(org.junit.Test)

Example 4 with CustomModelConstraint

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

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

the class CustomModelsImpl method getCustomModelConstraint.

@Override
public CustomModelConstraint getCustomModelConstraint(String modelName, String constraintName, Parameters parameters) {
    if (constraintName == null) {
        throw new InvalidArgumentException(CONSTRAINT_NAME_NULL_ERR);
    }
    final CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    QName constraintQname = QName.createQName(modelDef.getName().getNamespaceURI(), constraintName);
    ConstraintDefinition constraintDef = customModelService.getCustomConstraint(constraintQname);
    if (constraintDef == null) {
        throw new EntityNotFoundException(constraintName);
    }
    return new CustomModelConstraint(constraintDef, dictionaryService);
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

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