Search in sources :

Example 6 with CustomType

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

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

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

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

the class CustomModelsImpl method deleteCustomType.

@Override
public void deleteCustomType(String modelName, String typeName) {
    // Check the current user is authorised to delete the custom model's type
    validateCurrentUser();
    if (typeName == null) {
        throw new InvalidArgumentException(TYPE_NAME_NULL_ERR);
    }
    ModelDetails existingModelDetails = new ModelDetails(getCustomModelImpl(modelName));
    if (existingModelDetails.isActive()) {
        throw new ConstraintViolatedException("cmm.rest_api.type_cannot_delete");
    }
    Map<String, CustomType> allTypes = transformToMap(existingModelDetails.getTypes(), toNameFunction());
    CustomType typeToBeDeleted = allTypes.get(typeName);
    if (typeToBeDeleted == null) {
        throw new EntityNotFoundException(typeName);
    }
    // Validate type's dependency
    validateTypeAspectDelete(allTypes.values(), typeToBeDeleted.getPrefixedName());
    // Remove the validated type
    allTypes.remove(typeName);
    existingModelDetails.setTypes(new ArrayList<>(allTypes.values()));
    updateModel(existingModelDetails, "cmm.rest_api.type_delete_failure");
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 10 with CustomType

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

Aggregations

CustomType (org.alfresco.rest.api.model.CustomType)19 CustomAspect (org.alfresco.rest.api.model.CustomAspect)15 Test (org.junit.Test)14 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)12 ArrayList (java.util.ArrayList)9 CustomModel (org.alfresco.rest.api.model.CustomModel)9 CustomModelProperty (org.alfresco.rest.api.model.CustomModelProperty)9 List (java.util.List)7 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)7 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)3 CustomModelNamedValue (org.alfresco.rest.api.model.CustomModelNamedValue)2 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)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 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)1 M2Model (org.alfresco.repo.dictionary.M2Model)1