diff --git a/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java b/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java index 499e91d4..5e1c8a24 100644 --- a/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java +++ b/src/main/java/it/gov/pagopa/admissibility/service/onboarding/evaluate/RuleEngineServiceImpl.java @@ -41,40 +41,38 @@ public RuleEngineServiceImpl(OnboardingContextHolderService onboardingContextHol public EvaluationDTO applyRules(OnboardingDTO onboardingRequest, InitiativeConfig initiative) { log.trace("[ONBOARDING_REQUEST] [RULE_ENGINE] evaluating rules of user {} into initiative {}", onboardingRequest.getUserId(), onboardingRequest.getInitiativeId()); - StatelessKieSession statelessKieSession = onboardingContextHolderService.getBeneficiaryRulesKieBase().newStatelessKieSession(); - OnboardingDroolsDTO req = onboarding2OnboardingDroolsMapper.apply(onboardingRequest); - @SuppressWarnings("unchecked") - List> cmds = Arrays.asList( - CommandFactory.newInsert(req), - CommandFactory.newInsert(criteriaCodeService), - new AgendaGroupSetFocusCommand(req.getInitiativeId()) - ); - - long before = System.currentTimeMillis(); - statelessKieSession.execute(CommandFactory.newBatchExecution(cmds)); + if(checkIfKieBaseContainerIsReady(initiative)) { + StatelessKieSession statelessKieSession = onboardingContextHolderService.getBeneficiaryRulesKieBase().newStatelessKieSession(); - checkIfContainerWasReady(req, initiative); + @SuppressWarnings("unchecked") + List> cmds = Arrays.asList( + CommandFactory.newInsert(req), + CommandFactory.newInsert(criteriaCodeService), + new AgendaGroupSetFocusCommand(req.getInitiativeId()) + ); - PerformanceLogger.logTiming("ONBOARDING_RULE_ENGINE", before, "resulted into rejections %s".formatted(req.getOnboardingRejectionReasons())); - - log.trace("[ONBOARDING_REQUEST] [RULE_ENGINE] Send message prepared: {}", req); - - return onboarding2EvaluationMapper.apply(req, initiative, req.getOnboardingRejectionReasons()); - } + long before = System.currentTimeMillis(); + statelessKieSession.execute(CommandFactory.newBatchExecution(cmds)); - private void checkIfContainerWasReady(OnboardingDroolsDTO req, InitiativeConfig initiative) { - if (req.getOnboardingRejectionReasons().isEmpty() && // there is not rejection reason - !CollectionUtils.isEmpty(initiative.getAutomatedCriteria()) && // the drools container is supposed to be involved - !onboardingContextHolderService.getBeneficiaryRulesKieInitiativeIds() // the initiative was not inside the container drools - .contains(initiative.getInitiativeId()) - ) { + PerformanceLogger.logTiming("ONBOARDING_RULE_ENGINE", before, "resulted into rejections %s".formatted(req.getOnboardingRejectionReasons())); + } else { req.getOnboardingRejectionReasons().add(new OnboardingRejectionReason( OnboardingRejectionReason.OnboardingRejectionReasonType.TECHNICAL_ERROR, OnboardingConstants.REJECTION_REASON_RULE_ENGINE_NOT_READY, null, null, null )); } + + log.trace("[ONBOARDING_REQUEST] [RULE_ENGINE] Send message prepared: {}", req); + + return onboarding2EvaluationMapper.apply(req, initiative, req.getOnboardingRejectionReasons()); + } + + private boolean checkIfKieBaseContainerIsReady(InitiativeConfig initiative) { + return !CollectionUtils.isEmpty(initiative.getAutomatedCriteria()) && // the drools container is supposed to be involved + onboardingContextHolderService.getBeneficiaryRulesKieInitiativeIds() // the initiative is inside the container drools + .contains(initiative.getInitiativeId()); } }