class DatabaseFDFlowManager extends BaseService implements FDFlowManager {

    @Override
    public FDFlowEvent deleteFlowEvent(final String flowEventId) {
        if (StringUtils.isBlank(flowEventId)) {
            throw new IllegalArgumentException("Flow event id cannot be null or blank");
        }

        final Optional<FDFlowEventEntity> flowEventEntity = flowEventRepository.findById(flowEventId);
        if (!flowEventEntity.isPresent()) {
            throw new ResourceNotFoundException("No flow event exists for the given id");
        }

        if (flowEventEntity.get().getEventType() == FDFlowEventType.FLOW_PUBLISHED) {
            throw new IllegalArgumentException("Cannot delete event because deleting events of type "
                    + FDFlowEventType.FLOW_PUBLISHED + " is not allowed");
        }

        final FDFlowEntity flowEntity = flowEventEntity.get().getFlow();

        final Long numEventsForFlow = flowEventRepository.countByFlow(flowEntity);
        if (numEventsForFlow <= 1) {
            throw new IllegalStateException("Cannot delete event because there is only one event for the given flow");
        }

        flowEventRepository.delete(flowEventEntity.get());

        // we don't know if the event being deleted is the latest event, and if it is then the cached version of
        // the flow is no longer accurate because it was based on the deleted event
        if (flowsById.containsKey(flowEntity.getId())) {
            flowsById.remove(flowEntity.getId());
        }

        return modelMapper.map(flowEventEntity.get(), FDFlowEvent.class);
    }

    @Autowired public  DatabaseFDFlowManager(final FDFlowRepository flowRepository,
                                 final FDFlowEventRepository flowEventRepository,
                                 final OptionalModelMapper modelMapper,
                                 final ObjectMapper objectMapper,
                                 final Validator validator);

}