From 578d7054121e7c1b5d048de7273f7c3b20889814 Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Tue, 28 May 2024 11:00:05 +0300 Subject: [PATCH] Bug fix --- .../AffiliationCacheService.java | 14 ++++----- .../description/FieldCommonModelBuilder.java | 30 +++++++++++++++++-- .../builder/description/FieldBuilder.java | 4 +-- .../description/DescriptionServiceImpl.java | 5 +++- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/authorization/authorizationcontentresolver/AffiliationCacheService.java b/backend/core/src/main/java/org/opencdmp/authorization/authorizationcontentresolver/AffiliationCacheService.java index 5870f1e87..858adfce6 100644 --- a/backend/core/src/main/java/org/opencdmp/authorization/authorizationcontentresolver/AffiliationCacheService.java +++ b/backend/core/src/main/java/org/opencdmp/authorization/authorizationcontentresolver/AffiliationCacheService.java @@ -1,14 +1,12 @@ package org.opencdmp.authorization.authorizationcontentresolver; +import gr.cite.tools.cache.CacheService; import org.opencdmp.authorization.AffiliatedResource; import org.opencdmp.convention.ConventionService; -import gr.cite.tools.cache.CacheService; -import gr.cite.tools.exception.MyApplicationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; -import java.util.List; import java.util.Locale; import java.util.UUID; @@ -35,7 +33,7 @@ public class AffiliationCacheService extends CacheService referenceItemsMap = FieldType.isReferenceType(fieldType) ? this.collectReferences(data) : null; + Map tagItemsMap = FieldType.isTagType(fieldType) ? this.collectTags(data) : null; List> models = new ArrayList<>(); for (FieldEntity d : data) { @@ -95,13 +99,20 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder()); - for (UUID referenceId : d.getTextListValue().stream().map(UUID::fromString).toList()){ + for (UUID referenceId : d.getTextListValue().stream().map(UUID::fromString).distinct().toList()){ if (referenceItemsMap.containsKey(referenceId)) m.getReferences().add(referenceItemsMap.get(referenceId)); } } + if (FieldType.isTagType(fieldType) && tagItemsMap != null && d.getTextListValue() != null && !d.getTextListValue().isEmpty()) { + m.setTextListValue(new ArrayList<>()); + for (UUID tagId : d.getTextListValue().stream().map(UUID::fromString).distinct().toList()){ + if (tagItemsMap.containsKey(tagId)) m.getTextListValue().add(tagItemsMap.get(tagId)); + } + } if (FieldType.UPLOAD.equals(fieldType) && d.getTextValue() != null && !d.getTextValue().isEmpty()) { try { @@ -144,6 +155,21 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder collectTags(List data){ + if (data.isEmpty()) + return null; + this.logger.debug("checking related - {}", TagEntity.class.getSimpleName()); + + List tagIds = data.stream().map(FieldEntity::getTextListValue).filter(Objects::nonNull).flatMap(List::stream).filter(x-> !this.conventionService.isNullOrEmpty(x)).map(UUID::fromString).distinct().collect(Collectors.toList()); + List existingTags = this.queryFactory.query(TagQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).disableTracking().ids(tagIds).collectAs(new BaseFieldSet().ensure(Tag._id).ensure(Tag._label)); + + Map itemMap = new HashMap<>(); + for (UUID tag : tagIds){ + existingTags.stream().filter(x -> x.getId().equals(tag)).findFirst().ifPresent(existingTag -> itemMap.put(tag, existingTag.getId().toString())); + } + return itemMap; + } + private Map collectReferences(List data) throws MyApplicationException { if (data.isEmpty()) return null; @@ -151,7 +177,7 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder itemMap; - ReferenceQuery q = this.queryFactory.query(ReferenceQuery.class).disableTracking().authorize(this.authorize).ids(data.stream().filter(x-> x.getTextListValue() != null).map(FieldEntity::getTextListValue).flatMap(List::stream).filter(x-> !this.conventionService.isNullOrEmpty(x)).map(UUID::fromString).distinct().collect(Collectors.toList())); + ReferenceQuery q = this.queryFactory.query(ReferenceQuery.class).disableTracking().authorize(this.authorize).ids(data.stream().map(FieldEntity::getTextListValue).filter(Objects::nonNull).flatMap(List::stream).filter(x-> !this.conventionService.isNullOrEmpty(x)).map(UUID::fromString).distinct().collect(Collectors.toList())); itemMap = this.builderFactory.builder(ReferenceCommonModelBuilder.class).authorize(this.authorize).asForeignKey(q, ReferenceEntity::getId); return itemMap; diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/description/FieldBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/description/FieldBuilder.java index 09abd34c8..ccbe5d2f8 100644 --- a/backend/core/src/main/java/org/opencdmp/model/builder/description/FieldBuilder.java +++ b/backend/core/src/main/java/org/opencdmp/model/builder/description/FieldBuilder.java @@ -98,14 +98,14 @@ public class FieldBuilder extends BaseBuilder { } if (!referenceFields.isEmpty() && FieldType.isReferenceType(fieldType) && referenceItemsMap != null && d.getTextListValue() != null && !d.getTextListValue().isEmpty()) { m.setReferences(new ArrayList<>()); - for (UUID referenceId : d.getTextListValue().stream().map(UUID::fromString).toList()){ + for (UUID referenceId : d.getTextListValue().stream().map(UUID::fromString).distinct().toList()){ if (referenceItemsMap.containsKey(referenceId)) m.getReferences().add(referenceItemsMap.get(referenceId)); } } if (!tagFields.isEmpty() && FieldType.isTagType(fieldType) && tagsItemsMap != null && d.getTextListValue() != null && !d.getTextListValue().isEmpty()) { m.setTags(new ArrayList<>()); - for (UUID tagId : d.getTextListValue().stream().map(UUID::fromString).toList()){ + for (UUID tagId : d.getTextListValue().stream().map(UUID::fromString).distinct().toList()){ if (tagsItemsMap.containsKey(tagId)) m.getTags().add(tagsItemsMap.get(tagId)); } } diff --git a/backend/core/src/main/java/org/opencdmp/service/description/DescriptionServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/description/DescriptionServiceImpl.java index ce3dc76a1..b3653aec1 100644 --- a/backend/core/src/main/java/org/opencdmp/service/description/DescriptionServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/description/DescriptionServiceImpl.java @@ -644,7 +644,10 @@ public class DescriptionServiceImpl implements DescriptionService { fieldPersist.getReferences().add(fieldPersist.getReference()); } if (!this.conventionService.isListNullOrEmpty(fieldPersist.getReferences())) { + List usedReferences = new ArrayList<>(); for (ReferencePersist referencePersist : fieldPersist.getReferences()) { + if (usedReferences.contains(referencePersist.getId())) continue; + usedReferences.add(referencePersist.getId()); DescriptionReferencePersist descriptionReferencePersist = new DescriptionReferencePersist(); descriptionReferencePersist.setData(new DescriptionReferenceDataPersist()); descriptionReferencePersist.getData().setFieldId(fieldId); @@ -1053,7 +1056,7 @@ public class DescriptionServiceImpl implements DescriptionService { } } else if (FieldType.isTagType(fieldType) && fieldEntity != null ) { - if (!this.conventionService.isListNullOrEmpty(data.getTextListValue()) && !this.conventionService.isListNullOrEmpty(references)){ + if (!this.conventionService.isListNullOrEmpty(data.getTextListValue())){ List tagIdsInField = data.getTextListValue().stream().filter(x -> this.conventionService.isValidGuid(UUID.fromString(x))).toList().stream().map(UUID::fromString).collect(Collectors.toList()); if (!this.conventionService.isListNullOrEmpty(tagIdsInField)){ List tagsInField = this.queryFactory.query(TagQuery.class).isActive(IsActive.Active).ids(tagIdsInField).disableTracking().authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).collect();