Checker

📌 Purpose

CreateApplicationExecutionChecker is a component that automatically reacts to the completion of a campaign execution (CampaignExecution) and creates applications for the clients who were selected. It implements the observer pattern for entity changes using the EntityChecker abstraction provided by the timveroOS framework.


🏗️ Inheritance from EntityChecker

@Component
public class CreateApplicationExecutionChecker extends EntityChecker<CampaignExecution, Long> {

    @Autowired
    private CreateApplicationService createApplicationService;
    @Autowired
    private TransactionTemplateBuilder ttb;

    @Override
    protected void registerListeners(CheckerListenerRegistry<CampaignExecution> registry) {
        registry.entityChange().updated(CampaignExecution_.STATUS);
    }

    @Override
    protected boolean isAvailable(CampaignExecution execution) {
        return execution.getStatus() == CampaignExecutionStatus.FINISHED;
    }

    @Override
    protected void perform(CampaignExecution execution) {
        final Long executionId = execution.getId();
        TransactionUtils.afterTransaction(() -> {
            execution.getClients().forEach(borrower ->
                createApplicationService.create(borrower.getId(), executionId)
            );
        });
    }
}
  • EntityChecker<E, ID> is a base class in the platform used to observe changes to entity fields.

  • The implementation must override:

    • registerListeners(...) — to specify which fields to monitor

    • isAvailable(...) — to define when the checker should trigger

    • perform(...) — to define the actions to perform upon trigger


🔔 Reacting to FINISHED Status

@Override
protected void registerListeners(CheckerListenerRegistry<CampaignExecution> registry) {
    registry.entityChange().updated(CampaignExecution_.STATUS);
}

@Override
protected boolean isAvailable(CampaignExecution execution) {
    return execution.getStatus() == CampaignExecutionStatus.FINISHED;
}
  • registerListeners subscribes the checker to changes in the status field

  • isAvailable ensures perform(...) is only called when the status becomes FINISHED


🛠️ Core Logic in perform(...)

@Override
protected void perform(CampaignExecution execution) {
    final Long executionId = execution.getId();
    TransactionUtils.afterTransaction(() -> {
        execution.getClients().forEach(borrower ->
            createApplicationService.create(borrower.getId(), executionId)
        );
    });
}

✅ What happens here:

  • Transaction-safe execution: Uses TransactionUtils.afterTransaction(...) to defer logic until after the current transaction completes — important for compatibility with Spring @Transactional

  • Application creation: For each selected client, it triggers createApplicationService.create(...) with a reference to the current campaign execution


📦 Components and Dependencies

@Autowired
private CreateApplicationService createApplicationService;
  • CreateApplicationService is the internal service responsible for creating applications


🔄 How It Works in the System

  1. A campaign finishes execution → CampaignExecution.status becomes FINISHED

  2. The EntityChecker detects the status change

  3. perform(...) is triggered after the transaction ends

  4. Applications are created for each selected client


🧪 Example Usage

  • Campaign A has a filter like client.score > 800 and selects 3 clients

  • After execution completes:

    • CampaignExecution.statusFINISHED

    • CreateApplicationExecutionChecker is triggered

    • 3 applications are created — one for each selected client, linked to the campaign


📌 Summary

  • Inherits from EntityChecker<CampaignExecution, Long>

  • Monitors changes to the status field

  • Trigger condition: status == FINISHED

  • Executes after transaction completes

  • Main logic: notifying on completion and creating applications for clients

  • Depends on: CreateApplicationService

Last updated

Was this helpful?