Fixing duplicate selection from DmpDescriptionTemplates
This commit is contained in:
parent
3d18ed1fa6
commit
8ee149f094
|
@ -38,6 +38,8 @@ public class DmpDescriptionTemplateQuery extends QueryBase<DmpDescriptionTemplat
|
|||
|
||||
private Collection<UUID> descriptionTemplateGroupIds;
|
||||
|
||||
private Collection<UUID> sectionIds;
|
||||
|
||||
private Collection<IsActive> isActives;
|
||||
|
||||
private Collection<UUID> excludedIds;
|
||||
|
@ -90,18 +92,18 @@ public class DmpDescriptionTemplateQuery extends QueryBase<DmpDescriptionTemplat
|
|||
return this;
|
||||
}
|
||||
|
||||
public DmpDescriptionTemplateQuery dmpIds(UUID value) {
|
||||
this.dmpIds = List.of(value);
|
||||
public DmpDescriptionTemplateQuery sectionIds(UUID value) {
|
||||
this.sectionIds = List.of(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DmpDescriptionTemplateQuery dmpIds(UUID... value) {
|
||||
this.dmpIds = Arrays.asList(value);
|
||||
public DmpDescriptionTemplateQuery sectionIds(UUID... value) {
|
||||
this.sectionIds = Arrays.asList(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DmpDescriptionTemplateQuery dmpIds(Collection<UUID> values) {
|
||||
this.dmpIds = values;
|
||||
public DmpDescriptionTemplateQuery sectionIds(Collection<UUID> values) {
|
||||
this.sectionIds = values;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -120,6 +122,21 @@ public class DmpDescriptionTemplateQuery extends QueryBase<DmpDescriptionTemplat
|
|||
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) {
|
||||
this.authorize = values;
|
||||
return this;
|
||||
|
@ -193,6 +210,12 @@ public class DmpDescriptionTemplateQuery extends QueryBase<DmpDescriptionTemplat
|
|||
inClause.value(item);
|
||||
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) {
|
||||
CriteriaBuilder.In<IsActive> inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DmpDescriptionTemplateEntity._isActive));
|
||||
for (IsActive item : this.isActives)
|
||||
|
|
|
@ -28,6 +28,10 @@ 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.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import jakarta.xml.bind.JAXBException;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -127,36 +131,31 @@ public class DmpDatasetProfileMigrationService {
|
|||
|
||||
private void removeDuplicates() {
|
||||
logger.debug("Checking for duplicates on DmpDescriptionTemplate table after migration");
|
||||
int page = 0;
|
||||
|
||||
DmpDescriptionTemplateQuery dmpDescriptionTemplateQuery = this.queryFactory.query(DmpDescriptionTemplateQuery.class);
|
||||
long total = dmpDescriptionTemplateQuery.count();
|
||||
List<DmpDescriptionTemplateEntity> dmpDescriptionTemplateEntities;
|
||||
do {
|
||||
logger.debug("Checking " + page * PageSize + " of " + total);
|
||||
dmpDescriptionTemplateQuery.setPage(new Paging(page * PageSize, PageSize));
|
||||
dmpDescriptionTemplateEntities = dmpDescriptionTemplateQuery.collect();
|
||||
logger.debug("Record count to check: {}", total);
|
||||
|
||||
DmpDescriptionTemplateEntity previousEntity = null;
|
||||
for(DmpDescriptionTemplateEntity currentEntity : dmpDescriptionTemplateEntities) {
|
||||
if (previousEntity == null) {
|
||||
previousEntity = currentEntity;
|
||||
continue;
|
||||
}
|
||||
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();
|
||||
CriteriaBuilder b = this.entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Tuple> criteria = b.createQuery(Tuple.class);
|
||||
Root<DmpDescriptionTemplateEntity> root = criteria.from(DmpDescriptionTemplateEntity.class);
|
||||
criteria.groupBy(Arrays.asList(root.get("dmp"), root.get("descriptionTemplateGroupId"), root.get("sectionId")));
|
||||
criteria.multiselect(root.get("dmp"), root.get("descriptionTemplateGroupId"), root.get("sectionId"), b.count(root));
|
||||
List<Tuple> resultList = this.entityManager.createQuery(criteria).getResultList();
|
||||
|
||||
page++;
|
||||
} while (!dmpDescriptionTemplateEntities.isEmpty() && !TestMode);
|
||||
List<Tuple> duplicatesList = resultList.stream().filter(x -> (long) x.get(3) > 1).toList();
|
||||
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"})
|
||||
|
|
Loading…
Reference in New Issue