Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add publish rest api #56

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading