<commit_before>package org.innovateuk.ifs.documents.populator;

import org.innovateuk.ifs.competition.resource.CompetitionDocumentResource;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.competition.service.CompetitionRestService;
import org.innovateuk.ifs.documents.viewModel.AllDocumentsViewModel;
import org.innovateuk.ifs.documents.viewModel.DocumentViewModel;
import org.innovateuk.ifs.file.controller.viewmodel.FileDetailsViewModel;
import org.innovateuk.ifs.project.document.resource.DocumentStatus;
import org.innovateuk.ifs.project.document.resource.ProjectDocumentResource;
import org.innovateuk.ifs.project.document.resource.ProjectDocumentStatus;
import org.innovateuk.ifs.project.monitoring.service.MonitoringOfficerRestService;
import org.innovateuk.ifs.project.resource.PartnerOrganisationResource;
import org.innovateuk.ifs.project.resource.ProjectResource;
import org.innovateuk.ifs.project.resource.ProjectUserResource;
import org.innovateuk.ifs.project.service.PartnerOrganisationRestService;
import org.innovateuk.ifs.project.service.ProjectRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

import static org.innovateuk.ifs.competition.resource.CompetitionDocumentResource.COLLABORATION_AGREEMENT_TITLE;
import static org.innovateuk.ifs.util.CollectionFunctions.simpleFindAny;
import static org.innovateuk.ifs.util.CollectionFunctions.simpleMap;

@Component
public class DocumentsPopulator {

    @Autowired
    private PartnerOrganisationRestService partnerOrganisationRestService;

    @Autowired
    private CompetitionRestService competitionRestService;

    @Autowired
    private ProjectRestService projectRestService;

    @Autowired
    private MonitoringOfficerRestService monitoringOfficerRestService;

    @Value("${ifs.monitoringofficer.journey.update.enabled}")
    private boolean isMOJourneyUpdateEnabled;

    public AllDocumentsViewModel populateAllDocuments(long projectId, long loggedInUserId) {

        ProjectResource project = projectRestService.getProjectById(projectId).getSuccess();

        CompetitionResource competition = getCompetition(project.getCompetition());
        List<CompetitionDocumentResource> configuredProjectDocuments = competition.getCompetitionDocuments();

        List<PartnerOrganisationResource> partnerOrganisations =
                partnerOrganisationRestService.getProjectPartnerOrganisations(project.getId()).getSuccess();

        if (partnerOrganisations.size() == 1) {
            configuredProjectDocuments.removeIf(
                    document -> document.getTitle().equals(COLLABORATION_AGREEMENT_TITLE));
        }

        List<ProjectDocumentResource> projectDocuments = project.getProjectDocuments();

        List<ProjectDocumentStatus> documents = simpleMap(configuredProjectDocuments, configuredDocument ->
                new ProjectDocumentStatus(configuredDocument.getId(), configuredDocument.getTitle(),
                        getProjectDocumentStatus(projectDocuments, configuredDocument.getId())));

        return new AllDocumentsViewModel(project, documents, isProjectManager(loggedInUserId, projectId), competition.isProcurement(), isMonitoringOfficer(loggedInUserId, projectId));
    }

    private DocumentStatus getProjectDocumentStatus(List<ProjectDocumentResource> projectDocuments, Long documentConfigId) {

        return simpleFindAny(projectDocuments, projectDocumentResource -> projectDocumentResource.getCompetitionDocument().getId().equals(documentConfigId))
                .map(ProjectDocumentResource::getStatus)
                .orElse(DocumentStatus.UNSET);
    }

    public DocumentViewModel populateViewDocument(long projectId, long loggedInUserId, long documentConfigId) {

        ProjectResource project = projectRestService.getProjectById(projectId).getSuccess();

        List<CompetitionDocumentResource> configuredProjectDocuments = getCompetition(project.getCompetition()).getCompetitionDocuments();

        CompetitionDocumentResource configuredProjectDocument =
                simpleFindAny(configuredProjectDocuments,
                        projectDocumentResource -> projectDocumentResource.getId().equals(documentConfigId))
                        .get();

        Optional<ProjectDocumentResource> projectDocument = simpleFindAny(project.getProjectDocuments(),
                projectDocumentResource -> projectDocumentResource.getCompetitionDocument().getId().equals(documentConfigId));

        FileDetailsViewModel fileDetails = projectDocument.map(ProjectDocumentResource::getFileEntry)
                .map(FileDetailsViewModel::new)
                .orElse(null);

        boolean monitoringOfficerDocumentDecisionEnabled = isMOJourneyUpdateEnabled ? isMonitoringOfficer(loggedInUserId, projectId) : false;

        return new DocumentViewModel(project.getId(),
                project.getName(),
                project.getApplication(),
                configuredProjectDocument.getId(),
                configuredProjectDocument.getTitle(),
                configuredProjectDocument.getGuidance(),
                fileDetails,
                projectDocument.map(ProjectDocumentResource::getStatus).orElse(DocumentStatus.UNSET),
                projectDocument.map(ProjectDocumentResource::getStatusComments).orElse(""),
                isProjectManager(loggedInUserId, projectId),
                project.getProjectState().isActive(),
                monitoringOfficerDocumentDecisionEnabled);
    }

    private boolean isProjectManager(long loggedInUserId, long projectId) {
        return Optional.ofNullable(projectRestService.getProjectManager(projectId).getOptionalSuccessObject())
                .filter(Optional::isPresent)
                .map(Optional::get)
                .map(ProjectUserResource::getUser)
                .map(userId -> userId.equals(loggedInUserId))
                .orElse(false);
    }

    private boolean isMonitoringOfficer(long loggedInUserId, long projectId) {
        return monitoringOfficerRestService.isMonitoringOfficerOnProject(projectId, loggedInUserId).getSuccess();
    }

    private CompetitionResource getCompetition(long competitionId) {
        return competitionRestService.getCompetitionById(competitionId).getSuccess();
    }
}<commit_msg>IFS-9577: toggle handled in documents-all page<commit_after>package org.innovateuk.ifs.documents.populator;

import org.innovateuk.ifs.competition.resource.CompetitionDocumentResource;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.competition.service.CompetitionRestService;
import org.innovateuk.ifs.documents.viewModel.AllDocumentsViewModel;
import org.innovateuk.ifs.documents.viewModel.DocumentViewModel;
import org.innovateuk.ifs.file.controller.viewmodel.FileDetailsViewModel;
import org.innovateuk.ifs.project.document.resource.DocumentStatus;
import org.innovateuk.ifs.project.document.resource.ProjectDocumentResource;
import org.innovateuk.ifs.project.document.resource.ProjectDocumentStatus;
import org.innovateuk.ifs.project.monitoring.service.MonitoringOfficerRestService;
import org.innovateuk.ifs.project.resource.PartnerOrganisationResource;
import org.innovateuk.ifs.project.resource.ProjectResource;
import org.innovateuk.ifs.project.resource.ProjectUserResource;
import org.innovateuk.ifs.project.service.PartnerOrganisationRestService;
import org.innovateuk.ifs.project.service.ProjectRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

import static org.innovateuk.ifs.competition.resource.CompetitionDocumentResource.COLLABORATION_AGREEMENT_TITLE;
import static org.innovateuk.ifs.util.CollectionFunctions.simpleFindAny;
import static org.innovateuk.ifs.util.CollectionFunctions.simpleMap;

@Component
public class DocumentsPopulator {

    @Autowired
    private PartnerOrganisationRestService partnerOrganisationRestService;

    @Autowired
    private CompetitionRestService competitionRestService;

    @Autowired
    private ProjectRestService projectRestService;

    @Autowired
    private MonitoringOfficerRestService monitoringOfficerRestService;

    @Value("${ifs.monitoringofficer.journey.update.enabled}")
    private boolean isMOJourneyUpdateEnabled;

    public AllDocumentsViewModel populateAllDocuments(long projectId, long loggedInUserId) {

        ProjectResource project = projectRestService.getProjectById(projectId).getSuccess();

        CompetitionResource competition = getCompetition(project.getCompetition());
        List<CompetitionDocumentResource> configuredProjectDocuments = competition.getCompetitionDocuments();

        List<PartnerOrganisationResource> partnerOrganisations =
                partnerOrganisationRestService.getProjectPartnerOrganisations(project.getId()).getSuccess();

        if (partnerOrganisations.size() == 1) {
            configuredProjectDocuments.removeIf(
                    document -> document.getTitle().equals(COLLABORATION_AGREEMENT_TITLE));
        }

        List<ProjectDocumentResource> projectDocuments = project.getProjectDocuments();

        List<ProjectDocumentStatus> documents = simpleMap(configuredProjectDocuments, configuredDocument ->
                new ProjectDocumentStatus(configuredDocument.getId(), configuredDocument.getTitle(),
                        getProjectDocumentStatus(projectDocuments, configuredDocument.getId())));

        return new AllDocumentsViewModel(project, documents, isProjectManager(loggedInUserId, projectId), competition.isProcurement(), monitoringOfficerDocumentDecisionEnabled(loggedInUserId, projectId));
    }

    private DocumentStatus getProjectDocumentStatus(List<ProjectDocumentResource> projectDocuments, Long documentConfigId) {

        return simpleFindAny(projectDocuments, projectDocumentResource -> projectDocumentResource.getCompetitionDocument().getId().equals(documentConfigId))
                .map(ProjectDocumentResource::getStatus)
                .orElse(DocumentStatus.UNSET);
    }

    public DocumentViewModel populateViewDocument(long projectId, long loggedInUserId, long documentConfigId) {

        ProjectResource project = projectRestService.getProjectById(projectId).getSuccess();

        List<CompetitionDocumentResource> configuredProjectDocuments = getCompetition(project.getCompetition()).getCompetitionDocuments();

        CompetitionDocumentResource configuredProjectDocument =
                simpleFindAny(configuredProjectDocuments,
                        projectDocumentResource -> projectDocumentResource.getId().equals(documentConfigId))
                        .get();

        Optional<ProjectDocumentResource> projectDocument = simpleFindAny(project.getProjectDocuments(),
                projectDocumentResource -> projectDocumentResource.getCompetitionDocument().getId().equals(documentConfigId));

        FileDetailsViewModel fileDetails = projectDocument.map(ProjectDocumentResource::getFileEntry)
                .map(FileDetailsViewModel::new)
                .orElse(null);

        return new DocumentViewModel(project.getId(),
                project.getName(),
                project.getApplication(),
                configuredProjectDocument.getId(),
                configuredProjectDocument.getTitle(),
                configuredProjectDocument.getGuidance(),
                fileDetails,
                projectDocument.map(ProjectDocumentResource::getStatus).orElse(DocumentStatus.UNSET),
                projectDocument.map(ProjectDocumentResource::getStatusComments).orElse(""),
                isProjectManager(loggedInUserId, projectId),
                project.getProjectState().isActive(),
                monitoringOfficerDocumentDecisionEnabled(loggedInUserId, projectId));
    }

    private boolean isProjectManager(long loggedInUserId, long projectId) {
        return Optional.ofNullable(projectRestService.getProjectManager(projectId).getOptionalSuccessObject())
                .filter(Optional::isPresent)
                .map(Optional::get)
                .map(ProjectUserResource::getUser)
                .map(userId -> userId.equals(loggedInUserId))
                .orElse(false);
    }

    private boolean isMonitoringOfficer(long loggedInUserId, long projectId) {
        return monitoringOfficerRestService.isMonitoringOfficerOnProject(projectId, loggedInUserId).getSuccess();
    }

    private boolean monitoringOfficerDocumentDecisionEnabled(long loggedInUserId, long projectId) {
        return isMOJourneyUpdateEnabled ? isMonitoringOfficer(loggedInUserId, projectId) : false;
    }


    private CompetitionResource getCompetition(long competitionId) {
        return competitionRestService.getCompetitionById(competitionId).getSuccess();
    }
}
{'fraction_non_alphanumeric': 0.07988391629754085, 'fraction_numerical': 0.00045822514128608525, 'mean_word_length': 4.388888888888889, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1095700', 'n_tokens_mistral': 3281, 'n_tokens_neox': 3118, 'n_words': 435}