Skip to content

Commit

Permalink
Optimise query used to retrieve metadata links results
Browse files Browse the repository at this point in the history
  • Loading branch information
josegar74 committed Oct 24, 2023
1 parent 312c28f commit 9ff2e6a
Showing 1 changed file with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -82,12 +81,18 @@ public Predicate toPredicate(Root<Link> root, CriteriaQuery<?> query, CriteriaBu
predicates.add(metadataJoin.get("metadataUuid").in(associatedRecords));
}

if (editingGroupIds != null && editingGroupIds.length > 0) {
Join<Link, MetadataLink> metadataJoin = root.join(Link_.records, JoinType.INNER);
Join<Link, MetadataLink> metadataJoin = root.join(Link_.records, JoinType.INNER);
Subquery<Integer> subquery = query.subquery(Integer.class);
final Root<OperationAllowed> opAllowRoot = subquery.from(OperationAllowed.class);
final Root<Metadata> 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<Predicate> subQueryPredicates = new ArrayList<>();

Subquery<Integer> subquery = query.subquery(Integer.class);
final Root<OperationAllowed> opAllowRoot = subquery.from(OperationAllowed.class);
final Root<Metadata> 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));
Expand All @@ -96,52 +101,41 @@ public Predicate toPredicate(Root<Link> 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<Integer> 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<Link, MetadataLink> metadataJoin = root.join(Link_.records, JoinType.INNER);

Subquery<Integer> subquery = query.subquery(Integer.class);
Root<OperationAllowed> 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<Integer> 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<Link, MetadataLink> metadataJoin = root.join(Link_.records, JoinType.INNER);
Subquery<Integer> subquery = query.subquery(Integer.class);
final Root<Metadata> metadataRoot = subquery.from(Metadata.class);
if (groupOwnerQuery) {
final Predicate groupOwnerPredicate =
metadataRoot.get(Metadata_.sourceInfo).get(MetadataSourceInfo_.groupOwner).in(groupOwnerIds);
subquery.where(groupOwnerPredicate);

Path<Integer> metadataId = metadataRoot.get(Metadata_.id);
subquery.select(metadataId);
subQueryPredicates.add(groupOwnerPredicate);
}


if (subQueryPredicates.size() > 0) {
subquery.where(subQueryPredicates.toArray(new Predicate[]{}));

Path<Integer> 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[]{}));
}
};
Expand Down

0 comments on commit 9ff2e6a

Please sign in to comment.