Search in sources :

Example 1 with EntityImportView

use of com.haulmont.cuba.core.app.importexport.EntityImportView 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 EntityImportView

use of com.haulmont.cuba.core.app.importexport.EntityImportView in project cuba by cuba-platform.

the class EntityInspectorBrowse method createEntityImportView.

protected EntityImportView createEntityImportView(MetaClass metaClass) {
    @SuppressWarnings("unchecked") Class<? extends Entity> javaClass = metaClass.getJavaClass();
    EntityImportView entityImportView = new EntityImportView(javaClass);
    for (MetaProperty metaProperty : metaClass.getProperties()) {
        if (!metadata.getTools().isPersistent(metaProperty))
            continue;
        switch(metaProperty.getType()) {
            case DATATYPE:
            case ENUM:
                entityImportView.addLocalProperty(metaProperty.getName());
                break;
            case ASSOCIATION:
            case COMPOSITION:
                Range.Cardinality cardinality = metaProperty.getRange().getCardinality();
                switch(cardinality) {
                    case MANY_TO_ONE:
                        entityImportView.addManyToOneProperty(metaProperty.getName(), ReferenceImportBehaviour.IGNORE_MISSING);
                        break;
                    case ONE_TO_ONE:
                        entityImportView.addOneToOneProperty(metaProperty.getName(), ReferenceImportBehaviour.IGNORE_MISSING);
                        break;
                }
                break;
            default:
                throw new IllegalStateException("unknown property type");
        }
    }
    return entityImportView;
}
Also used : EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Range(com.haulmont.chile.core.model.Range)

Example 3 with EntityImportView

use of com.haulmont.cuba.core.app.importexport.EntityImportView 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

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