Skip to content

Commit

Permalink
Merge pull request #53 from kbss-cvut/feature/record-manager-ui-36-ex…
Browse files Browse the repository at this point in the history
…port-filtered-records-to-excel

Default export type is selected if exportType query parameter and Accept header do contain a supported export type
  • Loading branch information
blcham authored Jul 10, 2024
2 parents 4ec18b7 + 5c35392 commit 01d40a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import cz.cvut.kbss.study.util.Constants;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.EnumerationUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.Page;
Expand All @@ -27,9 +28,7 @@
import org.springframework.web.util.UriComponentsBuilder;

import java.io.InputStream;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Stream;

@RestController
Expand Down Expand Up @@ -67,15 +66,21 @@ public ResponseEntity<?> exportRecords(
@RequestParam(name = "institution", required = false) String institutionKey,
@RequestParam(required = false) MultiValueMap<String, String> params,
UriComponentsBuilder uriBuilder, HttpServletRequest request, HttpServletResponse response) {

MediaType exportType = Stream.of(
Optional.ofNullable(params).map(p -> p.getFirst(Constants.EXPORT_TYPE_PARAM)),
Optional.ofNullable(request.getHeader(HttpHeaders.ACCEPT))
).filter(Optional::isPresent)
Optional.ofNullable(params).map(p -> p.get(Constants.EXPORT_TYPE_PARAM)),
Optional.of(Collections.list(request.getHeaders(HttpHeaders.ACCEPT)))
)
.map(o -> o.filter(l -> !l.isEmpty()))
.filter(Optional::isPresent)
.map(o -> o.map(l -> l.stream().flatMap(s ->
MediaType.parseMediaTypes(s).stream()
.filter(RestUtils::isSupportedExportType)
).max(Comparator.comparing(MediaType::getQualityValue)).orElse(null)))
.filter(Optional::isPresent)
.map(o -> o.orElse(null))
.findFirst()
.orElse(Optional.empty())
.flatMap(s -> MediaType.parseMediaTypes(s).stream()
.max(Comparator.comparing(MediaType::getQualityValue))
).orElse(MediaType.APPLICATION_JSON)
.orElse(MediaType.APPLICATION_JSON)
.removeQualityValue();

return switch (exportType.toString()){
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/cz/cvut/kbss/study/rest/util/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
Expand All @@ -17,11 +18,18 @@
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class RestUtils {

/**
* Supported export media types
*/
public static List<String> SUPPORTED_EXPORT_MDEIA_TYPES = Arrays.asList(Constants.MEDIA_TYPE_EXCEL, MediaType.APPLICATION_JSON_VALUE);

/**
* Prefix indicating ascending sort order.
*/
Expand Down Expand Up @@ -146,4 +154,8 @@ public static Pageable resolvePaging(MultiValueMap<String, String> params) {
}
return PageRequest.of(page, size);
}

public static boolean isSupportedExportType(MediaType mt){
return SUPPORTED_EXPORT_MDEIA_TYPES.stream().filter(s -> mt.toString().contains(s)).findAny().isPresent();
}
}

0 comments on commit 01d40a0

Please sign in to comment.