class IntegrationOverviewHelper {

    @SuppressWarnings("PMD.NPathComplexity")
    IntegrationOverview toCurrentIntegrationOverview(final Integration integration) {
        final String id = integration.getId().get();
        final IntegrationOverview.Builder builder = new IntegrationOverview.Builder().createFrom(integration);

        // add board
        DataManagerSupport.fetchBoard(dataManager, IntegrationBulletinBoard.class, id).ifPresent(builder::board);

        // Defaults
        builder.isDraft(true);
        builder.currentState(IntegrationDeploymentState.Unpublished);
        builder.targetState(IntegrationDeploymentState.Unpublished);

        if (!integration.getFlows().isEmpty() && !integration.getSteps().isEmpty()) {
            throw new IllegalStateException(String.format("Integration has inconsistent state: flows=%d, steps=%d",
                integration.getFlows().size(), integration.getSteps().size()));
        }

        if (integration.getFlows().isEmpty() && !integration.getSteps().isEmpty()) {
            // this means that the integration is an old integration that
            // does not have the concept of flows so let's create a flow
            // which wraps the configured steps.
            //
            // We do generate a stable flowId based on integration id the
            // endpoint
            // is invoked multiple times, the flow id does not change.
            //
            builder.addFlow(new Flow.Builder().id(String.format("%s:flows:fromSteps", integration.getId().get()))
                .name(integration.getName()).description(integration.getDescription()).steps(integration.getSteps())
                .build());

            // and remove the steps
            builder.steps(Collections.emptyList());

            // when the integration is saved, it is automatically migrated to
            // the new style.
        } else {
            // get the latest flows, connections and steps
            builder.flows(integration.getFlows().stream().map(f -> UpdatesHelper.toCurrentFlow(f, dataManager))
                .collect(Collectors.toList()));
        }

        final ListResult<IntegrationDeployment> deployments = dataManager.fetchAll(IntegrationDeployment.class,
                new IdPrefixFilter<>(id + ":"), ReverseFilter.getInstance());

        final List<IntegrationDeployment> activeDeployments = new ArrayList<>();
        for (final IntegrationDeployment deployment : deployments) {
            builder.addDeployment(IntegrationDeploymentOverview.of(deployment));

            if (deployment.getCurrentState() == IntegrationDeploymentState.Pending ||
                deployment.getCurrentState() == IntegrationDeploymentState.Published) {
                activeDeployments.add(deployment);
            }
        }

        Optional<IntegrationDeployment> maybePublished = activeDeployments.stream()
                  .filter(deployment -> deployment.getCurrentState() == IntegrationDeploymentState.Published)
                  .findFirst();

        if (integration.isExposable()) {
            builder.exposureMeans(exposureHelper.getExposureMeans());
        }

        IntegrationDeployment exposedDeployment = null;
        if (maybePublished.isPresent()) {
            IntegrationDeployment published = maybePublished.get();
            builder.isDraft(computeDraft(integration, published.getSpec()));
            builder.version(published.getVersion());
            exposedDeployment = published;
        } else if (activeDeployments.size() == 1) {
            exposedDeployment = activeDeployments.get(0);
        }

        // Set the URL of the integration deployment if present
        if (exposedDeployment != null) {
            builder.deploymentVersion(exposedDeployment.getVersion());
            builder.targetState(exposedDeployment.getTargetState());
            builder.currentState(exposedDeployment.getCurrentState());

            if (exposedDeployment.getId().isPresent()) {
                builder.managementUrl(Optional.ofNullable(exposureHelper.getManagementUrl(integration.getExposure())));

                final IntegrationEndpoint endpoint = dataManager.fetch(IntegrationEndpoint.class,
                    exposedDeployment.getId().get());
                if (endpoint != null) {
                    builder.url(endpoint.getUrl());
                }
            }
        } else if (!deployments.getItems().isEmpty()) {
            // set overview status to latest deployment because there is no active deployment
            builder.targetState(deployments.getItems().get(0).getTargetState());
            builder.currentState(deployments.getItems().get(0).getCurrentState());
        }

        return builder.build();
    }

      IntegrationOverviewHelper(DataManager dataManager, ExposureHelper exposureHelper);

}