From ccd650217bae81a5a7ea3e41fd99e3410da3717d Mon Sep 17 00:00:00 2001 From: Thomas Georgios Giannos Date: Fri, 24 Nov 2023 17:19:20 +0200 Subject: [PATCH] Fix on deleting duplicates from DmpDescriptionTemplates, delete all but one --- .../DmpDatasetProfileMigrationService.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) 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 2e4eb1d35..89e035643 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 @@ -1,35 +1,23 @@ package eu.old.eudat.migration; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.core.JsonProcessingException; import eu.eudat.commons.JsonHandlingService; import eu.eudat.commons.XmlHandlingService; import eu.eudat.commons.enums.IsActive; -import eu.eudat.commons.enums.ReferenceFieldDataType; -import eu.eudat.commons.enums.ReferenceSourceType; -import eu.eudat.commons.enums.ReferenceType; -import eu.eudat.commons.types.reference.FieldEntity; -import eu.eudat.data.DescriptionTemplateEntity; import eu.eudat.data.DmpBlueprintEntity; import eu.eudat.data.DmpDescriptionTemplateEntity; -import eu.eudat.data.ReferenceEntity; -import eu.eudat.model.DmpBlueprint; -import eu.eudat.query.DescriptionTemplateQuery; import eu.eudat.query.DmpBlueprintQuery; import eu.eudat.query.DmpDescriptionTemplateQuery; import eu.old.eudat.data.dao.entities.DmpDatasetProfileDao; -import eu.old.eudat.data.dao.entities.ResearcherDao; import eu.old.eudat.data.entities.DMPDatasetProfile; -import eu.old.eudat.data.entities.Researcher; import eu.old.eudat.logic.services.operations.DatabaseRepository; -import eu.old.eudat.queryable.QueryableList; -import gr.cite.tools.data.query.Paging; 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.CriteriaDelete; import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.Root; import jakarta.xml.bind.JAXBException; @@ -40,6 +28,7 @@ import eu.eudat.commons.types.dmpblueprint.DefinitionEntity; import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; +import java.security.SecureRandom; import java.time.Instant; import java.util.*; @@ -144,6 +133,8 @@ public class DmpDatasetProfileMigrationService { List resultList = this.entityManager.createQuery(criteria).getResultList(); List duplicatesList = resultList.stream().filter(x -> (long) x.get(3) > 1).toList(); + CriteriaDelete delete = b.createCriteriaDelete(DmpDescriptionTemplateEntity.class); + Root root1 = delete.from(DmpDescriptionTemplateEntity.class); for (Tuple duplicate : duplicatesList) { List duplicateEntities = dmpDescriptionTemplateQuery .dmpIds((UUID) duplicate.get(0)) @@ -151,8 +142,11 @@ public class DmpDatasetProfileMigrationService { .sectionIds((UUID) duplicate.get(2)) .collect(); - for (int index = 0; index < duplicateEntities.size() - 1; index++) - this.entityManager.remove(duplicateEntities.get(index)); + List toDelete = new ArrayList<>(duplicateEntities.stream().map(DmpDescriptionTemplateEntity::getId).toList()); + int random = new SecureRandom().nextInt(0, toDelete.size()); + toDelete.remove(random); + delete.where(root1.get("id").in(toDelete)); + this.entityManager.createQuery(delete).executeUpdate(); } entityManager.flush();