Refactor DatasetMigrationService

This commit is contained in:
Thomas Georgios Giannos 2023-11-23 15:23:49 +02:00
parent 9433beaaa6
commit f38febc22b
1 changed files with 74 additions and 50 deletions

View File

@ -2,30 +2,56 @@ package eu.old.eudat.migration;
import com.fasterxml.jackson.core.JsonProcessingException;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.XmlHandlingService;
import eu.eudat.commons.enums.*;
import eu.eudat.commons.types.dmp.DmpBlueprintValueEntity;
import eu.eudat.commons.types.dmp.DmpContactEntity;
import eu.eudat.commons.types.dmp.DmpPropertiesEntity;
import eu.eudat.commons.types.dmpblueprint.DefinitionEntity;
import eu.eudat.data.DescriptionEntity;
import eu.eudat.data.DmpBlueprintEntity;
import eu.eudat.data.DmpDescriptionTemplateEntity;
import eu.eudat.data.DmpEntity;
import eu.eudat.query.DmpBlueprintQuery;
import eu.old.eudat.data.dao.entities.DatasetDao;
import eu.old.eudat.data.entities.DMP;
import eu.old.eudat.data.entities.DMPDatasetProfile;
import eu.old.eudat.data.entities.Dataset;
import eu.old.eudat.logic.services.operations.DatabaseRepository;
import eu.old.eudat.models.data.datasetwizard.DatasetWizardModel;
import eu.old.eudat.models.data.dmp.DataManagementPlan;
import eu.old.eudat.queryable.QueryableList;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.EntityManager;
import jakarta.xml.bind.JAXBException;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
@Service
public class DatasetMigrationService {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpDatasetProfileMigrationService.class));
private final DatabaseRepository databaseRepository;
private final JsonHandlingService jsonHandlingService;
private final QueryFactory queryFactory;
private final XmlHandlingService xmlHandlingService;
private static final int PageSize = 500;
private static final boolean TestMode = false;
private final EntityManager entityManager;
public DatasetMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager) {
public DatasetMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, QueryFactory queryFactory, XmlHandlingService xmlHandlingService, EntityManager entityManager) {
this.databaseRepository = databaseRepository;
this.jsonHandlingService = jsonHandlingService;
this.queryFactory = queryFactory;
this.xmlHandlingService = xmlHandlingService;
this.entityManager = entityManager;
}
@ -47,53 +73,51 @@ public class DatasetMigrationService {
}).toList();
}
public List<DatasetMigrationCollectedInfo> collectDatasetsInfo() throws JsonProcessingException {
List<DatasetMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
List<DatasetWizardModel> datasets = getDatasets(0, Integer.MAX_VALUE);
for (DatasetWizardModel datasetWizardModel : datasets) {
DatasetMigrationCollectedInfo datasetMigrationCollectedInfo = new DatasetMigrationCollectedInfo();
public void migrate() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
DatasetDao datasetDao = databaseRepository.getDatasetDao();
long total = datasetDao.asQueryable().count();
logger.debug("Migrate Dataset Total : " + total);
int page = 0;
//Collect basic dataset information
DescriptionEntity descriptionEntity = new DescriptionEntity();
descriptionEntity.setId(datasetWizardModel.getId());
descriptionEntity.setDescription(datasetWizardModel.getDescription());
descriptionEntity.setCreatedById(datasetWizardModel.getCreatedBy());
descriptionEntity.setDmpId(datasetWizardModel.getDmp().getId());
descriptionEntity.setLabel(datasetWizardModel.getLabel());
if (datasetWizardModel.getCreated() != null)
descriptionEntity.setCreatedAt(datasetWizardModel.getCreated().toInstant());
if (datasetWizardModel.getModified() != null)
descriptionEntity.setUpdatedAt(datasetWizardModel.getModified().toInstant());
if (datasetWizardModel.getFinalized() != null)
descriptionEntity.setFinalizedAt(datasetWizardModel.getFinalized().toInstant());
if (datasetWizardModel.getStatus() == 99) {
descriptionEntity.setIsActive(IsActive.Inactive);
descriptionEntity.setStatus(DescriptionStatus.Canceled);
} else {
descriptionEntity.setIsActive(IsActive.Active);
descriptionEntity.setStatus(DescriptionStatus.of(datasetWizardModel.getStatus()));
List<Dataset> items;
do {
items = datasetDao.asQueryable().orderBy((builder, root) -> builder.asc(root.get("Created"))).orderBy((builder, root) -> builder.asc(root.get("ID"))).skip(page * PageSize).take(PageSize).toList();
if (items != null && !items.isEmpty()) {
logger.debug("Migrate Dataset " + page * PageSize + " of " + total);
for (Dataset item : items) {
//entityManager.detach(item);
DatasetWizardModel model = new DatasetWizardModel();
model.fromDataModel(item);
DescriptionEntity data = new DescriptionEntity();
data.setId(model.getId());
data.setDescription(model.getDescription());
data.setCreatedById(model.getCreatedBy());
data.setDmpId(model.getDmp().getId());
data.setLabel(model.getLabel());
if (model.getCreated() != null)
data.setCreatedAt(model.getCreated().toInstant());
if (model.getModified() != null)
data.setUpdatedAt(model.getModified().toInstant());
if (model.getFinalized() != null)
data.setFinalizedAt(model.getFinalized().toInstant());
if (model.getStatus() == 99) {
data.setIsActive(IsActive.Inactive);
data.setStatus(DescriptionStatus.Canceled);
} else {
data.setIsActive(IsActive.Active);
data.setStatus(DescriptionStatus.of(model.getStatus()));
}
this.entityManager.persist(data);
this.entityManager.flush();
}
page++;
}
// descriptionEntity.setDmpDescriptionTemplateId(); //TODO MIGRATE DMPDESCRIPTIONTEMPLATES
datasetMigrationCollectedInfo.descriptionEntity = descriptionEntity;
collectedInfoList.add(datasetMigrationCollectedInfo);
}
return collectedInfoList;
}
public String migrate() throws JsonProcessingException {
for (DatasetMigrationCollectedInfo collectedInfo : collectDatasetsInfo()) {
this.entityManager.persist(collectedInfo.descriptionEntity);
}
this.entityManager.flush();
return "Migrated dmps";
}
public static class DatasetMigrationCollectedInfo {
public DescriptionEntity descriptionEntity;
} while (items != null && !items.isEmpty() && !TestMode);
}
}