From b230db5e66f7daf627f472f32d3b771ffa1746b3 Mon Sep 17 00:00:00 2001 From: amentis Date: Tue, 23 Jul 2024 17:05:25 +0300 Subject: [PATCH] Maintenance code clean up --- .../maintenance/MaintenanceServiceImpl.java | 77 +++++++------------ 1 file changed, 29 insertions(+), 48 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java index 29063f934..ec434510f 100644 --- a/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java @@ -213,22 +213,14 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); - List items = this.queryFactory.query(PlanQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Plan._id).ensure(Plan._isActive).ensure(Plan._creator)); + List items = this.queryFactory.query(PlanQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(Plan._id).ensure(Plan._isActive).ensure(Plan._creator).ensure(Plan._belongsToCurrentTenant)); List userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getCreatorId()).distinct().collect(Collectors.toList())).collect(); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); for (PlanEntity item : items) { - String subjectId; - if (userCredentials != null) { - UserCredentialEntity userCredential = userCredentials.stream().filter(x -> x.getUserId().equals(item.getCreatorId())).findFirst().orElse(null); - if (userCredential != null) subjectId = userCredential.getExternalId(); - else subjectId = item.getCreatorId().toString(); - } else { - subjectId = item.getCreatorId().toString(); - } + String subjectId = this.findUserCredential(userCredentials, item.getCreatorId()); String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PLAN_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); - else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PLAN_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PLAN_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -242,14 +234,13 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); - List items = this.queryFactory.query(PlanBlueprintQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(PlanBlueprint._id).ensure(PlanBlueprint._isActive)); + List items = this.queryFactory.query(PlanBlueprintQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(PlanBlueprint._id).ensure(PlanBlueprint._isActive).ensure(PlanBlueprint._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); for (PlanBlueprintEntity item : items) { String subjectId = accountingProperties.getSubjectId(); String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.BLUEPRINT_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); - else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.BLUEPRINT_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.BLUEPRINT_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -263,22 +254,14 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); - List items = this.queryFactory.query(DescriptionQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Description._id).ensure(Description._isActive).ensure(Description._createdBy)); + List items = this.queryFactory.query(DescriptionQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(Description._id).ensure(Description._isActive).ensure(Description._createdBy).ensure(Description._belongsToCurrentTenant)); List userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getCreatedById()).distinct().collect(Collectors.toList())).collect(); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); for (DescriptionEntity item : items) { - String subjectId; - if (userCredentials != null) { - UserCredentialEntity userCredential = userCredentials.stream().filter(x -> x.getUserId().equals(item.getCreatedById())).findFirst().orElse(null); - if (userCredential != null) subjectId = userCredential.getExternalId(); - else subjectId = item.getCreatedById().toString(); - } else { - subjectId = item.getCreatedById().toString(); - } + String subjectId = this.findUserCredential(userCredentials, item.getCreatedById()); String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); - else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -292,14 +275,13 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); - List items = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(DescriptionTemplate._id).ensure(DescriptionTemplate._isActive)); + List items = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(DescriptionTemplate._id).ensure(DescriptionTemplate._isActive).ensure(DescriptionTemplate._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); for (DescriptionTemplateEntity item : items) { String subjectId = accountingProperties.getSubjectId(); String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); - else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -313,14 +295,13 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); - List items = this.queryFactory.query(DescriptionTemplateTypeQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(DescriptionTemplateType._id).ensure(DescriptionTemplateType._isActive)); + List items = this.queryFactory.query(DescriptionTemplateTypeQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(DescriptionTemplateType._id).ensure(DescriptionTemplateType._isActive).ensure(DescriptionTemplateType._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); for (DescriptionTemplateTypeEntity item : items) { String subjectId = accountingProperties.getSubjectId(); String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); - else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -334,14 +315,13 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); - List items = this.queryFactory.query(PrefillingSourceQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(PrefillingSource._id).ensure(PrefillingSource._isActive)); + List items = this.queryFactory.query(PrefillingSourceQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(PrefillingSource._id).ensure(PrefillingSource._isActive).ensure(PrefillingSource._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); for (PrefillingSourceEntity item : items) { String subjectId = accountingProperties.getSubjectId(); String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); - else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -355,14 +335,13 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); - List items = this.queryFactory.query(ReferenceTypeQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(ReferenceType._id).ensure(ReferenceType._isActive)); + List items = this.queryFactory.query(ReferenceTypeQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(ReferenceType._id).ensure(ReferenceType._isActive).ensure(ReferenceType._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); for (ReferenceTypeEntity item : items) { String subjectId = accountingProperties.getSubjectId(); String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); - else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -376,22 +355,14 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); - List items = this.queryFactory.query(TenantUserQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(TenantUserEntity._id).ensure(TenantUserEntity._userId).ensure(TenantUserEntity._isActive)); + List items = this.queryFactory.query(TenantUserQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(TenantUserEntity._id).ensure(TenantUserEntity._userId).ensure(TenantUserEntity._isActive).ensure(TenantUserEntity._tenantId)); List userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getUserId()).distinct().collect(Collectors.toList())).collect(); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); for (TenantUserEntity item : items) { - String subjectId; - if (userCredentials != null) { - UserCredentialEntity userCredential = userCredentials.stream().filter(x -> x.getUserId().equals(item.getUserId())).findFirst().orElse(null); - if (userCredential != null) subjectId = userCredential.getExternalId(); - else subjectId = item.getUserId().toString(); - } else { - subjectId = item.getUserId().toString(); - } + String subjectId = this.findUserCredential(userCredentials, item.getUserId()); String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.USER_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); - else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.USER_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.USER_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -407,4 +378,14 @@ public class MaintenanceServiceImpl implements MaintenanceService { return this.tenantScope.getDefaultTenantCode(); } } + + private String findUserCredential(List userCredentials, UUID userId) { + if (userCredentials != null) { + UserCredentialEntity userCredential = userCredentials.stream().filter(x -> x.getUserId().equals(userId)).findFirst().orElse(null); + if (userCredential != null) return userCredential.getExternalId(); + else return userId.toString(); + } else { + return userId.toString(); + } + } }