migration fixes

This commit is contained in:
Efstratios Giannopoulos 2024-06-04 12:15:57 +03:00
parent 82b53314b9
commit 9695e32c17
2 changed files with 44 additions and 11 deletions

View File

@ -41,6 +41,7 @@ import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class DatasetMigrationService {
@ -97,16 +98,41 @@ public class DatasetMigrationService {
DefinitionEntity definitionEntity = this.xmlHandlingService.fromXml(DefinitionEntity.class, dmpBlueprint.getDefinition());
dmpBlueprintsMap.put(dmpBlueprint.getId(), definitionEntity);
}
List<UUID> changeTemplateToH2020 = Stream.of("0c82f2ce-5ff7-4ca9-a500-9f3062adf550",
"7acd3b36-e598-459c-8170-87c51ac80efb",
"64e7969a-6d0e-4895-ad4a-a382b6efb279",
"b4f3e186-98e7-424e-983e-c6f5c911e3be",
"8266cb3e-ef0e-4c19-b9fc-c33e44cebd03",
"dc7a7fbb-2a94-4e62-88b7-461a7b7411e1",
"7c16ef72-8728-41aa-9154-8f0f2105c75c",
"6382ffd2-0e13-45b7-9e4f-3bd2a695d394",
"4648c912-d514-460a-b6bf-fa9d3150438b",
"b6208df9-baef-4fa4-b483-9332e77ba3e5",
"28520240-c8bd-4272-a822-150cf23ca2e3",
"d243e090-aa83-4349-b64c-166cb132f4c8").map(UUID::fromString).toList();
UUID horizon2020UUID = UUID.fromString("6ddde24b-e7a0-477f-a2cd-0a8fad915dc8");
UUID horizonGroupId = UUID.fromString("3689bcce-405a-4d55-9854-669597b79c0a");
List<DmpDescriptionTemplateEntity> dmpDescriptionTemplateEntities = this.queryFactory.query(DmpDescriptionTemplateQuery.class).dmpIds(items.stream().map(x-> x.getDmp().getId()).distinct().toList()).collect();
List<DescriptionTemplateEntity> descriptionTemplates = this.queryFactory.query(DescriptionTemplateQuery.class).ids(items.stream().map(x-> x.getProfile().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));
List<UUID> profileIds = new ArrayList<>(items.stream().map(x -> x.getProfile().getId()).distinct().toList());
profileIds.add(horizon2020UUID);
List<DescriptionTemplateEntity> descriptionTemplates = this.queryFactory.query(DescriptionTemplateQuery.class).ids(profileIds).collect();
Map<UUID, org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity> descriptionTemplateDefinitionMap = new HashMap<>();
for (DescriptionTemplateEntity descriptionTemplateEntity : descriptionTemplates){
descriptionTemplateDefinitionMap.put(descriptionTemplateEntity.getId(), this.xmlHandlingService.fromXml(org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplateEntity.getDefinition()));
}
for (Dataset item : items) {
UUID profileId = item.getProfile().getId();
UUID profileGroupId = item.getProfile().getGroupId();
if (changeTemplateToH2020.contains(item.getId())) {
profileId = horizon2020UUID;
profileGroupId = horizonGroupId;
}
DefinitionEntity definition = dmpBlueprintsMap.getOrDefault(item.getDmp().getProfile().getId(), null);
if (definition == null || definition.getSections() == null || definition.getSections().size() <= item.getDmpSectionIndex()) {
@ -117,7 +143,7 @@ public class DatasetMigrationService {
throw new MyApplicationException("Migrate Dataset " + item.getId() + " cannot found section id for section " + item.getDmpSectionIndex());
}
List<DmpDescriptionTemplateEntity> itemDescriptionTemplates = this.getOrCreateDmpDescriptionTemplateEntity(item, sectionId, dmpDescriptionTemplateEntities);
List<DmpDescriptionTemplateEntity> itemDescriptionTemplates = this.getOrCreateDmpDescriptionTemplateEntity(item, sectionId, profileGroupId, dmpDescriptionTemplateEntities);
if (itemDescriptionTemplates.size() > 1) {
throw new MyApplicationException("Migrate Dataset " + item.getId() + " multiple DmpDescriptionTemplateEntity for section " + item.getDmpSectionIndex());
@ -134,7 +160,7 @@ public class DatasetMigrationService {
data.setDmpId(item.getDmp().getId());
data.setLabel(item.getLabel());
data.setDmpDescriptionTemplateId(itemDescriptionTemplates.getFirst().getId());
data.setDescriptionTemplateId(item.getProfile().getId());
data.setDescriptionTemplateId(profileId);
data.setCreatedAt(item.getCreated() != null ? item.getCreated().toInstant() : Instant.now());
data.setUpdatedAt(item.getModified() != null ? item.getModified().toInstant() : Instant.now());
if (item.getFinalizedAt() != null)
@ -149,7 +175,8 @@ public class DatasetMigrationService {
if (data.getCreatedById() == null){
throw new MyApplicationException("Migration skipped creator not found " + item.getId());
}
org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinitionEntity = descriptionTemplateDefinitionMap.getOrDefault(item.getProfile().getId(), null);
org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinitionEntity = descriptionTemplateDefinitionMap.getOrDefault(profileId, null);
this.entityManager.persist(data);
this.entityManager.flush();
@ -164,20 +191,21 @@ public class DatasetMigrationService {
} while (items != null && !items.isEmpty() && !TestMode);
}
private List<DmpDescriptionTemplateEntity> getOrCreateDmpDescriptionTemplateEntity(Dataset item, UUID sectionId, List<DmpDescriptionTemplateEntity> dmpDescriptionTemplateEntities){
List<DmpDescriptionTemplateEntity> itemDescriptionTemplates = dmpDescriptionTemplateEntities.stream().filter(x-> x.getDescriptionTemplateGroupId().equals(item.getProfile().getGroupId()) && x.getDmpId().equals(item.getDmp().getId()) && x.getSectionId().equals(sectionId)).toList();
private List<DmpDescriptionTemplateEntity> getOrCreateDmpDescriptionTemplateEntity(Dataset item, UUID sectionId, UUID groupId, List<DmpDescriptionTemplateEntity> dmpDescriptionTemplateEntities){
List<DmpDescriptionTemplateEntity> itemDescriptionTemplates = dmpDescriptionTemplateEntities.stream().filter(x-> x.getDescriptionTemplateGroupId().equals(groupId) && x.getDmpId().equals(item.getDmp().getId()) && x.getSectionId().equals(sectionId)).toList();
if (itemDescriptionTemplates.isEmpty()) {
if (!item.getStatus().equals(Dataset.Status.DELETED.getValue())) logger.warn("Migrate Dataset " + item.getId() + " cannot found DmpDescriptionTemplateEntity for section " + item.getDmpSectionIndex());
if (dmpDescriptionTemplateEntities.stream().anyMatch(x -> x.getDmpId().equals(item.getDmp().getId()) && x.getSectionId().equals(sectionId))) {
DmpDescriptionTemplateEntity dmpDescriptionTemplateEntity = new DmpDescriptionTemplateEntity();
dmpDescriptionTemplateEntity.setId(UUID.randomUUID());
dmpDescriptionTemplateEntity.setDescriptionTemplateGroupId(item.getProfile().getGroupId());
dmpDescriptionTemplateEntity.setDescriptionTemplateGroupId(groupId);
dmpDescriptionTemplateEntity.setDmpId(item.getDmp().getId());
dmpDescriptionTemplateEntity.setCreatedAt(item.getCreated() != null ? item.getCreated().toInstant() : Instant.now());
dmpDescriptionTemplateEntity.setUpdatedAt(item.getModified() != null ? item.getModified().toInstant() : Instant.now());
dmpDescriptionTemplateEntity.setSectionId(sectionId);
dmpDescriptionTemplateEntity.setIsActive(!item.getStatus().equals(Dataset.Status.DELETED.getValue()) ? IsActive.Active : IsActive.Inactive);
this.entityManager.persist(dmpDescriptionTemplateEntity);
this.entityManager.flush();
itemDescriptionTemplates = List.of(dmpDescriptionTemplateEntity);
} else {
throw new MyApplicationException("Migrate Dataset " + item.getId() + " " + item.getLabel() + " cannot found DmpDescriptionTemplateEntity for section " + item.getDmpSectionIndex());

View File

@ -1,6 +1,7 @@
package eu.old.eudat.migration;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import eu.old.eudat.data.entities.DMP;
import org.opencdmp.commons.JsonHandlingService;
import org.opencdmp.commons.XmlHandlingService;
import org.opencdmp.commons.enums.IsActive;
@ -103,9 +104,9 @@ public class DmpDatasetProfileMigrationService {
DmpDescriptionTemplateEntity data = new DmpDescriptionTemplateEntity();
data.setId(UUID.randomUUID());
data.setDescriptionTemplateGroupId(item.getDatasetprofile().getGroupId());
data.setDmpId(item.getDmp().getId());
data.setCreatedAt(Instant.now());
data.setUpdatedAt(Instant.now());
data.setDmpId(getDmp(item).getId());
data.setCreatedAt(item.getDmp().getCreated() != null ? item.getDmp().getCreated().toInstant() : Instant.now());
data.setUpdatedAt(item.getDmp().getModified() != null ? item.getDmp().getModified().toInstant() : Instant.now());
data.setSectionId(sectionId);
data.setIsActive(IsActive.Active);
this.entityManager.persist(data);
@ -120,6 +121,10 @@ public class DmpDatasetProfileMigrationService {
removeDuplicates();
}
private DMP getDmp(DMPDatasetProfile item) {
return item.getDmp();
}
private void removeDuplicates() {
logger.debug("Checking for duplicates on DmpDescriptionTemplate table after migration");