Skip to content

Commit

Permalink
[kbss-cvut/record-manager-ui#184] Implement detached import through T…
Browse files Browse the repository at this point in the history
…SV endpoint

I.e. import that is fully implemented by external service. In this case database is modified by separate service.
  • Loading branch information
blcham committed Jul 21, 2024
1 parent f8b2dde commit 9746483
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ public RecordImportResult importRecordsJson(@RequestPart("file") MultipartFile f
}

@PostMapping(value = "/import/excel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public RecordImportResult importRecordsExcel(@RequestPart("file") MultipartFile file,
@RequestParam(name = "phase", required = false) String phase) {
public RecordImportResult importRecordsExcel(
@RequestPart("file") MultipartFile file,
@RequestParam(name = "phase", required = false) String phase) {

List<PatientRecord> records;

Expand Down Expand Up @@ -210,6 +211,44 @@ public RecordImportResult importRecordsExcel(@RequestPart("file") MultipartFile
return importRecords(records, phase);
}

@PostMapping(value = "/import/tsv", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public RecordImportResult importRecordsTsv(
@RequestPart("file") MultipartFile file,
@RequestParam(name = "phase", required = false) String phase) {

List<PatientRecord> records;

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 TSV, excelImportServiceUrl is not configured");

HttpHeaders headers = new HttpHeaders();
headers.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA);

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", file.getResource());

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(
URI.create(excelImportServiceUrl),
requestEntity,
byte[].class
);

LOG.info("Import finished with status {}", responseEntity.getStatusCode());
if (responseEntity.getStatusCode() == HttpStatus.OK) {
byte[] responseBody = responseEntity.getBody();
LOG.debug("Response body length is {}", responseBody.length);
}

return new RecordImportResult();
}

public RecordImportResult importRecords(List<PatientRecord> records, String phase) {
final RecordImportResult importResult;
if (phase != null) {
Expand Down

0 comments on commit 9746483

Please sign in to comment.