From 0934774b86989ea002218128ecfe219d6dd9dc1d Mon Sep 17 00:00:00 2001 From: Thomas Georgios Giannos Date: Fri, 24 Nov 2023 15:11:43 +0200 Subject: [PATCH] Adding UserDMP migration --- .../migration/DmpUserMigrationService.java | 66 +++++++++++++++++++ .../migration/MigrationController.java | 11 +++- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DmpUserMigrationService.java diff --git a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DmpUserMigrationService.java b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DmpUserMigrationService.java new file mode 100644 index 000000000..fb0ebaa33 --- /dev/null +++ b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DmpUserMigrationService.java @@ -0,0 +1,66 @@ +package eu.old.eudat.migration; + +import eu.eudat.commons.enums.DmpUserRole; +import eu.eudat.commons.enums.IsActive; +import eu.eudat.data.DmpUserEntity; +import eu.old.eudat.data.dao.entities.UserDmpDao; +import eu.old.eudat.data.entities.UserDMP; +import eu.old.eudat.logic.services.operations.DatabaseRepository; +import gr.cite.tools.logging.LoggerService; +import jakarta.persistence.EntityManager; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import java.time.Instant; +import java.util.List; + +@Service +public class DmpUserMigrationService { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpUserMigrationService.class)); + + private final DatabaseRepository databaseRepository; + + private final EntityManager entityManager; + + private static final int PageSize = 500; + + private static final boolean TestMode = false; + + public DmpUserMigrationService(DatabaseRepository databaseRepository, EntityManager entityManager) { + this.databaseRepository = databaseRepository; + this.entityManager = entityManager; + } + + public void migrate() { + UserDmpDao userDmpDao = databaseRepository.getUserDmpDao(); + long total = userDmpDao.asQueryable().count(); + logger.debug("Migrate UserDMP Total : " + total); + int page = 0; + + List items; + do { + items = userDmpDao.asQueryable().orderBy((builder, root) -> builder.asc(root.get("ID"))).skip(page * PageSize).take(PageSize).toList(); + if (items != null && !items.isEmpty()) { + logger.debug("Migrate UserDMP " + page * PageSize + " of " + total); + for (UserDMP item : items) { + entityManager.detach(item); + + DmpUserEntity data = new DmpUserEntity(); + data.setId(item.getId()); + data.setDmp(item.getDmp().getId()); + data.setUserId(item.getUser().getId()); + data.setRole(DmpUserRole.of(item.getRole().shortValue())); + data.setCreatedAt(Instant.now()); + data.setUpdatedAt(Instant.now()); + data.setIsActive(IsActive.Active); + this.entityManager.persist(data); + } + this.entityManager.flush(); + + page++; + } + } while (items != null && !items.isEmpty() && !TestMode); + } + +} diff --git a/dmp-migration-tool/web/src/main/java/eu/old/eudat/publicapi/migration/MigrationController.java b/dmp-migration-tool/web/src/main/java/eu/old/eudat/publicapi/migration/MigrationController.java index 5940961a5..dbcd031c2 100644 --- a/dmp-migration-tool/web/src/main/java/eu/old/eudat/publicapi/migration/MigrationController.java +++ b/dmp-migration-tool/web/src/main/java/eu/old/eudat/publicapi/migration/MigrationController.java @@ -33,6 +33,7 @@ public class MigrationController { private final DmpDatasetProfileMigrationService dmpDatasetProfileMigrationService; private final OrganizationMigrationService organizationMigrationService; private final ReferenceMigrationService referenceMigrationService; + private final DmpUserMigrationService dmpUserMigrationService; public MigrationController( DmpMigrationService dmpMigrationService, @@ -47,7 +48,7 @@ public class MigrationController { ResearcherMigrationService researcherMigrationService, ServiceMigrationService serviceMigrationService, OrganizationMigrationService organizationMigrationService, - ReferenceMigrationService referenceMigrationService) { + ReferenceMigrationService referenceMigrationService, DmpUserMigrationService dmpUserMigrationService) { this.dmpMigrationService = dmpMigrationService; this.datasetMigrationService = datasetMigrationService; this.dmpDatasetProfileMigrationService = dmpDatasetProfileMigrationService; @@ -61,6 +62,7 @@ public class MigrationController { this.serviceMigrationService = serviceMigrationService; this.organizationMigrationService = organizationMigrationService; this.referenceMigrationService = referenceMigrationService; + this.dmpUserMigrationService = dmpUserMigrationService; } @GetMapping("dmps") @@ -106,6 +108,13 @@ public class MigrationController { return true; } + @GetMapping("dmp-users") + @Transactional + public boolean migrateDmpUsers() { + this.dmpUserMigrationService.migrate(); + return true; + } + @GetMapping("dataset-references") @Transactional public boolean migrateDatasetReferences() {