From 268c8abbfecf10e0fc232db816d93e0af6988ecf Mon Sep 17 00:00:00 2001 From: Evgeniy Bulgakov Date: Wed, 28 Apr 2021 09:23:11 +0400 Subject: [PATCH] Fixed creation of entity array from JSON. Haulmont/jmix-rest#49 --- .../java/io/jmix/core/EntityImportExport.java | 2 + .../importexport/EntityImportExportImpl.java | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/core/src/main/java/io/jmix/core/EntityImportExport.java b/core/src/main/java/io/jmix/core/EntityImportExport.java index 9385f270fd..b75dd4d8fe 100644 --- a/core/src/main/java/io/jmix/core/EntityImportExport.java +++ b/core/src/main/java/io/jmix/core/EntityImportExport.java @@ -111,6 +111,8 @@ public interface EntityImportExport { */ Collection importEntities(Collection entities, EntityImportPlan importPlan, boolean validate); + void fillSaveContextWithEntity(Object srcEntity, EntityImportPlan importPlan, boolean validate, SaveContext saveContext); + /** * Persists entities according to the rules, described by the {@code importPlan} parameter. If the entity is not * present in the database, it will be saved. Otherwise the fields of the existing entity that are in the {@code diff --git a/core/src/main/java/io/jmix/core/impl/importexport/EntityImportExportImpl.java b/core/src/main/java/io/jmix/core/impl/importexport/EntityImportExportImpl.java index 21fccdd2dd..accaea1c02 100644 --- a/core/src/main/java/io/jmix/core/impl/importexport/EntityImportExportImpl.java +++ b/core/src/main/java/io/jmix/core/impl/importexport/EntityImportExportImpl.java @@ -208,6 +208,57 @@ public Collection importEntities(Collection entities, EntityImportPlan importPla return importEntities(entities, importPlan, validate, false); } + @Override + public void fillSaveContextWithEntity(Object srcEntity, EntityImportPlan importPlan, boolean validate, SaveContext saveContext) { + boolean optimisticLocking = false; + List referenceInfoList = new ArrayList<>(); + if (saveContext == null) { + return; + } + saveContext.setHint("jmix.softDeletion", false); + + EntityPreconditions.checkEntityType(srcEntity); + FetchPlan fetchPlan = constructFetchPlanFromImportPlan(importPlan).build(); + + LoadContext ctx = new LoadContext<>(metadata.getClass(srcEntity.getClass())) + .setFetchPlan(fetchPlan) + .setHint("jmix.dynattr", true) + .setHint("jmix.softDeletion", false) + .setId(EntityValues.getId(srcEntity)) + .setAccessConstraints(accessConstraintsRegistry.getConstraints()); + Object dstEntity = dataManager.load(ctx); + + importEntity(srcEntity, dstEntity, importPlan, fetchPlan, saveContext, referenceInfoList, optimisticLocking); + + Set loadedEntities = new HashSet<>(); + for (ReferenceInfo referenceInfo : referenceInfoList) { + processReferenceInfo(referenceInfo, saveContext, loadedEntities); + } + + for (Object instance : saveContext.getEntitiesToSave()) { + if (!entityStates.isNew(instance)) { + if (EntityValues.isSoftDeleted(instance)) { + EntityValues.setDeletedDate(instance, null); + } + } + } + + if (validate) { + for (Object entity : saveContext.getEntitiesToSave()) { + Set> violations = validator.validate(entity, Default.class, RestApiChecks.class); + if (!violations.isEmpty()) { + throw new EntityValidationException("Entity validation failed", violations); + } + } + } + + if (!saveContext.getEntitiesToRemove().isEmpty()) { + saveContext.setHint("jmix.softDeletion", true); + } + + saveContext.setAccessConstraints(accessConstraintsRegistry.getConstraints()); + } + @Override public Collection importEntities(Collection entities, EntityImportPlan importPlan, boolean validate, boolean optimisticLocking) { List referenceInfoList = new ArrayList<>();