From 21de37e980ef5b6d88d8b4c841dc88cf1695aef2 Mon Sep 17 00:00:00 2001 From: amentis Date: Thu, 25 Apr 2024 14:23:09 +0300 Subject: [PATCH] fix buildPersist for description validate --- .../description/DescriptionServiceImpl.java | 81 +++++++++++++++++-- 1 file changed, 73 insertions(+), 8 deletions(-) diff --git a/dmp-backend/core/src/main/java/eu/eudat/service/description/DescriptionServiceImpl.java b/dmp-backend/core/src/main/java/eu/eudat/service/description/DescriptionServiceImpl.java index d11c87a4f..e3a6d5e13 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/service/description/DescriptionServiceImpl.java +++ b/dmp-backend/core/src/main/java/eu/eudat/service/description/DescriptionServiceImpl.java @@ -976,29 +976,63 @@ public class DescriptionServiceImpl implements DescriptionService { private @NotNull PropertyDefinitionPersist buildPropertyDefinitionPersist(PropertyDefinitionEntity data, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition){ PropertyDefinitionPersist persist = new PropertyDefinitionPersist(); if (data == null) return persist; + + List referenceIds = this.calculateAllReferenceIdsFromData(data, definition); + List references = null; + if (!this.conventionService.isListNullOrEmpty(referenceIds)){ + references = this.queryFactory.query(ReferenceQuery.class).ids(referenceIds).collect(); + } if (data.getFieldSets() != null && !data.getFieldSets().isEmpty()){ persist.setFieldSets(new HashMap<>()); for (String key: data.getFieldSets().keySet()) { FieldSetEntity fieldSetEntity = definition != null ? definition.getFieldSetById(key).stream().findFirst().orElse(null) : null; - persist.getFieldSets().put(key, this.buildPropertyDefinitionFieldSetPersist(data.getFieldSets().get(key), fieldSetEntity)); + persist.getFieldSets().put(key, this.buildPropertyDefinitionFieldSetPersist(data.getFieldSets().get(key), fieldSetEntity, references)); } } return persist; } - private @NotNull PropertyDefinitionFieldSetPersist buildPropertyDefinitionFieldSetPersist(PropertyDefinitionFieldSetEntity data, FieldSetEntity fieldSetEntity){ + private List calculateAllReferenceIdsFromData(PropertyDefinitionEntity data, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition){ + List referenceIds = new ArrayList<>(); + + if (data.getFieldSets() != null && !data.getFieldSets().isEmpty()){ + for (PropertyDefinitionFieldSetEntity fieldSet: data.getFieldSets().values()) { + if (!this.conventionService.isListNullOrEmpty(fieldSet.getItems())); + for (PropertyDefinitionFieldSetItemEntity item: fieldSet.getItems()) { + if (item.getFields() != null && !item.getFields().isEmpty()); + for (String key: item.getFields().keySet()) { + if (definition.getFieldById(key).getFirst() != null && FieldType.isReferenceType(definition.getFieldById(key).getFirst().getData().getFieldType())){ + if (!this.conventionService.isListNullOrEmpty(item.getFields().get(key).getTextListValue())){ + referenceIds.addAll(item.getFields().get(key).getTextListValue()); + } + } + } + } + } + } + + if (!this.conventionService.isListNullOrEmpty(referenceIds)) { + referenceIds = referenceIds.stream().distinct().collect(Collectors.toList()); + return referenceIds.stream().filter(x -> this.conventionService.isValidGuid(UUID.fromString(x))).collect(Collectors.toList()).stream().map(x -> UUID.fromString(x)).collect(Collectors.toList()); + + } + + return null; + } + + private @NotNull PropertyDefinitionFieldSetPersist buildPropertyDefinitionFieldSetPersist(PropertyDefinitionFieldSetEntity data, FieldSetEntity fieldSetEntity, List references){ PropertyDefinitionFieldSetPersist persist = new PropertyDefinitionFieldSetPersist(); if (data == null) return persist; if (!this.conventionService.isListNullOrEmpty(data.getItems())){ persist.setItems(new ArrayList<>()); for (PropertyDefinitionFieldSetItemEntity itemsPersist: data.getItems()) { - persist.getItems().add(this.buildPropertyDefinitionFieldSetItemPersist(itemsPersist, fieldSetEntity)); + persist.getItems().add(this.buildPropertyDefinitionFieldSetItemPersist(itemsPersist, fieldSetEntity, references)); } } return persist; } - private @NotNull PropertyDefinitionFieldSetItemPersist buildPropertyDefinitionFieldSetItemPersist(PropertyDefinitionFieldSetItemEntity data, FieldSetEntity fieldSetEntity){ + private @NotNull PropertyDefinitionFieldSetItemPersist buildPropertyDefinitionFieldSetItemPersist(PropertyDefinitionFieldSetItemEntity data, FieldSetEntity fieldSetEntity, List references){ PropertyDefinitionFieldSetItemPersist persist = new PropertyDefinitionFieldSetItemPersist(); if (data == null) return persist; if (data.getFields() != null && !data.getFields().isEmpty()){ @@ -1006,14 +1040,14 @@ public class DescriptionServiceImpl implements DescriptionService { persist.setComment(data.getComment()); persist.setFields(new HashMap<>()); for (String key: data.getFields().keySet()) { - eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity = fieldSetEntity != null ? fieldSetEntity.getAllField().stream().findFirst().orElse(null) : null; - persist.getFields().put(key, this.buildFieldPersist(data.getFields().get(key), fieldEntity)); + eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity = fieldSetEntity != null ? fieldSetEntity.getFieldById(key).stream().findFirst().orElse(null) : null; + persist.getFields().put(key, this.buildFieldPersist(data.getFields().get(key), fieldEntity, references)); } } return persist; } - private @NotNull FieldPersist buildFieldPersist(FieldEntity data, eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity) { + private @NotNull FieldPersist buildFieldPersist(FieldEntity data, eu.eudat.commons.types.descriptiontemplate.FieldEntity fieldEntity, List references) { FieldType fieldType = fieldEntity != null && fieldEntity.getData() != null ? fieldEntity.getData().getFieldType() : FieldType.FREE_TEXT; FieldPersist persist = new FieldPersist(); @@ -1023,7 +1057,17 @@ public class DescriptionServiceImpl implements DescriptionService { else if (FieldType.isTextListType(fieldType)) persist.setTextListValue(data.getTextListValue()); else if (FieldType.isDateType(fieldType)) persist.setDateValue(persist.getDateValue()); else if (FieldType.isExternalIdentifierType(fieldType) && data.getExternalIdentifier() != null) persist.setExternalIdentifier(this.buildExternalIdentifierPersist(data.getExternalIdentifier())); - else if (FieldType.isReferenceType(fieldType) && fieldEntity != null ) persist.setTextListValue(data.getTextListValue()); + else if (FieldType.isReferenceType(fieldType) && fieldEntity != null ) { + if (!this.conventionService.isListNullOrEmpty(data.getTextListValue()) && !this.conventionService.isListNullOrEmpty(references)){ + List referenceIdsInField = data.getTextListValue().stream().filter(x -> this.conventionService.isValidGuid(UUID.fromString(x))).collect(Collectors.toList()).stream().map(x -> UUID.fromString(x)).collect(Collectors.toList()); + if (!this.conventionService.isListNullOrEmpty(referenceIdsInField)){ + List referencesInField = references.stream().filter(x -> referenceIdsInField.contains(x.getId())).collect(Collectors.toList()); + if (!this.conventionService.isListNullOrEmpty(referencesInField)){ + persist.setReferences(this.buildReferencePersist(referencesInField)); + } + } + } + } return persist; } @@ -1036,4 +1080,25 @@ public class DescriptionServiceImpl implements DescriptionService { persist.setType(data.getType()); return persist; } + + private @NotNull List buildReferencePersist(List referenceEntities){ + List referencesPersist = new ArrayList<>(); + if (this.conventionService.isListNullOrEmpty(referenceEntities)) return referencesPersist; + + for (ReferenceEntity entity: referenceEntities) { + ReferencePersist persist = new ReferencePersist(); + persist.setId(entity.getId()); + persist.setLabel(entity.getLabel()); + persist.setDescription(entity.getDescription()); + persist.setReference(entity.getReference()); + persist.setSource(entity.getSource()); + persist.setSourceType(entity.getSourceType()); + persist.setTypeId(entity.getTypeId()); + persist.setAbbreviation(entity.getAbbreviation()); + + referencesPersist.add(persist); + } + + return referencesPersist; + } }