2023-11-23 09:26:40 +01:00
|
|
|
package eu.old.eudat.migration;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import eu.eudat.commons.JsonHandlingService;
|
|
|
|
import eu.eudat.commons.enums.*;
|
|
|
|
import eu.eudat.data.DescriptionEntity;
|
|
|
|
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.queryable.QueryableList;
|
|
|
|
import jakarta.persistence.EntityManager;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class DatasetMigrationService {
|
|
|
|
|
|
|
|
private final DatabaseRepository databaseRepository;
|
|
|
|
|
|
|
|
private final JsonHandlingService jsonHandlingService;
|
|
|
|
|
|
|
|
private final EntityManager entityManager;
|
|
|
|
|
|
|
|
public DatasetMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager) {
|
|
|
|
this.databaseRepository = databaseRepository;
|
|
|
|
this.jsonHandlingService = jsonHandlingService;
|
|
|
|
this.entityManager = entityManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public QueryableList<Dataset> datasetQueryableList() {
|
|
|
|
return databaseRepository.getDatasetDao().asQueryable();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<DatasetWizardModel> getDatasets(Integer skip, Integer take) {
|
|
|
|
List<Dataset> datasets = datasetQueryableList()
|
|
|
|
.orderBy((builder, root) -> builder.desc(root.get("created")))
|
|
|
|
.skip(skip)
|
|
|
|
.take(take)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
return datasets.stream().map(x -> {
|
|
|
|
DatasetWizardModel dataset = new DatasetWizardModel();
|
|
|
|
dataset.fromDataModel(x);
|
|
|
|
return dataset;
|
|
|
|
}).toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<DatasetMigrationCollectedInfo> collectDatasetsInfo() throws JsonProcessingException {
|
|
|
|
List<DatasetMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
|
2023-11-23 13:40:44 +01:00
|
|
|
List<DatasetWizardModel> datasets = getDatasets(0, Integer.MAX_VALUE);
|
2023-11-23 09:26:40 +01:00
|
|
|
for (DatasetWizardModel datasetWizardModel : datasets) {
|
|
|
|
DatasetMigrationCollectedInfo datasetMigrationCollectedInfo = new DatasetMigrationCollectedInfo();
|
|
|
|
|
|
|
|
//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()));
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|