fix buildPersist for description validate

This commit is contained in:
amentis 2024-04-25 14:23:09 +03:00
parent 5ff5825f00
commit 21de37e980
1 changed files with 73 additions and 8 deletions

View File

@ -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<UUID> referenceIds = this.calculateAllReferenceIdsFromData(data, definition);
List<ReferenceEntity> 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<UUID> calculateAllReferenceIdsFromData(PropertyDefinitionEntity data, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition){
List<String> 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<ReferenceEntity> 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<ReferenceEntity> 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<ReferenceEntity> 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<UUID> 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<ReferenceEntity> 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<ReferencePersist> buildReferencePersist(List<ReferenceEntity> referenceEntities){
List<ReferencePersist> 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;
}
}