class CreateExternalDatabaseHandler implements EventHandler<CreateExternalDatabaseRequest> {

    @Override
    public void accept(Event<CreateExternalDatabaseRequest> createExternalDatabaseRequest) {
        LOGGER.debug("In CreateExternalDatabaseHandler.accept");
        CreateExternalDatabaseRequest request = createExternalDatabaseRequest.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 {
            String resourceCrn = null;
            if (externalDatabase == DatabaseAvailabilityType.NONE) {
                LOGGER.info("External database for stack {} is not requested.", stack.getName());
            } else {
                LOGGER.debug("Updating stack {} status from {} to {}",
                        stack.getName(), stack.getStatus().name(), DetailedStackStatus.EXTERNAL_DATABASE_CREATION_IN_PROGRESS.name());
                stackUpdaterService.updateStatus(stack.getId(), DetailedStackStatus.EXTERNAL_DATABASE_CREATION_IN_PROGRESS,
                        ResourceEvent.CLUSTER_EXTERNAL_DATABASE_CREATION_STARTED, "External database creation in progress");
                LOGGER.debug("Getting environment CRN for stack {}", stack.getName());
                DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn());
                environmentValidator.checkValidEnvironment(stack.getName(), externalDatabase, environment);
                provisionService.provisionDatabase(stack.getCluster(), externalDatabase, environment);
                LOGGER.debug("Updating stack {} status from {} to {}",
                        stack.getName(), stack.getStatus().name(), DetailedStackStatus.PROVISION_REQUESTED.name());
                stackUpdaterService.updateStatus(stack.getId(), DetailedStackStatus.PROVISION_REQUESTED,
                        ResourceEvent.CLUSTER_EXTERNAL_DATABASE_CREATION_FINISHED, "External database creation finished");
                resourceCrn = stack.getCluster().getDatabaseServerCrn();
            }
            result = new CreateExternalDatabaseResult(stack.getId(), EXTERNAL_DATABASE_WAIT_SUCCESS_EVENT.event(), stack.getName(), resourceCrn);
        } catch (UserBreakException e) {
            LOGGER.info("Database polling exited before timeout. Cause: ", e);
            result = createFailedEvent(stack, e);
        } catch (PollerStoppedException e) {
            LOGGER.info(String.format("Database poller stopped for stack: %s", stack.getName()), e);
            result = createFailedEvent(stack, e);
        } catch (PollerException e) {
            LOGGER.info(String.format("Database polling failed for stack: %s", stack.getName()), e);
            result = createFailedEvent(stack, e);
        } catch (Exception e) {
            LOGGER.error(String.format("Exception during DB creation for stack/cluster: %s", stack.getName()), e);
            result = createFailedEvent(stack, e);
        }
        LOGGER.debug("Sending reactor notification for selector {}", result.selector());
        eventBus.notify(result.selector(), new Event<>(createExternalDatabaseRequest.getHeaders(), result));
    }

}