migrate organization reference

This commit is contained in:
Efstratios Giannopoulos 2023-11-21 18:33:26 +02:00
parent 08ea46f79b
commit a7f70568bb
3 changed files with 89 additions and 6 deletions

View File

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

View File

@ -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<DMP> 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;

View File

@ -25,7 +25,11 @@
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</logger>
<logger name="eu.old.eudat" level="DEBUG" additivity="false">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</logger>
<root level="info" additivity="false">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>