From fe506968886a845a39284ea99b3af2cc29732c5b Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Sat, 20 Jul 2024 11:40:05 +0200 Subject: [PATCH] [#184] Changed handler for importing json and excel. Divided into two handlers --- .../study/rest/PatientRecordController.java | 80 ++++++++++--------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java index a33488b7..d2f7d58f 100644 --- a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java +++ b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java @@ -158,15 +158,55 @@ public ResponseEntity createRecord(@RequestBody PatientRecord record) { return new ResponseEntity<>(headers, HttpStatus.CREATED); } - @PostMapping(value = "/import/{format}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public RecordImportResult importRecords(@RequestPart("file") MultipartFile file, - @PathVariable("format") String format, + @PostMapping(value = "/import/json", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public RecordImportResult importRecordsJson(@RequestPart("file") MultipartFile file, @RequestParam(name = "phase", required = false) String phase) { + List records; + if(file.isEmpty()) throw new IllegalArgumentException("Cannot import records, missing input file"); + try { + records = new ObjectMapper().readValue(file.getBytes(), new TypeReference>(){}); + } catch (IOException e) { + throw new RuntimeException("Failed to parse JSON content", e); + } + return importRecords(records, phase); + } + + @PostMapping(value = "/import/excel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public RecordImportResult importRecordsExcel(@RequestPart("file") MultipartFile file, + @RequestParam(name = "phase", required = false) String phase) { + List records; - records = parseRecords(file, format); + + if(file.isEmpty()) + throw new IllegalArgumentException("Cannot import records, missing input file"); + + String excelImportServiceUrl = configReader.getConfig(ConfigParam.EXCEL_IMPORT_SERVICE_URL); + + if (excelImportServiceUrl == null) + throw new IllegalArgumentException("Cannot import XLS, excelImportServiceUrl is not configured"); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA); + + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("file", file.getResource()); + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + + ResponseEntity> responseEntity = restTemplate.exchange( + URI.create(excelImportServiceUrl), + HttpMethod.POST, + requestEntity, + new ParameterizedTypeReference>() {} + ); + records = responseEntity.getBody(); + return importRecords(records, phase); + } + + public RecordImportResult importRecords(List records, String phase) { final RecordImportResult importResult; if (phase != null) { final RecordPhase targetPhase = RecordPhase.fromIriOrName(phase); @@ -178,38 +218,6 @@ public RecordImportResult importRecords(@RequestPart("file") MultipartFile file, return importResult; } - private List parseRecords(MultipartFile file, String format) { - format = format.toLowerCase(); - if (format.equals("json")) { - try { - return new ObjectMapper().readValue(file.getBytes(), new TypeReference>(){}); - } catch (IOException e) { - throw new RuntimeException("Failed to parse JSON content", e); - } - } else if (format.equals("xls") || format.equals("xlsx")) { - String excelImportServiceUrl = configReader.getConfig(ConfigParam.EXCEL_IMPORT_SERVICE_URL); - if (excelImportServiceUrl == null) { - throw new IllegalArgumentException("Cannot import XLS, excelImportServiceUrl is not configured"); - } - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA); - - MultiValueMap body = new LinkedMultiValueMap<>(); - body.add("file", file.getResource()); - - HttpEntity> requestEntity = new HttpEntity<>(body, headers); - - ResponseEntity> responseEntity = restTemplate.exchange( - URI.create(excelImportServiceUrl), - HttpMethod.POST, - requestEntity, - new ParameterizedTypeReference>() {} - ); - return responseEntity.getBody(); - } else { - throw new RuntimeException("Unsupported file format: " + format); - } - } @PutMapping(value = "/{key}", consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.NO_CONTENT)