From 9ff2e6a5aa5725da759f7b548d15c1a9ccdf574c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Garc=C3=ADa?= Date: Tue, 24 Oct 2023 11:52:44 +0200 Subject: [PATCH] Optimise query used to retrieve metadata links results --- .../repository/specification/LinkSpecs.java | 64 +++++++++---------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/domain/src/main/java/org/fao/geonet/repository/specification/LinkSpecs.java b/domain/src/main/java/org/fao/geonet/repository/specification/LinkSpecs.java index cd77c680549..3efe086e68e 100644 --- a/domain/src/main/java/org/fao/geonet/repository/specification/LinkSpecs.java +++ b/domain/src/main/java/org/fao/geonet/repository/specification/LinkSpecs.java @@ -33,7 +33,6 @@ import org.fao.geonet.domain.OperationAllowed; import org.fao.geonet.domain.OperationAllowedId_; import org.fao.geonet.domain.OperationAllowed_; -import org.fao.geonet.domain.ReservedGroup; import org.fao.geonet.domain.ReservedOperation; import org.springframework.data.jpa.domain.Specification; @@ -82,12 +81,18 @@ public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBu predicates.add(metadataJoin.get("metadataUuid").in(associatedRecords)); } - if (editingGroupIds != null && editingGroupIds.length > 0) { - Join metadataJoin = root.join(Link_.records, JoinType.INNER); + Join metadataJoin = root.join(Link_.records, JoinType.INNER); + Subquery subquery = query.subquery(Integer.class); + final Root opAllowRoot = subquery.from(OperationAllowed.class); + final Root metadataRoot = subquery.from(Metadata.class); + + boolean editinGroupQuery = editingGroupIds != null && editingGroupIds.length > 0; + boolean groupPublishedQuery = groupPublishedIds != null && groupPublishedIds.length > 0; + boolean groupOwnerQuery = groupOwnerIds != null && groupOwnerIds.length > 0; + + List subQueryPredicates = new ArrayList<>(); - Subquery subquery = query.subquery(Integer.class); - final Root opAllowRoot = subquery.from(OperationAllowed.class); - final Root metadataRoot = subquery.from(Metadata.class); + if (editinGroupQuery) { final Predicate groupOwnerPredicate = metadataRoot.get(Metadata_.sourceInfo).get(MetadataSourceInfo_.groupOwner).in(editingGroupIds); final Predicate metadataOperations = cb.equal(metadataRoot.get(Metadata_.id), opAllowRoot.get(OperationAllowed_.id).get(OperationAllowedId_.metadataId)); @@ -96,52 +101,41 @@ public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBu cb.equal( opAllowRoot.get(OperationAllowed_.id).get(OperationAllowedId_.operationId), cb.literal(ReservedOperation.editing.getId())); - subquery.where( - cb.or( - cb.and(metadataOperations, groupOwnerPredicate), - cb.and(editableGroups, operationTypeEdit))); - - Path opAllowedMetadataId = opAllowRoot.get(OperationAllowed_.id).get(OperationAllowedId_.metadataId); - subquery.select(opAllowedMetadataId); - predicates.add(metadataJoin.get(MetadataLink_.metadataId).in(subquery)); - query.distinct(true); + subQueryPredicates.add(cb.or( + cb.and(metadataOperations, groupOwnerPredicate), + cb.and(editableGroups, operationTypeEdit))); } - if (groupPublishedIds != null && groupPublishedIds.length > 0) { - Join metadataJoin = root.join(Link_.records, JoinType.INNER); - - Subquery subquery = query.subquery(Integer.class); - Root opAllowRoot = subquery.from(OperationAllowed.class); + if (groupPublishedQuery) { Predicate publishedToIndicatedGroup = opAllowRoot.get(OperationAllowed_.id).get(OperationAllowedId_.groupId).in(groupPublishedIds); Predicate operationTypeView = cb.equal( opAllowRoot.get(OperationAllowed_.id).get(OperationAllowedId_.operationId), cb.literal(ReservedOperation.view.getId())); - subquery.where( - cb.and(publishedToIndicatedGroup, operationTypeView)); - - Path opAllowedMetadataId = opAllowRoot.get(OperationAllowed_.id).get(OperationAllowedId_.metadataId); - subquery.select(opAllowedMetadataId); - predicates.add(metadataJoin.get(MetadataLink_.metadataId).in(subquery)); - query.distinct(true); + subQueryPredicates.add(cb.and(publishedToIndicatedGroup, operationTypeView)); } - if (groupOwnerIds != null && groupOwnerIds.length > 0) { - Join metadataJoin = root.join(Link_.records, JoinType.INNER); - Subquery subquery = query.subquery(Integer.class); - final Root metadataRoot = subquery.from(Metadata.class); + if (groupOwnerQuery) { final Predicate groupOwnerPredicate = metadataRoot.get(Metadata_.sourceInfo).get(MetadataSourceInfo_.groupOwner).in(groupOwnerIds); - subquery.where(groupOwnerPredicate); - Path metadataId = metadataRoot.get(Metadata_.id); - subquery.select(metadataId); + subQueryPredicates.add(groupOwnerPredicate); + } + + + if (subQueryPredicates.size() > 0) { + subquery.where(subQueryPredicates.toArray(new Predicate[]{})); + + Path opAllowedMetadataId = opAllowRoot.get(OperationAllowed_.id).get(OperationAllowedId_.metadataId); + subquery.select(opAllowedMetadataId); predicates.add(metadataJoin.get(MetadataLink_.metadataId).in(subquery)); - query.distinct(true); } + + query.distinct(true); + return cb.and(predicates.toArray(new Predicate[]{})); } };