FINERACT-2181: make 'BusinessEventNotifierServiceImpl#isExternalEventConfiguredForPosting` overridable

This commit is contained in:
adam.magyari
2025-02-25 11:14:53 +01:00
committed by Adam Saghy
parent e542ca6870
commit 9310f0e1ea
5 changed files with 116 additions and 28 deletions
@@ -34,7 +34,6 @@ import org.apache.fineract.infrastructure.event.business.BusinessEventListener;
import org.apache.fineract.infrastructure.event.business.domain.BulkBusinessEvent;
import org.apache.fineract.infrastructure.event.business.domain.BusinessEvent;
import org.apache.fineract.infrastructure.event.business.domain.NoExternalEvent;
import org.apache.fineract.infrastructure.event.external.repository.ExternalEventConfigurationRepository;
import org.apache.fineract.infrastructure.event.external.service.ExternalEventService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
@@ -56,10 +55,10 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe
private final ThreadLocal<List<BusinessEvent<?>>> recordedEvents = ThreadLocal.withInitial(ArrayList::new);
private final ExternalEventService externalEventService;
private final ExternalEventConfigurationRepository eventConfigurationRepository;
private final FineractProperties fineractProperties;
private final ThreadLocal<Stack<List<BusinessEventWithContext>>> transactionBusinessEvents = ThreadLocal.withInitial(Stack::new);
private final TransactionHelper transactionHelper;
private final ExternalBusinessEventConfigurationService externalBusinessEventConfigurationService;
@Override
public void afterPropertiesSet() throws Exception {
@@ -100,7 +99,7 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe
}
if (isExternalEvent && isExternalEventPostingEnabled()) {
// we only want to create external events for operations that were successful, hence the post listener
if (isExternalEventConfiguredForPosting(businessEvent.getType())) {
if (externalBusinessEventConfigurationService.isExternalEventConfiguredForPosting(businessEvent)) {
if (isExternalEventRecordingEnabled()) {
recordedEvents.get().add(businessEvent);
} else {
@@ -143,10 +142,6 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe
return fineractProperties.getEvents().getExternal().isEnabled();
}
private boolean isExternalEventConfiguredForPosting(String eventType) {
return eventConfigurationRepository.findExternalEventConfigurationByTypeWithNotFoundDetection(eventType).isEnabled();
}
private void throwExceptionIfBulkEvent(BusinessEvent<?> businessEvent) {
if (businessEvent instanceof BulkBusinessEvent) {
throw new IllegalArgumentException("BulkBusinessEvent cannot be raised directly");
@@ -0,0 +1,26 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.event.business.service;
import org.apache.fineract.infrastructure.event.business.domain.BusinessEvent;
public interface ExternalBusinessEventConfigurationService {
boolean isExternalEventConfiguredForPosting(BusinessEvent<?> businessEvent);
}
@@ -0,0 +1,41 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.event.business.service;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.event.business.domain.BusinessEvent;
import org.apache.fineract.infrastructure.event.external.repository.ExternalEventConfigurationRepository;
/**
* Default implementation for {@link ExternalBusinessEventConfigurationService}, configured by
* {@link org.apache.fineract.infrastructure.event.external.config.ExternalBusinessEventConfiguration}
*/
@RequiredArgsConstructor
public class ExternalBusinessEventConfigurationServiceImpl implements ExternalBusinessEventConfigurationService {
private final ExternalEventConfigurationRepository eventConfigurationRepository;
@Override
public boolean isExternalEventConfiguredForPosting(BusinessEvent<?> businessEvent) {
if (businessEvent == null) {
return false;
}
return eventConfigurationRepository.findExternalEventConfigurationByTypeWithNotFoundDetection(businessEvent.getType()).isEnabled();
}
}
@@ -0,0 +1,37 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.event.external.config;
import org.apache.fineract.infrastructure.event.business.service.ExternalBusinessEventConfigurationService;
import org.apache.fineract.infrastructure.event.business.service.ExternalBusinessEventConfigurationServiceImpl;
import org.apache.fineract.infrastructure.event.external.repository.ExternalEventConfigurationRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ExternalBusinessEventConfiguration {
@ConditionalOnMissingBean(ExternalBusinessEventConfigurationService.class)
@Bean
public ExternalBusinessEventConfigurationService externalBusinessEventConfigurationService(
ExternalEventConfigurationRepository externalEventConfigurationRepository) {
return new ExternalBusinessEventConfigurationServiceImpl(externalEventConfigurationRepository);
}
}