class WatchdogDistribution {

    public String getOverallDistributionStatus(String distributionId) throws Exception {
        logger.debug("Entered getOverallDistributionStatus method for distrubutionId: {}", distributionId);

        String status = null;
        try {
            WatchdogDistributionStatus watchdogDistributionStatus =
                    watchdogDistributionStatusRepository.findById(distributionId).orElseGet(() -> null);
            if (watchdogDistributionStatus == null) {
                watchdogDistributionStatus = new WatchdogDistributionStatus();
                watchdogDistributionStatus.setDistributionId(distributionId);
                watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
            }
            String distributionStatus = watchdogDistributionStatus.getDistributionIdStatus();

            if (DistributionStatus.TIMEOUT.name().equalsIgnoreCase(distributionStatus)) {
                logger.debug("Ignoring to update WatchdogDistributionStatus as distributionId: {} status is set to: {}",
                        distributionId, distributionStatus);
                return DistributionStatus.TIMEOUT.name();
            } else {
                List<WatchdogComponentDistributionStatus> results =
                        watchdogCDStatusRepository.findByDistributionId(distributionId);
                logger.debug("Executed RequestDB getWatchdogComponentDistributionStatus for distrubutionId: {}",
                        distributionId);

                // *************************************************************************************************************************************************
                // **** Compare config values verse DB watchdog component names to see if every component has reported
                // status before returning final result back to ASDC
                // **************************************************************************************************************************************************

                List<WatchdogComponentDistributionStatus> cdStatuses =
                        watchdogCDStatusRepository.findByDistributionId(distributionId);

                boolean allComponentsComplete = true;

                for (String name : componentNames) {

                    boolean match = false;

                    for (WatchdogComponentDistributionStatus cdStatus : cdStatuses) {
                        if (name.equals(cdStatus.getComponentName())) {
                            logger.debug("Found componentName {} in the WatchDog Component DB", name);
                            match = true;
                            break;
                        }
                    }
                    if (!match) {
                        logger.debug("{} has not be updated in the the WatchDog Component DB yet, so ending the loop",
                                name);
                        allComponentsComplete = false;
                        break;
                    }
                }


                if (allComponentsComplete) {
                    logger.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");

                    for (WatchdogComponentDistributionStatus componentDist : results) {
                        String componentDistributionStatus = componentDist.getComponentDistributionStatus();
                        logger.debug("Component status: {} on componentName: ", componentDistributionStatus,
                                componentDist.getComponentName());
                        if (componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_ERROR")) {
                            status = DistributionStatus.FAILURE.name();
                            break;
                        } else if (componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {
                            status = DistributionStatus.SUCCESS.name();
                        } else {
                            throw new Exception(
                                    "Invalid Component distribution status: " + componentDistributionStatus);
                        }
                    }

                    logger.debug("Updating overall DistributionStatus to: {} for distributionId: ", status,
                            distributionId);

                    watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
                } else {
                    logger.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");
                    status = DistributionStatus.INCOMPLETE.name();
                    return status;
                }
            }
        } catch (Exception e) {
            logger.debug("Exception occurred on getOverallDistributionStatus : {}", e.getMessage());
            logger.error("Exception occurred", e);
            throw new Exception(e);
        }
        logger.debug("Exiting getOverallDistributionStatus method in WatchdogDistribution");
        return status;
    }

}