migration changes

This commit is contained in:
Efstratios Giannopoulos 2024-02-26 10:53:11 +02:00
parent e0deaef950
commit 0e7813cded
1 changed files with 83 additions and 35 deletions

View File

@ -21,6 +21,7 @@ import eu.eudat.query.*;
import eu.old.eudat.data.dao.entities.DatasetDao; import eu.old.eudat.data.dao.entities.DatasetDao;
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.user.components.datasetprofile.Field;
import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.fieldset.BaseFieldSet; import gr.cite.tools.fieldset.BaseFieldSet;
@ -228,21 +229,37 @@ public class DatasetMigrationService {
} }
} }
} }
this.addMultipleField(properties, descriptionTemplateDefinitionEntity, propertyDefinitionEntity, referenceTypeDefinitionEntityMap, referenceMap);
Map<String, Integer> 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<String, Object> properties, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinitionEntity, PropertyDefinitionEntity propertyDefinitionEntity, Map<UUID, ReferenceTypeDefinitionEntity> referenceTypeDefinitionEntityMap, Map<ReferenceKey, ReferenceEntity> referenceMap){
Map<MultipleFieldKey, Integer> groupOrdinalMapping = new HashMap<>();
for (String key : properties.keySet()) { for (String key : properties.keySet()) {
if (key.trim().toLowerCase(Locale.ROOT).startsWith("multiple_")) { if (key.trim().toLowerCase(Locale.ROOT).startsWith("multiple_")) {
String newKey = key.trim().substring("multiple_".length()); String newKey = key.trim().substring("multiple_".length());
String[] keyParts = newKey.split("_", 4); String[] keyParts = newKey.split("_", 4);
String fieldSetId = ""; String fieldSetId = "";
String groupId = ""; String groupId = "";
int ordinal = 0; int ordinal = Integer.MIN_VALUE;
String fieldId = ""; String fieldId = "";
try {
if (keyParts.length == 4) { if (keyParts.length == 4) {
fieldSetId = keyParts[0].trim(); fieldSetId = keyParts[0].trim();
groupId = keyParts[1].trim(); groupId = keyParts[1].trim();
ordinal = Integer.parseInt(keyParts[2].trim());
fieldId = keyParts[3].trim(); fieldId = keyParts[3].trim();
} else if (keyParts.length == 3) { } else if (keyParts.length == 3) {
fieldSetId = keyParts[0].trim(); fieldSetId = keyParts[0].trim();
@ -251,50 +268,45 @@ public class DatasetMigrationService {
} else { } else {
logger.error("Field group key has invalid format " + key + " " + properties.get(key).toString()); logger.error("Field group key has invalid format " + key + " " + properties.get(key).toString());
continue; continue;
//throw new MyApplicationException("Invalid multiple key " + key);
} }
try {
if (keyParts.length == 4) ordinal = Integer.parseInt(keyParts[2].trim());
} catch (Exception e){ } catch (Exception e){
logger.error("Field group key has invalid format " + key + " " + properties.get(key).toString()); 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); eu.eudat.commons.types.descriptiontemplate.FieldEntity currentField = descriptionTemplateDefinitionEntity.getFieldById(fieldId).stream().findFirst().orElse(null);
if (currentField == null) { if (currentField == null) {
logger.error("Field set not found for field " + key + " " + properties.get(key).toString()); logger.error("Field set not found for field " + key + " " + properties.get(key).toString());
continue; continue;
} }
PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity = propertyDefinitionEntity.getFieldSets().getOrDefault(fieldSetId, null); PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity = propertyDefinitionEntity.getFieldSets().getOrDefault(fieldSetId, null);
if (ordinal == 0) { if (propertyDefinitionFieldSetEntity == null) {
if (propertyDefinitionFieldSetEntity != null ) { propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity();
if (groupOrdinalMapping.containsKey(groupId + fieldSetId)){ propertyDefinitionFieldSetEntity.setItems(new ArrayList<>());
ordinal = groupOrdinalMapping.get(groupId + fieldSetId); propertyDefinitionEntity.getFieldSets().put(fieldId, propertyDefinitionFieldSetEntity);
} else { }
if (propertyDefinitionFieldSetEntity.getItems().stream().anyMatch(x-> x.getOrdinal() == 0 && !x.getFields().isEmpty())) { if (ordinal == Integer.MIN_VALUE) {
ordinal = propertyDefinitionFieldSetEntity.getItems().stream().filter(x-> !x.getFields().isEmpty()).map(PropertyDefinitionFieldSetItemEntity::getOrdinal).max(Comparator.comparing(x -> x)).orElse(0) + 1; if (groupOrdinalMapping.containsKey(new MultipleFieldKey(fieldSetId, groupId))){
int finalOrdinal = ordinal; ordinal = groupOrdinalMapping.get(new MultipleFieldKey(fieldSetId, groupId));
if (propertyDefinitionFieldSetEntity.getItems().stream().anyMatch(x -> x.getOrdinal() == finalOrdinal && !x.getFields().isEmpty())) } else {
throw new MyApplicationException("Invalid multiple key " + key); ordinal = propertyDefinitionFieldSetEntity.getItems().stream().filter(x-> !x.getFields().isEmpty()).map(PropertyDefinitionFieldSetItemEntity::getOrdinal).min(Comparator.comparing(x -> x)).orElse(-1) - 1;
logger.error("Ordinal of group set to " + ordinal + " " + key); ordinal = Math.min(ordinal, -1);
} }
} }
} else {
propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity(); if (!groupOrdinalMapping.containsKey(new MultipleFieldKey(fieldSetId, groupId))){
propertyDefinitionFieldSetEntity.setItems(new ArrayList<>()); 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 (!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) throw new MyApplicationException("Invalid multiple key group " + key);
this.addMultipleField(propertyDefinitionFieldSetEntity, ordinal, currentField, properties, referenceTypeDefinitionEntityMap, referenceMap); 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<String, Object> properties, Map<UUID, ReferenceTypeDefinitionEntity> referenceTypeDefinitionEntityMap, Map<ReferenceKey, ReferenceEntity> referenceMap){ private void addMultipleField(PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity, int ordinal, eu.eudat.commons.types.descriptiontemplate.FieldEntity currentField, Map<String, Object> properties, Map<UUID, ReferenceTypeDefinitionEntity> referenceTypeDefinitionEntityMap, Map<ReferenceKey, ReferenceEntity> referenceMap){
@ -440,7 +452,7 @@ public class DatasetMigrationService {
case INTERNAL_ENTRIES_DMPS -> throw new MyApplicationException("Found INTERNAL_ENTRIES_DMPS into description"); 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 INTERNAL_ENTRIES_DESCRIPTIONS -> throw new MyApplicationException("Found INTERNAL_ENTRIES_DMPS into description");
case REFERENCE_TYPES -> { case REFERENCE_TYPES -> {
if(!this.conventionService.isNullOrEmpty(textValue)) { if(!this.conventionService.isNullOrEmpty(textValue)) { //TODO
Map<String, Object>[] references = this.jsonHandlingService.fromJsonSafe(Map[].class, textValue); Map<String, Object>[] references = this.jsonHandlingService.fromJsonSafe(Map[].class, textValue);
if (references == null) references = this.jsonHandlingService.fromJsonSafe(Map[].class, this.cleanAsObjectString(textValue)); if (references == null) references = this.jsonHandlingService.fromJsonSafe(Map[].class, this.cleanAsObjectString(textValue));
if (references == null) { if (references == null) {
@ -587,8 +599,44 @@ public class DatasetMigrationService {
this.name = name; 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 UUID type;
private final String source; private final String source;
private final String reference; private final String reference;