From 6fee2264d88c92df5a3ddf1501d8ec1923118c7b Mon Sep 17 00:00:00 2001 From: Sofia Papacharalampous Date: Tue, 9 Jul 2024 15:20:47 +0300 Subject: [PATCH] tenant filter fixes on plan listing --- .../java/org/opencdmp/query/PlanQuery.java | 24 +++++------------ .../org/opencdmp/query/lookup/PlanLookup.java | 8 +++--- frontend/src/app/core/query/plan.lookup.ts | 3 ++- .../filtering/services/plan-filter.service.ts | 15 +++++++++++ .../ui/plan/listing/plan-listing.component.ts | 26 +++++++++++++++---- 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/query/PlanQuery.java b/backend/core/src/main/java/org/opencdmp/query/PlanQuery.java index 548393d6c..6b195c891 100644 --- a/backend/core/src/main/java/org/opencdmp/query/PlanQuery.java +++ b/backend/core/src/main/java/org/opencdmp/query/PlanQuery.java @@ -31,7 +31,7 @@ public class PlanQuery extends QueryBase { private Collection ids; - private Collection tenantIds; + private TenantQuery tenantQuery; private Collection creatorIds; @@ -97,18 +97,8 @@ public class PlanQuery extends QueryBase { return this; } - public PlanQuery tenantIds(UUID value) { - this.tenantIds = List.of(value); - return this; - } - - public PlanQuery tenantIds(UUID... value) { - this.tenantIds = Arrays.asList(value); - return this; - } - - public PlanQuery tenantIds(Collection values) { - this.tenantIds = values; + public PlanQuery tenantSubQuery(TenantQuery tenantQuery) { + this.tenantQuery = tenantQuery; return this; } @@ -335,11 +325,9 @@ public class PlanQuery extends QueryBase { inClause.value(item); predicates.add(inClause); } - if (this.tenantIds != null) { - CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(PlanEntity._tenantId)); - for (UUID item : this.tenantIds) - inClause.value(item); - predicates.add(inClause); + if (this.tenantQuery != null) { + QueryContext subQuery = this.applySubQuery(this.tenantQuery, queryContext, UUID.class, tenantEntityRoot -> tenantEntityRoot.get(TenantEntity._id)); + predicates.add(queryContext.CriteriaBuilder.in(queryContext.Root.get(PlanEntity._tenantId)).value(subQuery.Query)); } if (this.creatorIds != null) { CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(PlanEntity._creatorId)); diff --git a/backend/core/src/main/java/org/opencdmp/query/lookup/PlanLookup.java b/backend/core/src/main/java/org/opencdmp/query/lookup/PlanLookup.java index 1df327c9c..7bdd56b3e 100644 --- a/backend/core/src/main/java/org/opencdmp/query/lookup/PlanLookup.java +++ b/backend/core/src/main/java/org/opencdmp/query/lookup/PlanLookup.java @@ -19,7 +19,7 @@ public class PlanLookup extends Lookup { private String like; private List ids; - private List tenantIds; + private TenantLookup tenantSubQuery; private List excludedIds; private List groupIds; @@ -53,9 +53,9 @@ public class PlanLookup extends Lookup { this.ids = ids; } - public List getTenantIds() { return this.tenantIds; } + public TenantLookup getTenantSubQuery() { return this.tenantSubQuery; } - public void setTenantIds(List tenantIds) { this.tenantIds = tenantIds; } + public void setTenantSubQuery(TenantLookup tenantSubQuery) { this.tenantSubQuery = tenantSubQuery; } public List getExcludedIds() { return this.excludedIds; @@ -142,7 +142,7 @@ public class PlanLookup extends Lookup { PlanQuery query = queryFactory.query(PlanQuery.class); if (this.like != null) query.like(this.like); if (this.ids != null) query.ids(this.ids); - if (this.tenantIds != null) query.tenantIds(this.tenantIds); + if (this.tenantSubQuery != null) query.tenantSubQuery(this.tenantSubQuery.enrich(queryFactory)); if (this.groupIds != null) query.groupIds(this.groupIds); if (this.excludedIds != null) query.excludedIds(this.excludedIds); if (this.accessTypes != null) query.accessTypes(this.accessTypes); diff --git a/frontend/src/app/core/query/plan.lookup.ts b/frontend/src/app/core/query/plan.lookup.ts index efb438d1f..da778bb2d 100644 --- a/frontend/src/app/core/query/plan.lookup.ts +++ b/frontend/src/app/core/query/plan.lookup.ts @@ -8,10 +8,10 @@ import { PlanDescriptionTemplateLookup } from './plan-description-template.looku import { PlanUserLookup } from './plan-user.lookup'; import { PlanBlueprintLookup } from './plan-blueprint.lookup'; import { PlanReferenceLookup } from './reference.lookup'; +import { TenantLookup } from './tenant.lookup'; export class PlanLookup extends Lookup implements PlanFilter { ids: Guid[]; - tenantIds: Guid[]; excludedIds: Guid[]; like: string; isActive: IsActive[]; @@ -21,6 +21,7 @@ export class PlanLookup extends Lookup implements PlanFilter { versions: Number[]; groupIds: Guid[]; + tenantSubQuery: TenantLookup; planUserSubQuery: PlanUserLookup; planBlueprintSubQuery: PlanBlueprintLookup; planDescriptionTemplateSubQuery: PlanDescriptionTemplateLookup; diff --git a/frontend/src/app/ui/plan/listing/filtering/services/plan-filter.service.ts b/frontend/src/app/ui/plan/listing/filtering/services/plan-filter.service.ts index d331bac89..ab6b2cf52 100644 --- a/frontend/src/app/ui/plan/listing/filtering/services/plan-filter.service.ts +++ b/frontend/src/app/ui/plan/listing/filtering/services/plan-filter.service.ts @@ -2,10 +2,12 @@ import { Injectable } from "@angular/core"; import { PlanBlueprint } from "@app/core/model/plan-blueprint/plan-blueprint"; import { Plan, PlanDescriptionTemplate, PlanUser } from "@app/core/model/plan/plan"; import { Reference } from "@app/core/model/reference/reference"; +import { Tenant } from "@app/core/model/tenant/tenant"; import { PlanBlueprintLookup } from "@app/core/query/plan-blueprint.lookup"; import { PlanDescriptionTemplateLookup } from "@app/core/query/plan-description-template.lookup"; import { PlanUserLookup } from "@app/core/query/plan-user.lookup"; import { PlanReferenceLookup } from "@app/core/query/reference.lookup"; +import { TenantLookup } from "@app/core/query/tenant.lookup"; import { IsActive } from "@notification-service/core/enum/is-active.enum"; import { nameof } from "ts-simple-nameof"; @@ -65,4 +67,17 @@ export class PlanFilterService { return lookup; } + + public static initializeTenantLookup(): TenantLookup { + const lookup = new TenantLookup(); + lookup.metadata = { countAll: true }; + lookup.isActive = [IsActive.Active]; + lookup.project = { + fields: [ + nameof(x => x.code), + ] + }; + + return lookup; + } } \ No newline at end of file diff --git a/frontend/src/app/ui/plan/listing/plan-listing.component.ts b/frontend/src/app/ui/plan/listing/plan-listing.component.ts index e5d95cd51..f4e63082a 100644 --- a/frontend/src/app/ui/plan/listing/plan-listing.component.ts +++ b/frontend/src/app/ui/plan/listing/plan-listing.component.ts @@ -94,7 +94,8 @@ export class PlanListingComponent extends BaseListingComponent 0) { + const tenantFilter = lookup.tenantSubQuery?.codes[0]; + + if (tenantFilter != this.authService.selectedTenant()) { + lookup.tenantSubQuery = null; + this.filterChanged(lookup); + return lookup; + } + } + return lookup; } @@ -380,9 +392,12 @@ export class PlanListingComponent extends BaseListingComponent t.code && t.code?.toString() == this.authService.selectedTenant()); - if (tenant && tenant?.id) lookup.tenantIds = [tenant.id] - else lookup.tenantIds = null; - } else lookup.tenantIds = null; + if (tenant && tenant?.code) { + lookup.tenantSubQuery = PlanFilterService.initializeTenantLookup(); + lookup.tenantSubQuery.codes = [tenant.code] + } + else lookup.tenantSubQuery = null; + } else lookup.tenantSubQuery = null; // Description Templates let descriptionTemplates = formGroup.get("descriptionTemplates")?.value ?? null; @@ -426,7 +441,7 @@ export class PlanListingComponent extends BaseListingComponent 0 ? lookup.statuses[0] : null], - viewOnlyTenant: [lookup.tenantIds?.length > 0 ? true : false], + viewOnlyTenant: [lookup.tenantSubQuery?.codes?.length > 0 ? true : false], descriptionTemplates: lookup.planDescriptionTemplateSubQuery?.descriptionTemplateGroupIds ? [lookup.planDescriptionTemplateSubQuery?.descriptionTemplateGroupIds] : [], planBlueprints: lookup.planBlueprintSubQuery?.ids ? [lookup.planBlueprintSubQuery?.ids]: [], role: lookup.planUserSubQuery?.userRoles ? lookup.planUserSubQuery?.userRoles[0] : null, @@ -437,6 +452,7 @@ export class PlanListingComponent extends BaseListingComponent