2023-11-23 09:26:40 +01:00
|
|
|
package eu.old.eudat.migration;
|
|
|
|
|
|
|
|
import eu.eudat.commons.JsonHandlingService;
|
2023-11-23 14:23:49 +01:00
|
|
|
import eu.eudat.commons.XmlHandlingService;
|
2024-02-22 17:33:52 +01:00
|
|
|
import eu.eudat.commons.enums.DescriptionStatus;
|
|
|
|
import eu.eudat.commons.enums.IsActive;
|
|
|
|
import eu.eudat.commons.types.description.*;
|
2024-02-21 17:38:28 +01:00
|
|
|
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
|
2023-11-23 14:23:49 +01:00
|
|
|
import eu.eudat.commons.types.dmpblueprint.DefinitionEntity;
|
2024-02-22 17:33:52 +01:00
|
|
|
import eu.eudat.convention.ConventionService;
|
2024-02-21 17:38:28 +01:00
|
|
|
import eu.eudat.data.*;
|
2023-11-23 17:09:17 +01:00
|
|
|
import eu.eudat.model.Dmp;
|
2024-02-21 17:38:28 +01:00
|
|
|
import eu.eudat.query.DescriptionTemplateQuery;
|
2023-11-23 14:23:49 +01:00
|
|
|
import eu.eudat.query.DmpBlueprintQuery;
|
2023-11-23 17:09:17 +01:00
|
|
|
import eu.eudat.query.DmpDescriptionTemplateQuery;
|
|
|
|
import eu.eudat.query.DmpQuery;
|
2023-11-23 14:23:49 +01:00
|
|
|
import eu.old.eudat.data.dao.entities.DatasetDao;
|
2023-11-23 09:26:40 +01:00
|
|
|
import eu.old.eudat.data.entities.Dataset;
|
|
|
|
import eu.old.eudat.logic.services.operations.DatabaseRepository;
|
2023-11-23 14:23:49 +01:00
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
|
|
import gr.cite.tools.exception.MyApplicationException;
|
2023-11-23 17:09:17 +01:00
|
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
2023-11-23 14:23:49 +01:00
|
|
|
import gr.cite.tools.logging.LoggerService;
|
2023-11-23 09:26:40 +01:00
|
|
|
import jakarta.persistence.EntityManager;
|
2023-11-23 14:23:49 +01:00
|
|
|
import jakarta.xml.bind.JAXBException;
|
2023-11-23 17:09:17 +01:00
|
|
|
import org.json.JSONObject;
|
2023-11-23 14:23:49 +01:00
|
|
|
import org.slf4j.LoggerFactory;
|
2023-11-23 09:26:40 +01:00
|
|
|
import org.springframework.stereotype.Service;
|
2023-11-23 14:23:49 +01:00
|
|
|
import org.xml.sax.SAXException;
|
2023-11-23 09:26:40 +01:00
|
|
|
|
2023-11-23 14:23:49 +01:00
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.time.Instant;
|
2024-02-22 17:33:52 +01:00
|
|
|
import java.time.LocalDate;
|
|
|
|
import java.time.ZoneOffset;
|
|
|
|
import java.time.format.DateTimeParseException;
|
2023-11-23 14:23:49 +01:00
|
|
|
import java.util.*;
|
2023-11-23 17:09:17 +01:00
|
|
|
import java.util.stream.Collectors;
|
2023-11-23 09:26:40 +01:00
|
|
|
|
|
|
|
@Service
|
|
|
|
public class DatasetMigrationService {
|
|
|
|
|
2023-11-23 14:23:49 +01:00
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpDatasetProfileMigrationService.class));
|
2023-11-23 09:26:40 +01:00
|
|
|
private final DatabaseRepository databaseRepository;
|
|
|
|
private final JsonHandlingService jsonHandlingService;
|
2023-11-23 14:23:49 +01:00
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final XmlHandlingService xmlHandlingService;
|
|
|
|
private static final int PageSize = 500;
|
|
|
|
private static final boolean TestMode = false;
|
2023-11-23 09:26:40 +01:00
|
|
|
private final EntityManager entityManager;
|
2024-02-22 17:33:52 +01:00
|
|
|
private final ConventionService conventionService;
|
2023-11-23 09:26:40 +01:00
|
|
|
|
2024-02-22 17:33:52 +01:00
|
|
|
public DatasetMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, QueryFactory queryFactory, XmlHandlingService xmlHandlingService, EntityManager entityManager, ConventionService conventionService) {
|
2023-11-23 09:26:40 +01:00
|
|
|
this.databaseRepository = databaseRepository;
|
|
|
|
this.jsonHandlingService = jsonHandlingService;
|
2023-11-23 14:23:49 +01:00
|
|
|
this.queryFactory = queryFactory;
|
|
|
|
this.xmlHandlingService = xmlHandlingService;
|
2023-11-23 09:26:40 +01:00
|
|
|
this.entityManager = entityManager;
|
2024-02-22 17:33:52 +01:00
|
|
|
this.conventionService = conventionService;
|
2023-11-23 09:26:40 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 14:23:49 +01:00
|
|
|
public void migrate() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
|
|
|
|
DatasetDao datasetDao = databaseRepository.getDatasetDao();
|
|
|
|
long total = datasetDao.asQueryable().count();
|
|
|
|
logger.debug("Migrate Dataset Total : " + total);
|
|
|
|
int page = 0;
|
|
|
|
|
|
|
|
List<Dataset> items;
|
|
|
|
do {
|
2023-11-23 17:09:17 +01:00
|
|
|
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();
|
2023-11-23 14:23:49 +01:00
|
|
|
if (items != null && !items.isEmpty()) {
|
|
|
|
logger.debug("Migrate Dataset " + page * PageSize + " of " + total);
|
|
|
|
|
2023-11-23 17:09:17 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-02-21 17:38:28 +01:00
|
|
|
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();
|
2023-11-23 17:09:17 +01:00
|
|
|
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));;
|
|
|
|
|
2024-02-21 17:38:28 +01:00
|
|
|
Map<UUID, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity> descriptionTemplateDefinitionMap = new HashMap<>();
|
|
|
|
for (DescriptionTemplateEntity descriptionTemplateEntity : descriptionTemplates){
|
|
|
|
descriptionTemplateDefinitionMap.put(descriptionTemplateEntity.getId(), this.xmlHandlingService.fromXml(eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplateEntity.getDefinition()));
|
|
|
|
}
|
2023-11-23 14:23:49 +01:00
|
|
|
for (Dataset item : items) {
|
2023-11-23 17:09:17 +01:00
|
|
|
DefinitionEntity definition = dmpBlueprintsMap.getOrDefault(item.getDmp().getProfile().getId(), null);
|
|
|
|
|
2023-11-29 15:56:25 +01:00
|
|
|
if (definition == null || definition.getSections() == null || definition.getSections().size() <= item.getDmpSectionIndex()) {
|
2023-11-23 17:09:17 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2024-02-21 17:38:28 +01:00
|
|
|
List<DmpDescriptionTemplateEntity> itemDescriptionTemplates = this.getOrCreateDmpDescriptionTemplateEntity(item, sectionId, dmpDescriptionTemplateEntities);
|
2023-11-23 17:09:17 +01:00
|
|
|
|
|
|
|
if (itemDescriptionTemplates.size() > 1) {
|
|
|
|
logger.error("Migrate Dataset " + item.getId() + " multiple DmpDescriptionTemplateEntity for section " + item.getDmpSectionIndex());
|
|
|
|
//TODO CLEAN Duplicates dmp, DescriptionTemplate, SectionId
|
|
|
|
}
|
|
|
|
|
2023-11-23 14:23:49 +01:00
|
|
|
DescriptionEntity data = new DescriptionEntity();
|
2023-11-23 17:09:17 +01:00
|
|
|
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());
|
2024-02-21 17:38:28 +01:00
|
|
|
data.setDmpDescriptionTemplateId(itemDescriptionTemplates.getFirst().getId());
|
2023-11-24 16:18:28 +01:00
|
|
|
data.setDescriptionTemplateId(item.getProfile().getId());
|
2023-11-23 17:09:17 +01:00
|
|
|
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) {
|
2023-11-23 14:23:49 +01:00
|
|
|
data.setIsActive(IsActive.Inactive);
|
|
|
|
data.setStatus(DescriptionStatus.Canceled);
|
|
|
|
} else {
|
|
|
|
data.setIsActive(IsActive.Active);
|
2023-11-23 17:09:17 +01:00
|
|
|
data.setStatus(DescriptionStatus.of(item.getStatus()));
|
|
|
|
}
|
|
|
|
|
2024-02-21 17:38:28 +01:00
|
|
|
eu.eudat.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinitionEntity = descriptionTemplateDefinitionMap.getOrDefault(item.getProfile().getId(), null);
|
2024-02-22 17:33:52 +01:00
|
|
|
List<Tag> existingTags = new ArrayList<>();
|
2024-02-21 17:38:28 +01:00
|
|
|
|
2024-02-22 17:33:52 +01:00
|
|
|
data.setProperties(this.jsonHandlingService.toJson(this.buildPropertyDefinitionEntity(item, descriptionTemplateDefinitionEntity, existingTags)));
|
2023-11-23 17:09:17 +01:00
|
|
|
|
2023-11-24 16:18:28 +01:00
|
|
|
if (data.getCreatedById() == null){
|
2023-11-23 17:09:17 +01:00
|
|
|
logger.error("Migration skipped creator not found " + item.getId());
|
|
|
|
throw new MyApplicationException("Migration skipped creator not found " + item.getId());
|
2023-11-23 14:23:49 +01:00
|
|
|
}
|
2024-02-22 17:33:52 +01:00
|
|
|
|
|
|
|
this.persistTags(existingTags, data);
|
2023-11-23 14:23:49 +01:00
|
|
|
|
|
|
|
this.entityManager.persist(data);
|
|
|
|
this.entityManager.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
page++;
|
2023-11-23 09:26:40 +01:00
|
|
|
}
|
2023-11-23 14:23:49 +01:00
|
|
|
} while (items != null && !items.isEmpty() && !TestMode);
|
2024-02-22 17:33:52 +01:00
|
|
|
throw new MyApplicationException("");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void persistTags(List<Tag> existingTags, DescriptionEntity data){
|
|
|
|
for (Tag tag : existingTags) {
|
|
|
|
TagEntity tagEntity = new TagEntity();
|
|
|
|
tagEntity.setId(UUID.fromString(tag.getId()));
|
|
|
|
tagEntity.setLabel(tag.getName());
|
|
|
|
tagEntity.setCreatedAt(Instant.now());
|
|
|
|
tagEntity.setUpdatedAt(Instant.now());
|
|
|
|
tagEntity.setCreatedById(data.getCreatedById());
|
|
|
|
tagEntity.setIsActive(IsActive.Active);
|
|
|
|
this.entityManager.persist(tagEntity);
|
|
|
|
}
|
2023-11-23 09:26:40 +01:00
|
|
|
}
|
2024-02-21 17:38:28 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
if (itemDescriptionTemplates.isEmpty()) {
|
|
|
|
logger.error("Migrate Dataset " + item.getId() + " cannot found DmpDescriptionTemplateEntity for section " + item.getDmpSectionIndex());
|
2024-02-22 17:33:52 +01:00
|
|
|
if (dmpDescriptionTemplateEntities.stream().anyMatch(x -> x.getDmpId().equals(item.getDmp().getId()) && x.getSectionId().equals(sectionId))) {
|
2024-02-21 17:38:28 +01:00
|
|
|
DmpDescriptionTemplateEntity dmpDescriptionTemplateEntity = new DmpDescriptionTemplateEntity();
|
|
|
|
dmpDescriptionTemplateEntity.setId(UUID.randomUUID());
|
|
|
|
dmpDescriptionTemplateEntity.setDescriptionTemplateGroupId(item.getProfile().getGroupId());
|
|
|
|
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(IsActive.Active);
|
|
|
|
this.entityManager.persist(dmpDescriptionTemplateEntity);
|
|
|
|
itemDescriptionTemplates = List.of(dmpDescriptionTemplateEntity);
|
|
|
|
} else {
|
|
|
|
throw new MyApplicationException("Migrate Dataset " + item.getId() + " cannot found DmpDescriptionTemplateEntity for section " + item.getDmpSectionIndex());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return itemDescriptionTemplates;
|
|
|
|
}
|
|
|
|
|
2024-02-22 17:33:52 +01:00
|
|
|
private PropertyDefinitionEntity buildPropertyDefinitionEntity(Dataset item, eu.eudat.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinitionEntity, List<Tag> existingTags) {
|
|
|
|
if (this.conventionService.isNullOrEmpty(item.getProperties())) return null;
|
2024-02-21 17:38:28 +01:00
|
|
|
JSONObject jObject = new JSONObject(item.getProperties());
|
|
|
|
Map<String, Object> properties = jObject.toMap();
|
|
|
|
|
|
|
|
PropertyDefinitionEntity propertyDefinitionEntity = new PropertyDefinitionEntity();
|
2024-02-22 17:33:52 +01:00
|
|
|
propertyDefinitionEntity.setFieldSets(new HashMap<>());
|
|
|
|
|
2024-02-21 17:38:28 +01:00
|
|
|
List<FieldSetEntity> fieldSetEntities = descriptionTemplateDefinitionEntity.getAllFieldSets();
|
|
|
|
|
2024-02-22 17:33:52 +01:00
|
|
|
for (String key : properties.keySet()) {
|
|
|
|
if (key.toLowerCase(Locale.ROOT).trim().startsWith("commentfieldvalue")){
|
|
|
|
if (properties.get(key) != null && this.conventionService.isNullOrEmpty(properties.get(key).toString())) { continue; }
|
|
|
|
FieldSetEntity currentFieldSet = fieldSetEntities.stream().filter(x-> x.getId().equals(key.trim().substring("commentfieldvalue".length()))).findFirst().orElse(null);
|
|
|
|
|
|
|
|
if (currentFieldSet == null) {
|
|
|
|
logger.error("Field set " + key + " not found for comment " + properties.get(key).toString());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addSimpleCommentField(propertyDefinitionEntity, currentFieldSet, properties.get(key).toString());
|
|
|
|
} else {
|
|
|
|
if (!key.toLowerCase(Locale.ROOT).trim().startsWith("multiple_")) {
|
|
|
|
FieldSetEntity currentFieldSet = null;
|
|
|
|
eu.eudat.commons.types.descriptiontemplate.FieldEntity currentField = null;
|
|
|
|
for (FieldSetEntity fieldSetEntity : fieldSetEntities) {
|
|
|
|
List<eu.eudat.commons.types.descriptiontemplate.FieldEntity> fieldEntities = fieldSetEntity.getFieldById(key.trim());
|
|
|
|
if (!fieldEntities.isEmpty()) {
|
|
|
|
currentFieldSet = fieldSetEntity;
|
|
|
|
currentField = fieldEntities.getFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentField == null) {
|
|
|
|
logger.error("Field set not found for field " + key + " " + properties.get(key).toString());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addSimpleField(propertyDefinitionEntity, currentFieldSet, currentField, properties, existingTags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, 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;
|
|
|
|
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();
|
|
|
|
groupId = keyParts[1].trim();
|
|
|
|
fieldId = keyParts[2].trim();
|
|
|
|
} else {
|
|
|
|
logger.error("Field group key has invalid format " + key + " " + properties.get(key).toString());
|
|
|
|
continue;
|
|
|
|
//throw new MyApplicationException("Invalid multiple key " + key);
|
|
|
|
}
|
|
|
|
} catch (Exception e){
|
|
|
|
logger.error("Field group key has invalid format " + key + " " + properties.get(key).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) throw new MyApplicationException("Invalid multiple key group " + key);
|
|
|
|
|
|
|
|
this.addMultipleField(propertyDefinitionFieldSetEntity, ordinal, currentField, properties, existingTags);
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 17:38:28 +01:00
|
|
|
return propertyDefinitionEntity;
|
2024-02-22 17:33:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addMultipleField(PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity, int ordinal, eu.eudat.commons.types.descriptiontemplate.FieldEntity currentField, Map<String, Object> properties, List<Tag> existingTags){
|
|
|
|
PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity = propertyDefinitionFieldSetEntity.getItems().stream().filter(x-> x.getOrdinal() == ordinal).findFirst().orElse(null);
|
|
|
|
|
|
|
|
if (propertyDefinitionFieldSetItemEntity == null){
|
|
|
|
propertyDefinitionFieldSetItemEntity = new PropertyDefinitionFieldSetItemEntity();
|
|
|
|
propertyDefinitionFieldSetItemEntity.setFields(new HashMap<>());
|
|
|
|
propertyDefinitionFieldSetItemEntity.setOrdinal(ordinal);
|
|
|
|
propertyDefinitionFieldSetEntity.getItems().add(propertyDefinitionFieldSetItemEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
propertyDefinitionFieldSetItemEntity.getFields().put(currentField.getId(), this.buildField(currentField, properties, existingTags));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addSimpleField(PropertyDefinitionEntity propertyDefinitionEntity, FieldSetEntity currentFieldSet, eu.eudat.commons.types.descriptiontemplate.FieldEntity currentField, Map<String, Object> properties, List<Tag> existingTags){
|
|
|
|
PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity = propertyDefinitionEntity.getFieldSets().getOrDefault(currentFieldSet.getId(), null);
|
|
|
|
if (propertyDefinitionFieldSetEntity == null) {
|
|
|
|
propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity();
|
|
|
|
propertyDefinitionFieldSetEntity.setItems(new ArrayList<>());
|
|
|
|
propertyDefinitionEntity.getFieldSets().put(currentFieldSet.getId(), propertyDefinitionFieldSetEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity = null;
|
|
|
|
if (this.conventionService.isListNullOrEmpty(propertyDefinitionFieldSetEntity.getItems())){
|
|
|
|
propertyDefinitionFieldSetItemEntity = new PropertyDefinitionFieldSetItemEntity();
|
|
|
|
propertyDefinitionFieldSetItemEntity.setFields(new HashMap<>());
|
|
|
|
propertyDefinitionFieldSetItemEntity.setOrdinal(0);
|
|
|
|
propertyDefinitionFieldSetEntity.getItems().add(propertyDefinitionFieldSetItemEntity);
|
|
|
|
} else {
|
|
|
|
propertyDefinitionFieldSetItemEntity = propertyDefinitionFieldSetEntity.getItems().getFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
propertyDefinitionFieldSetItemEntity.getFields().put(currentField.getId(), this.buildField(currentField, properties, existingTags));
|
|
|
|
}
|
2024-02-21 17:38:28 +01:00
|
|
|
|
2024-02-22 17:33:52 +01:00
|
|
|
|
|
|
|
private void addSimpleCommentField(PropertyDefinitionEntity propertyDefinitionEntity, FieldSetEntity currentFieldSet, String comment){
|
|
|
|
PropertyDefinitionFieldSetEntity propertyDefinitionFieldSetEntity = propertyDefinitionEntity.getFieldSets().getOrDefault(currentFieldSet.getId(), null);
|
|
|
|
if (propertyDefinitionFieldSetEntity == null) {
|
|
|
|
propertyDefinitionFieldSetEntity = new PropertyDefinitionFieldSetEntity();
|
|
|
|
propertyDefinitionFieldSetEntity.setItems(new ArrayList<>());
|
|
|
|
propertyDefinitionEntity.getFieldSets().put(currentFieldSet.getId(), propertyDefinitionFieldSetEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity = null;
|
|
|
|
if (this.conventionService.isListNullOrEmpty(propertyDefinitionFieldSetEntity.getItems())){
|
|
|
|
propertyDefinitionFieldSetItemEntity = new PropertyDefinitionFieldSetItemEntity();
|
|
|
|
propertyDefinitionFieldSetItemEntity.setFields(new HashMap<>());
|
|
|
|
propertyDefinitionFieldSetItemEntity.setOrdinal(0);
|
|
|
|
propertyDefinitionFieldSetEntity.getItems().add(propertyDefinitionFieldSetItemEntity);
|
|
|
|
} else {
|
|
|
|
propertyDefinitionFieldSetItemEntity = propertyDefinitionFieldSetEntity.getItems().getFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
propertyDefinitionFieldSetItemEntity.setComment(comment);
|
2024-02-21 17:38:28 +01:00
|
|
|
}
|
2024-02-22 17:33:52 +01:00
|
|
|
|
|
|
|
private FieldEntity buildField(eu.eudat.commons.types.descriptiontemplate.FieldEntity currentField, Map<String, Object> properties, List<Tag> existingTags){
|
|
|
|
FieldEntity fieldEntity = new FieldEntity();
|
|
|
|
String textValue = properties.get(currentField.getId()) != null ? properties.get(currentField.getId()).toString() : null;
|
|
|
|
if (textValue == null || textValue.isEmpty()) return fieldEntity;
|
|
|
|
switch (currentField.getData().getFieldType()){
|
|
|
|
case FREE_TEXT, TEXT_AREA, RICH_TEXT_AREA, RADIO_BOX -> fieldEntity.setTextValue(textValue.trim());
|
|
|
|
case CHECK_BOX, BOOLEAN_DECISION -> fieldEntity.setTextValue(textValue.trim().toLowerCase(Locale.ROOT));
|
|
|
|
case DATE_PICKER -> {
|
|
|
|
Instant instant = null;
|
|
|
|
if(!this.conventionService.isNullOrEmpty(textValue)) {
|
|
|
|
try {
|
|
|
|
instant = Instant.parse(textValue);
|
|
|
|
} catch (DateTimeParseException ex) {
|
|
|
|
instant = LocalDate.parse(textValue).atStartOfDay().toInstant(ZoneOffset.UTC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fieldEntity.setDateValue(instant);
|
|
|
|
}
|
|
|
|
case SELECT -> {
|
|
|
|
if(!this.conventionService.isNullOrEmpty(textValue)) {
|
|
|
|
String[] valuesParsed = this.jsonHandlingService.fromJsonSafe(String[].class, textValue);
|
|
|
|
if (valuesParsed == null) valuesParsed = this.jsonHandlingService.fromJsonSafe(String[].class, this.cleanAsObjectString(textValue));
|
|
|
|
fieldEntity.setTextListValue(valuesParsed == null ? List.of(textValue) : Arrays.stream(valuesParsed).toList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case DATASET_IDENTIFIER, VALIDATION -> {
|
|
|
|
if(!this.conventionService.isNullOrEmpty(textValue)) {
|
|
|
|
ExternalIdentifierEntity externalIdentifierEntity = this.jsonHandlingService.fromJsonSafe(ExternalIdentifierEntity.class, textValue);
|
|
|
|
if (externalIdentifierEntity == null) externalIdentifierEntity = this.jsonHandlingService.fromJsonSafe(ExternalIdentifierEntity.class, this.cleanAsObjectString(textValue));
|
|
|
|
if (externalIdentifierEntity == null) throw new MyApplicationException("Could not parse dataset External Identifier : " + textValue);
|
|
|
|
fieldEntity.setExternalIdentifier(externalIdentifierEntity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case UPLOAD -> {
|
|
|
|
if(!this.conventionService.isNullOrEmpty(textValue)) {
|
|
|
|
Map<String, String> valuesParsed = this.jsonHandlingService.fromJsonSafe(Map.class, textValue);
|
|
|
|
if (valuesParsed == null) {
|
|
|
|
valuesParsed = (Map<String, String>) properties.get(currentField.getId());
|
|
|
|
}
|
|
|
|
if (valuesParsed == null) valuesParsed = this.jsonHandlingService.fromJsonSafe(Map.class, this.cleanAsObjectString(textValue));
|
|
|
|
if (valuesParsed == null) throw new MyApplicationException("Could not parse upload : " + textValue);
|
|
|
|
String id = valuesParsed.getOrDefault("id", null);
|
|
|
|
if (id == null) throw new MyApplicationException("Could not parse upload : " + textValue);
|
|
|
|
fieldEntity.setTextValue(UUID.fromString(id).toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case CURRENCY -> {
|
|
|
|
if(!this.conventionService.isNullOrEmpty(textValue)) {
|
|
|
|
Currency currency = this.jsonHandlingService.fromJsonSafe(Currency.class, textValue);
|
|
|
|
if (currency == null) currency = this.jsonHandlingService.fromJsonSafe(Currency.class, this.cleanAsObjectString(textValue));
|
|
|
|
//if (currency == null) throw new MyApplicationException("Could not parse Currency : " + textValue);
|
|
|
|
//TODO: {"name":"Euro","value":"EUR"} what we want to keep ?
|
|
|
|
|
|
|
|
fieldEntity.setTextValue(currency == null ? textValue : currency.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case TAGS -> {
|
|
|
|
if(!this.conventionService.isNullOrEmpty(textValue)) {
|
|
|
|
Tag[] tags = this.jsonHandlingService.fromJsonSafe(Tag[].class, textValue);
|
|
|
|
if (tags == null) tags = this.jsonHandlingService.fromJsonSafe(Tag[].class, this.cleanAsObjectString(textValue));
|
|
|
|
if (tags == null) {
|
|
|
|
Tag tag = this.jsonHandlingService.fromJsonSafe(Tag.class, textValue);
|
|
|
|
if (tag == null) tag = this.jsonHandlingService.fromJsonSafe(Tag.class, this.cleanAsObjectString(textValue));
|
|
|
|
if (tag != null) tags = List.of(tag).toArray(Tag[]::new);
|
|
|
|
}
|
|
|
|
if (tags == null)
|
|
|
|
logger.error("Could not parse tag : " + textValue);
|
|
|
|
else {
|
|
|
|
fieldEntity.setTextListValue(new ArrayList<>());
|
|
|
|
for (Tag tag : tags){
|
|
|
|
Tag existingTag = existingTags.stream().filter(x-> x.getName().equalsIgnoreCase(tag.getName())).findFirst().orElse(null);
|
|
|
|
if (existingTag == null){
|
|
|
|
try {
|
|
|
|
UUID.fromString(tag.getId());
|
|
|
|
} catch (IllegalArgumentException e){
|
|
|
|
tag.setId(UUID.randomUUID().toString());
|
|
|
|
}
|
|
|
|
existingTag = tag;
|
|
|
|
existingTags.add(tag);
|
|
|
|
}
|
|
|
|
fieldEntity.getTextListValue().add(existingTag.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO: should create Description tag maybe should also check the persist
|
|
|
|
//TODO: migrate values maybe we should merge
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case INTERNAL_ENTRIES_DMPS -> fieldEntity.setTextValue(textValue.trim());
|
|
|
|
case INTERNAL_ENTRIES_DESCRIPTIONS -> fieldEntity.setTextValue(textValue.trim());
|
|
|
|
case REFERENCE_TYPES -> fieldEntity.setTextValue(textValue.trim());
|
|
|
|
case EXTERNAL_DATASETS -> fieldEntity.setTextValue(textValue.trim());
|
|
|
|
}
|
|
|
|
return fieldEntity;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String cleanAsObjectString(String value){
|
|
|
|
value = value.trim().replace("\\", "");
|
|
|
|
while (!this.conventionService.isNullOrEmpty(value) && value.startsWith("\"")) value = value.substring(1);
|
|
|
|
while (!this.conventionService.isNullOrEmpty(value) && value.endsWith("\"")) value = value.substring(0, value.length() - 1);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Currency{
|
|
|
|
private String name;
|
|
|
|
private String value;
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getValue() {
|
|
|
|
return value;
|
|
|
|
}
|
2023-11-23 09:26:40 +01:00
|
|
|
|
2024-02-22 17:33:52 +01:00
|
|
|
public void setValue(String value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static class Tag{
|
|
|
|
private String id;
|
|
|
|
private String name;
|
|
|
|
|
|
|
|
public String getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setId(String id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
}
|
2023-11-23 09:26:40 +01:00
|
|
|
}
|