Skip to content

Commit

Permalink
[Fix partially kbss-cvut/record-manager-ui#180] Add publish rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
kostobog committed Jul 11, 2024
1 parent dcc2cab commit d336501
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 27 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 @@ -12,22 +12,25 @@
import cz.cvut.kbss.study.rest.util.RecordFilterMapper;
import cz.cvut.kbss.study.rest.util.RestUtils;
import cz.cvut.kbss.study.security.SecurityConstants;
import cz.cvut.kbss.study.service.ConfigReader;
import cz.cvut.kbss.study.service.ExcelRecordConverter;
import cz.cvut.kbss.study.service.PatientRecordService;
import cz.cvut.kbss.study.util.ConfigParam;
import cz.cvut.kbss.study.util.Constants;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.EnumerationUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.Page;
import org.springframework.http.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.InputStream;
import java.net.URI;
import java.util.*;
import java.util.stream.Stream;

Expand All @@ -40,11 +43,15 @@ public class PatientRecordController extends BaseController {

private final ApplicationEventPublisher eventPublisher;
private final ExcelRecordConverter excelRecordConverter;
private final RestTemplate restTemplate;
private final ConfigReader configReader;

public PatientRecordController(PatientRecordService recordService, ApplicationEventPublisher eventPublisher, ExcelRecordConverter excelRecordConverter) {
public PatientRecordController(PatientRecordService recordService, ApplicationEventPublisher eventPublisher, ExcelRecordConverter excelRecordConverter, RestTemplate restTemplate, ConfigReader configReader) {
this.recordService = recordService;
this.eventPublisher = eventPublisher;
this.excelRecordConverter = excelRecordConverter;
this.restTemplate = restTemplate;
this.configReader = configReader;
}

@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)")
Expand Down Expand Up @@ -145,6 +152,24 @@ public ResponseEntity<Void> 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){
// final Page<PatientRecord> result = recordService.findAllFull(RecordFilterMapper.constructRecordFilter(params),
// RestUtils.resolvePaging(params));
// List<PatientRecord> records = result.getContent();
List<PatientRecord> records = new ArrayList<>();
String publishServiceUrl = configReader.getConfig(ConfigParam.PUBLISH_SERVICE_URL);
LOG.debug("Publishing records.");
RecordImportResult response = restTemplate.postForEntity(URI.create(publishServiceUrl), records, RecordImportResult.class).getBody();

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

@PostMapping(value = "/import", consumes = MediaType.APPLICATION_JSON_VALUE)
public RecordImportResult importRecords(@RequestBody List<PatientRecord> records,
@RequestParam(name = "phase", required = false) String phase) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cz/cvut/kbss/study/util/ConfigParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public enum ConfigParam {
FORM_GEN_REPOSITORY_URL("formGenRepositoryUrl"),
FORM_GEN_SERVICE_URL("formGenServiceUrl"),

PUBLISH_SERVICE_URL("publishServiceUrl"),

APP_CONTEXT("appContext"),

SMTP_HOST("smtp.host"),
Expand Down

0 comments on commit d336501

Please sign in to comment.