Search in sources :

Example 1 with ContributionResponseData

use of org.ehrbase.response.openehr.ContributionResponseData in project ehrbase by ehrbase.

the class OpenehrContributionController method buildContributionResponseData.

private <T extends ContributionResponseData> Optional<InternalResponse<T>> buildContributionResponseData(UUID contributionId, UUID ehrId, String accept, URI uri, List<String> headerList, Supplier<T> factory) {
    // create either CompositionResponseData or null (means no body, only headers incl. link to resource), via lambda request
    T minimalOrRepresentation = factory.get();
    // do minimal scope steps
    // create and supplement headers with data depending on which headers are requested
    HttpHeaders respHeaders = new HttpHeaders();
    for (String header : headerList) {
        switch(header) {
            case LOCATION:
                respHeaders.setLocation(uri);
                break;
            case ETAG:
                respHeaders.setETag("\"" + contributionId + "\"");
                break;
            case LAST_MODIFIED:
                // TODO should be VERSION.commit_audit.time_committed.value which is not implemented yet - mock for now
                respHeaders.setLastModified(123124442);
                break;
            default:
        }
    }
    // if response data objects was created as "representation" do all task from wider scope, too
    if (minimalOrRepresentation != null) {
        // when this "if" is true the following casting can be executed and data manipulated by reference (handled by temporary variable)
        ContributionResponseData objByReference = minimalOrRepresentation;
        // retrieve contribution
        Optional<ContributionDto> contribution = contributionService.getContribution(ehrId, contributionId);
        // set all response field according to retrieved contribution
        objByReference.setUid(new HierObjectId(contributionId.toString()));
        List<ObjectRef<ObjectVersionId>> refs = new LinkedList<>();
        contribution.get().getObjectReferences().forEach((id, type) -> refs.add(new ObjectRef<>(new ObjectVersionId(id), "local", type)));
        objByReference.setVersions(refs);
        objByReference.setAudit(contribution.get().getAuditDetails());
        CompositionFormat format = extractCompositionFormat(accept);
        // finally set last header
        if (format.equals(CompositionFormat.XML)) {
            respHeaders.setContentType(MediaType.APPLICATION_XML);
        } else if (format.equals(CompositionFormat.JSON) || format.equals(CompositionFormat.FLAT) || format.equals(CompositionFormat.ECISFLAT) || format.equals(CompositionFormat.RAW)) {
            respHeaders.setContentType(MediaType.APPLICATION_JSON);
        } else {
            throw new NotAcceptableException("Wrong Accept header in request");
        }
    }
    return Optional.of(new InternalResponse<>(minimalOrRepresentation, respHeaders));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) CompositionFormat(org.ehrbase.response.ehrscape.CompositionFormat) ContributionResponseData(org.ehrbase.response.openehr.ContributionResponseData) ContributionDto(org.ehrbase.response.ehrscape.ContributionDto) ObjectVersionId(com.nedap.archie.rm.support.identification.ObjectVersionId) LinkedList(java.util.LinkedList) NotAcceptableException(org.ehrbase.api.exception.NotAcceptableException) ObjectRef(com.nedap.archie.rm.support.identification.ObjectRef) HierObjectId(com.nedap.archie.rm.support.identification.HierObjectId)

Example 2 with ContributionResponseData

use of org.ehrbase.response.openehr.ContributionResponseData in project ehrbase by ehrbase.

the class OpenehrContributionController method createContribution.

@PostMapping(value = "/{ehr_id}/contribution", consumes = { "application/xml", "application/json" })
// checkAbacPre /-Post attributes (type, subject, payload, content type)
@PreAuthorize("checkAbacPre(@openehrContributionController.CONTRIBUTION, " + "@ehrService.getSubjectExtRef(#ehrIdString), #contribution, #contentType)")
// overwrites default 200, fixes the wrong listing of 200 in swagger-ui (EHR-56)
@ResponseStatus(value = HttpStatus.CREATED)
@Override
public ResponseEntity createContribution(@RequestHeader(value = "openEHR-VERSION", required = false) String openehrVersion, @RequestHeader(value = "openEHR-AUDIT_DETAILS", required = false) String openehrAuditDetails, @RequestHeader(value = CONTENT_TYPE) String contentType, @RequestHeader(value = HttpHeaders.ACCEPT, required = false) String accept, @RequestHeader(value = PREFER, required = false) String prefer, @PathVariable(value = "ehr_id") String ehrIdString, @RequestBody String contribution) {
    UUID ehrId = getEhrUuid(ehrIdString);
    UUID contributionId = contributionService.commitContribution(ehrId, contribution, extractCompositionFormat(contentType));
    URI uri = URI.create(this.encodePath(getBaseEnvLinkURL() + "/rest/openehr/v1/ehr/" + ehrId.toString() + "/contribution/" + contributionId.toString()));
    // whatever is required by REST spec - CONTENT_TYPE only needed for 201, so handled separately
    List<String> headerList = Arrays.asList(LOCATION, ETAG);
    // variable to overload with more specific object if requested
    Optional<InternalResponse<ContributionResponseData>> respData;
    if (Optional.ofNullable(prefer).map(i -> i.equals(RETURN_REPRESENTATION)).orElse(false)) {
        // null safe way to test prefer header
        respData = buildContributionResponseData(contributionId, ehrId, accept, uri, headerList, () -> new ContributionResponseData(null, null, null));
    } else {
        // "minimal" is default fallback
        respData = buildContributionResponseData(contributionId, ehrId, accept, uri, headerList, () -> null);
    }
    // returns 201 with body + headers, 204 only with headers or 500 error depending on what processing above yields
    return respData.map(i -> Optional.ofNullable(i.getResponseData()).map(j -> ResponseEntity.created(uri).headers(i.getHeaders()).body(j)).orElse(ResponseEntity.noContent().headers(i.getHeaders()).build())).orElse(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) ContributionService(org.ehrbase.api.service.ContributionService) ContributionResponseData(org.ehrbase.response.openehr.ContributionResponseData) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Supplier(java.util.function.Supplier) HierObjectId(com.nedap.archie.rm.support.identification.HierObjectId) RequestBody(org.springframework.web.bind.annotation.RequestBody) Operation(io.swagger.v3.oas.annotations.Operation) GetMapping(org.springframework.web.bind.annotation.GetMapping) URI(java.net.URI) ObjectVersionId(com.nedap.archie.rm.support.identification.ObjectVersionId) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) LinkedList(java.util.LinkedList) ObjectRef(com.nedap.archie.rm.support.identification.ObjectRef) PostMapping(org.springframework.web.bind.annotation.PostMapping) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) NotAcceptableException(org.ehrbase.api.exception.NotAcceptableException) UUID(java.util.UUID) RestController(org.springframework.web.bind.annotation.RestController) BaseController(org.ehrbase.rest.BaseController) Objects(java.util.Objects) HttpStatus(org.springframework.http.HttpStatus) ContributionDto(org.ehrbase.response.ehrscape.ContributionDto) List(java.util.List) Tag(io.swagger.v3.oas.annotations.tags.Tag) InternalResponse(org.ehrbase.rest.util.InternalResponse) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) RequestHeader(org.springframework.web.bind.annotation.RequestHeader) CompositionFormat(org.ehrbase.response.ehrscape.CompositionFormat) ContributionApiSpecification(org.ehrbase.rest.openehr.specification.ContributionApiSpecification) ContributionResponseData(org.ehrbase.response.openehr.ContributionResponseData) InternalResponse(org.ehrbase.rest.util.InternalResponse) UUID(java.util.UUID) URI(java.net.URI) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with ContributionResponseData

use of org.ehrbase.response.openehr.ContributionResponseData in project ehrbase by ehrbase.

the class OpenehrContributionController method getContribution.

@GetMapping(value = "/{ehr_id}/contribution/{contribution_uid}")
@Override
public ResponseEntity getContribution(@RequestHeader(value = "openEHR-VERSION", required = false) String openehrVersion, @RequestHeader(value = "openEHR-AUDIT_DETAILS", required = false) String openehrAuditDetails, @RequestHeader(value = HttpHeaders.ACCEPT, required = false) String accept, @PathVariable(value = "ehr_id") String ehrIdString, @PathVariable(value = "contribution_uid") String contributionUidString) {
    UUID ehrId = getEhrUuid(ehrIdString);
    UUID contributionUid = getContributionVersionedObjectUidString(contributionUidString);
    URI uri = URI.create(this.encodePath(getBaseEnvLinkURL() + "/rest/openehr/v1/ehr/" + ehrId.toString() + "/contribution/" + contributionUid.toString()));
    // whatever is required by REST spec - CONTENT_TYPE handled separately
    List<String> headerList = Arrays.asList(LOCATION, ETAG, LAST_MODIFIED);
    // variable to overload with more specific object if requested
    Optional<InternalResponse<ContributionResponseData>> respData;
    // building full / representation response
    respData = buildContributionResponseData(contributionUid, ehrId, accept, uri, headerList, () -> new ContributionResponseData(null, null, null));
    // returns 200 with body + headers or 500 in case of unexpected error
    return respData.map(i -> Optional.ofNullable(i.getResponseData()).map(j -> ResponseEntity.ok().headers(i.getHeaders()).body(j)).orElse(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build())).orElse(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) ContributionService(org.ehrbase.api.service.ContributionService) ContributionResponseData(org.ehrbase.response.openehr.ContributionResponseData) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Supplier(java.util.function.Supplier) HierObjectId(com.nedap.archie.rm.support.identification.HierObjectId) RequestBody(org.springframework.web.bind.annotation.RequestBody) Operation(io.swagger.v3.oas.annotations.Operation) GetMapping(org.springframework.web.bind.annotation.GetMapping) URI(java.net.URI) ObjectVersionId(com.nedap.archie.rm.support.identification.ObjectVersionId) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) LinkedList(java.util.LinkedList) ObjectRef(com.nedap.archie.rm.support.identification.ObjectRef) PostMapping(org.springframework.web.bind.annotation.PostMapping) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) NotAcceptableException(org.ehrbase.api.exception.NotAcceptableException) UUID(java.util.UUID) RestController(org.springframework.web.bind.annotation.RestController) BaseController(org.ehrbase.rest.BaseController) Objects(java.util.Objects) HttpStatus(org.springframework.http.HttpStatus) ContributionDto(org.ehrbase.response.ehrscape.ContributionDto) List(java.util.List) Tag(io.swagger.v3.oas.annotations.tags.Tag) InternalResponse(org.ehrbase.rest.util.InternalResponse) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) RequestHeader(org.springframework.web.bind.annotation.RequestHeader) CompositionFormat(org.ehrbase.response.ehrscape.CompositionFormat) ContributionApiSpecification(org.ehrbase.rest.openehr.specification.ContributionApiSpecification) ContributionResponseData(org.ehrbase.response.openehr.ContributionResponseData) InternalResponse(org.ehrbase.rest.util.InternalResponse) UUID(java.util.UUID) URI(java.net.URI) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

HierObjectId (com.nedap.archie.rm.support.identification.HierObjectId)3 ObjectRef (com.nedap.archie.rm.support.identification.ObjectRef)3 ObjectVersionId (com.nedap.archie.rm.support.identification.ObjectVersionId)3 LinkedList (java.util.LinkedList)3 NotAcceptableException (org.ehrbase.api.exception.NotAcceptableException)3 CompositionFormat (org.ehrbase.response.ehrscape.CompositionFormat)3 ContributionDto (org.ehrbase.response.ehrscape.ContributionDto)3 ContributionResponseData (org.ehrbase.response.openehr.ContributionResponseData)3 HttpHeaders (org.springframework.http.HttpHeaders)3 Operation (io.swagger.v3.oas.annotations.Operation)2 Tag (io.swagger.v3.oas.annotations.tags.Tag)2 URI (java.net.URI)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Supplier (java.util.function.Supplier)2 ContributionService (org.ehrbase.api.service.ContributionService)2 BaseController (org.ehrbase.rest.BaseController)2