Search in sources :

Example 6 with CustomModelDefinition

use of org.alfresco.service.cmr.dictionary.CustomModelDefinition in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method getCustomAspect.

@Override
public CustomAspect getCustomAspect(String modelName, String aspectName, Parameters parameters) {
    if (aspectName == null) {
        throw new InvalidArgumentException(ASPECT_NAME_NULL_ERR);
    }
    final CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    QName aspectQname = QName.createQName(modelDef.getName().getNamespaceURI(), aspectName);
    AspectDefinition customAspectDef = customModelService.getCustomAspect(aspectQname);
    if (customAspectDef == null) {
        throw new EntityNotFoundException(aspectName);
    }
    // Check if inherited properties have been requested
    boolean includeInheritedProps = hasSelectProperty(parameters, SELECT_ALL_PROPS);
    return convertToCustomAspect(customAspectDef, includeInheritedProps);
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)

Example 7 with CustomModelDefinition

use of org.alfresco.service.cmr.dictionary.CustomModelDefinition in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method getCustomType.

@Override
public CustomType getCustomType(String modelName, String typeName, Parameters parameters) {
    if (typeName == null) {
        throw new InvalidArgumentException(TYPE_NAME_NULL_ERR);
    }
    final CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    QName typeQname = QName.createQName(modelDef.getName().getNamespaceURI(), typeName);
    TypeDefinition customTypeDef = customModelService.getCustomType(typeQname);
    if (customTypeDef == null) {
        throw new EntityNotFoundException(typeName);
    }
    // Check if inherited properties have been requested
    boolean includeInheritedProps = hasSelectProperty(parameters, SELECT_ALL_PROPS);
    return convertToCustomType(customTypeDef, includeInheritedProps);
}
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) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition)

Example 8 with CustomModelDefinition

use of org.alfresco.service.cmr.dictionary.CustomModelDefinition in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method updateCustomModel.

@Override
public CustomModel updateCustomModel(String modelName, CustomModel model, Parameters parameters) {
    // Check the current user is authorised to update the custom model
    validateCurrentUser();
    // Check to see if the model exists
    ModelDetails existingModelDetails = new ModelDetails(getCustomModelImpl(modelName));
    CustomModel existingModel = existingModelDetails.getModel();
    // the other properties should be untouched)
    if (hasSelectProperty(parameters, SELECT_STATUS)) {
        ModelStatus status = model.getStatus();
        if (status == null) {
            throw new InvalidArgumentException("cmm.rest_api.model_status_null");
        }
        try {
            if (ModelStatus.ACTIVE.equals(status)) {
                customModelService.activateCustomModel(modelName);
            } else {
                customModelService.deactivateCustomModel(modelName);
            }
            // update the model's status
            existingModel.setStatus(status);
            return existingModel;
        } catch (CustomModelConstraintException mce) {
            throw new ConstraintViolatedException(mce.getMessage());
        } catch (Exception ex) {
            throw new ApiException(ex.getMessage(), ex);
        }
    } else {
        if (model.getName() != null && !(existingModel.getName().equals(model.getName()))) {
            throw new InvalidArgumentException("cmm.rest_api.model_name_cannot_update");
        }
        existingModel.setNamespaceUri(model.getNamespaceUri());
        final boolean isNamespacePrefixChanged = !(existingModel.getNamespacePrefix().equals(model.getNamespacePrefix()));
        if (isNamespacePrefixChanged) {
            // Change types' and aspects' parents as well as the property constraint's Ref namespace prefix
            replacePrefix(existingModelDetails.getTypes(), existingModel.getNamespacePrefix(), model.getNamespacePrefix());
            replacePrefix(existingModelDetails.getAspects(), existingModel.getNamespacePrefix(), model.getNamespacePrefix());
        }
        existingModel.setNamespacePrefix(model.getNamespacePrefix());
        existingModel.setAuthor(model.getAuthor());
        existingModel.setDescription(model.getDescription());
        CustomModelDefinition modelDef = updateModel(existingModelDetails, "cmm.rest_api.model_update_failure");
        return new CustomModel(modelDef);
    }
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ModelStatus(org.alfresco.rest.api.model.CustomModel.ModelStatus) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) CustomModel(org.alfresco.rest.api.model.CustomModel) CustomModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException) 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) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Example 9 with CustomModelDefinition

use of org.alfresco.service.cmr.dictionary.CustomModelDefinition in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method getCustomTypes.

@Override
public CollectionWithPagingInfo<CustomType> getCustomTypes(String modelName, Parameters parameters) {
    CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    Collection<TypeDefinition> typeDefinitions = modelDef.getTypeDefinitions();
    // TODO Should we support paging?
    Paging paging = Paging.DEFAULT;
    List<CustomType> customTypes = convertToCustomTypes(typeDefinitions, false);
    return CollectionWithPagingInfo.asPaged(paging, customTypes, false, typeDefinitions.size());
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) Paging(org.alfresco.rest.framework.resource.parameters.Paging) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition)

Example 10 with CustomModelDefinition

use of org.alfresco.service.cmr.dictionary.CustomModelDefinition in project alfresco-remote-api by Alfresco.

the class TestCustomTypeAspect method testCreateAspectsAndTypes_ExistingModel.

@Test
public void testCreateAspectsAndTypes_ExistingModel() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModel" + System.currentTimeMillis();
    Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    final String aspectName = "testAspect1" + System.currentTimeMillis();
    {
        // Add aspect
        CustomAspect aspect = new CustomAspect();
        aspect.setName(aspectName);
        setRequestContext(nonAdminUserName);
        // Try to create aspect as a non Admin user
        post("cmm/" + modelName + "/aspects", RestApiUtil.toJsonAsString(aspect), 403);
        setRequestContext(customModelAdmin);
        // Set the aspect's parent with a type name!
        aspect.setParentName("cm:content");
        // Try to create an invalid aspect as a Model Administrator
        post("cmm/" + modelName + "/aspects", RestApiUtil.toJsonAsString(aspect), 409);
        // Create aspect as a Model Administrator
        aspect.setParentName(null);
        post("cmm/" + modelName + "/aspects", RestApiUtil.toJsonAsString(aspect), 201);
        // Create the aspect again - duplicate name
        post("cmm/" + modelName + "/aspects", RestApiUtil.toJsonAsString(aspect), 409);
        // Retrieve the created aspect
        HttpResponse response = getSingle("cmm/" + modelName + "/aspects", aspect.getName(), 200);
        CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
        compareCustomTypesAspects(aspect, returnedAspect, "prefixedName");
        assertEquals(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + aspectName, returnedAspect.getPrefixedName());
    }
    String typeName = "testType" + System.currentTimeMillis();
    {
        // Add type
        CustomType type = new CustomType();
        type.setName(typeName);
        type.setDescription("test type Desc");
        type.setTitle("test type title");
        type.setParentName("cm:content");
        setRequestContext(nonAdminUserName);
        // Try to create type as a non Admin user
        post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type), 403);
        setRequestContext(customModelAdmin);
        // Set the type's parent with an aspect name!
        type.setParentName("cm:titled");
        // Try to create an invalid type as a Model Administrator
        post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type), 409);
        // Create type as a Model Administrator
        type.setParentName("cm:content");
        post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type), 201);
        // Create the type again - duplicate name
        post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type), 409);
        // Retrieve the created type
        HttpResponse response = getSingle("cmm/" + modelName + "/types", type.getName(), 200);
        CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        compareCustomTypesAspects(type, returnedType, "prefixedName");
        assertEquals(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typeName, returnedType.getPrefixedName());
        // for now this is the simplest way to check for the imported namespaces
        CustomModelDefinition modelDef = getModelDefinition(modelName);
        assertNotNull(modelDef);
        Collection<NamespaceDefinition> importedNamespaces = modelDef.getImportedNamespaces();
        assertTrue(hasNamespaceUri(importedNamespaces, "http://www.alfresco.org/model/content/1.0"));
        assertTrue(hasNamespacePrefix(importedNamespaces, "cm"));
    }
    // Existing name
    {
        // Add aspect
        CustomAspect aspect = new CustomAspect();
        // Set the aspect name with an existing type name. The model
        // cannot have a type and an aspect with the same name.
        aspect.setName(typeName);
        post("cmm/" + modelName + "/aspects", RestApiUtil.toJsonAsString(aspect), 409);
        CustomType type = new CustomType();
        // Set the type name with an existing aspect name
        type.setName(aspectName);
        type.setParentName("cm:content");
        post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type), 409);
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) CustomAspect(org.alfresco.rest.api.model.CustomAspect) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)10 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)5 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)5 Paging (org.alfresco.rest.framework.resource.parameters.Paging)4 CustomModel (org.alfresco.rest.api.model.CustomModel)3 QName (org.alfresco.service.namespace.QName)3 CustomAspect (org.alfresco.rest.api.model.CustomAspect)2 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)2 CustomType (org.alfresco.rest.api.model.CustomType)2 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)2 AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)2 ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)2 CustomModelException (org.alfresco.service.cmr.dictionary.CustomModelException)2 ActiveModelConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException)2 CustomModelConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException)2 InvalidCustomModelException (org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException)2 ModelDoesNotExistException (org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException)2 ModelExistsException (org.alfresco.service.cmr.dictionary.CustomModelException.ModelExistsException)2