Search in sources :

Example 1 with EntityImportException

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);
}
Also used : EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView) MetaClass(com.haulmont.chile.core.model.MetaClass) EntityImportException(com.haulmont.cuba.core.app.importexport.EntityImportException) RestAPIException(com.haulmont.restapi.exception.RestAPIException) RestFilterParseException(com.haulmont.restapi.service.filter.RestFilterParseException) EntityImportException(com.haulmont.cuba.core.app.importexport.EntityImportException) RestAPIException(com.haulmont.restapi.exception.RestAPIException)

Example 2 with EntityImportException

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);
}
Also used : EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView) MetaClass(com.haulmont.chile.core.model.MetaClass) EntityImportException(com.haulmont.cuba.core.app.importexport.EntityImportException) RestAPIException(com.haulmont.restapi.exception.RestAPIException) JsonObject(com.google.gson.JsonObject) RestFilterParseException(com.haulmont.restapi.service.filter.RestFilterParseException) EntityImportException(com.haulmont.cuba.core.app.importexport.EntityImportException) RestAPIException(com.haulmont.restapi.exception.RestAPIException)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)2 EntityImportException (com.haulmont.cuba.core.app.importexport.EntityImportException)2 EntityImportView (com.haulmont.cuba.core.app.importexport.EntityImportView)2 RestAPIException (com.haulmont.restapi.exception.RestAPIException)2 RestFilterParseException (com.haulmont.restapi.service.filter.RestFilterParseException)2 JsonObject (com.google.gson.JsonObject)1