class ImageService {

    @Measure(ImageService.class)
    public StatedImage determineImageFromCatalog(Long workspaceId, ImageSettingsV4Request imageSettings, String platformString,
            Blueprint blueprint, boolean useBaseImage, boolean baseImageEnabled,
            User user, Predicate<com.sequenceiq.cloudbreak.cloud.model.catalog.Image> imagePredicate)
            throws CloudbreakImageNotFoundException, CloudbreakImageCatalogException {
        if (useBaseImage && !baseImageEnabled) {
            throw new CloudbreakImageCatalogException("Inconsistent request, base images are disabled but custom repo information is submitted!");
        }

        if (imageSettings != null && StringUtils.isNotEmpty(imageSettings.getId())) {
            LOGGER.debug("Image id is specified for the stack.");
            StatedImage image = imageCatalogService.getImageByCatalogName(workspaceId, imageSettings.getId(), imageSettings.getCatalog());
            return checkIfBasePermitted(image, baseImageEnabled);
        }
        String clusterVersion = getClusterVersion(blueprint);

        Set<String> operatingSystems;
        try {
            operatingSystems = getSupportedOperatingSystems(workspaceId, imageSettings, clusterVersion, platformString);
        } catch (Exception ex) {
            throw new CloudbreakImageCatalogException(ex);
        }

        ImageCatalog imageCatalog = getImageCatalogFromRequestOrDefault(workspaceId, imageSettings, user);
        ImageFilter imageFilter = new ImageFilter(imageCatalog, ImmutableSet.of(platformString), null, baseImageEnabled, operatingSystems, clusterVersion);
        LOGGER.info("Image id is not specified for the stack.");
        if (baseImageEnabled && useBaseImage) {
            LOGGER.info("Trying to select a base image.");
            return imageCatalogService.getLatestBaseImageDefaultPreferred(imageFilter, imagePredicate);
        }

        LOGGER.info("Trying to select a prewarmed image.");
        return imageCatalogService.getImagePrewarmedDefaultPreferred(imageFilter, imagePredicate);
    }

}