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

import org.innovateuk.ifs.applicant.service.ApplicantRestService;
import org.innovateuk.ifs.application.common.viewmodel.SummaryViewModel;
import org.innovateuk.ifs.application.form.ApplicationForm;
import org.innovateuk.ifs.application.populator.AbstractApplicationModelPopulator;
import org.innovateuk.ifs.application.populator.forminput.FormInputViewModelGenerator;
import org.innovateuk.ifs.application.resource.ApplicationResource;
import org.innovateuk.ifs.application.resource.FormInputResponseResource;
import org.innovateuk.ifs.application.resource.QuestionStatusResource;
import org.innovateuk.ifs.application.service.*;
import org.innovateuk.ifs.application.viewmodel.forminput.AbstractFormInputViewModel;
import org.innovateuk.ifs.assessment.resource.AssessmentResource;
import org.innovateuk.ifs.assessment.service.AssessmentRestService;
import org.innovateuk.ifs.assessment.service.AssessorFormInputResponseRestService;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.form.resource.FormInputResource;
import org.innovateuk.ifs.form.resource.QuestionResource;
import org.innovateuk.ifs.form.resource.SectionResource;
import org.innovateuk.ifs.form.service.FormInputResponseRestService;
import org.innovateuk.ifs.form.service.FormInputResponseService;
import org.innovateuk.ifs.form.service.FormInputRestService;
import org.innovateuk.ifs.organisation.resource.OrganisationResource;
import org.innovateuk.ifs.user.resource.ProcessRoleResource;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.service.ProcessRoleService;
import org.innovateuk.ifs.user.service.UserService;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.innovateuk.ifs.form.resource.FormInputScope.APPLICATION;
import static org.innovateuk.ifs.util.CollectionFunctions.simpleFilter;

@Component
public class SummaryViewModelFragmentPopulator extends AbstractApplicationModelPopulator {

    private ApplicationService applicationService;
    private CompetitionService competitionService;
    private SectionService sectionService;
    private QuestionService questionService;
    private AssessorFormInputResponseRestService assessorFormInputResponseRestService;
    private ProcessRoleService processRoleService;
    private OrganisationService organisationService;
    private FormInputRestService formInputRestService;
    private FormInputResponseRestService formInputResponseRestService;
    private FormInputResponseService formInputResponseService;
    private AssessmentRestService assessmentRestService;
    private ApplicationFinanceSummaryViewModelPopulator applicationFinanceSummaryViewModelPopulator;
    private ApplicationFundingBreakdownViewModelPopulator applicationFundingBreakdownViewModelPopulator;
    private ApplicationResearchParticipationViewModelPopulator applicationResearchParticipationViewModelPopulator;
    private ApplicantRestService applicantRestService;
    private FormInputViewModelGenerator formInputViewModelGenerator;
    private UserService userService;

    public SummaryViewModelFragmentPopulator(ApplicationService applicationService,
                                             CompetitionService competitionService,
                                             SectionService sectionService,
                                             QuestionService questionService,
                                             AssessorFormInputResponseRestService assessorFormInputResponseRestService,
                                             ProcessRoleService processRoleService,
                                             OrganisationService organisationService,
                                             FormInputRestService formInputRestService,
                                             FormInputResponseRestService formInputResponseRestService,
                                             FormInputResponseService formInputResponseService,
                                             AssessmentRestService assessmentRestService,
                                             ApplicationFinanceSummaryViewModelPopulator applicationFinanceSummaryViewModelPopulator,
                                             ApplicationFundingBreakdownViewModelPopulator applicationFundingBreakdownViewModelPopulator,
                                             ApplicationResearchParticipationViewModelPopulator applicationResearchParticipationViewModelPopulator,
                                             ApplicantRestService applicantRestService,
                                             FormInputViewModelGenerator formInputViewModelGenerator,
                                             UserService userService) {
        super(sectionService, questionService);
        this.applicationService = applicationService;
        this.competitionService = competitionService;
        this.sectionService = sectionService;
        this.questionService = questionService;
        this.assessorFormInputResponseRestService = assessorFormInputResponseRestService;
        this.processRoleService = processRoleService;
        this.organisationService = organisationService;
        this.formInputRestService = formInputRestService;
        this.formInputResponseRestService = formInputResponseRestService;
        this.formInputResponseService = formInputResponseService;
        this.assessmentRestService = assessmentRestService;
        this.applicationFinanceSummaryViewModelPopulator = applicationFinanceSummaryViewModelPopulator;
        this.applicationFundingBreakdownViewModelPopulator = applicationFundingBreakdownViewModelPopulator;
        this.applicationResearchParticipationViewModelPopulator = applicationResearchParticipationViewModelPopulator;
        this.applicantRestService = applicantRestService;
        this.formInputViewModelGenerator = formInputViewModelGenerator;
        this.userService = userService;
    }

    public SummaryViewModel populate (long applicationId, UserResource user, ApplicationForm form) {

        ApplicationResource application = applicationService.getById(applicationId);
        CompetitionResource competition = competitionService.getById(application.getCompetition());

        Map<Long, List<QuestionResource>> sectionQuestions = getSectionQuestions(competition.getId());

        List<ProcessRoleResource> userApplicationRoles = processRoleService.findProcessRolesByApplicationId(application.getId());
        Optional<OrganisationResource> userOrganisation = organisationService.getOrganisationForUser(user.getId(), userApplicationRoles);

        List<FormInputResponseResource> responses = formInputResponseRestService.getResponsesByApplicationId(application.getId()).getSuccess();
        Map<Long, FormInputResponseResource> mappedResponses = formInputResponseService.mapFormInputResponsesToFormInput(responses);

        final Map<Long, QuestionStatusResource> questionAssignees;
        if (userOrganisation.isPresent()) {
            questionAssignees = questionService.getQuestionStatusesForApplicationAndOrganisation(application.getId(), userOrganisation.get().getId());
        } else {
            questionAssignees = Collections.emptyMap();
        }
        List<AssessmentResource> feedbackSummary = assessmentRestService
                .getByUserAndApplication(user.getId(), applicationId)
                .getSuccess();

        SectionResource financeSection = sectionService.getFinanceSection(application.getCompetition());
        final boolean hasFinanceSection = financeSection != null;
        Long financeSectionId = null;
        if (hasFinanceSection) {
            financeSectionId = financeSection.getId();
        }

        //Comp admin user doesn't have user organisation
        long applicantId;
        if (!userOrganisation.isPresent())  {
            ProcessRoleResource leadApplicantProcessRole = userService.getLeadApplicantProcessRoleOrNull(application.getId());
            applicantId = leadApplicantProcessRole.getUser();
        } else {
            applicantId = user.getId();
        }

        Map<String, String> values = form.getFormInput();
        mappedResponses.forEach((k, v) ->
                values.put(k.toString(), v.getValue())
        );
        form.setFormInput(values);

        boolean showApplicationTeamLink = applicationService.showApplicationTeam(application.getId(), user.getId());

        return new SummaryViewModel(
                application,
                getSections(competition.getId()),
                sectionQuestions,
                getQuestionFormInputs(sectionQuestions, competition.getId()),<commit_msg>IFS-3982 add applicant organisation if comp admin<commit_after>package org.innovateuk.ifs.application.common.populator;

import org.innovateuk.ifs.applicant.service.ApplicantRestService;
import org.innovateuk.ifs.application.common.viewmodel.SummaryViewModel;
import org.innovateuk.ifs.application.form.ApplicationForm;
import org.innovateuk.ifs.application.populator.AbstractApplicationModelPopulator;
import org.innovateuk.ifs.application.populator.forminput.FormInputViewModelGenerator;
import org.innovateuk.ifs.application.resource.ApplicationResource;
import org.innovateuk.ifs.application.resource.FormInputResponseResource;
import org.innovateuk.ifs.application.resource.QuestionStatusResource;
import org.innovateuk.ifs.application.service.*;
import org.innovateuk.ifs.application.viewmodel.forminput.AbstractFormInputViewModel;
import org.innovateuk.ifs.assessment.resource.AssessmentResource;
import org.innovateuk.ifs.assessment.service.AssessmentRestService;
import org.innovateuk.ifs.assessment.service.AssessorFormInputResponseRestService;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.form.resource.FormInputResource;
import org.innovateuk.ifs.form.resource.QuestionResource;
import org.innovateuk.ifs.form.resource.SectionResource;
import org.innovateuk.ifs.form.service.FormInputResponseRestService;
import org.innovateuk.ifs.form.service.FormInputResponseService;
import org.innovateuk.ifs.form.service.FormInputRestService;
import org.innovateuk.ifs.organisation.resource.OrganisationResource;
import org.innovateuk.ifs.user.resource.ProcessRoleResource;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.service.ProcessRoleService;
import org.innovateuk.ifs.user.service.UserService;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.innovateuk.ifs.form.resource.FormInputScope.APPLICATION;
import static org.innovateuk.ifs.util.CollectionFunctions.simpleFilter;

@Component
public class SummaryViewModelFragmentPopulator extends AbstractApplicationModelPopulator {

    private ApplicationService applicationService;
    private CompetitionService competitionService;
    private SectionService sectionService;
    private QuestionService questionService;
    private AssessorFormInputResponseRestService assessorFormInputResponseRestService;
    private ProcessRoleService processRoleService;
    private OrganisationService organisationService;
    private FormInputRestService formInputRestService;
    private FormInputResponseRestService formInputResponseRestService;
    private FormInputResponseService formInputResponseService;
    private AssessmentRestService assessmentRestService;
    private ApplicationFinanceSummaryViewModelPopulator applicationFinanceSummaryViewModelPopulator;
    private ApplicationFundingBreakdownViewModelPopulator applicationFundingBreakdownViewModelPopulator;
    private ApplicationResearchParticipationViewModelPopulator applicationResearchParticipationViewModelPopulator;
    private ApplicantRestService applicantRestService;
    private FormInputViewModelGenerator formInputViewModelGenerator;
    private UserService userService;

    public SummaryViewModelFragmentPopulator(ApplicationService applicationService,
                                             CompetitionService competitionService,
                                             SectionService sectionService,
                                             QuestionService questionService,
                                             AssessorFormInputResponseRestService assessorFormInputResponseRestService,
                                             ProcessRoleService processRoleService,
                                             OrganisationService organisationService,
                                             FormInputRestService formInputRestService,
                                             FormInputResponseRestService formInputResponseRestService,
                                             FormInputResponseService formInputResponseService,
                                             AssessmentRestService assessmentRestService,
                                             ApplicationFinanceSummaryViewModelPopulator applicationFinanceSummaryViewModelPopulator,
                                             ApplicationFundingBreakdownViewModelPopulator applicationFundingBreakdownViewModelPopulator,
                                             ApplicationResearchParticipationViewModelPopulator applicationResearchParticipationViewModelPopulator,
                                             ApplicantRestService applicantRestService,
                                             FormInputViewModelGenerator formInputViewModelGenerator,
                                             UserService userService) {
        super(sectionService, questionService);
        this.applicationService = applicationService;
        this.competitionService = competitionService;
        this.sectionService = sectionService;
        this.questionService = questionService;
        this.assessorFormInputResponseRestService = assessorFormInputResponseRestService;
        this.processRoleService = processRoleService;
        this.organisationService = organisationService;
        this.formInputRestService = formInputRestService;
        this.formInputResponseRestService = formInputResponseRestService;
        this.formInputResponseService = formInputResponseService;
        this.assessmentRestService = assessmentRestService;
        this.applicationFinanceSummaryViewModelPopulator = applicationFinanceSummaryViewModelPopulator;
        this.applicationFundingBreakdownViewModelPopulator = applicationFundingBreakdownViewModelPopulator;
        this.applicationResearchParticipationViewModelPopulator = applicationResearchParticipationViewModelPopulator;
        this.applicantRestService = applicantRestService;
        this.formInputViewModelGenerator = formInputViewModelGenerator;
        this.userService = userService;
    }

    public SummaryViewModel populate (long applicationId, UserResource user, ApplicationForm form) {

        ApplicationResource application = applicationService.getById(applicationId);
        CompetitionResource competition = competitionService.getById(application.getCompetition());

        Map<Long, List<QuestionResource>> sectionQuestions = getSectionQuestions(competition.getId());

        List<ProcessRoleResource> userApplicationRoles = processRoleService.findProcessRolesByApplicationId(application.getId());
        Optional<OrganisationResource> userOrganisation = organisationService.getOrganisationForUser(user.getId(), userApplicationRoles);

        List<FormInputResponseResource> responses = formInputResponseRestService.getResponsesByApplicationId(application.getId()).getSuccess();
        Map<Long, FormInputResponseResource> mappedResponses = formInputResponseService.mapFormInputResponsesToFormInput(responses);

        final Map<Long, QuestionStatusResource> questionAssignees;
        if (userOrganisation.isPresent()) {
            questionAssignees = questionService.getQuestionStatusesForApplicationAndOrganisation(application.getId(), userOrganisation.get().getId());
        } else {
            questionAssignees = Collections.emptyMap();
        }
        List<AssessmentResource> feedbackSummary = assessmentRestService
                .getByUserAndApplication(user.getId(), applicationId)
                .getSuccess();

        SectionResource financeSection = sectionService.getFinanceSection(application.getCompetition());
        final boolean hasFinanceSection = financeSection != null;
        Long financeSectionId = null;
        if (hasFinanceSection) {
            financeSectionId = financeSection.getId();
        }

        //Comp admin user doesn't have user organisation
        long applicantId;
        if (!userOrganisation.isPresent())  {
            ProcessRoleResource leadApplicantProcessRole = userService.getLeadApplicantProcessRoleOrNull(application.getId());
            applicantId = leadApplicantProcessRole.getUser();
            userOrganisation = organisationService.getOrganisationForUser(applicantId, userApplicationRoles);
        } else {
            applicantId = user.getId();
        }

        Map<String, String> values = form.getFormInput();
        mappedResponses.forEach((k, v) ->
                values.put(k.toString(), v.getValue())
        );
        form.setFormInput(values);

        boolean showApplicationTeamLink = applicationService.showApplicationTeam(application.getId(), user.getId());

        return new SummaryViewModel(
                application,
                getSections(competition.getId()),
                sectionQuestions,
                getQuestionFormInputs(sectionQuestions, competition.getId()),
{'fraction_non_alphanumeric': 0.0589575461213603, 'fraction_numerical': 0.0002222716159146477, 'mean_word_length': 3.587560540402753, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 64, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2685190', 'n_tokens_mistral': 4007, 'n_tokens_neox': 3832, 'n_words': 620}