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));
}
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());
}
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());
}
Aggregations