From 8ee149f0948d66c93136fe3d56536a4c50de35cb Mon Sep 17 00:00:00 2001 From: Thomas Georgios Giannos Date: Fri, 24 Nov 2023 16:46:30 +0200 Subject: [PATCH] Fixing duplicate selection from DmpDescriptionTemplates --- .../query/DmpDescriptionTemplateQuery.java | 35 ++++++++++--- .../DmpDatasetProfileMigrationService.java | 49 +++++++++---------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/dmp-backend/core/src/main/java/eu/eudat/query/DmpDescriptionTemplateQuery.java b/dmp-backend/core/src/main/java/eu/eudat/query/DmpDescriptionTemplateQuery.java index eaf4c5ee4..0f8a5c9f9 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/query/DmpDescriptionTemplateQuery.java +++ b/dmp-backend/core/src/main/java/eu/eudat/query/DmpDescriptionTemplateQuery.java @@ -38,6 +38,8 @@ public class DmpDescriptionTemplateQuery extends QueryBase descriptionTemplateGroupIds; + private Collection sectionIds; + private Collection isActives; private Collection excludedIds; @@ -90,18 +92,18 @@ public class DmpDescriptionTemplateQuery extends QueryBase values) { - this.dmpIds = values; + public DmpDescriptionTemplateQuery sectionIds(Collection values) { + this.sectionIds = values; return this; } @@ -120,6 +122,21 @@ public class DmpDescriptionTemplateQuery extends QueryBase values) { + this.dmpIds = values; + return this; + } + public DmpDescriptionTemplateQuery authorize(EnumSet values) { this.authorize = values; return this; @@ -193,6 +210,12 @@ public class DmpDescriptionTemplateQuery extends QueryBase inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpDescriptionTemplateEntity._sectionId)); + for (UUID item : this.sectionIds) + inClause.value(item); + predicates.add(inClause); + } if (this.isActives != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpDescriptionTemplateEntity._isActive)); for (IsActive item : this.isActives) diff --git a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DmpDatasetProfileMigrationService.java b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DmpDatasetProfileMigrationService.java index 83eb43d11..2e4eb1d35 100644 --- a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DmpDatasetProfileMigrationService.java +++ b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DmpDatasetProfileMigrationService.java @@ -28,6 +28,10 @@ import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.logging.LoggerService; import jakarta.persistence.EntityManager; +import jakarta.persistence.Tuple; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; import jakarta.xml.bind.JAXBException; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -127,36 +131,31 @@ public class DmpDatasetProfileMigrationService { private void removeDuplicates() { logger.debug("Checking for duplicates on DmpDescriptionTemplate table after migration"); - int page = 0; DmpDescriptionTemplateQuery dmpDescriptionTemplateQuery = this.queryFactory.query(DmpDescriptionTemplateQuery.class); long total = dmpDescriptionTemplateQuery.count(); - List dmpDescriptionTemplateEntities; - do { - logger.debug("Checking " + page * PageSize + " of " + total); - dmpDescriptionTemplateQuery.setPage(new Paging(page * PageSize, PageSize)); - dmpDescriptionTemplateEntities = dmpDescriptionTemplateQuery.collect(); + logger.debug("Record count to check: {}", total); - DmpDescriptionTemplateEntity previousEntity = null; - for(DmpDescriptionTemplateEntity currentEntity : dmpDescriptionTemplateEntities) { - if (previousEntity == null) { - previousEntity = currentEntity; - continue; - } - if (currentEntity.getDmpId().equals(previousEntity.getDmpId()) - && currentEntity.getDescriptionTemplateGroupId().equals(previousEntity.getDescriptionTemplateGroupId()) - && currentEntity.getSectionId().equals(previousEntity.getSectionId()) - ) { - logger.warn("Removing found duplicate DmpDescriptionTemplate entity ({}) after DmpDatasetProfiles migration", currentEntity.getId()); - this.entityManager.remove(currentEntity); - } else { - previousEntity = currentEntity; - } - } - entityManager.flush(); + CriteriaBuilder b = this.entityManager.getCriteriaBuilder(); + CriteriaQuery criteria = b.createQuery(Tuple.class); + Root root = criteria.from(DmpDescriptionTemplateEntity.class); + criteria.groupBy(Arrays.asList(root.get("dmp"), root.get("descriptionTemplateGroupId"), root.get("sectionId"))); + criteria.multiselect(root.get("dmp"), root.get("descriptionTemplateGroupId"), root.get("sectionId"), b.count(root)); + List resultList = this.entityManager.createQuery(criteria).getResultList(); - page++; - } while (!dmpDescriptionTemplateEntities.isEmpty() && !TestMode); + List duplicatesList = resultList.stream().filter(x -> (long) x.get(3) > 1).toList(); + for (Tuple duplicate : duplicatesList) { + List duplicateEntities = dmpDescriptionTemplateQuery + .dmpIds((UUID) duplicate.get(0)) + .descriptionTemplateGroupIds((UUID) duplicate.get(1)) + .sectionIds((UUID) duplicate.get(2)) + .collect(); + + for (int index = 0; index < duplicateEntities.size() - 1; index++) + this.entityManager.remove(duplicateEntities.get(index)); + } + + entityManager.flush(); } @JsonIgnoreProperties({"validationErrorModel"})