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