class CommonYourFinancesViewModelPopulator {

    public CommonYourProjectFinancesViewModel populate(long organisationId, long applicationId, long sectionId, UserResource user) {

        ApplicationResource application = applicationRestService.getApplicationById(applicationId).getSuccess();

        CompetitionResource competition = competitionRestService.getCompetitionById(application.getCompetition()).getSuccess();

        OrganisationResource organisation = organisationRestService.getOrganisationById(organisationId).getSuccess();

        List<Long> completedSectionIds = sectionService.getCompleted(applicationId, organisationId);

        boolean sectionMarkedAsComplete = completedSectionIds.contains(sectionId);

        boolean userCanEdit = !(user.isInternalUser() || user.hasRole(Role.EXTERNAL_FINANCE)) && userRestService.findProcessRole(user.getId(), applicationId).getOptionalSuccessObject()
                .map(role -> role.getOrganisationId() != null && role.getOrganisationId().equals(organisationId))
                .orElse(false);
        boolean open = userCanEdit && application.isOpen() && competition.isOpen();

        return new CommonYourProjectFinancesViewModel(
                getYourFinancesUrl(applicationId, organisationId),
                application.getCompetitionName(),
                application.getName(),
                applicationId,
                sectionId,
                open,
                competition.isH2020(),
                sectionMarkedAsComplete,
                competition.isProcurement(),
                organisation.isInternational());
    }

}