Skip to content

Commit

Permalink
Fixed creation of entity array from JSON. jmix-projects/jmix-rest#49
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeniy Bulgakov committed Apr 28, 2021
1 parent 9a4fb95 commit 268c8ab
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/io/jmix/core/EntityImportExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public interface EntityImportExport {
*/
Collection<Object> importEntities(Collection<Object> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReferenceInfo> 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<Object> 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<ConstraintViolation<Object>> 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<ReferenceInfo> referenceInfoList = new ArrayList<>();
Expand Down

0 comments on commit 268c8ab

Please sign in to comment.