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.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;
@ -229,20 +230,36 @@ public class DatasetMigrationService {
}
}
Map<String, Integer> groupOrdinalMapping = new HashMap<>();
this.addMultipleField(properties, descriptionTemplateDefinitionEntity, propertyDefinitionEntity, referenceTypeDefinitionEntityMap, referenceMap);
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()) {
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,14 +268,14 @@ 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());
@ -266,35 +283,30 @@ public class DatasetMigrationService {
}
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);
}
}
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 {
propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity();
propertyDefinitionFieldSetEntity.setItems(new ArrayList<>());
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(groupId + fieldSetId)){
groupOrdinalMapping.put(groupId + fieldSetId, ordinal);
if (!groupOrdinalMapping.containsKey(new MultipleFieldKey(fieldSetId, groupId))){
groupOrdinalMapping.put(new MultipleFieldKey(fieldSetId, groupId), ordinal);
} else {
if (groupOrdinalMapping.get(groupId + fieldSetId) != ordinal) throw new MyApplicationException("Invalid multiple key ordinal " + key);
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<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_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<String, Object>[] references = this.jsonHandlingService.fromJsonSafe(Map[].class, textValue);
if (references == null) references = this.jsonHandlingService.fromJsonSafe(Map[].class, this.cleanAsObjectString(textValue));
if (references == null) {
@ -588,7 +600,43 @@ public class DatasetMigrationService {
}
}
public class ReferenceKey {
public static class MultipleFieldKey{
private final String fieldSetId;
private final String groupKey;
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;