diff --git a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/OrganizationMigrationService.java b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/OrganizationMigrationService.java new file mode 100644 index 000000000..d4191503b --- /dev/null +++ b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/OrganizationMigrationService.java @@ -0,0 +1,72 @@ +package eu.old.eudat.migration; + +import eu.eudat.commons.enums.IsActive; +import eu.eudat.commons.enums.ReferenceSourceType; +import eu.eudat.commons.enums.ReferenceType; +import eu.eudat.data.ReferenceEntity; +import eu.old.eudat.data.dao.entities.OrganisationDao; +import eu.old.eudat.data.entities.DMP; +import eu.old.eudat.data.entities.Organisation; +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.util.ArrayList; +import java.util.List; + +@Service +public class OrganizationMigrationService { + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(OrganizationMigrationService.class)); + + private static final int PageSize = 100; + private static final String InternalReferenceSource = "Internal"; + private final DatabaseRepository databaseRepository; + private final EntityManager entityManager; + public OrganizationMigrationService(DatabaseRepository databaseRepository, EntityManager entityManager) { + this.databaseRepository = databaseRepository; + this.entityManager = entityManager; + } + + public void migrate(){ + OrganisationDao organisationDao = databaseRepository.getOrganisationDao(); + long total = organisationDao.asQueryable().count(); + logger.debug("Migrate Organisation Total : " + total); + int page = 0; + + List items; + do { + items = organisationDao.asQueryable().orderBy((builder, root) -> builder.desc(root.get("created"))).skip(page * PageSize).take(PageSize).toList(); + if (items != null && !items.isEmpty()) { + logger.debug("Migrate Organisation " + page * PageSize + " of " + total); + for (Organisation item : items) { + eu.old.eudat.models.data.dmp.Organisation model = new eu.old.eudat.models.data.dmp.Organisation().fromDataModel(item); + + if (item.getReference() == null || !item.getReference().contains(":")){ + logger.debug("Can not migrate Organisation " + item.getId()); + continue; + } + String[] referenceParts = item.getReference().split(":", 2); + boolean isInternal = referenceParts[0].equals(InternalReferenceSource); + + ReferenceEntity data = new ReferenceEntity(); + data.setId(item.getId()); + data.setLabel(item.getLabel()); + data.setIsActive(DMP.DMPStatus.fromInteger(item.getStatus()).equals(DMP.DMPStatus.DELETED) ? IsActive.Inactive : IsActive.Active); + data.setType(ReferenceType.Organizations); + data.setCreatedAt(item.getCreated().toInstant()); + data.setUpdatedAt(item.getModified().toInstant()); + data.setReference(referenceParts[1]); + data.setAbbreviation(item.getAbbreviation()); + data.setSource(referenceParts[0]); + data.setSourceType(isInternal? ReferenceSourceType.Internal : ReferenceSourceType.External); + this.entityManager.persist(data); + } + this.entityManager.flush(); + + page++; + } + } while (items != null && !items.isEmpty()); + } +} 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 2fad20958..b5f97e34e 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 @@ -2,13 +2,10 @@ package eu.old.eudat.publicapi.migration; import eu.eudat.commons.enums.*; import eu.eudat.data.DmpEntity; -import eu.eudat.data.ReferenceEntity; -import eu.old.eudat.data.dao.criteria.DataManagementPlanCriteria; -import eu.old.eudat.data.dao.criteria.OrganisationCriteria; -import eu.old.eudat.data.dao.entities.DMPDao; import eu.old.eudat.data.entities.DMP; import eu.old.eudat.data.entities.Organisation; import eu.old.eudat.logic.services.operations.DatabaseRepository; +import eu.old.eudat.migration.OrganizationMigrationService; import eu.old.eudat.models.data.dmp.DataManagementPlan; import eu.old.eudat.queryable.QueryableList; import io.swagger.annotations.Api; @@ -32,9 +29,12 @@ public class MigrationController { @PersistenceContext private final EntityManager entityManager; - public MigrationController(DatabaseRepository databaseRepository, EntityManager entityManager) { + private final OrganizationMigrationService organizationMigrationService; + + public MigrationController(DatabaseRepository databaseRepository, EntityManager entityManager, OrganizationMigrationService organizationMigrationService) { this.databaseRepository = databaseRepository; this.entityManager = entityManager; + this.organizationMigrationService = organizationMigrationService; } public QueryableList dmpQueryableList() { @@ -119,6 +119,13 @@ public class MigrationController { return "Migrated dmps"; } + @GetMapping("organizations/migrate") + @Transactional + public boolean migrateOrganizations() { + this.organizationMigrationService.migrate(); + return true; + } + public static class DmpMigrationCollectedInfo { public DmpEntity dmpEntity; diff --git a/dmp-migration-tool/web/src/main/resources/logging/logback-devel.xml b/dmp-migration-tool/web/src/main/resources/logging/logback-devel.xml index ddf6798c2..df18ee6fc 100644 --- a/dmp-migration-tool/web/src/main/resources/logging/logback-devel.xml +++ b/dmp-migration-tool/web/src/main/resources/logging/logback-devel.xml @@ -25,7 +25,11 @@ - + + + + +