From 08f753bba068b0ab97dbb57f03b53a2c6d0eaeaa Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Thu, 30 May 2024 12:45:37 +0300 Subject: [PATCH] fix contact info query --- .../opencdmp/query/UserContactInfoQuery.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/query/UserContactInfoQuery.java b/backend/core/src/main/java/org/opencdmp/query/UserContactInfoQuery.java index 833ba0101..9a4f46803 100644 --- a/backend/core/src/main/java/org/opencdmp/query/UserContactInfoQuery.java +++ b/backend/core/src/main/java/org/opencdmp/query/UserContactInfoQuery.java @@ -11,8 +11,11 @@ import org.opencdmp.authorization.AuthorizationFlags; import org.opencdmp.authorization.Permission; import org.opencdmp.commons.enums.ContactInfoType; import org.opencdmp.commons.scope.user.UserScope; +import org.opencdmp.data.DmpUserEntity; import org.opencdmp.data.UserContactInfoEntity; import org.opencdmp.model.UserContactInfo; +import org.opencdmp.query.utils.BuildSubQueryInput; +import org.opencdmp.query.utils.QueryUtilsService; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -31,11 +34,12 @@ public class UserContactInfoQuery extends QueryBase { private Collection types; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); - + private final QueryUtilsService queryUtilsService; private final UserScope userScope; private final AuthorizationService authService; - public UserContactInfoQuery(UserScope userScope, AuthorizationService authService) { - this.userScope = userScope; + public UserContactInfoQuery(QueryUtilsService queryUtilsService, UserScope userScope, AuthorizationService authService) { + this.queryUtilsService = queryUtilsService; + this.userScope = userScope; this.authService = authService; } @@ -168,7 +172,17 @@ public class UserContactInfoQuery extends QueryBase { List predicates = new ArrayList<>(); if (userId != null) { - predicates.add(queryContext.CriteriaBuilder.in(queryContext.Root.get(UserContactInfoEntity._userId)).value(userId)); + predicates.add(queryContext.CriteriaBuilder.or( + queryContext.CriteriaBuilder.in(queryContext.Root.get(UserContactInfoEntity._userId)).value(userId), //Creates a false query + queryContext.CriteriaBuilder.in(queryContext.Root.get(UserContactInfoEntity._userId)).value(this.queryUtilsService.buildSubQuery(new BuildSubQueryInput<>(new BuildSubQueryInput.Builder<>(DmpUserEntity.class, UUID.class) + .query(queryContext.Query) + .criteriaBuilder(queryContext.CriteriaBuilder) + .keyPathFunc((subQueryRoot) -> subQueryRoot.get(DmpUserEntity._userId)) + .filterFunc((subQueryRoot, cb) -> + cb.in(subQueryRoot.get(DmpUserEntity._dmpId)).value(this.queryUtilsService.buildDmpAuthZSubQuery(queryContext.Query, queryContext.CriteriaBuilder, userId, false)) + ) + ))) + )); } if (!predicates.isEmpty()) { Predicate[] predicatesArray = predicates.toArray(new Predicate[0]); @@ -187,6 +201,12 @@ public class UserContactInfoQuery extends QueryBase { inClause.value(item); predicates.add(inClause); } + if (this.types != null) { + CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(UserContactInfoEntity._type)); + for (ContactInfoType item : this.types) + inClause.value(item); + predicates.add(inClause); + } if (this.userIds != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(UserContactInfoEntity._userId)); for (UUID item : this.userIds)