migration service changes

This commit is contained in:
Efstratios Giannopoulos 2023-11-23 18:09:17 +02:00
parent eae9a5f7d3
commit e4801f94ed
4 changed files with 119 additions and 219 deletions

View File

@ -4,6 +4,8 @@ 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.description.FieldEntity;
import eu.eudat.commons.types.description.PropertyDefinitionEntity;
import eu.eudat.commons.types.dmp.DmpBlueprintValueEntity;
import eu.eudat.commons.types.dmp.DmpContactEntity;
import eu.eudat.commons.types.dmp.DmpPropertiesEntity;
@ -12,7 +14,11 @@ import eu.eudat.data.DescriptionEntity;
import eu.eudat.data.DmpBlueprintEntity;
import eu.eudat.data.DmpDescriptionTemplateEntity;
import eu.eudat.data.DmpEntity;
import eu.eudat.model.Dmp;
import eu.eudat.model.DmpDescriptionTemplate;
import eu.eudat.query.DmpBlueprintQuery;
import eu.eudat.query.DmpDescriptionTemplateQuery;
import eu.eudat.query.DmpQuery;
import eu.old.eudat.data.dao.entities.DatasetDao;
import eu.old.eudat.data.entities.DMP;
import eu.old.eudat.data.entities.DMPDatasetProfile;
@ -23,9 +29,11 @@ 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.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.EntityManager;
import jakarta.xml.bind.JAXBException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;
@ -34,6 +42,7 @@ import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class DatasetMigrationService {
@ -63,34 +72,94 @@ public class DatasetMigrationService {
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();
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);
List<DmpBlueprintEntity> dmpBlueprints = this.queryFactory.query(DmpBlueprintQuery.class).ids(items.stream().map(x-> x.getDmp().getProfile().getId()).distinct().toList()).collect();
Map<UUID, DefinitionEntity> dmpBlueprintsMap = new HashMap<>();
for (DmpBlueprintEntity dmpBlueprint : dmpBlueprints) {
DefinitionEntity definitionEntity = this.xmlHandlingService.fromXml(DefinitionEntity.class, dmpBlueprint.getDefinition());
dmpBlueprintsMap.put(dmpBlueprint.getId(), definitionEntity);
}
List<DmpDescriptionTemplateEntity> descriptionTemplates = this.queryFactory.query(DmpDescriptionTemplateQuery.class).dmpIds(items.stream().map(x-> x.getDmp().getId()).distinct().toList()).collect();
Map<UUID, UUID> creatorsByDmp = this.queryFactory.query(DmpQuery.class).ids(items.stream().map(x-> x.getDmp().getId()).distinct().toList()).collectAs(new BaseFieldSet().ensure(Dmp._id).ensure(Dmp._creator)).stream().collect(Collectors.toMap(DmpEntity::getId, DmpEntity::getCreatorId));;
for (Dataset item : items) {
//entityManager.detach(item);
DatasetWizardModel model = new DatasetWizardModel();
model.fromDataModel(item);
//DatasetWizardModel model = new DatasetWizardModel();
//model.fromDataModel(item);
DefinitionEntity definition = dmpBlueprintsMap.getOrDefault(item.getDmp().getProfile().getId(), null);
if (definition == null || definition.getSections() == null || definition.getSections().size() < item.getDmpSectionIndex()) {
logger.error("Migrate Dataset " + item.getId() + " cannot found section id for section " + item.getDmpSectionIndex());
throw new MyApplicationException("Migrate Dataset " + item.getId() + " cannot found section id for section " + item.getDmpSectionIndex());
}
UUID sectionId = definition.getSections().get(item.getDmpSectionIndex()).getId();
if (sectionId == null) {
logger.error("Migrate Dataset " + item.getId() + " cannot found section id for section " + item.getDmpSectionIndex());
throw new MyApplicationException("Migrate Dataset " + item.getId() + " cannot found section id for section " + item.getDmpSectionIndex());
}
List<DmpDescriptionTemplateEntity> itemDescriptionTemplates = descriptionTemplates.stream().filter(x-> x.getDescriptionTemplateId().equals(item.getProfile().getId()) && x.getDmpId().equals(item.getDmp().getId()) && x.getSectionId().equals(sectionId)).toList();
if (itemDescriptionTemplates.isEmpty()) {
logger.error("Migrate Dataset " + item.getId() + " cannot found DmpDescriptionTemplateEntity for section " + item.getDmpSectionIndex());
if(descriptionTemplates.stream().filter(x-> x.getDmpId().equals(item.getDmp().getId()) && x.getSectionId().equals(sectionId)).count() > 0) continue;
throw new MyApplicationException("Migrate Dataset " + item.getId() + " cannot found DmpDescriptionTemplateEntity for section " + item.getDmpSectionIndex());
}
if (itemDescriptionTemplates.size() > 1) {
logger.error("Migrate Dataset " + item.getId() + " multiple DmpDescriptionTemplateEntity for section " + item.getDmpSectionIndex());
//TODO CLEAN Duplicates dmp, DescriptionTemplate, SectionId
}
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.setId(item.getId());
data.setDescription(item.getDescription());
if (item.getCreator() != null) {
data.setCreatedById(item.getCreator().getId());
} else {
data.setCreatedById(creatorsByDmp.getOrDefault(item.getDmp().getId(), null));
}
data.setDmpId(item.getDmp().getId());
data.setLabel(item.getLabel());
data.setDmpDescriptionTemplateId(itemDescriptionTemplates.get(0).getId());
data.setCreatedAt(item.getCreated() != null ? item.getCreated().toInstant() : Instant.now());
data.setUpdatedAt(item.getModified() != null ? item.getModified().toInstant() : Instant.now());
if (item.getFinalizedAt() != null)
data.setFinalizedAt(item.getFinalizedAt().toInstant());
if (item.getStatus() == 99) {
data.setIsActive(IsActive.Inactive);
data.setStatus(DescriptionStatus.Canceled);
} else {
data.setIsActive(IsActive.Active);
data.setStatus(DescriptionStatus.of(model.getStatus()));
data.setStatus(DescriptionStatus.of(item.getStatus()));
}
if (item.getProperties() != null) {
JSONObject jObject = new JSONObject(item.getProperties());
Map<String, Object> properties = jObject.toMap();
PropertyDefinitionEntity propertyDefinitionEntity = new PropertyDefinitionEntity();
List<FieldEntity> fieldEntities = new ArrayList<>();
for (String key: properties.keySet() ) {
FieldEntity fieldEntity = new FieldEntity();
fieldEntity.setKey(key);
fieldEntity.setValue(properties.get(key) != null ? properties.get(key).toString(): null);
fieldEntities.add(fieldEntity);
}
propertyDefinitionEntity.setFields(fieldEntities);
data.setProperties(this.jsonHandlingService.toJson(propertyDefinitionEntity));
}
if (data.getCreatedById() == null){//TODO
logger.error("Migration skipped creator not found " + item.getId());
throw new MyApplicationException("Migration skipped creator not found " + item.getId());
}
this.entityManager.persist(data);

View File

@ -84,22 +84,23 @@ public class DmpDatasetProfileMigrationService {
if (profileData == null || profileData.dmpSectionIndex == null || profileData.dmpSectionIndex.isEmpty()){
logger.error("Migrate DmpDatasetProfile " + item.getId() + " failed no section info ");
throw new MyApplicationException();
throw new MyApplicationException("Migrate DmpDatasetProfile " + item.getId() + " failed no section info ");
}
DefinitionEntity definition = dmpBlueprintsMap.getOrDefault(item.getDmp().getProfile().getId(), null);
if (definition == null){
logger.error("Migrate DmpDatasetProfile " + item.getId() + " failed blueprint definition not found for blueprint " + item.getDmp().getProfile().getId());
throw new MyApplicationException("Migrate DmpDatasetProfile " + item.getId() + " failed blueprint definition not found for blueprint " + item.getDmp().getProfile().getId());
}
for (int sectionIndex: profileData.dmpSectionIndex) {
if (definition == null || definition.getSections() == null || definition.getSections().size() < sectionIndex) {
logger.error("Migrate DmpDatasetProfile " + item.getId() + " can nto found section id for section " + sectionIndex);
throw new MyApplicationException();
if (definition.getSections() == null || definition.getSections().size() < sectionIndex) {
logger.error("Migrate DmpDatasetProfile " + item.getId() + " cannot found section id for section " + sectionIndex);
throw new MyApplicationException("Migrate DmpDatasetProfile " + item.getId() + " cannot found section id for section " + sectionIndex);
}
UUID sectionId = definition.getSections().get(sectionIndex).getId();
if (sectionId == null) {
logger.error("Migrate DmpDatasetProfile " + item.getId() + " can nto found section id for section " + sectionIndex);
throw new MyApplicationException();
logger.error("Migrate DmpDatasetProfile " + item.getId() + " cannot found section id for section " + sectionIndex);
throw new MyApplicationException("Migrate DmpDatasetProfile " + item.getId() + " cannot found section id for section " + sectionIndex);
}
DmpDescriptionTemplateEntity data = new DmpDescriptionTemplateEntity();
@ -118,84 +119,12 @@ public class DmpDatasetProfileMigrationService {
page++;
}
} while (items != null && !items.isEmpty() && !TestMode);
//TODO CLEAN Duplicates dmp, DescriptionTemplate, SectionId
}
//
// public QueryableList<DMPDatasetProfile> dmpDatasetProfileQueryableList() {
// return databaseRepository.getDmpDatasetProfileDao().asQueryable();
// }
//
// public List<DMPDatasetProfile> getDatasets(Integer skip, Integer take) {
// return dmpDatasetProfileQueryableList()
//// .orderBy((builder, root) -> builder.desc(root.get("created")))
// .skip(skip)
// .take(take)
// .toList();
// }
//
// public List<DmpDatasetProfileMigrationCollectedInfo> collectDatasetsInfo() throws Exception {
// List<DmpDatasetProfileMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
// List<DMPDatasetProfile> items = getDatasets(0, 4);
//
// List<DmpBlueprintEntity> dmpBlueprints = this.queryFactory.query(DmpBlueprintQuery.class).ids(items.stream().map(x-> x.getDmp().getProfile().getId()).distinct().toList()).collect();
// Map<UUID, DefinitionEntity> dmpBlueprintsMap = new HashMap<>();
// for (DmpBlueprintEntity dmpBlueprint : dmpBlueprints) {
// DefinitionEntity definitionEntity = this.xmlHandlingService.fromXml(DefinitionEntity.class, dmpBlueprint.getDefinition());
// dmpBlueprintsMap.put(dmpBlueprint.getId(), definitionEntity);
// }
//
// for (DMPDatasetProfile item : items) {
// this.entityManager.detach(item);
//
// DmpDatasetProfileMigrationCollectedInfo dmpDatasetProfileMigrationCollectedInfo = new DmpDatasetProfileMigrationCollectedInfo();
//
// DmpDatasetProfileData profileData = jsonHandlingService.fromJson(DmpDatasetProfileData.class, item.getData());
//
// if (profileData == null || profileData.dmpSectionIndex == null || profileData.dmpSectionIndex.isEmpty()){
// throw new Exception();
// }
// DefinitionEntity definition = dmpBlueprintsMap.getOrDefault(item.getDmp().getProfile().getId(), null);
// if (definition == null){
// throw new Exception();
// }
//
// for (int sectionIndex: profileData.dmpSectionIndex) {
// if ( definition.getSections().size() < sectionIndex) {
// throw new Exception();
// }
// UUID sectionId = definition.getSections().get(sectionIndex).getId();
//
// DmpDescriptionTemplateEntity data = new DmpDescriptionTemplateEntity();
// data.setDescriptionTemplateId(item.getDatasetprofile().getId());
// data.setDmpId(item.getDmp().getId());
// data.setCreatedAt(Instant.now());
// data.setUpdatedAt(Instant.now());
// data.setSectionId(sectionId); //TODO
// data.setIsActive(IsActive.Active);
// }
//
//
//
// dmpDatasetProfileMigrationCollectedInfo.dmpDescriptionTemplateEntity = data;
//
// collectedInfoList.add(dmpDatasetProfileMigrationCollectedInfo);
// }
// return collectedInfoList;
// }
//
// public String migrate() throws JsonProcessingException {
// for (DmpDatasetProfileMigrationCollectedInfo collectedInfo : collectDatasetsInfo()) {
//// this.entityManager.persist(collectedInfo.dmpDescriptionTemplateEntity);
// }
// this.entityManager.flush();
// return "Migrated dmps";
// }
//
// public static class DmpDatasetProfileMigrationCollectedInfo {
//
// public DmpDescriptionTemplateEntity dmpDescriptionTemplateEntity;
//
// }
//
@JsonIgnoreProperties({"validationErrorModel"})
public static class DmpDatasetProfileData {

View File

@ -9,30 +9,18 @@ import eu.eudat.commons.enums.IsActive;
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.DmpBlueprintEntity;
import eu.eudat.data.DmpDescriptionTemplateEntity;
import eu.eudat.data.DmpEntity;
import eu.eudat.query.DmpBlueprintQuery;
import eu.old.eudat.data.dao.entities.DMPDao;
import eu.old.eudat.data.dao.entities.DmpDatasetProfileDao;
import eu.old.eudat.data.entities.DMP;
import eu.old.eudat.data.entities.DMPDatasetProfile;
import eu.old.eudat.logic.services.operations.DatabaseRepository;
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.springframework.web.bind.annotation.PathVariable;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
@Service
@ -42,7 +30,6 @@ public class DmpMigrationService {
private final DatabaseRepository databaseRepository;
private final JsonHandlingService jsonHandlingService;
private final EntityManager entityManager;
private static final int PageSize = 500;
private static final boolean TestMode = false;
@ -79,8 +66,19 @@ public class DmpMigrationService {
data.setId(model.getId());
data.setLabel(model.getLabel());
data.setDescription(model.getDescription());
if (model.getCreator() != null)
if (model.getCreator() != null) {
data.setCreatorId(model.getCreator().getId());
} else {
List<DMP> groupDmps = dmpDao.asQueryable().where((builder, root) -> root.get("GroupId").in(List.of(item.getGroupId()))).toList();
for (DMP groupDmp: groupDmps) {
DataManagementPlan groupDmpModel = new DataManagementPlan();
groupDmpModel.fromDataModel(groupDmp);
if (groupDmpModel.getCreator() != null) {
data.setCreatorId(groupDmpModel.getCreator().getId());
break;
}
}
}
data.setGroupId(model.getGroupId());
data.setVersion((short) model.getVersion());
data.setVersionStatus(DmpVersionStatus.of((short) model.getVersionStatus()));
@ -119,21 +117,19 @@ public class DmpMigrationService {
}
data.setProperties(jsonHandlingService.toJson(dmpProperties));
if (data.getAccessType() == null) {
if (data.getAccessType() == null && item.getStatus().equals(DmpStatus.Finalized.getValue())) {
logger.warn("AccessType not found set to default for dmp " + item.getId());
data.setAccessType(DmpAccessType.Restricted); //TODO
data.setAccessType(item.isPublic() ? DmpAccessType.Public : DmpAccessType.Restricted);
}
if (data.getLanguage() == null) {
if ((data.getLanguage() == null || data.getLanguage().isBlank()) && item.getStatus().equals(DmpStatus.Finalized.getValue())) {
logger.warn("Language not found set to default for dmp " + item.getId());
data.setLanguage(""); //TODO
data.setLanguage("en");
}
if (data.getCreatorId() == null){
logger.warn("Migration skipped creator not found " + item.getId());
continue;
throw new MyApplicationException("Migration skipped creator not found " + item.getId());
}
this.entityManager.persist(data);
this.entityManager.flush();
}
@ -143,99 +139,4 @@ public class DmpMigrationService {
} while (items != null && !items.isEmpty() && !TestMode);
}
// public QueryableList<DMP> dmpQueryableList() {
// return databaseRepository.getDmpDao().asQueryable();
// }
//
// public List<DataManagementPlan> getDmps(Integer skip, Integer take) {
// List<DMP> dmps = dmpQueryableList()
// .orderBy((builder, root) -> builder.desc(root.get("created")))
// .skip(skip)
// .take(take)
// .toList();
//
// return dmps.stream().map(x -> {
// DataManagementPlan dmp = new DataManagementPlan();
// dmp.fromDataModel(x);
// return dmp;
// }).toList();
// }
// public List<DmpMigrationCollectedInfo> collectDmpsInfo() throws JsonProcessingException {
// List<DmpMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
// List<DataManagementPlan> dmps = getDmps(0, Integer.MAX_VALUE);
// for (DataManagementPlan dataManagementPlan : dmps) {
// DmpMigrationCollectedInfo dmpMigrationCollectedInfo = new DmpMigrationCollectedInfo();
//
// DmpPropertiesEntity dmpProperties = new DmpPropertiesEntity();
// dmpProperties.setDmpBlueprintValues(new ArrayList<>());
// dmpProperties.setContacts(new ArrayList<>());
//
// //Collect basic dmp information
// DmpEntity dmpEntity = new DmpEntity();
// dmpEntity.setId(dataManagementPlan.getId());
// dmpEntity.setLabel(dataManagementPlan.getLabel());
// dmpEntity.setDescription(dataManagementPlan.getDescription());
// if (dataManagementPlan.getCreator() != null)
// dmpEntity.setCreatorId(dataManagementPlan.getCreator().getId());
// dmpEntity.setGroupId(dataManagementPlan.getGroupId());
// dmpEntity.setVersion((short) dataManagementPlan.getVersion());
// dmpEntity.setVersionStatus(DmpVersionStatus.of((short) dataManagementPlan.getVersionStatus()));
// if (dataManagementPlan.getCreated() != null)
// dmpEntity.setCreatedAt(dataManagementPlan.getCreated().toInstant());
// if (dataManagementPlan.getModified() != null)
// dmpEntity.setUpdatedAt(dataManagementPlan.getModified().toInstant());
// if (dataManagementPlan.getFinalized() != null)
// dmpEntity.setFinalizedAt(dataManagementPlan.getFinalized().toInstant());
// dmpEntity.setBlueprintId(dataManagementPlan.getProfile().getId());
// if (dataManagementPlan.getExtraProperties() != null) {
// if (dataManagementPlan.getExtraProperties().containsKey("language") && dataManagementPlan.getExtraProperties().get("language") != null)
// dmpEntity.setLanguage((String) dataManagementPlan.getExtraProperties().get("language"));
// if (dataManagementPlan.getExtraProperties().containsKey("visible") && dataManagementPlan.getExtraProperties().get("visible") != null)
// dmpEntity.setAccessType((boolean) dataManagementPlan.getExtraProperties().get("visible") ? DmpAccessType.Public : DmpAccessType.Restricted);
// if (dataManagementPlan.getExtraProperties().containsKey("contact") && dataManagementPlan.getExtraProperties().get("contact") != null) {
// DmpContactEntity contactEntity = new DmpContactEntity();
// contactEntity.setUserId((String) dataManagementPlan.getExtraProperties().get("contact"));
// dmpProperties.getContacts().add(contactEntity);
// }
// }
// if (dataManagementPlan.getProperties() != null) {
// dataManagementPlan.getProperties().forEach((key,val) -> {
// DmpBlueprintValueEntity valueEntity = new DmpBlueprintValueEntity();
// valueEntity.setFieldId(key);
// valueEntity.setValue((String) val);
// dmpProperties.getDmpBlueprintValues().add(valueEntity);
// });
// }
// if (dataManagementPlan.getStatus() == 99) {
// dmpEntity.setIsActive(IsActive.Inactive);
// dmpEntity.setStatus(DmpStatus.Draft);
// } else {
// dmpEntity.setIsActive(IsActive.Active);
// dmpEntity.setStatus(DmpStatus.of((short) dataManagementPlan.getStatus()));
// }
// dmpEntity.setProperties(jsonHandlingService.toJson(dmpProperties));
// dmpMigrationCollectedInfo.dmpEntity = dmpEntity;
//
// //Collect dmp Organization info
//
// collectedInfoList.add(dmpMigrationCollectedInfo);
// }
// return collectedInfoList;
// }
//
// public String migrate() throws JsonProcessingException {
// for (DmpMigrationCollectedInfo collectedInfo : collectDmpsInfo()) {
// this.entityManager.persist(collectedInfo.dmpEntity);
// }
// this.entityManager.flush();
// return "Migrated dmps";
// }
//
// public static class DmpMigrationCollectedInfo {
//
// public DmpEntity dmpEntity;
//
// }
}

View File

@ -68,10 +68,11 @@ public class MigrationController {
return true;
}
@GetMapping("datasets/migrate")
@GetMapping("datasets")
@Transactional
public String migrateDatasets() throws JsonProcessingException {
return this.datasetMigrationService.migrate();
public boolean migrateDatasets() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
this.datasetMigrationService.migrate();
return true;
}
@GetMapping("dmp-dataset-profiles")