<commit_before>package org.innovateuk.ifs.application.overview.populator;

import org.innovateuk.ifs.application.ApplicationUrlHelper;
import org.innovateuk.ifs.application.overview.ApplicationOverviewData;
import org.innovateuk.ifs.application.overview.viewmodel.ApplicationOverviewRowViewModel;
import org.innovateuk.ifs.application.overview.viewmodel.ApplicationOverviewSectionViewModel;
import org.innovateuk.ifs.application.overview.viewmodel.ApplicationOverviewViewModel;
import org.innovateuk.ifs.application.resource.ApplicationResource;
import org.innovateuk.ifs.application.resource.QuestionStatusResource;
import org.innovateuk.ifs.application.service.*;
import org.innovateuk.ifs.application.viewmodel.AssignButtonsViewModel;
import org.innovateuk.ifs.async.generation.AsyncAdaptor;
import org.innovateuk.ifs.async.generation.AsyncFuturesGenerator;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.competition.service.CompetitionRestService;
import org.innovateuk.ifs.competition.service.CompetitionThirdPartyConfigRestService;
import org.innovateuk.ifs.form.resource.QuestionResource;
import org.innovateuk.ifs.form.resource.SectionResource;
import org.innovateuk.ifs.form.resource.SectionType;
import org.innovateuk.ifs.organisation.resource.OrganisationResource;
import org.innovateuk.ifs.question.resource.QuestionSetupType;
import org.innovateuk.ifs.user.resource.ProcessRoleResource;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.service.OrganisationRestService;
import org.innovateuk.ifs.user.service.ProcessRoleRestService;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.Future;

import static java.lang.Boolean.TRUE;
import static java.lang.String.format;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toCollection;
import static org.innovateuk.ifs.competition.resource.CollaborationLevel.SINGLE;
import static org.innovateuk.ifs.form.resource.SectionType.OVERVIEW_FINANCES;
import static org.innovateuk.ifs.question.resource.QuestionSetupType.ASSESSED_QUESTION;
import static org.innovateuk.ifs.question.resource.QuestionSetupType.TERMS_AND_CONDITIONS;


/**
 * view model for the application overview page
 */
@Component
public class ApplicationOverviewModelPopulator extends AsyncAdaptor {

    private final CompetitionRestService competitionRestService;
    private final SectionRestService sectionRestService;
    private final QuestionRestService questionRestService;
    private final ProcessRoleRestService processRoleRestService;
    private final MessageSource messageSource;
    private final OrganisationRestService organisationRestService;
    private final QuestionStatusRestService questionStatusRestService;
    private final SectionStatusRestService sectionStatusRestService;
    private final QuestionService questionService;
    private final ApplicationUrlHelper applicationUrlHelper;
    private static CompetitionThirdPartyConfigRestService competitionThirdPartyConfigRestService;

    public ApplicationOverviewModelPopulator(AsyncFuturesGenerator asyncFuturesGenerator, CompetitionRestService competitionRestService,
                                             SectionRestService sectionRestService, QuestionRestService questionRestService,
                                             ProcessRoleRestService processRoleRestService, MessageSource messageSource,
                                             OrganisationRestService organisationRestService, QuestionStatusRestService questionStatusRestService,
                                             SectionStatusRestService sectionStatusRestService,
                                             QuestionService questionService,
                                             ApplicationUrlHelper applicationUrlHelper,
                                             CompetitionThirdPartyConfigRestService competitionThirdPartyConfigRestService) {
        super(asyncFuturesGenerator);
        this.competitionRestService = competitionRestService;
        this.sectionRestService = sectionRestService;
        this.questionRestService = questionRestService;
        this.processRoleRestService = processRoleRestService;
        this.messageSource = messageSource;
        this.organisationRestService = organisationRestService;
        this.questionStatusRestService = questionStatusRestService;
        this.sectionStatusRestService = sectionStatusRestService;
        this.questionService = questionService;
        this.applicationUrlHelper = applicationUrlHelper;
        this.competitionThirdPartyConfigRestService = competitionThirdPartyConfigRestService;
    }

    public ApplicationOverviewViewModel populateModel(ApplicationResource application, UserResource user) {
        Future<OrganisationResource> organisation = async(() -> organisationRestService.getByUserAndApplicationId(user.getId(), application.getId()).getSuccess());
        Future<CompetitionResource> competition = async(() -> competitionRestService.getCompetitionById(application.getCompetition()).getSuccess());
        Future<List<SectionResource>> sections = async(() -> sectionRestService.getByCompetition(application.getCompetition()).getSuccess());
        Future<List<QuestionResource>> questions = async(() -> questionRestService.findByCompetition(application.getCompetition()).getSuccess());
        Future<List<ProcessRoleResource>> processRoles = async(() -> processRoleRestService.findProcessRole(application.getId()).getSuccess());
        Future<List<QuestionStatusResource>> statuses = async(() -> questionStatusRestService.findByApplicationAndOrganisation(application.getId(), resolve(organisation).getId()).getSuccess());
        Future<List<Long>> completedSectionIds = async(() -> sectionStatusRestService.getCompletedSectionIds(application.getId(), resolve(organisation).getId()).getSuccess());
        Future<Map<Long, Set<Long>>> completedSectionsByOrganisation = async(() -> sectionStatusRestService.getCompletedSectionsByOrganisation(application.getId()).getSuccess());

        async(() -> {
            List<QuestionStatusResource> notifications = questionService.getNotificationsForUser(resolve(statuses), user.getId());
            questionService.removeNotifications(notifications);
        });

        ApplicationOverviewData data = new ApplicationOverviewData(resolve(competition), application, resolve(sections),
                resolve(questions), resolve(processRoles), resolve(organisation), resolve(statuses),
                resolve(completedSectionIds), resolve(completedSectionsByOrganisation), user);

        Set<ApplicationOverviewSectionViewModel> sectionViewModels = data.getSections()
                .values()
                .stream()
                .sorted(comparing(SectionResource::getPriority))
                .filter(section -> section.getParentSection() == null)
                .filter(section -> section.getType() != SectionType.KTP_ASSESSMENT)
                .map(section -> sectionViewModel(section, data))
                .collect(toCollection(LinkedHashSet::new));

        return new ApplicationOverviewViewModel(data.getUserProcessRole(), data.getCompetition(), application, sectionViewModels, application.hasBeenReopened(), application.getLastStateChangeDate());
    }

    private ApplicationOverviewSectionViewModel sectionViewModel(SectionResource section, ApplicationOverviewData data) {
        Set<ApplicationOverviewRowViewModel> rows;
        if (!section.getChildSections().isEmpty()) {
            rows = section.getChildSections()
                    .stream()
                    .map(data.getSections()::get)
                    .filter(childSection -> !(data.getCompetition().isFullyFunded() && childSection.getType().equals(OVERVIEW_FINANCES)))
                    .map(childSection ->
                            new ApplicationOverviewRowViewModel(
                                    childSection.getName(),
                                    format("/application/%d/form/section/%d", data.getApplication().getId(), childSection.getId()),
                                    data.getCompletedSectionIds().contains(childSection.getId()),
                                    true
                            )
                    )
                    .collect(toCollection(LinkedHashSet::new));
        } else {
            rows = section.getQuestions()
                    .stream()
                    .map(data.getQuestions()::get)
                    .map(question -> getApplicationOverviewRowViewModel(data, question, section))
                    .collect(toCollection(LinkedHashSet::new));
        }

        if (section.isTermsAndConditions() && data.getCompetition().getTermsAndConditions().isProcurementThirdParty()) {
            String labelName = competitionThirdPartyConfigRestService.findOneByCompetitionId(data.getCompetition().getId()).getSuccess().getTermsAndConditionsLabel();
            section.setName(labelName);
        }

        String subtitle = subtitle(data.getCompetition(), section);<commit_msg>IFS-11221 initial commit<commit_after>package org.innovateuk.ifs.application.overview.populator;

import org.innovateuk.ifs.application.ApplicationUrlHelper;
import org.innovateuk.ifs.application.overview.ApplicationOverviewData;
import org.innovateuk.ifs.application.overview.viewmodel.ApplicationOverviewRowViewModel;
import org.innovateuk.ifs.application.overview.viewmodel.ApplicationOverviewSectionViewModel;
import org.innovateuk.ifs.application.overview.viewmodel.ApplicationOverviewViewModel;
import org.innovateuk.ifs.application.resource.ApplicationResource;
import org.innovateuk.ifs.application.resource.QuestionStatusResource;
import org.innovateuk.ifs.application.service.*;
import org.innovateuk.ifs.application.viewmodel.AssignButtonsViewModel;
import org.innovateuk.ifs.async.generation.AsyncAdaptor;
import org.innovateuk.ifs.async.generation.AsyncFuturesGenerator;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.competition.service.CompetitionRestService;
import org.innovateuk.ifs.competition.service.CompetitionThirdPartyConfigRestService;
import org.innovateuk.ifs.form.resource.QuestionResource;
import org.innovateuk.ifs.form.resource.SectionResource;
import org.innovateuk.ifs.form.resource.SectionType;
import org.innovateuk.ifs.organisation.resource.OrganisationResource;
import org.innovateuk.ifs.question.resource.QuestionSetupType;
import org.innovateuk.ifs.user.resource.ProcessRoleResource;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.service.OrganisationRestService;
import org.innovateuk.ifs.user.service.ProcessRoleRestService;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.Future;

import static java.lang.Boolean.TRUE;
import static java.lang.String.format;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toCollection;
import static org.innovateuk.ifs.competition.resource.CollaborationLevel.SINGLE;
import static org.innovateuk.ifs.form.resource.SectionType.OVERVIEW_FINANCES;
import static org.innovateuk.ifs.question.resource.QuestionSetupType.ASSESSED_QUESTION;
import static org.innovateuk.ifs.question.resource.QuestionSetupType.TERMS_AND_CONDITIONS;


/**
 * view model for the application overview page
 */
@Component
public class ApplicationOverviewModelPopulator extends AsyncAdaptor {

    private final CompetitionRestService competitionRestService;
    private final SectionRestService sectionRestService;
    private final QuestionRestService questionRestService;
    private final ProcessRoleRestService processRoleRestService;
    private final MessageSource messageSource;
    private final OrganisationRestService organisationRestService;
    private final QuestionStatusRestService questionStatusRestService;
    private final SectionStatusRestService sectionStatusRestService;
    private final QuestionService questionService;
    private final ApplicationUrlHelper applicationUrlHelper;
    private static CompetitionThirdPartyConfigRestService competitionThirdPartyConfigRestService;

    public ApplicationOverviewModelPopulator(AsyncFuturesGenerator asyncFuturesGenerator, CompetitionRestService competitionRestService,
                                             SectionRestService sectionRestService, QuestionRestService questionRestService,
                                             ProcessRoleRestService processRoleRestService, MessageSource messageSource,
                                             OrganisationRestService organisationRestService, QuestionStatusRestService questionStatusRestService,
                                             SectionStatusRestService sectionStatusRestService,
                                             QuestionService questionService,
                                             ApplicationUrlHelper applicationUrlHelper,
                                             CompetitionThirdPartyConfigRestService competitionThirdPartyConfigRestService) {
        super(asyncFuturesGenerator);
        this.competitionRestService = competitionRestService;
        this.sectionRestService = sectionRestService;
        this.questionRestService = questionRestService;
        this.processRoleRestService = processRoleRestService;
        this.messageSource = messageSource;
        this.organisationRestService = organisationRestService;
        this.questionStatusRestService = questionStatusRestService;
        this.sectionStatusRestService = sectionStatusRestService;
        this.questionService = questionService;
        this.applicationUrlHelper = applicationUrlHelper;
        this.competitionThirdPartyConfigRestService = competitionThirdPartyConfigRestService;
    }

    public ApplicationOverviewViewModel populateModel(ApplicationResource application, UserResource user) {
        Future<OrganisationResource> organisation = async(() -> organisationRestService.getByUserAndApplicationId(user.getId(), application.getId()).getSuccess());
        Future<CompetitionResource> competition = async(() -> competitionRestService.getCompetitionById(application.getCompetition()).getSuccess());
        Future<List<SectionResource>> sections = async(() -> sectionRestService.getByCompetition(application.getCompetition()).getSuccess());
        Future<List<QuestionResource>> questions = async(() -> questionRestService.findByCompetition(application.getCompetition()).getSuccess());
        Future<List<ProcessRoleResource>> processRoles = async(() -> processRoleRestService.findProcessRole(application.getId()).getSuccess());
        Future<List<QuestionStatusResource>> statuses = async(() -> questionStatusRestService.findByApplicationAndOrganisation(application.getId(), resolve(organisation).getId()).getSuccess());
        Future<List<Long>> completedSectionIds = async(() -> sectionStatusRestService.getCompletedSectionIds(application.getId(), resolve(organisation).getId()).getSuccess());
        Future<Map<Long, Set<Long>>> completedSectionsByOrganisation = async(() -> sectionStatusRestService.getCompletedSectionsByOrganisation(application.getId()).getSuccess());

        async(() -> {
            List<QuestionStatusResource> notifications = questionService.getNotificationsForUser(resolve(statuses), user.getId());
            questionService.removeNotifications(notifications);
        });

        ApplicationOverviewData data = new ApplicationOverviewData(resolve(competition), application, resolve(sections),
                resolve(questions), resolve(processRoles), resolve(organisation), resolve(statuses),
                resolve(completedSectionIds), resolve(completedSectionsByOrganisation), user);

        Set<ApplicationOverviewSectionViewModel> sectionViewModels = data.getSections()
                .values()
                .stream()
                .sorted(comparing(SectionResource::getPriority))
                .filter(section -> section.getParentSection() == null)
                .filter(section -> section.getType() != SectionType.KTP_ASSESSMENT)
                .map(section -> sectionViewModel(section, data))
                .collect(toCollection(LinkedHashSet::new));

        return new ApplicationOverviewViewModel(data.getUserProcessRole(), data.getCompetition(), application, sectionViewModels, application.hasBeenReopened(), application.getLastStateChangeDate());
    }

    private ApplicationOverviewSectionViewModel sectionViewModel(SectionResource section, ApplicationOverviewData data) {
        Set<ApplicationOverviewRowViewModel> rows;
        if (!section.getChildSections().isEmpty()) {
            rows = section.getChildSections()
                    .stream()
                    .map(data.getSections()::get)
                    .filter(childSection -> !(data.getCompetition().isFullyFunded() && childSection.getType().equals(OVERVIEW_FINANCES)))
                    .map(childSection ->
                            new ApplicationOverviewRowViewModel(
                                    childSection.getName(),
                                    format("/application/%d/form/section/%d", data.getApplication().getId(), childSection.getId()),
                                    data.getCompletedSectionIds().contains(childSection.getId()),
                                    true
                            )
                    )
                    .collect(toCollection(LinkedHashSet::new));
        } else {
            rows = section.getQuestions()
                    .stream()
                    .map(data.getQuestions()::get)
                    .filter(question -> !question.getQuestionSetupType().equals(QuestionSetupType.EQUALITY_DIVERSITY_INCLUSION))
                    .map(question -> getApplicationOverviewRowViewModel(data, question, section))
                    .collect(toCollection(LinkedHashSet::new));
        }

        if (section.isTermsAndConditions() && data.getCompetition().getTermsAndConditions().isProcurementThirdParty()) {
            String labelName = competitionThirdPartyConfigRestService.findOneByCompetitionId(data.getCompetition().getId()).getSuccess().getTermsAndConditionsLabel();
            section.setName(labelName);
        }

        String subtitle = subtitle(data.getCompetition(), section);
{'fraction_non_alphanumeric': 0.08566810344827586, 'fraction_numerical': 0.00026939655172413793, 'mean_word_length': 3.7592307692307694, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '147055', 'n_tokens_mistral': 4401, 'n_tokens_neox': 4183, 'n_words': 639}