<commit_before>package org.innovateuk.ifs.application.transactional.sectionupdater;

import org.innovateuk.ifs.application.domain.Application;
import org.innovateuk.ifs.application.domain.Section;
import org.innovateuk.ifs.application.resource.QuestionType;
import org.innovateuk.ifs.application.resource.SectionType;
import org.innovateuk.ifs.application.transactional.QuestionService;
import org.innovateuk.ifs.application.transactional.SectionService;
import org.innovateuk.ifs.finance.domain.ApplicationFinance;
import org.innovateuk.ifs.finance.transactional.FinanceRowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.innovateuk.ifs.commons.service.ServiceResult.serviceSuccess;

/**
 * Handle specific actions when this part gets save in the applicationForm
 */
@Service
public class ApplicationFinanceOrganisationUpdater implements ApplicationFinanceUpdater {

    @Autowired
    private SectionService sectionService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private FinanceRowService financeRowService;

    @Override
    public SectionType getRelatedSection() {
        return SectionType.ORGANISATION_FINANCES;
    }

    @Override
    public void handleMarkAsInComplete(Application currentApplication, Section currentSection, Long processRoleId) {

    }

    private Set<Long> completedSections;

    @Override
    public void handleMarkAsComplete(Application currentApplication, Section currentSection, Long processRoleId) {

        Optional<ApplicationFinance> applicationFinance = getApplicationFinance(currentApplication.getApplicationFinances(), processRoleId);

        getCompletedSections(applicationFinance);

        sectionService.getSectionsByCompetitionIdAndType(currentSection.getCompetition().getId(), SectionType.FUNDING_FINANCES)
        .andOnSuccess(sectionList -> {
            sectionList.stream()
                .filter(fundingSection -> completedSections != null && completedSections.contains(fundingSection.getId()))
                .forEach(fundingSection -> {
                    resetFundingSectionToMarkedAsIncomplete(fundingSection.getId(), currentApplication.getId(), processRoleId);
                    resetFundingSectionQuestionInputs(fundingSection.getId(), applicationFinance);
                });
            return serviceSuccess();
        });
    }

    private void getCompletedSections(Optional<ApplicationFinance> applicationFinance) {
        applicationFinance.ifPresent(applicationFinanceFound -> {
                sectionService.getCompletedSections(applicationFinanceFound.getApplication().getId(), applicationFinanceFound.getOrganisation().getId())
                        .andOnSuccess(successObject -> {
                            completedSections = successObject;
                            return serviceSuccess();
                        });
        });
    }

    private Optional<ApplicationFinance> getApplicationFinance(List<ApplicationFinance> applicationFinances, Long processRoleId) {
        return applicationFinances.stream()
                .filter(applicationFinance -> applicationFinance.getOrganisation().getProcessRoles().stream()
                        .anyMatch(processRole -> processRoleId.equals(processRole.getId())))<commit_msg>INFUND-6894 Fix for singleton services<commit_after>package org.innovateuk.ifs.application.transactional.sectionupdater;

import org.innovateuk.ifs.application.domain.Application;
import org.innovateuk.ifs.application.domain.Section;
import org.innovateuk.ifs.application.resource.QuestionType;
import org.innovateuk.ifs.application.resource.SectionType;
import org.innovateuk.ifs.application.transactional.QuestionService;
import org.innovateuk.ifs.application.transactional.SectionService;
import org.innovateuk.ifs.finance.domain.ApplicationFinance;
import org.innovateuk.ifs.finance.transactional.FinanceRowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.innovateuk.ifs.commons.service.ServiceResult.serviceSuccess;

/**
 * Handle specific actions when this part gets save in the applicationForm
 */
@Service
public class ApplicationFinanceOrganisationUpdater implements ApplicationFinanceUpdater {

    @Autowired
    private SectionService sectionService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private FinanceRowService financeRowService;

    @Override
    public SectionType getRelatedSection() {
        return SectionType.ORGANISATION_FINANCES;
    }

    @Override
    public void handleMarkAsInComplete(Application currentApplication, Section currentSection, Long processRoleId) {

    }

    @Override
    public void handleMarkAsComplete(Application currentApplication, Section currentSection, Long processRoleId) {

        Optional<ApplicationFinance> applicationFinance = getApplicationFinance(currentApplication.getApplicationFinances(), processRoleId);

        Optional<Set<Long>> completedSections = getCompletedSections(applicationFinance);

        sectionService.getSectionsByCompetitionIdAndType(currentSection.getCompetition().getId(), SectionType.FUNDING_FINANCES)
        .andOnSuccess(sectionList -> {
            sectionList.stream()
                .filter(fundingSection -> completedSections.isPresent() && completedSections.get().contains(fundingSection.getId()))
                .forEach(fundingSection -> {
                    resetFundingSectionToMarkedAsIncomplete(fundingSection.getId(), currentApplication.getId(), processRoleId);
                    resetFundingSectionQuestionInputs(fundingSection.getId(), applicationFinance);
                });
            return serviceSuccess();
        });
    }

    private Optional<Set<Long>> getCompletedSections(Optional<ApplicationFinance> applicationFinance) {
        if(applicationFinance.isPresent()) {
            return Optional.ofNullable(
                    sectionService
                            .getCompletedSections(applicationFinance.get().getApplication().getId(),
                                                    applicationFinance.get().getOrganisation().getId())
                            .getSuccessObjectOrThrowException());
        }

        return Optional.empty();
    }

    private Optional<ApplicationFinance> getApplicationFinance(List<ApplicationFinance> applicationFinances, Long processRoleId) {
        return applicationFinances.stream()
                .filter(applicationFinance -> applicationFinance.getOrganisation().getProcessRoles().stream()
                        .anyMatch(processRole -> processRoleId.equals(processRole.getId())))
{'fraction_non_alphanumeric': 0.08293460925039872, 'fraction_numerical': 0.0005799623024503408, 'mean_word_length': 3.790277777777778, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5666212', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1669, 'n_words': 266}