class TerminateExternalDatabaseHandler implements EventHandler<TerminateExternalDatabaseRequest> {

    @Override
    public void accept(Event<TerminateExternalDatabaseRequest> terminateExternalDatabaseRequest) {
        LOGGER.debug("In TerminateExternalDatabaseHandler.accept");
        TerminateExternalDatabaseRequest request = terminateExternalDatabaseRequest.getData();
        Stack stack = request.getStack();
        DatabaseAvailabilityType externalDatabase = ObjectUtils.defaultIfNull(stack.getExternalDatabaseCreationType(), DatabaseAvailabilityType.NONE);
        LOGGER.debug("External database: {} for stack {}", externalDatabase.name(), stack.getName());
        Selectable result;
        try {
            if (externalDatabase == DatabaseAvailabilityType.NONE) {
                LOGGER.info("External database for stack {} is not requested. Termination is not necessary.", stack.getName());
                result = new TerminateExternalDatabaseResult(stack.getId(), EXTERNAL_DATABASE_WAIT_TERMINATION_SUCCESS_EVENT.event(),
                        stack.getName(), null);

            } else {
                LOGGER.debug("Updating stack {} status from {} to {}",
                        stack.getName(), stack.getStatus().name(), DetailedStackStatus.EXTERNAL_DATABASE_DELETION_IN_PROGRESS.name());
                stackUpdaterService.updateStatus(stack.getId(), DetailedStackStatus.EXTERNAL_DATABASE_DELETION_IN_PROGRESS,
                        ResourceEvent.CLUSTER_EXTERNAL_DATABASE_DELETION_STARTED, "External database deletion in progress");
                LOGGER.debug("Getting environment CRN for stack {}", stack.getName());
                DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn());
                terminationService.terminateDatabase(stack.getCluster(), externalDatabase, environment, request.isForced());
                LOGGER.debug("Updating stack {} status from {} to {}",
                        stack.getName(), stack.getStatus().name(), DetailedStackStatus.DELETE_COMPLETED.name());
                stackUpdaterService.updateStatus(stack.getId(), DetailedStackStatus.DELETE_COMPLETED,
                        ResourceEvent.CLUSTER_EXTERNAL_DATABASE_DELETION_FINISHED, "External database deletion finished");
                result = new TerminateExternalDatabaseResult(stack.getId(), EXTERNAL_DATABASE_WAIT_TERMINATION_SUCCESS_EVENT.event(),
                        stack.getName(), stack.getCluster().getDatabaseServerCrn());
            }
        } catch (UserBreakException e) {
            LOGGER.info("Database termination polling exited before timeout. Cause: ", e);
            result = terminateFailedEvent(stack, e);
        } catch (PollerStoppedException e) {
            LOGGER.info(String.format("Database termination poller stopped for stack: %s", stack.getName()), e);
            result = terminateFailedEvent(stack, e);
        } catch (PollerException e) {
            LOGGER.info(String.format("Database termination polling failed for stack: %s", stack.getName()), e);
            result = terminateFailedEvent(stack, e);
        } catch (Exception e) {
            LOGGER.error(String.format("Exception during DB termination for stack/cluster: %s", stack.getName()), e);
            result = terminateFailedEvent(stack, e);
        }
        LOGGER.debug("Sending reactor notification for selector {}", result.selector());
        eventBus.notify(result.selector(), new Event<>(terminateExternalDatabaseRequest.getHeaders(), result));
    }

}