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;
}
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);
}
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());
}
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");
}
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;
}
Aggregations