use of org.ehrbase.response.ehrscape.ContributionDto 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.ehrscape.ContributionDto in project ehrbase by ehrbase.
the class OpenehrVersionedCompositionController method getOriginalVersionResponseDataResponseEntity.
private ResponseEntity<OriginalVersionResponseData<Composition>> getOriginalVersionResponseDataResponseEntity(String accept, UUID ehrId, UUID versionedObjectId, int version) {
Optional<OriginalVersion<Composition>> compositionOriginalVersion = compositionService.getOriginalVersionComposition(versionedObjectId, version);
UUID contributionId = compositionOriginalVersion.map(i -> UUID.fromString(i.getContribution().getId().getValue())).orElseThrow(() -> new InvalidApiParameterException("Couldn't retrieve Composition with given parameters"));
Optional<ContributionDto> optionalContributionDto = contributionService.getContribution(ehrId, contributionId);
// shouldn't happen
ContributionDto contributionDto = optionalContributionDto.orElseThrow(() -> new InternalServerException("Couldn't fetch contribution for existing Composition"));
OriginalVersionResponseData<Composition> originalVersionResponseData = new OriginalVersionResponseData<>(compositionOriginalVersion.orElseThrow(() -> new InternalServerException("Composition exists but can't be retrieved as Original Version.")), contributionDto);
HttpHeaders respHeaders = new HttpHeaders();
respHeaders.setContentType(resolveContentType(accept));
return ResponseEntity.ok().headers(respHeaders).body(originalVersionResponseData);
}
use of org.ehrbase.response.ehrscape.ContributionDto in project ehrbase by ehrbase.
the class OpenehrVersionedEhrStatusController method retrieveVersionOfEhrStatusByVersionUid.
@GetMapping(path = "/version/{version_uid}")
// checkAbacPre /-Post attributes (type, subject, payload, content type)
@PreAuthorize("checkAbacPre(@openehrVersionedEhrStatusController.EHR_STATUS, " + "@ehrService.getSubjectExtRef(#ehrIdString), null, null)")
@Override
public ResponseEntity<OriginalVersionResponseData<EhrStatus>> retrieveVersionOfEhrStatusByVersionUid(@RequestHeader(value = HttpHeaders.ACCEPT, required = false) String accept, @PathVariable(value = "ehr_id") String ehrIdString, @PathVariable(value = "version_uid") String versionUid) {
UUID ehrId = getEhrUuid(ehrIdString);
// check if EHR is valid
if (!ehrService.hasEhr(ehrId)) {
throw new ObjectNotFoundException("ehr", "No EHR with this ID can be found.");
}
// parse given version uid
UUID versionedObjectId;
int version;
try {
versionedObjectId = UUID.fromString(versionUid.split("::")[0]);
version = Integer.parseInt(versionUid.split("::")[2]);
} catch (Exception e) {
throw new InvalidApiParameterException("VERSION UID parameter has wrong format: " + e.getMessage());
}
if (version < 1)
throw new InvalidApiParameterException("Version can't be negative.");
if (!ehrService.hasStatus(versionedObjectId)) {
throw new ObjectNotFoundException("ehr_status", "No EHR_STATUS with given ID can be found.");
}
Optional<OriginalVersion<EhrStatus>> ehrStatusOriginalVersion = ehrService.getEhrStatusAtVersion(ehrId, versionedObjectId, version);
UUID contributionId = ehrStatusOriginalVersion.map(i -> UUID.fromString(i.getContribution().getId().getValue())).orElseThrow(() -> new InvalidApiParameterException("Couldn't retrieve EhrStatus with given parameters"));
Optional<ContributionDto> optionalContributionDto = contributionService.getContribution(ehrId, contributionId);
// shouldn't happen
ContributionDto contributionDto = optionalContributionDto.orElseThrow(() -> new InternalServerException("Couldn't fetch contribution for existing EhrStatus"));
OriginalVersionResponseData<EhrStatus> originalVersionResponseData = new OriginalVersionResponseData<>(ehrStatusOriginalVersion.get(), contributionDto);
HttpHeaders respHeaders = new HttpHeaders();
respHeaders.setContentType(resolveContentType(accept));
return ResponseEntity.ok().headers(respHeaders).body(originalVersionResponseData);
}
use of org.ehrbase.response.ehrscape.ContributionDto in project ehrbase by ehrbase.
the class OpenehrVersionedEhrStatusController method retrieveVersionOfEhrStatusByTime.
@GetMapping(path = "/version")
// checkAbacPre /-Post attributes (type, subject, payload, content type)
@PreAuthorize("checkAbacPre(@openehrVersionedEhrStatusController.EHR_STATUS, " + "@ehrService.getSubjectExtRef(#ehrIdString), null, null)")
@Override
public ResponseEntity<OriginalVersionResponseData<EhrStatus>> retrieveVersionOfEhrStatusByTime(@RequestHeader(value = HttpHeaders.ACCEPT, required = false) String accept, @PathVariable(value = "ehr_id") String ehrIdString, @RequestParam(value = "version_at_time", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime versionAtTime) {
UUID ehrId = getEhrUuid(ehrIdString);
// check if EHR is valid
if (!ehrService.hasEhr(ehrId)) {
throw new ObjectNotFoundException("ehr", "No EHR with this ID can be found");
}
UUID versionedObjectId = ehrService.getEhrStatusVersionedObjectUidByEhr(ehrId);
int version;
if (versionAtTime != null) {
version = ehrService.getEhrStatusVersionByTimestamp(ehrId, Timestamp.valueOf(versionAtTime));
} else {
version = Integer.parseInt(ehrService.getLatestVersionUidOfStatus(ehrId).split("::")[2]);
}
Optional<OriginalVersion<EhrStatus>> ehrStatusOriginalVersion = ehrService.getEhrStatusAtVersion(ehrId, versionedObjectId, version);
UUID contributionId = ehrStatusOriginalVersion.map(i -> UUID.fromString(i.getContribution().getId().getValue())).orElseThrow(() -> new InvalidApiParameterException("Couldn't retrieve EhrStatus with given parameters"));
Optional<ContributionDto> optionalContributionDto = contributionService.getContribution(ehrId, contributionId);
// shouldn't happen
ContributionDto contributionDto = optionalContributionDto.orElseThrow(() -> new InternalServerException("Couldn't fetch contribution for existing EhrStatus"));
OriginalVersionResponseData<EhrStatus> originalVersionResponseData = new OriginalVersionResponseData<>(ehrStatusOriginalVersion.get(), contributionDto);
HttpHeaders respHeaders = new HttpHeaders();
respHeaders.setContentType(resolveContentType(accept));
return ResponseEntity.ok().headers(respHeaders).body(originalVersionResponseData);
}
Aggregations