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 com.fasterxml.jackson.core.JsonProcessingException;
import eu.eudat.commons.JsonHandlingService; import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.XmlHandlingService;
import eu.eudat.commons.enums.*; 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.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.data.entities.Dataset;
import eu.old.eudat.logic.services.operations.DatabaseRepository; import eu.old.eudat.logic.services.operations.DatabaseRepository;
import eu.old.eudat.models.data.datasetwizard.DatasetWizardModel; import eu.old.eudat.models.data.datasetwizard.DatasetWizardModel;
import eu.old.eudat.models.data.dmp.DataManagementPlan;
import eu.old.eudat.queryable.QueryableList; 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.persistence.EntityManager;
import jakarta.xml.bind.JAXBException;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;
import java.util.ArrayList; import javax.xml.parsers.ParserConfigurationException;
import java.util.List; import java.io.IOException;
import java.time.Instant;
import java.util.*;
@Service @Service
public class DatasetMigrationService { public class DatasetMigrationService {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpDatasetProfileMigrationService.class));
private final DatabaseRepository databaseRepository; private final DatabaseRepository databaseRepository;
private final JsonHandlingService jsonHandlingService; 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; 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.databaseRepository = databaseRepository;
this.jsonHandlingService = jsonHandlingService; this.jsonHandlingService = jsonHandlingService;
this.queryFactory = queryFactory;
this.xmlHandlingService = xmlHandlingService;
this.entityManager = entityManager; this.entityManager = entityManager;
} }
@ -47,53 +73,51 @@ public class DatasetMigrationService {
}).toList(); }).toList();
} }
public List<DatasetMigrationCollectedInfo> collectDatasetsInfo() throws JsonProcessingException { public void migrate() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
List<DatasetMigrationCollectedInfo> collectedInfoList = new ArrayList<>(); DatasetDao datasetDao = databaseRepository.getDatasetDao();
List<DatasetWizardModel> datasets = getDatasets(0, Integer.MAX_VALUE); long total = datasetDao.asQueryable().count();
for (DatasetWizardModel datasetWizardModel : datasets) { logger.debug("Migrate Dataset Total : " + total);
DatasetMigrationCollectedInfo datasetMigrationCollectedInfo = new DatasetMigrationCollectedInfo(); int page = 0;
//Collect basic dataset information List<Dataset> items;
DescriptionEntity descriptionEntity = new DescriptionEntity(); do {
descriptionEntity.setId(datasetWizardModel.getId()); 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();
descriptionEntity.setDescription(datasetWizardModel.getDescription()); if (items != null && !items.isEmpty()) {
descriptionEntity.setCreatedById(datasetWizardModel.getCreatedBy()); logger.debug("Migrate Dataset " + page * PageSize + " of " + total);
descriptionEntity.setDmpId(datasetWizardModel.getDmp().getId());
descriptionEntity.setLabel(datasetWizardModel.getLabel()); for (Dataset item : items) {
if (datasetWizardModel.getCreated() != null) //entityManager.detach(item);
descriptionEntity.setCreatedAt(datasetWizardModel.getCreated().toInstant());
if (datasetWizardModel.getModified() != null) DatasetWizardModel model = new DatasetWizardModel();
descriptionEntity.setUpdatedAt(datasetWizardModel.getModified().toInstant()); model.fromDataModel(item);
if (datasetWizardModel.getFinalized() != null)
descriptionEntity.setFinalizedAt(datasetWizardModel.getFinalized().toInstant()); DescriptionEntity data = new DescriptionEntity();
if (datasetWizardModel.getStatus() == 99) { data.setId(model.getId());
descriptionEntity.setIsActive(IsActive.Inactive); data.setDescription(model.getDescription());
descriptionEntity.setStatus(DescriptionStatus.Canceled); data.setCreatedById(model.getCreatedBy());
} else { data.setDmpId(model.getDmp().getId());
descriptionEntity.setIsActive(IsActive.Active); data.setLabel(model.getLabel());
descriptionEntity.setStatus(DescriptionStatus.of(datasetWizardModel.getStatus())); 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 } while (items != null && !items.isEmpty() && !TestMode);
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;
} }
} }