Adding UserDMP migration

This commit is contained in:
Thomas Georgios Giannos 2023-11-24 15:11:43 +02:00
parent 58d224b739
commit 0934774b86
2 changed files with 76 additions and 1 deletions

View File

@ -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<UserDMP> 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);
}
}

View File

@ -33,6 +33,7 @@ public class MigrationController {
private final DmpDatasetProfileMigrationService dmpDatasetProfileMigrationService; private final DmpDatasetProfileMigrationService dmpDatasetProfileMigrationService;
private final OrganizationMigrationService organizationMigrationService; private final OrganizationMigrationService organizationMigrationService;
private final ReferenceMigrationService referenceMigrationService; private final ReferenceMigrationService referenceMigrationService;
private final DmpUserMigrationService dmpUserMigrationService;
public MigrationController( public MigrationController(
DmpMigrationService dmpMigrationService, DmpMigrationService dmpMigrationService,
@ -47,7 +48,7 @@ public class MigrationController {
ResearcherMigrationService researcherMigrationService, ResearcherMigrationService researcherMigrationService,
ServiceMigrationService serviceMigrationService, ServiceMigrationService serviceMigrationService,
OrganizationMigrationService organizationMigrationService, OrganizationMigrationService organizationMigrationService,
ReferenceMigrationService referenceMigrationService) { ReferenceMigrationService referenceMigrationService, DmpUserMigrationService dmpUserMigrationService) {
this.dmpMigrationService = dmpMigrationService; this.dmpMigrationService = dmpMigrationService;
this.datasetMigrationService = datasetMigrationService; this.datasetMigrationService = datasetMigrationService;
this.dmpDatasetProfileMigrationService = dmpDatasetProfileMigrationService; this.dmpDatasetProfileMigrationService = dmpDatasetProfileMigrationService;
@ -61,6 +62,7 @@ public class MigrationController {
this.serviceMigrationService = serviceMigrationService; this.serviceMigrationService = serviceMigrationService;
this.organizationMigrationService = organizationMigrationService; this.organizationMigrationService = organizationMigrationService;
this.referenceMigrationService = referenceMigrationService; this.referenceMigrationService = referenceMigrationService;
this.dmpUserMigrationService = dmpUserMigrationService;
} }
@GetMapping("dmps") @GetMapping("dmps")
@ -106,6 +108,13 @@ public class MigrationController {
return true; return true;
} }
@GetMapping("dmp-users")
@Transactional
public boolean migrateDmpUsers() {
this.dmpUserMigrationService.migrate();
return true;
}
@GetMapping("dataset-references") @GetMapping("dataset-references")
@Transactional @Transactional
public boolean migrateDatasetReferences() { public boolean migrateDatasetReferences() {