Fixing duplicate selection from DmpDescriptionTemplates

This commit is contained in:
Thomas Georgios Giannos 2023-11-24 16:46:30 +02:00
parent 3d18ed1fa6
commit 8ee149f094
2 changed files with 53 additions and 31 deletions

View File

@ -38,6 +38,8 @@ public class DmpDescriptionTemplateQuery extends QueryBase<DmpDescriptionTemplat
private Collection<UUID> descriptionTemplateGroupIds; private Collection<UUID> descriptionTemplateGroupIds;
private Collection<UUID> sectionIds;
private Collection<IsActive> isActives; private Collection<IsActive> isActives;
private Collection<UUID> excludedIds; private Collection<UUID> excludedIds;
@ -90,18 +92,18 @@ public class DmpDescriptionTemplateQuery extends QueryBase<DmpDescriptionTemplat
return this; return this;
} }
public DmpDescriptionTemplateQuery dmpIds(UUID value) { public DmpDescriptionTemplateQuery sectionIds(UUID value) {
this.dmpIds = List.of(value); this.sectionIds = List.of(value);
return this; return this;
} }
public DmpDescriptionTemplateQuery dmpIds(UUID... value) { public DmpDescriptionTemplateQuery sectionIds(UUID... value) {
this.dmpIds = Arrays.asList(value); this.sectionIds = Arrays.asList(value);
return this; return this;
} }
public DmpDescriptionTemplateQuery dmpIds(Collection<UUID> values) { public DmpDescriptionTemplateQuery sectionIds(Collection<UUID> values) {
this.dmpIds = values; this.sectionIds = values;
return this; return this;
} }
@ -120,6 +122,21 @@ public class DmpDescriptionTemplateQuery extends QueryBase<DmpDescriptionTemplat
return this; return this;
} }
public DmpDescriptionTemplateQuery dmpIds(UUID value) {
this.dmpIds = List.of(value);
return this;
}
public DmpDescriptionTemplateQuery dmpIds(UUID... value) {
this.dmpIds = Arrays.asList(value);
return this;
}
public DmpDescriptionTemplateQuery dmpIds(Collection<UUID> values) {
this.dmpIds = values;
return this;
}
public DmpDescriptionTemplateQuery authorize(EnumSet<AuthorizationFlags> values) { public DmpDescriptionTemplateQuery authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values; this.authorize = values;
return this; return this;
@ -193,6 +210,12 @@ public class DmpDescriptionTemplateQuery extends QueryBase<DmpDescriptionTemplat
inClause.value(item); inClause.value(item);
predicates.add(inClause); predicates.add(inClause);
} }
if (this.sectionIds != null) {
CriteriaBuilder.In<UUID> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpDescriptionTemplateEntity._sectionId));
for (UUID item : this.sectionIds)
inClause.value(item);
predicates.add(inClause);
}
if (this.isActives != null) { if (this.isActives != null) {
CriteriaBuilder.In<IsActive> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpDescriptionTemplateEntity._isActive)); CriteriaBuilder.In<IsActive> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpDescriptionTemplateEntity._isActive));
for (IsActive item : this.isActives) for (IsActive item : this.isActives)

View File

@ -28,6 +28,10 @@ import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.Tuple;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.JAXBException;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -127,36 +131,31 @@ public class DmpDatasetProfileMigrationService {
private void removeDuplicates() { private void removeDuplicates() {
logger.debug("Checking for duplicates on DmpDescriptionTemplate table after migration"); logger.debug("Checking for duplicates on DmpDescriptionTemplate table after migration");
int page = 0;
DmpDescriptionTemplateQuery dmpDescriptionTemplateQuery = this.queryFactory.query(DmpDescriptionTemplateQuery.class); DmpDescriptionTemplateQuery dmpDescriptionTemplateQuery = this.queryFactory.query(DmpDescriptionTemplateQuery.class);
long total = dmpDescriptionTemplateQuery.count(); long total = dmpDescriptionTemplateQuery.count();
List<DmpDescriptionTemplateEntity> dmpDescriptionTemplateEntities; logger.debug("Record count to check: {}", total);
do {
logger.debug("Checking " + page * PageSize + " of " + total);
dmpDescriptionTemplateQuery.setPage(new Paging(page * PageSize, PageSize));
dmpDescriptionTemplateEntities = dmpDescriptionTemplateQuery.collect();
DmpDescriptionTemplateEntity previousEntity = null; CriteriaBuilder b = this.entityManager.getCriteriaBuilder();
for(DmpDescriptionTemplateEntity currentEntity : dmpDescriptionTemplateEntities) { CriteriaQuery<Tuple> criteria = b.createQuery(Tuple.class);
if (previousEntity == null) { Root<DmpDescriptionTemplateEntity> root = criteria.from(DmpDescriptionTemplateEntity.class);
previousEntity = currentEntity; criteria.groupBy(Arrays.asList(root.get("dmp"), root.get("descriptionTemplateGroupId"), root.get("sectionId")));
continue; criteria.multiselect(root.get("dmp"), root.get("descriptionTemplateGroupId"), root.get("sectionId"), b.count(root));
} List<Tuple> resultList = this.entityManager.createQuery(criteria).getResultList();
if (currentEntity.getDmpId().equals(previousEntity.getDmpId())
&& currentEntity.getDescriptionTemplateGroupId().equals(previousEntity.getDescriptionTemplateGroupId())
&& currentEntity.getSectionId().equals(previousEntity.getSectionId())
) {
logger.warn("Removing found duplicate DmpDescriptionTemplate entity ({}) after DmpDatasetProfiles migration", currentEntity.getId());
this.entityManager.remove(currentEntity);
} else {
previousEntity = currentEntity;
}
}
entityManager.flush();
page++; List<Tuple> duplicatesList = resultList.stream().filter(x -> (long) x.get(3) > 1).toList();
} while (!dmpDescriptionTemplateEntities.isEmpty() && !TestMode); for (Tuple duplicate : duplicatesList) {
List<DmpDescriptionTemplateEntity> duplicateEntities = dmpDescriptionTemplateQuery
.dmpIds((UUID) duplicate.get(0))
.descriptionTemplateGroupIds((UUID) duplicate.get(1))
.sectionIds((UUID) duplicate.get(2))
.collect();
for (int index = 0; index < duplicateEntities.size() - 1; index++)
this.entityManager.remove(duplicateEntities.get(index));
}
entityManager.flush();
} }
@JsonIgnoreProperties({"validationErrorModel"}) @JsonIgnoreProperties({"validationErrorModel"})