Search in sources :

Example 6 with CustomModel

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

the class BaseCustomModelApiTest method createCustomModel.

protected CustomModel createCustomModel(String modelName, Pair<String, String> namespacePair, ModelStatus status, String desc, String author) throws Exception {
    CustomModel customModel = new CustomModel();
    customModel.setName(modelName);
    customModel.setNamespaceUri(namespacePair.getFirst());
    customModel.setNamespacePrefix(namespacePair.getSecond());
    customModel.setDescription(desc);
    customModel.setStatus(status);
    customModel.setAuthor(author);
    // Create the model as a Model Administrator
    HttpResponse response = post("cmm", RestApiUtil.toJsonAsString(customModel), 201);
    CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
    if (author == null) {
        // ignore 'author' in the comparison
        compareCustomModels(customModel, returnedModel, "author");
    } else {
        compareCustomModels(customModel, returnedModel);
    }
    return customModel;
}
Also used : HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) CustomModel(org.alfresco.rest.api.model.CustomModel)

Example 7 with CustomModel

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

the class CustomModelsImpl method createCustomModelImpl.

private CustomModel createCustomModelImpl(CustomModel model, boolean basicModelOnly) {
    M2Model m2Model = null;
    if (basicModelOnly) {
        m2Model = convertToM2Model(model, null, null, null);
    } else {
        m2Model = convertToM2Model(model, model.getTypes(), model.getAspects(), model.getConstraints());
    }
    boolean activate = ModelStatus.ACTIVE.equals(model.getStatus());
    try {
        CustomModelDefinition modelDefinition = customModelService.createCustomModel(m2Model, activate);
        return new CustomModel(modelDefinition);
    } catch (ModelExistsException me) {
        throw new ConstraintViolatedException(me.getMessage());
    } catch (CustomModelConstraintException ncx) {
        throw new ConstraintViolatedException(ncx.getMessage());
    } catch (InvalidCustomModelException iex) {
        throw new InvalidArgumentException(iex.getMessage());
    } catch (Exception e) {
        throw new ApiException("cmm.rest_api.model_invalid", e);
    }
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ModelExistsException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelExistsException) InvalidCustomModelException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException) M2Model(org.alfresco.repo.dictionary.M2Model) 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 8 with CustomModel

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

the class CustomModelsImpl method getCustomModels.

@Override
public CollectionWithPagingInfo<CustomModel> getCustomModels(Parameters parameters) {
    Paging paging = parameters.getPaging();
    PagingRequest pagingRequest = Util.getPagingRequest(paging);
    PagingResults<CustomModelDefinition> results = customModelService.getCustomModels(pagingRequest);
    Integer totalItems = results.getTotalResultCount().getFirst();
    List<CustomModelDefinition> page = results.getPage();
    List<CustomModel> models = new ArrayList<>(page.size());
    for (CustomModelDefinition modelDefinition : page) {
        models.add(new CustomModel(modelDefinition));
    }
    return CollectionWithPagingInfo.asPaged(paging, models, results.hasMoreItems(), (totalItems == null ? null : totalItems.intValue()));
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) Paging(org.alfresco.rest.framework.resource.parameters.Paging) ArrayList(java.util.ArrayList) PagingRequest(org.alfresco.query.PagingRequest) CustomModel(org.alfresco.rest.api.model.CustomModel)

Example 9 with CustomModel

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

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

Aggregations

CustomModel (org.alfresco.rest.api.model.CustomModel)23 Test (org.junit.Test)17 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)16 CustomAspect (org.alfresco.rest.api.model.CustomAspect)9 CustomType (org.alfresco.rest.api.model.CustomType)9 ArrayList (java.util.ArrayList)8 CustomModelProperty (org.alfresco.rest.api.model.CustomModelProperty)6 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)5 List (java.util.List)4 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)4 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)4 CustomModelNamedValue (org.alfresco.rest.api.model.CustomModelNamedValue)3 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)3 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)3 CustomModelConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException)3 InvalidCustomModelException (org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException)3 M2Model (org.alfresco.repo.dictionary.M2Model)2 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)2