2023-12-14 16:37:43 +01:00
|
|
|
package eu.old.eudat.migration;
|
|
|
|
|
|
|
|
import eu.eudat.commons.enums.IsActive;
|
|
|
|
import eu.eudat.data.DescriptionTagEntity;
|
2024-03-12 15:28:07 +01:00
|
|
|
import eu.eudat.data.ReferenceEntity;
|
2023-12-14 16:37:43 +01:00
|
|
|
import eu.eudat.data.TagEntity;
|
|
|
|
import eu.old.eudat.data.dao.entities.DatasetDao;
|
|
|
|
import eu.old.eudat.data.entities.Dataset;
|
|
|
|
import eu.old.eudat.elastic.entities.Tag;
|
|
|
|
import eu.old.eudat.elastic.repository.DatasetRepository;
|
|
|
|
import eu.old.eudat.logic.services.operations.DatabaseRepository;
|
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
|
|
import gr.cite.tools.logging.LoggerService;
|
|
|
|
import jakarta.persistence.EntityManager;
|
|
|
|
import jakarta.xml.bind.JAXBException;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.time.Instant;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class TagMigrationService {
|
|
|
|
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TagMigrationService.class));
|
|
|
|
private final DatabaseRepository databaseRepository;
|
|
|
|
private final DatasetRepository datasetRepository;
|
|
|
|
private static final int PageSize = 500;
|
|
|
|
private static final boolean TestMode = false;
|
|
|
|
private final EntityManager entityManager;
|
|
|
|
|
2024-03-12 15:28:07 +01:00
|
|
|
public TagMigrationService(DatabaseRepository databaseRepository, DatasetRepository datasetRepository, EntityManager entityManager) {
|
2023-12-14 16:37:43 +01:00
|
|
|
this.databaseRepository = databaseRepository;
|
|
|
|
this.datasetRepository = datasetRepository;
|
|
|
|
this.entityManager = entityManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void migrate() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
|
|
|
|
DatasetDao datasetDao = databaseRepository.getDatasetDao();
|
|
|
|
long total = datasetDao.asQueryable().count();
|
|
|
|
logger.debug("Migrate Tags for Dataset Total : " + total);
|
|
|
|
int page = 0;
|
|
|
|
|
2024-03-12 15:28:07 +01:00
|
|
|
Map<TagKey, UUID> savedTagIdsByName = new HashMap<>();
|
|
|
|
HashSet<UUID> existingTagIds = new HashSet<>();
|
2023-12-14 16:37:43 +01:00
|
|
|
|
|
|
|
List<Dataset> items;
|
|
|
|
do {
|
|
|
|
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();
|
|
|
|
if (items != null && !items.isEmpty()) {
|
|
|
|
logger.debug("Migrate Dataset tags " + page * PageSize + " of " + total);
|
|
|
|
|
2023-12-19 14:57:13 +01:00
|
|
|
List<eu.old.eudat.elastic.entities.Dataset> elasticDatasets = this.datasetRepository.findByIds(items.stream().map(x -> x.getId().toString()).toList());
|
|
|
|
|
2024-02-28 17:48:46 +01:00
|
|
|
for (Dataset item : items) {
|
2023-12-19 14:57:13 +01:00
|
|
|
List<eu.old.eudat.elastic.entities.Dataset> found = elasticDatasets.stream().filter(x -> item.getId().toString().equals(x.getId())).toList();
|
|
|
|
if (found.isEmpty()) {
|
2023-12-14 16:37:43 +01:00
|
|
|
logger.error("No dataset with id {} found on elastic search. Skipping tag migration for this dataset", item.getId());
|
|
|
|
continue;
|
|
|
|
}
|
2023-12-19 14:57:13 +01:00
|
|
|
eu.old.eudat.elastic.entities.Dataset elasticDataset = found.getFirst();
|
2023-12-14 16:37:43 +01:00
|
|
|
boolean tagAlreadyExists;
|
|
|
|
if (elasticDataset.getTags() != null && !elasticDataset.getTags().isEmpty()) {
|
2024-02-28 17:48:46 +01:00
|
|
|
for (Tag tag : elasticDataset.getTags()) {
|
2024-03-12 15:28:07 +01:00
|
|
|
tagAlreadyExists = savedTagIdsByName.containsKey(new TagKey(item, tag)); //TODO we want owner logic ?
|
2023-12-14 16:37:43 +01:00
|
|
|
if (!tagAlreadyExists) {
|
|
|
|
TagEntity tagEntity = new TagEntity();
|
2024-03-12 15:28:07 +01:00
|
|
|
if (!existingTagIds.contains(UUID.fromString(tag.getId()))) tagEntity.setId(UUID.fromString(tag.getId()));
|
|
|
|
else tagEntity.setId(UUID.randomUUID());
|
2023-12-14 16:37:43 +01:00
|
|
|
tagEntity.setLabel(tag.getName());
|
|
|
|
tagEntity.setCreatedAt(Instant.now());
|
|
|
|
tagEntity.setUpdatedAt(Instant.now());
|
2024-02-22 17:33:52 +01:00
|
|
|
if (item.getCreator() != null) tagEntity.setCreatedById(item.getCreator().getId());
|
2023-12-14 16:37:43 +01:00
|
|
|
tagEntity.setIsActive(IsActive.Active);
|
2024-03-12 15:28:07 +01:00
|
|
|
savedTagIdsByName.put(new TagKey(item, tag), tagEntity.getId());
|
2023-12-14 16:37:43 +01:00
|
|
|
this.entityManager.persist(tagEntity);
|
|
|
|
}
|
|
|
|
DescriptionTagEntity descriptionTagEntity = new DescriptionTagEntity();
|
|
|
|
descriptionTagEntity.setId(UUID.randomUUID());
|
2024-03-12 15:28:07 +01:00
|
|
|
descriptionTagEntity.setTagId(savedTagIdsByName.get(new TagKey(item, tag)));
|
2023-12-14 16:37:43 +01:00
|
|
|
descriptionTagEntity.setDescriptionId(item.getId());
|
|
|
|
descriptionTagEntity.setCreatedAt(Instant.now());
|
|
|
|
descriptionTagEntity.setUpdatedAt(Instant.now());
|
|
|
|
descriptionTagEntity.setIsActive(IsActive.Active);
|
|
|
|
this.entityManager.persist(descriptionTagEntity);
|
2024-03-12 15:28:07 +01:00
|
|
|
existingTagIds.add(descriptionTagEntity.getTagId());
|
2023-12-14 16:37:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.entityManager.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
page++;
|
|
|
|
}
|
|
|
|
} while (items != null && !items.isEmpty() && !TestMode);
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:28:07 +01:00
|
|
|
public static class TagKey {
|
|
|
|
private final UUID owner;
|
|
|
|
private final String name;
|
|
|
|
private final int hashCode;
|
|
|
|
|
|
|
|
|
|
|
|
public TagKey(Dataset item, Tag tag) {
|
|
|
|
this.name = tag.getName();
|
|
|
|
if (item.getCreator() != null) this.owner = item.getCreator().getId();
|
|
|
|
else this.owner = null;
|
|
|
|
hashCode = Objects.hash(this.owner, this.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public UUID getOwner() {
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o)
|
|
|
|
return true;
|
|
|
|
if (o == null || getClass() != o.getClass())
|
|
|
|
return false;
|
|
|
|
TagKey that = (TagKey) o;
|
|
|
|
return Objects.equals(owner, that.getOwner()) && Objects.equals(name, that.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return this.hashCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-14 16:37:43 +01:00
|
|
|
}
|