Search in sources :

Example 11 with CustomAspect

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

the class CustomModelsImpl method validateTypeAspectParent.

private void validateTypeAspectParent(AbstractClassModel typeAspect, CustomModel existingModel) {
    String parentPrefixedName = typeAspect.getParentName();
    if (StringUtils.isBlank(parentPrefixedName)) {
        return;
    }
    Pair<String, String> prefixLocaNamePair = splitPrefixedQName(parentPrefixedName);
    String parentPrefix = prefixLocaNamePair.getFirst();
    String parentLocalName = prefixLocaNamePair.getSecond();
    // Validate parent prefix and localName
    // We know that the values are not null, we just check against the defined RegEx
    validateName(parentPrefix, null);
    validateName(parentLocalName, null);
    final boolean isAspect = (typeAspect instanceof CustomAspect);
    ClassDefinition classDefinition = null;
    QName qname = null;
    if (existingModel.getNamespacePrefix().equals(parentPrefix)) {
        // Check for types/aspects within the model
        qname = QName.createQName(existingModel.getNamespaceUri(), parentLocalName);
        classDefinition = (isAspect) ? customModelService.getCustomAspect(qname) : customModelService.getCustomType(qname);
    } else {
        // Make sure the namespace URI and Prefix are registered
        Pair<String, String> uriPrefixPair = resolveToUriAndPrefix(parentPrefixedName);
        qname = QName.createQName(uriPrefixPair.getFirst(), parentLocalName);
        classDefinition = (isAspect) ? dictionaryService.getAspect(qname) : dictionaryService.getType(qname);
    }
    if (classDefinition == null) {
        String msgId = (isAspect) ? "cmm.rest_api.aspect_parent_not_exist" : "cmm.rest_api.type_parent_not_exist";
        throw new ConstraintViolatedException(I18NUtil.getMessage(msgId, parentPrefixedName));
    } else {
        checkCircularDependency(classDefinition.getModel(), existingModel, parentPrefixedName);
    }
}
Also used : QName(org.alfresco.service.namespace.QName) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ClassDefinition(org.alfresco.service.cmr.dictionary.ClassDefinition) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 12 with CustomAspect

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

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

the class BaseCustomModelApiTest method createTypeAspect.

protected <T extends AbstractClassModel> T createTypeAspect(Class<T> glazz, String modelName, String typeAspectName, String title, String desc, String parent) throws Exception {
    AbstractClassModel classModel = null;
    String uri = "cmm/" + modelName;
    if (glazz.equals(CustomType.class)) {
        classModel = new CustomType();
        uri += "/types";
    } else {
        classModel = new CustomAspect();
        uri += "/aspects";
    }
    classModel.setName(typeAspectName);
    classModel.setDescription(desc);
    classModel.setTitle(title);
    classModel.setParentName(parent);
    // Create type as a Model Administrator
    HttpResponse response = post(uri, RestApiUtil.toJsonAsString(classModel), 201);
    T returnedClassModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), glazz);
    compareCustomTypesAspects(classModel, returnedClassModel, "prefixedName");
    return returnedClassModel;
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) AbstractClassModel(org.alfresco.rest.api.model.AbstractClassModel) CustomAspect(org.alfresco.rest.api.model.CustomAspect) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse)

Example 14 with CustomAspect

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

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

CustomAspect (org.alfresco.rest.api.model.CustomAspect)24 Test (org.junit.Test)17 CustomType (org.alfresco.rest.api.model.CustomType)15 CustomModelProperty (org.alfresco.rest.api.model.CustomModelProperty)14 ArrayList (java.util.ArrayList)13 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)13 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)11 CustomModel (org.alfresco.rest.api.model.CustomModel)9 List (java.util.List)7 CustomModelNamedValue (org.alfresco.rest.api.model.CustomModelNamedValue)5 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)3 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)3 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 QName (org.alfresco.service.namespace.QName)2 Pair (org.alfresco.util.Pair)2 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 CompiledModel (org.alfresco.repo.dictionary.CompiledModel)1 M2Aspect (org.alfresco.repo.dictionary.M2Aspect)1