Skip to content

Commit

Permalink
Merge pull request #56 from kbss-cvut/feature/record-manager-ui-180-p…
Browse files Browse the repository at this point in the history
…ublish-records

 Add publish rest api
  • Loading branch information
blcham authored Jul 23, 2024
2 parents c9e9b44 + f119fd9 commit 133193b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 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 @@ -173,6 +175,66 @@ public ResponseEntity<String> createRecord(@RequestBody PatientRecord record) {
return new ResponseEntity<>(headers, HttpStatus.CREATED);
}

@PreAuthorize(
"hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)")
@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,
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.");
RecordImportResult result = new RecordImportResult(0);
result.addError("Cannot publish completed records. Publish server not configured.");
return result;
}

// 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.");
ResponseEntity<RecordImportResult> responseEntity = restTemplate.postForEntity(
onPublishRecordsServiceUrl, requestEntity, RecordImportResult.class);

// TODO make records published

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

@PostMapping(value = "/import/json", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public RecordImportResult importRecordsJson(@RequestPart("file") MultipartFile file,
@RequestParam(name = "phase", required = false) String phase) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cz/cvut/kbss/study/util/ConfigParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum ConfigParam {
FORM_GEN_SERVICE_URL("formGenServiceUrl"),

ON_UPDATE_RECORD_SERVICE_URL("onRecordUpdateServiceUrl"),
ON_PUBLISH_RECORDS_SERVICE_URL("onPublishRecordsServiceUrl"),
EXCEL_IMPORT_SERVICE_URL("excelImportServiceUrl"),

APP_CONTEXT("appContext"),
Expand Down

0 comments on commit 133193b

Please sign in to comment.