Search in sources :

Example 6 with CustomAspect

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

the class TestCustomTypeAspect method testUpdateAspectsTypes.

@Test
public void testUpdateAspectsTypes() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModeEditAspectType" + System.currentTimeMillis();
    final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    // Test update aspect
    {
        // Create aspect
        String aspectName = "testAspect" + System.currentTimeMillis();
        createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
        // Update the aspect
        CustomAspect aspectPayload = new CustomAspect();
        aspectPayload.setDescription(null);
        aspectPayload.setTitle("title modified");
        aspectPayload.setParentName("cm:titled");
        setRequestContext(nonAdminUserName);
        // Try to update the aspect as a non Admin user
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 403);
        setRequestContext(customModelAdmin);
        // Modify the name
        aspectPayload.setName(aspectName + "Modified");
        // Try to update the aspect as a Model Administrator
        // Note: aspect/type name cannot be modified
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 404);
        aspectPayload.setName(aspectName);
        // Update the aspect as a Model Administrator
        HttpResponse response = put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 200);
        CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
        compareCustomTypesAspects(aspectPayload, returnedAspect, "prefixedName");
        // Update the aspect with an invalid parent
        aspectPayload.setParentName("cm:titled" + System.currentTimeMillis());
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 409);
        // Activate the model
        CustomModel statusPayload = new CustomModel();
        statusPayload.setStatus(ModelStatus.ACTIVE);
        put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
        // Remove the aspect's parent
        // Note: cannot update the parent of an ACTIVE type/aspect.
        aspectPayload.setParentName(null);
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 409);
        statusPayload = new CustomModel();
        statusPayload.setStatus(ModelStatus.DRAFT);
        put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
        // now update the aspect's parent - model is inactive
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 200);
    }
    // Test update type
    {
        // Create type
        String typeName = "testType" + System.currentTimeMillis();
        createTypeAspect(CustomType.class, modelName, typeName, "title", "desc", "cm:content");
        // Add property
        CustomType addPropertyPayload = new CustomType();
        addPropertyPayload.setName(typeName);
        String typePropName = "testTypeProp1" + System.currentTimeMillis();
        CustomModelProperty typeProp = new CustomModelProperty();
        typeProp.setName(typePropName);
        typeProp.setTitle("property title");
        typeProp.setDataType("d:text");
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(typeProp);
        addPropertyPayload.setProperties(props);
        // Create the property
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(addPropertyPayload), SELECT_PROPS_QS, 200);
        // Update the type
        CustomType typePayload = new CustomType();
        typePayload.setDescription("desc modified");
        typePayload.setTitle("title modified");
        setRequestContext(nonAdminUserName);
        // Try to update the type as a non Admin user
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 403);
        setRequestContext(customModelAdmin);
        // Modify the name
        typePayload.setName(typeName + "Modified");
        // Try to update the type as a Model Administrator
        // Note: type/type name cannot be modified
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 404);
        typePayload.setName(typeName);
        // Update the type as a Model Administrator
        HttpResponse response = put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 200);
        CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        assertEquals(typePayload.getDescription(), returnedType.getDescription());
        assertEquals(typePayload.getTitle(), returnedType.getTitle());
        // Check that properties are unmodified
        assertNotNull(returnedType.getProperties());
        assertEquals(1, returnedType.getProperties().size());
        assertEquals(typePropName, returnedType.getProperties().iterator().next().getName());
        // Update the type with an invalid parent
        typePayload.setParentName("cm:folder" + System.currentTimeMillis());
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 409);
        // Activate the model
        CustomModel statusPayload = new CustomModel();
        statusPayload.setStatus(ModelStatus.ACTIVE);
        put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
        // Remove the type's parent
        // Note: cannot update the parent of an ACTIVE type/type.
        typePayload.setParentName("cm:folder");
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 409);
        statusPayload = new CustomModel();
        statusPayload.setStatus(ModelStatus.DRAFT);
        put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
        // now update the type's parent - model is inactive
        response = put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 200);
        returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        assertEquals(typePayload.getParentName(), returnedType.getParentName());
    }
}
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) CustomModel(org.alfresco.rest.api.model.CustomModel) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) Test(org.junit.Test)

Example 7 with CustomAspect

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

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

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

the class CustomModelsImpl method deleteCustomAspect.

@Override
public void deleteCustomAspect(String modelName, String aspectName) {
    // Check the current user is authorised to delete the custom model's aspect
    validateCurrentUser();
    if (aspectName == null) {
        throw new InvalidArgumentException(ASPECT_NAME_NULL_ERR);
    }
    ModelDetails existingModelDetails = new ModelDetails(getCustomModelImpl(modelName));
    if (existingModelDetails.isActive()) {
        throw new ConstraintViolatedException("cmm.rest_api.aspect_cannot_delete");
    }
    Map<String, CustomAspect> allAspects = transformToMap(existingModelDetails.getAspects(), toNameFunction());
    CustomAspect aspectToBeDeleted = allAspects.get(aspectName);
    if (aspectToBeDeleted == null) {
        throw new EntityNotFoundException(aspectName);
    }
    // Validate aspect's dependency
    validateTypeAspectDelete(allAspects.values(), aspectToBeDeleted.getPrefixedName());
    // Remove the validated aspect
    allAspects.remove(aspectName);
    existingModelDetails.setAspects(new ArrayList<>(allAspects.values()));
    updateModel(existingModelDetails, "cmm.rest_api.aspect_delete_failure");
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) CustomAspect(org.alfresco.rest.api.model.CustomAspect) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 10 with CustomAspect

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

the class CustomModelsImpl method getCustomAspects.

@Override
public CollectionWithPagingInfo<CustomAspect> getCustomAspects(String modelName, Parameters parameters) {
    CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    Collection<AspectDefinition> aspectDefinitions = modelDef.getAspectDefinitions();
    // TODO Should we support paging?
    Paging paging = Paging.DEFAULT;
    List<CustomAspect> customAspects = convertToCustomAspects(aspectDefinitions, false);
    return CollectionWithPagingInfo.asPaged(paging, customAspects, false, aspectDefinitions.size());
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) Paging(org.alfresco.rest.framework.resource.parameters.Paging) CustomAspect(org.alfresco.rest.api.model.CustomAspect) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition)

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