class ProjectSpendProfileController {

    @PreAuthorize("hasPermission(#projectId, 'org.innovateuk.ifs.project.resource.ProjectCompositeId', 'ACCESS_SPEND_PROFILE_SECTION')")
    @PostMapping("/edit")
    public String saveSpendProfile(Model model,
                                   @ModelAttribute(FORM_ATTR_NAME) SpendProfileForm form,
                                   @SuppressWarnings("unused") BindingResult bindingResult,
                                   ValidationHandler validationHandler,
                                   @P("projectId")@PathVariable("projectId") final Long projectId,
                                   @PathVariable("organisationId") final Long organisationId,
                                   UserResource loggedInUser) {

        Supplier<String> failureView = () -> {

            SpendProfileTableResource updatedTable = form.getTable();
            SpendProfileTableResource originalTableWithUpdatedCosts = spendProfileService.getSpendProfileTable(projectId, organisationId);
            originalTableWithUpdatedCosts.setMonthlyCostsPerCategoryMap(updatedTable.getMonthlyCostsPerCategoryMap());

            ProjectResource project = projectService.getById(projectId);

            return doEditSpendProfile(model, form, organisationId, loggedInUser, project, originalTableWithUpdatedCosts);
        };


        spendProfileCostValidator.validate(form.getTable(), bindingResult);

        return validationHandler.failNowOrSucceedWith(failureView, () -> {
            SpendProfileTableResource spendProfileTableResource = spendProfileService.getSpendProfileTable(projectId, organisationId);
            spendProfileTableResource.setMonthlyCostsPerCategoryMap(form.getTable().getMonthlyCostsPerCategoryMap()); // update existing resource with user entered fields
            ServiceResult<Void> result = spendProfileService.saveSpendProfile(projectId, organisationId, spendProfileTableResource);
            return validationHandler.addAnyErrors(result).failNowOrSucceedWith(failureView,
                    () -> saveSpendProfileSuccessView(projectId, organisationId, loggedInUser.getId()));
        });
    }

}