Fix on deleting duplicates from DmpDescriptionTemplates, delete all but one
This commit is contained in:
parent
8ee149f094
commit
ccd650217b
|
@ -1,35 +1,23 @@
|
|||
package eu.old.eudat.migration;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import eu.eudat.commons.JsonHandlingService;
|
||||
import eu.eudat.commons.XmlHandlingService;
|
||||
import eu.eudat.commons.enums.IsActive;
|
||||
import eu.eudat.commons.enums.ReferenceFieldDataType;
|
||||
import eu.eudat.commons.enums.ReferenceSourceType;
|
||||
import eu.eudat.commons.enums.ReferenceType;
|
||||
import eu.eudat.commons.types.reference.FieldEntity;
|
||||
import eu.eudat.data.DescriptionTemplateEntity;
|
||||
import eu.eudat.data.DmpBlueprintEntity;
|
||||
import eu.eudat.data.DmpDescriptionTemplateEntity;
|
||||
import eu.eudat.data.ReferenceEntity;
|
||||
import eu.eudat.model.DmpBlueprint;
|
||||
import eu.eudat.query.DescriptionTemplateQuery;
|
||||
import eu.eudat.query.DmpBlueprintQuery;
|
||||
import eu.eudat.query.DmpDescriptionTemplateQuery;
|
||||
import eu.old.eudat.data.dao.entities.DmpDatasetProfileDao;
|
||||
import eu.old.eudat.data.dao.entities.ResearcherDao;
|
||||
import eu.old.eudat.data.entities.DMPDatasetProfile;
|
||||
import eu.old.eudat.data.entities.Researcher;
|
||||
import eu.old.eudat.logic.services.operations.DatabaseRepository;
|
||||
import eu.old.eudat.queryable.QueryableList;
|
||||
import gr.cite.tools.data.query.Paging;
|
||||
import gr.cite.tools.data.query.QueryFactory;
|
||||
import gr.cite.tools.exception.MyApplicationException;
|
||||
import gr.cite.tools.logging.LoggerService;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.Tuple;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaDelete;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import jakarta.xml.bind.JAXBException;
|
||||
|
@ -40,6 +28,7 @@ import eu.eudat.commons.types.dmpblueprint.DefinitionEntity;
|
|||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
|
@ -144,6 +133,8 @@ public class DmpDatasetProfileMigrationService {
|
|||
List<Tuple> resultList = this.entityManager.createQuery(criteria).getResultList();
|
||||
|
||||
List<Tuple> duplicatesList = resultList.stream().filter(x -> (long) x.get(3) > 1).toList();
|
||||
CriteriaDelete<DmpDescriptionTemplateEntity> delete = b.createCriteriaDelete(DmpDescriptionTemplateEntity.class);
|
||||
Root<DmpDescriptionTemplateEntity> root1 = delete.from(DmpDescriptionTemplateEntity.class);
|
||||
for (Tuple duplicate : duplicatesList) {
|
||||
List<DmpDescriptionTemplateEntity> duplicateEntities = dmpDescriptionTemplateQuery
|
||||
.dmpIds((UUID) duplicate.get(0))
|
||||
|
@ -151,8 +142,11 @@ public class DmpDatasetProfileMigrationService {
|
|||
.sectionIds((UUID) duplicate.get(2))
|
||||
.collect();
|
||||
|
||||
for (int index = 0; index < duplicateEntities.size() - 1; index++)
|
||||
this.entityManager.remove(duplicateEntities.get(index));
|
||||
List<UUID> toDelete = new ArrayList<>(duplicateEntities.stream().map(DmpDescriptionTemplateEntity::getId).toList());
|
||||
int random = new SecureRandom().nextInt(0, toDelete.size());
|
||||
toDelete.remove(random);
|
||||
delete.where(root1.get("id").in(toDelete));
|
||||
this.entityManager.createQuery(delete).executeUpdate();
|
||||
}
|
||||
|
||||
entityManager.flush();
|
||||
|
|
Loading…
Reference in New Issue