<commit_before>package uk.ac.ebi.subs.api.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mashape.unirest.http.HttpResponse;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.core.event.AfterCreateEvent;
import org.springframework.data.rest.core.event.BeforeCreateEvent;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.method.P;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.subs.api.processors.LinkHelper;
import uk.ac.ebi.subs.api.processors.StoredSubmittableAssembler;
import uk.ac.ebi.subs.api.processors.StoredSubmittableResourceProcessor;
import uk.ac.ebi.subs.api.services.OperationControlService;
import uk.ac.ebi.subs.repository.model.Checklist;
import uk.ac.ebi.subs.repository.model.DataType;
import uk.ac.ebi.subs.repository.model.StoredSubmittable;
import uk.ac.ebi.subs.repository.model.Submission;
import uk.ac.ebi.subs.repository.model.sheets.Spreadsheet;
import uk.ac.ebi.subs.repository.repos.DataTypeRepository;
import uk.ac.ebi.subs.repository.repos.SubmissionRepository;
import uk.ac.ebi.subs.repository.repos.submittables.SubmittableRepository;
import uk.ac.ebi.subs.validator.data.ValidationResult;
import uk.ac.ebi.subs.validator.data.structures.SingleValidationResultStatus;
import uk.ac.ebi.subs.validator.repository.ValidationResultRepository;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

@RestController
@RequiredArgsConstructor
public class SubmissionContentsController {

    @NonNull
    private DataTypeRepository dataTypeRepository;
    @NonNull
    private ValidationResultRepository validationResultRepository;
    @NonNull
    private RepositoryEntityLinks repositoryEntityLinks;
    @NonNull
    private ApplicationEventPublisher publisher;
    @NonNull
    private Map<Class<? extends StoredSubmittable>, SubmittableRepository<? extends StoredSubmittable>> submittableRepositoryMap;

    @NonNull
    private StoredSubmittableAssembler storedSubmittableAssembler;

    @NonNull
    private StoredSubmittableResourceProcessor<StoredSubmittable> storedSubmittableResourceProcessor;

    @NonNull
    private OperationControlService operationControlService;

    @NonNull
    private SubmissionRepository submissionRepository;

    @NonNull
    private ObjectMapper objectMapper;

    @NonNull
    private LinkHelper linkHelper;


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/submissions/{submissionId}/contents/{dataTypeId}", method = RequestMethod.GET)
    public ResponseEntity<PagedResources<Resource<StoredSubmittable>>> getSubmissionContentsForDataType(
            @PathVariable @P("submissionId") String submissionId,
            @PathVariable @P("dataTypeId") String dataTypeId,
            Pageable pageable) {

        Submission submission = submissionRepository.findOne(submissionId);
        DataType dataType = dataTypeRepository.findOne(dataTypeId);

        if (dataType == null || submission == null) {
            throw new ResourceNotFoundException();
        }

        Class submittableClass = submittableClassForDataType(dataType);
        SubmittableRepository repository = submittableRepositoryMap.get(submittableClass);

        Page<StoredSubmittable> page = repository.findBySubmissionIdAndDataTypeId(submissionId, dataTypeId, pageable);

        List<Resource<StoredSubmittable>> resourceList = page.getContent().stream()
                .map(item -> storedSubmittableAssembler.toResource(item))
                .map(resource -> storedSubmittableResourceProcessor.process(resource))
                .collect(Collectors.toList());


        PagedResources<Resource<StoredSubmittable>> pagedResources = new PagedResources<>(
                resourceList,
                new PagedResources.PageMetadata(page.getSize(), page.getNumber(), page.getTotalElements(), page.getTotalPages())
        );

        addContentListPageLinks(pageable, submission, dataType, submittableClass, pagedResources);


        return new ResponseEntity(
                pagedResources,
                new LinkedMultiValueMap<>(),
                HttpStatus.OK
        );
    }

    private void addContentListPageLinks(Pageable pageable, Submission submission, DataType dataType, Class submittableClass, PagedResources<Resource<StoredSubmittable>> pagedResources) {
        Map<String, String> params = new HashMap<>();
        params.put("submissionId", submission.getId());
        params.put("dataTypeId", dataType.getId());


        Link selfLink = linkTo(methodOn(this.getClass()).getSubmissionContentsForDataType(submission.getId(), dataType.getId(), pageable))
                .withSelfRel();

        Link summaryLink = linkTo(methodOn(this.getClass()).summariseSubmissionDataTypeErrorStatus(submission.getId(), dataType.getId()))
                .withRel("validationSummaryCounts");

        Link checklistLink = repositoryEntityLinks
                .linkToSearchResource(Checklist.class, "by-data-type-id")
                .expand(params)
                .withRel("checklists");

        Link spreadsheetLink = repositoryEntityLinks
                .linkToSearchResource(Spreadsheet.class, "by-submission-and-data-type")
                .expand(params)
                .withRel("spreadsheets");

        Link dataTypeLink = repositoryEntityLinks.linkToSingleResource(dataType);

        Link withErrorsLink = repositoryEntityLinks.linkToSearchResource(submittableClass, "by-submission-and-data-type-with-errors")
                .expand(params)
                .withRel("documents-with-errors");
<commit_msg>simplify return type for GET method<commit_after>package uk.ac.ebi.subs.api.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mashape.unirest.http.HttpResponse;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.core.event.AfterCreateEvent;
import org.springframework.data.rest.core.event.BeforeCreateEvent;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.method.P;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.subs.api.processors.LinkHelper;
import uk.ac.ebi.subs.api.processors.StoredSubmittableAssembler;
import uk.ac.ebi.subs.api.processors.StoredSubmittableResourceProcessor;
import uk.ac.ebi.subs.api.services.OperationControlService;
import uk.ac.ebi.subs.repository.model.Checklist;
import uk.ac.ebi.subs.repository.model.DataType;
import uk.ac.ebi.subs.repository.model.StoredSubmittable;
import uk.ac.ebi.subs.repository.model.Submission;
import uk.ac.ebi.subs.repository.model.sheets.Spreadsheet;
import uk.ac.ebi.subs.repository.repos.DataTypeRepository;
import uk.ac.ebi.subs.repository.repos.SubmissionRepository;
import uk.ac.ebi.subs.repository.repos.submittables.SubmittableRepository;
import uk.ac.ebi.subs.validator.data.ValidationResult;
import uk.ac.ebi.subs.validator.data.structures.SingleValidationResultStatus;
import uk.ac.ebi.subs.validator.repository.ValidationResultRepository;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

@RestController
@RequiredArgsConstructor
public class SubmissionContentsController {

    @NonNull
    private DataTypeRepository dataTypeRepository;
    @NonNull
    private ValidationResultRepository validationResultRepository;
    @NonNull
    private RepositoryEntityLinks repositoryEntityLinks;
    @NonNull
    private ApplicationEventPublisher publisher;
    @NonNull
    private Map<Class<? extends StoredSubmittable>, SubmittableRepository<? extends StoredSubmittable>> submittableRepositoryMap;

    @NonNull
    private StoredSubmittableAssembler storedSubmittableAssembler;

    @NonNull
    private StoredSubmittableResourceProcessor<StoredSubmittable> storedSubmittableResourceProcessor;

    @NonNull
    private OperationControlService operationControlService;

    @NonNull
    private SubmissionRepository submissionRepository;

    @NonNull
    private ObjectMapper objectMapper;

    @NonNull
    private LinkHelper linkHelper;


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/submissions/{submissionId}/contents/{dataTypeId}", method = RequestMethod.GET)
    public PagedResources<Resource<StoredSubmittable>> getSubmissionContentsForDataType(
            @PathVariable @P("submissionId") String submissionId,
            @PathVariable @P("dataTypeId") String dataTypeId,
            Pageable pageable) {

        Submission submission = submissionRepository.findOne(submissionId);
        DataType dataType = dataTypeRepository.findOne(dataTypeId);

        if (dataType == null || submission == null) {
            throw new ResourceNotFoundException();
        }

        Class submittableClass = submittableClassForDataType(dataType);
        SubmittableRepository repository = submittableRepositoryMap.get(submittableClass);

        Page<StoredSubmittable> page = repository.findBySubmissionIdAndDataTypeId(submissionId, dataTypeId, pageable);

        List<Resource<StoredSubmittable>> resourceList = page.getContent().stream()
                .map(item -> storedSubmittableAssembler.toResource(item))
                .map(resource -> storedSubmittableResourceProcessor.process(resource))
                .collect(Collectors.toList());


        PagedResources<Resource<StoredSubmittable>> pagedResources = new PagedResources<>(
                resourceList,
                new PagedResources.PageMetadata(page.getSize(), page.getNumber(), page.getTotalElements(), page.getTotalPages())
        );

        addContentListPageLinks(pageable, submission, dataType, submittableClass, pagedResources);


        return pagedResources;
    }

    private void addContentListPageLinks(Pageable pageable, Submission submission, DataType dataType, Class submittableClass, PagedResources<Resource<StoredSubmittable>> pagedResources) {
        Map<String, String> params = new HashMap<>();
        params.put("submissionId", submission.getId());
        params.put("dataTypeId", dataType.getId());


        Link selfLink = linkTo(methodOn(this.getClass()).getSubmissionContentsForDataType(submission.getId(), dataType.getId(), pageable))
                .withSelfRel();

        Link summaryLink = linkTo(methodOn(this.getClass()).summariseSubmissionDataTypeErrorStatus(submission.getId(), dataType.getId()))
                .withRel("validationSummaryCounts");

        Link checklistLink = repositoryEntityLinks
                .linkToSearchResource(Checklist.class, "by-data-type-id")
                .expand(params)
                .withRel("checklists");

        Link spreadsheetLink = repositoryEntityLinks
                .linkToSearchResource(Spreadsheet.class, "by-submission-and-data-type")
                .expand(params)
                .withRel("spreadsheets");

        Link dataTypeLink = repositoryEntityLinks.linkToSingleResource(dataType);

        Link withErrorsLink = repositoryEntityLinks.linkToSearchResource(submittableClass, "by-submission-and-data-type-with-errors")
                .expand(params)
                .withRel("documents-with-errors");

{'fraction_non_alphanumeric': 0.09347764970480742, 'fraction_numerical': 0.000281135788585887, 'mean_word_length': 4.586572438162544, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1376397', 'n_tokens_mistral': 3911, 'n_tokens_neox': 3842, 'n_words': 614}