Skip to content

Commit

Permalink
[Fix partially kbss-cvut/record-manager-ui#180] Implement call to itself
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Jul 22, 2024
1 parent 84c127c commit 046b656
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.cvut.kbss.study.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.cvut.kbss.study.dto.PatientRecordDto;
Expand All @@ -25,6 +26,7 @@
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.Page;
import org.springframework.http.*;
Expand Down Expand Up @@ -178,7 +180,9 @@ public ResponseEntity<String> createRecord(@RequestBody PatientRecord record) {
@PostMapping(value = "/publish", produces = {MediaType.APPLICATION_JSON_VALUE})
public RecordImportResult publishRecords(
@RequestParam(name = "institution", required = false) String institutionKey,
@RequestParam(required = false) MultiValueMap<String, String> params){
@RequestParam(required = false) MultiValueMap<String, String> params,
HttpServletRequest request) {

String onPublishRecordsServiceUrl = configReader.getConfig(ConfigParam.ON_PUBLISH_RECORDS_SERVICE_URL);
if(onPublishRecordsServiceUrl == null || onPublishRecordsServiceUrl.isBlank()) {
LOG.info("No publish service url provided, noop.");
Expand All @@ -187,16 +191,48 @@ public RecordImportResult publishRecords(
return result;
}

// TODO fetch records completed records
// final Page<PatientRecord> result = recordService.findAllFull(RecordFilterMapper.constructRecordFilter(params),
// RestUtils.resolvePaging(params));
// List<PatientRecord> records = result.getContent();
List<PatientRecord> records = new ArrayList<>();
// export
final Page<PatientRecord> result = recordService.findAllFull(RecordFilterMapper.constructRecordFilter(params),
RestUtils.resolvePaging(params));
List<PatientRecord> records = result.getContent();

// Convert the records to JSON
String recordsJson;
try {
recordsJson = objectMapper.writeValueAsString(records);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to convert records to JSON", e);
}

// Create a MultiValueMap to hold the file part
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new ByteArrayResource(recordsJson.getBytes()) {
@Override
public String getFilename() {
return "records.json";
}
});

// Create HttpEntity
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
String authHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
if (authHeader != null && !authHeader.isBlank()) {
headers.set(HttpHeaders.AUTHORIZATION, authHeader);
} else {
throw new RuntimeException("Authorization header missing in request");
}
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

// Call the import endpoint
LOG.debug("Publishing records.");
RecordImportResult response = restTemplate.postForEntity(URI.create(onPublishRecordsServiceUrl), records, RecordImportResult.class).getBody();
ResponseEntity<RecordImportResult> responseEntity = restTemplate.postForEntity(
onPublishRecordsServiceUrl, requestEntity, RecordImportResult.class);

// TODO make records published

LOG.debug("Publish server response: ", response);
return response;
LOG.debug("Publish server response: ", responseEntity.getBody());
return responseEntity.getBody();
}

@PostMapping(value = "/import/json", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Expand Down

0 comments on commit 046b656

Please sign in to comment.