use of com.haulmont.cuba.core.app.importexport.EntityImportException in project cuba by cuba-platform.
the class EntitiesControllerManager method createEntity.
public CreatedEntityInfo createEntity(String entityJson, String entityName, String modelVersion) {
String transformedEntityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION);
MetaClass metaClass = restControllerUtils.getMetaClass(transformedEntityName);
checkCanCreateEntity(metaClass);
entityJson = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION, entityJson);
Entity entity;
try {
entity = entitySerializationAPI.entityFromJson(entityJson, metaClass);
} catch (Exception e) {
throw new RestAPIException("Cannot deserialize an entity from JSON", "", HttpStatus.BAD_REQUEST, e);
}
EntityImportView entityImportView = entityImportViewBuilderAPI.buildFromJson(entityJson, metaClass);
Collection<Entity> importedEntities;
try {
importedEntities = entityImportExportService.importEntities(Collections.singletonList(entity), entityImportView, true);
} catch (EntityImportException e) {
throw new RestAPIException("Entity creation failed", e.getMessage(), HttpStatus.BAD_REQUEST, e);
}
// if many entities were created (because of @Composition references) we must find the main entity
return getMainEntityInfo(importedEntities, metaClass, modelVersion);
}
use of com.haulmont.cuba.core.app.importexport.EntityImportException in project cuba by cuba-platform.
the class EntitiesControllerManager method updateEntity.
public CreatedEntityInfo updateEntity(String entityJson, String entityName, String entityId, String modelVersion) {
String transformedEntityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION);
MetaClass metaClass = restControllerUtils.getMetaClass(transformedEntityName);
checkCanUpdateEntity(metaClass);
Object id = getIdFromString(entityId, metaClass);
LoadContext loadContext = new LoadContext(metaClass).setId(id);
@SuppressWarnings("unchecked") Entity existingEntity = dataManager.load(loadContext);
checkEntityIsNotNull(transformedEntityName, entityId, existingEntity);
entityJson = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION, entityJson);
Entity entity;
try {
entity = entitySerializationAPI.entityFromJson(entityJson, metaClass);
} catch (Exception e) {
throw new RestAPIException("Cannot deserialize an entity from JSON", "", HttpStatus.BAD_REQUEST, e);
}
if (entity instanceof BaseGenericIdEntity) {
// noinspection unchecked
((BaseGenericIdEntity) entity).setId(id);
}
EntityImportView entityImportView = entityImportViewBuilderAPI.buildFromJson(entityJson, metaClass);
Collection<Entity> importedEntities;
try {
importedEntities = entityImportExportService.importEntities(Collections.singletonList(entity), entityImportView, true);
importedEntities.forEach(it -> restControllerUtils.applyAttributesSecurity(it));
} catch (EntityImportException e) {
throw new RestAPIException("Entity update failed", e.getMessage(), HttpStatus.BAD_REQUEST, e);
}
// the main entity that will be returned
return getMainEntityInfo(importedEntities, metaClass, modelVersion);
}
Aggregations