From 0e7813cded193d9898a9d4871b956c4e71ab273c Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Mon, 26 Feb 2024 10:53:11 +0200 Subject: [PATCH] migration changes --- .../migration/DatasetMigrationService.java | 118 ++++++++++++------ 1 file changed, 83 insertions(+), 35 deletions(-) diff --git a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DatasetMigrationService.java b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DatasetMigrationService.java index 4de512061..f197cefb1 100644 --- a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DatasetMigrationService.java +++ b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DatasetMigrationService.java @@ -21,6 +21,7 @@ import eu.eudat.query.*; import eu.old.eudat.data.dao.entities.DatasetDao; import eu.old.eudat.data.entities.Dataset; import eu.old.eudat.logic.services.operations.DatabaseRepository; +import eu.old.eudat.models.data.user.components.datasetprofile.Field; import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.fieldset.BaseFieldSet; @@ -228,21 +229,37 @@ public class DatasetMigrationService { } } } + + this.addMultipleField(properties, descriptionTemplateDefinitionEntity, propertyDefinitionEntity, referenceTypeDefinitionEntityMap, referenceMap); - Map groupOrdinalMapping = new HashMap<>(); + for (PropertyDefinitionFieldSetEntity fieldSetEntity : propertyDefinitionEntity.getFieldSets().values()){ + int newOrdinal = 0; + for (PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity : fieldSetEntity.getItems().stream().filter(x-> x.getOrdinal() >= 0).sorted(Comparator.comparingInt(PropertyDefinitionFieldSetItemEntity::getOrdinal)).toList()){ + propertyDefinitionFieldSetItemEntity.setOrdinal(newOrdinal); + newOrdinal++; + } + for (PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity : fieldSetEntity.getItems().stream().filter(x-> x.getOrdinal() < 0).sorted(Comparator.comparingInt(PropertyDefinitionFieldSetItemEntity::getOrdinal)).toList()){ + propertyDefinitionFieldSetItemEntity.setOrdinal(newOrdinal); + newOrdinal++; + } + } + return propertyDefinitionEntity; + } + + private void addMultipleField(Map properties, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinitionEntity, PropertyDefinitionEntity propertyDefinitionEntity, Map referenceTypeDefinitionEntityMap, Map referenceMap){ + Map groupOrdinalMapping = new HashMap<>(); for (String key : properties.keySet()) { if (key.trim().toLowerCase(Locale.ROOT).startsWith("multiple_")) { String newKey = key.trim().substring("multiple_".length()); String[] keyParts = newKey.split("_", 4); String fieldSetId = ""; String groupId = ""; - int ordinal = 0; + int ordinal = Integer.MIN_VALUE; String fieldId = ""; - try { + if (keyParts.length == 4) { fieldSetId = keyParts[0].trim(); groupId = keyParts[1].trim(); - ordinal = Integer.parseInt(keyParts[2].trim()); fieldId = keyParts[3].trim(); } else if (keyParts.length == 3) { fieldSetId = keyParts[0].trim(); @@ -251,50 +268,45 @@ public class DatasetMigrationService { } else { logger.error("Field group key has invalid format " + key + " " + properties.get(key).toString()); continue; - //throw new MyApplicationException("Invalid multiple key " + key); } + try { + if (keyParts.length == 4) ordinal = Integer.parseInt(keyParts[2].trim()); } catch (Exception e){ logger.error("Field group key has invalid format " + key + " " + properties.get(key).toString()); + continue; } - - - + eu.eudat.commons.types.descriptiontemplate.FieldEntity currentField = descriptionTemplateDefinitionEntity.getFieldById(fieldId).stream().findFirst().orElse(null); if (currentField == null) { logger.error("Field set not found for field " + key + " " + properties.get(key).toString()); continue; } - + PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity = propertyDefinitionEntity.getFieldSets().getOrDefault(fieldSetId, null); - if (ordinal == 0) { - if (propertyDefinitionFieldSetEntity != null ) { - if (groupOrdinalMapping.containsKey(groupId + fieldSetId)){ - ordinal = groupOrdinalMapping.get(groupId + fieldSetId); - } else { - if (propertyDefinitionFieldSetEntity.getItems().stream().anyMatch(x-> x.getOrdinal() == 0 && !x.getFields().isEmpty())) { - ordinal = propertyDefinitionFieldSetEntity.getItems().stream().filter(x-> !x.getFields().isEmpty()).map(PropertyDefinitionFieldSetItemEntity::getOrdinal).max(Comparator.comparing(x -> x)).orElse(0) + 1; - int finalOrdinal = ordinal; - if (propertyDefinitionFieldSetEntity.getItems().stream().anyMatch(x -> x.getOrdinal() == finalOrdinal && !x.getFields().isEmpty())) - throw new MyApplicationException("Invalid multiple key " + key); - logger.error("Ordinal of group set to " + ordinal + " " + key); - } - } - } else { - propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity(); - propertyDefinitionFieldSetEntity.setItems(new ArrayList<>()); - } - } - if (!groupOrdinalMapping.containsKey(groupId + fieldSetId)){ - groupOrdinalMapping.put(groupId + fieldSetId, ordinal); - } else { - if (groupOrdinalMapping.get(groupId + fieldSetId) != ordinal) throw new MyApplicationException("Invalid multiple key ordinal " + key); + if (propertyDefinitionFieldSetEntity == null) { + propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity(); + propertyDefinitionFieldSetEntity.setItems(new ArrayList<>()); + propertyDefinitionEntity.getFieldSets().put(fieldId, propertyDefinitionFieldSetEntity); + } + if (ordinal == Integer.MIN_VALUE) { + if (groupOrdinalMapping.containsKey(new MultipleFieldKey(fieldSetId, groupId))){ + ordinal = groupOrdinalMapping.get(new MultipleFieldKey(fieldSetId, groupId)); + } else { + ordinal = propertyDefinitionFieldSetEntity.getItems().stream().filter(x-> !x.getFields().isEmpty()).map(PropertyDefinitionFieldSetItemEntity::getOrdinal).min(Comparator.comparing(x -> x)).orElse(-1) - 1; + ordinal = Math.min(ordinal, -1); + } + } + + if (!groupOrdinalMapping.containsKey(new MultipleFieldKey(fieldSetId, groupId))){ + groupOrdinalMapping.put(new MultipleFieldKey(fieldSetId, groupId), ordinal); + } else { + if (groupOrdinalMapping.get(new MultipleFieldKey(fieldSetId, groupId)) != ordinal) throw new MyApplicationException("Invalid multiple key ordinal " + key); } - if (propertyDefinitionFieldSetEntity == null) throw new MyApplicationException("Invalid multiple key group " + key); this.addMultipleField(propertyDefinitionFieldSetEntity, ordinal, currentField, properties, referenceTypeDefinitionEntityMap, referenceMap); + } } - return propertyDefinitionEntity; } private void addMultipleField(PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity, int ordinal, eu.eudat.commons.types.descriptiontemplate.FieldEntity currentField, Map properties, Map referenceTypeDefinitionEntityMap, Map referenceMap){ @@ -440,7 +452,7 @@ public class DatasetMigrationService { case INTERNAL_ENTRIES_DMPS -> throw new MyApplicationException("Found INTERNAL_ENTRIES_DMPS into description"); case INTERNAL_ENTRIES_DESCRIPTIONS -> throw new MyApplicationException("Found INTERNAL_ENTRIES_DMPS into description"); case REFERENCE_TYPES -> { - if(!this.conventionService.isNullOrEmpty(textValue)) { + if(!this.conventionService.isNullOrEmpty(textValue)) { //TODO Map[] references = this.jsonHandlingService.fromJsonSafe(Map[].class, textValue); if (references == null) references = this.jsonHandlingService.fromJsonSafe(Map[].class, this.cleanAsObjectString(textValue)); if (references == null) { @@ -587,8 +599,44 @@ public class DatasetMigrationService { this.name = name; } } + + public static class MultipleFieldKey{ + private final String fieldSetId; + private final String groupKey; - public class ReferenceKey { + private final int hashCode; + + public MultipleFieldKey(String fieldSetId, String groupKey) { + this.fieldSetId = fieldSetId; + this.groupKey = groupKey; + this.hashCode = Objects.hash(this.fieldSetId, this.groupKey); + } + + public String getFieldSetId() { + return fieldSetId; + } + + public String getGroupKey() { + return groupKey; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + MultipleFieldKey that = (MultipleFieldKey) o; + return Objects.equals(fieldSetId, that.getFieldSetId()) && Objects.equals(groupKey, that.getGroupKey()); + } + + @Override + public int hashCode() { + return this.hashCode; + } + } + + public static class ReferenceKey { private final UUID type; private final String source; private final String reference;