Merge branch 'dmp-refactoring' of https://code-repo.d4science.org/MaDgiK-CITE/argos into dmp-refactoring
This commit is contained in:
commit
07daf34077
|
@ -2,98 +2,104 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryableList<Dataset> datasetQueryableList() {
|
public void migrate() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
|
||||||
return databaseRepository.getDatasetDao().asQueryable();
|
DatasetDao datasetDao = databaseRepository.getDatasetDao();
|
||||||
}
|
long total = datasetDao.asQueryable().count();
|
||||||
|
logger.debug("Migrate Dataset Total : " + total);
|
||||||
|
int page = 0;
|
||||||
|
|
||||||
public List<DatasetWizardModel> getDatasets(Integer skip, Integer take) {
|
List<Dataset> items;
|
||||||
List<Dataset> datasets = datasetQueryableList()
|
do {
|
||||||
.orderBy((builder, root) -> builder.desc(root.get("created")))
|
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();
|
||||||
.skip(skip)
|
if (items != null && !items.isEmpty()) {
|
||||||
.take(take)
|
logger.debug("Migrate Dataset " + page * PageSize + " of " + total);
|
||||||
.toList();
|
|
||||||
|
|
||||||
return datasets.stream().map(x -> {
|
for (Dataset item : items) {
|
||||||
DatasetWizardModel dataset = new DatasetWizardModel();
|
//entityManager.detach(item);
|
||||||
dataset.fromDataModel(x);
|
|
||||||
return dataset;
|
|
||||||
}).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<DatasetMigrationCollectedInfo> collectDatasetsInfo() throws JsonProcessingException {
|
DatasetWizardModel model = new DatasetWizardModel();
|
||||||
List<DatasetMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
|
model.fromDataModel(item);
|
||||||
List<DatasetWizardModel> datasets = getDatasets(0, Integer.MAX_VALUE);
|
|
||||||
for (DatasetWizardModel datasetWizardModel : datasets) {
|
|
||||||
DatasetMigrationCollectedInfo datasetMigrationCollectedInfo = new DatasetMigrationCollectedInfo();
|
|
||||||
|
|
||||||
//Collect basic dataset information
|
DescriptionEntity data = new DescriptionEntity();
|
||||||
DescriptionEntity descriptionEntity = new DescriptionEntity();
|
data.setId(model.getId());
|
||||||
descriptionEntity.setId(datasetWizardModel.getId());
|
data.setDescription(model.getDescription());
|
||||||
descriptionEntity.setDescription(datasetWizardModel.getDescription());
|
data.setCreatedById(model.getCreatedBy());
|
||||||
descriptionEntity.setCreatedById(datasetWizardModel.getCreatedBy());
|
data.setDmpId(model.getDmp().getId());
|
||||||
descriptionEntity.setDmpId(datasetWizardModel.getDmp().getId());
|
data.setLabel(model.getLabel());
|
||||||
descriptionEntity.setLabel(datasetWizardModel.getLabel());
|
if (model.getCreated() != null)
|
||||||
if (datasetWizardModel.getCreated() != null)
|
data.setCreatedAt(model.getCreated().toInstant());
|
||||||
descriptionEntity.setCreatedAt(datasetWizardModel.getCreated().toInstant());
|
if (model.getModified() != null)
|
||||||
if (datasetWizardModel.getModified() != null)
|
data.setUpdatedAt(model.getModified().toInstant());
|
||||||
descriptionEntity.setUpdatedAt(datasetWizardModel.getModified().toInstant());
|
if (model.getFinalized() != null)
|
||||||
if (datasetWizardModel.getFinalized() != null)
|
data.setFinalizedAt(model.getFinalized().toInstant());
|
||||||
descriptionEntity.setFinalizedAt(datasetWizardModel.getFinalized().toInstant());
|
if (model.getStatus() == 99) {
|
||||||
if (datasetWizardModel.getStatus() == 99) {
|
data.setIsActive(IsActive.Inactive);
|
||||||
descriptionEntity.setIsActive(IsActive.Inactive);
|
data.setStatus(DescriptionStatus.Canceled);
|
||||||
descriptionEntity.setStatus(DescriptionStatus.Canceled);
|
|
||||||
} else {
|
} else {
|
||||||
descriptionEntity.setIsActive(IsActive.Active);
|
data.setIsActive(IsActive.Active);
|
||||||
descriptionEntity.setStatus(DescriptionStatus.of(datasetWizardModel.getStatus()));
|
data.setStatus(DescriptionStatus.of(model.getStatus()));
|
||||||
}
|
|
||||||
// descriptionEntity.setDmpDescriptionTemplateId(); //TODO MIGRATE DMPDESCRIPTIONTEMPLATES
|
|
||||||
|
|
||||||
datasetMigrationCollectedInfo.descriptionEntity = descriptionEntity;
|
|
||||||
|
|
||||||
collectedInfoList.add(datasetMigrationCollectedInfo);
|
|
||||||
}
|
|
||||||
return collectedInfoList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String migrate() throws JsonProcessingException {
|
this.entityManager.persist(data);
|
||||||
for (DatasetMigrationCollectedInfo collectedInfo : collectDatasetsInfo()) {
|
|
||||||
this.entityManager.persist(collectedInfo.descriptionEntity);
|
|
||||||
}
|
|
||||||
this.entityManager.flush();
|
this.entityManager.flush();
|
||||||
return "Migrated dmps";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class DatasetMigrationCollectedInfo {
|
page++;
|
||||||
|
}
|
||||||
public DescriptionEntity descriptionEntity;
|
} while (items != null && !items.isEmpty() && !TestMode);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue