<commit_before>package org.innovateuk.ifs.form.controller;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.innovateuk.ifs.commons.rest.RestResult;
import org.innovateuk.ifs.commons.rest.ValidationMessages;
import org.innovateuk.ifs.commons.service.ServiceResult;
import org.innovateuk.ifs.form.repository.FormInputResponseRepository;
import org.innovateuk.ifs.form.resource.FormInputResponseCommand;
import org.innovateuk.ifs.form.resource.FormInputResponseResource;
import org.innovateuk.ifs.form.transactional.FormInputService;
import org.innovateuk.ifs.validator.util.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * ApplicationController exposes Application data and operations through a REST API.
 */
@RestController
@RequestMapping("/forminputresponse")
public class FormInputResponseController {

    @Autowired
    private FormInputResponseRepository formInputResponseRepository;

    @Autowired
    private FormInputService formInputService;

    @Autowired
    private ValidationUtil validationUtil;

    private static final Log LOG = LogFactory.getLog(FormInputResponseController.class);

    @GetMapping("/findResponsesByApplication/{applicationId}")
    public RestResult<List<FormInputResponseResource>> findResponsesByApplication(@PathVariable("applicationId") final Long applicationId){
        return formInputService.findResponsesByApplication(applicationId).toGetResponse();
    }

    @GetMapping("/findResponseByFormInputIdAndApplicationId/{formInputId}/{applicationId}")
    public RestResult<List<FormInputResponseResource>> findByFormInputIdAndApplication(@PathVariable("formInputId") final Long formInputId, @PathVariable("applicationId") final Long applicationId){
        return formInputService.findResponsesByFormInputIdAndApplicationId(formInputId, applicationId).toGetResponse();
    }

    @GetMapping("/findByApplicationIdAndQuestionName/{applicationId}/{questionName}")
    public RestResult<FormInputResponseResource> findByApplicationIdAndQuestionName(@PathVariable long applicationId,
                                                                                 @PathVariable String questionName) {
        return formInputService.findResponseByApplicationIdAndQuestionName(applicationId, questionName).toGetResponse();
    }

    @GetMapping("/findByApplicationIdAndQuestionId/{applicationId}/{questionId}")
    public RestResult<List<FormInputResponseResource>> findByApplicationIdAndQuestionId(@PathVariable long applicationId,
                                                                                  @PathVariable long questionId) {
        return formInputService.findResponseByApplicationIdAndQuestionId(applicationId, questionId).toGetResponse();
    }

    @PostMapping("/saveQuestionResponse")
    public RestResult<ValidationMessages> saveQuestionResponse(@RequestBody JsonNode jsonObj) {

        Long userId = jsonObj.get("userId").asLong();
        Long applicationId = jsonObj.get("applicationId").asLong();
        Long formInputId = jsonObj.get("formInputId").asLong();
        JsonNode ignoreEmptyNode = jsonObj.get("ignoreEmpty");
        Boolean ignoreEmpty = ignoreEmptyNode != null && ignoreEmptyNode.asBoolean();
        String value = HtmlUtils.htmlUnescape(jsonObj.get("value").asText(""));

        ServiceResult<ValidationMessages> result = formInputService.saveQuestionResponse(new FormInputResponseCommand(formInputId, applicationId,  userId, value)).andOnSuccessReturn(response -> {

            BindingResult bindingResult = validationUtil.validateResponse(response, ignoreEmpty);
            if (bindingResult.hasErrors()) {
                LOG.debug("Got validation errors: ");
                bindingResult.getAllErrors().stream().forEach(e -> LOG.debug("Validation: " + e.getDefaultMessage()));
            }

            formInputResponseRepository.save(response);
            LOG.debug("Single question saved!");

            return new ValidationMessages(bindingResult);
        });

        return result.toPostWithBodyResponse();
    }
}<commit_msg>IFS-1003 Reformatting and FormInputResponseRespository within the transactional<commit_after>package org.innovateuk.ifs.form.controller;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.innovateuk.ifs.commons.rest.RestResult;
import org.innovateuk.ifs.commons.rest.ValidationMessages;
import org.innovateuk.ifs.commons.service.ServiceResult;
import org.innovateuk.ifs.form.repository.FormInputResponseRepository;
import org.innovateuk.ifs.form.resource.FormInputResponseCommand;
import org.innovateuk.ifs.form.resource.FormInputResponseResource;
import org.innovateuk.ifs.form.transactional.FormInputService;
import org.innovateuk.ifs.validator.util.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * ApplicationController exposes Application data and operations through a REST API.
 */
@RestController
@RequestMapping("/forminputresponse")
public class FormInputResponseController {

    @Autowired
    private FormInputResponseRepository formInputResponseRepository;

    @Autowired
    private FormInputService formInputService;

    @Autowired
    private ValidationUtil validationUtil;

    private static final Log LOG = LogFactory.getLog(FormInputResponseController.class);

    @GetMapping("/findResponsesByApplication/{applicationId}")
    public RestResult<List<FormInputResponseResource>> findResponsesByApplication(@PathVariable("applicationId") final Long applicationId) {
        return formInputService.findResponsesByApplication(applicationId).toGetResponse();
    }

    @GetMapping("/findResponseByFormInputIdAndApplicationId/{formInputId}/{applicationId}")
    public RestResult<List<FormInputResponseResource>> findByFormInputIdAndApplication(@PathVariable("formInputId") final Long formInputId, @PathVariable("applicationId") final Long applicationId) {
        return formInputService.findResponsesByFormInputIdAndApplicationId(formInputId, applicationId).toGetResponse();
    }

    @GetMapping("/findByApplicationIdAndQuestionName/{applicationId}/{questionName}")
    public RestResult<FormInputResponseResource> findByApplicationIdAndQuestionName(@PathVariable long applicationId,
                                                                                    @PathVariable String questionName) {
        return formInputService.findResponseByApplicationIdAndQuestionName(applicationId, questionName).toGetResponse();
    }

    @GetMapping("/findByApplicationIdAndQuestionId/{applicationId}/{questionId}")
    public RestResult<List<FormInputResponseResource>> findByApplicationIdAndQuestionId(@PathVariable long applicationId,
                                                                                        @PathVariable long questionId) {
        return formInputService.findResponseByApplicationIdAndQuestionId(applicationId, questionId).toGetResponse();
    }

    @PostMapping("/saveQuestionResponse")
    public RestResult<ValidationMessages> saveQuestionResponse(@RequestBody JsonNode jsonObj) {

        Long userId = jsonObj.get("userId").asLong();
        Long applicationId = jsonObj.get("applicationId").asLong();
        Long formInputId = jsonObj.get("formInputId").asLong();
        JsonNode ignoreEmptyNode = jsonObj.get("ignoreEmpty");
        Boolean ignoreEmpty = ignoreEmptyNode != null && ignoreEmptyNode.asBoolean();
        String value = HtmlUtils.htmlUnescape(jsonObj.get("value").asText(""));

        ServiceResult<ValidationMessages> result = formInputService.saveQuestionResponse(new FormInputResponseCommand(formInputId, applicationId, userId, value))
                .andOnSuccessReturn(response -> {
                    BindingResult bindingResult = validationUtil.validateResponse(response, ignoreEmpty);
                    if (bindingResult.hasErrors()) {
                        LOG.debug("Got validation errors: ");
                        bindingResult.getAllErrors().stream().forEach(e -> LOG.debug("Validation: " + e.getDefaultMessage()));
                    }

                    return new ValidationMessages(bindingResult);
                });

        return result.toPostWithBodyResponse();
    }
}
{'fraction_non_alphanumeric': 0.09155413138017852, 'fraction_numerical': 0.00045777065690089265, 'mean_word_length': 4.223550508069336, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6749678', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2084, 'n_words': 339}