Merge branch 'dmp-refactoring' of https://code-repo.d4science.org/MaDgiK-CITE/argos into dmp-refactoring

This commit is contained in:
amentis 2023-11-24 15:12:26 +02:00
commit 3d18ed1fa6
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 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() {