use of org.ehrbase.response.openehr.EhrStatusResponseData in project ehrbase by ehrbase.
the class OpenehrEhrStatusController method updateEhrStatus.
/**
* {@inheritDoc}
*/
@Override
@PutMapping
@PreAuthorize("checkAbacPre(@openehrEhrStatusController.EHR_STATUS, @ehrService.getSubjectExtRef(#ehrIdString))")
public ResponseEntity<EhrStatusResponseData> updateEhrStatus(@PathVariable("ehr_id") UUID ehrId, @RequestHeader(name = IF_MATCH) String versionUid, @RequestHeader(name = PREFER, required = false) String prefer, @RequestHeader(name = HttpHeaders.ACCEPT, required = false) String accept, @RequestHeader(name = HttpHeaders.CONTENT_TYPE, required = false) String contentType, @RequestBody EhrStatus ehrStatus) {
assertEhrExists(ehrId);
// If-Match header check
String latestVersionUid = ehrService.getLatestVersionUidOfStatus(ehrId);
if (!latestVersionUid.equals(versionUid)) {
throw new PreconditionFailedException("Given If-Match header does not match latest existing version");
}
// update EHR_STATUS and check for success
Optional<EhrStatus> updateStatus = ehrService.updateStatus(ehrId, ehrStatus, null);
EhrStatus status = updateStatus.orElseThrow(() -> new InvalidApiParameterException("Could not update EHR_STATUS"));
// update and prepare current version number
String newLatestVersionUid = ehrService.getLatestVersionUidOfStatus(ehrId);
String[] split = latestVersionUid.split("::");
if (latestVersionUid.equals(newLatestVersionUid) || split.length != 3) {
throw new InvalidApiParameterException("Update of EHR_STATUS failed");
}
int version = Integer.parseInt(split[split.length - 1]) + 1;
List<String> headerList = Arrays.asList(CONTENT_TYPE, LOCATION, ETAG, // whatever is required by REST spec
LAST_MODIFIED);
// variable to overload with more specific object if requested
Optional<InternalResponse<EhrStatusResponseData>> respData;
UUID statusUid = UUID.fromString(status.getUid().getValue().split("::")[0]);
respData = buildEhrStatusResponseData(EhrStatusResponseData::new, ehrId, statusUid, version, accept, headerList);
return respData.map(i -> ResponseEntity.ok().headers(i.getHeaders()).body(i.getResponseData())).orElse(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
}
use of org.ehrbase.response.openehr.EhrStatusResponseData in project ehrbase by ehrbase.
the class OpenehrEhrStatusController method buildEhrStatusResponseData.
/**
* Builder method to prepare appropriate HTTP response. Flexible to either allow minimal or full
* representation of resource.
*
* @param factory Lambda function to constructor of desired object
* @param ehrId Ehr reference
* @param ehrStatusId EhrStatus versioned object ID
* @param version EhrStatus version number
* @param accept Requested content format
* @param headerList Requested headers that need to be set
* @param <T> Either only header response or specific class EhrStatusResponseData
* @return
*/
private <T extends EhrStatusResponseData> Optional<InternalResponse<T>> buildEhrStatusResponseData(Supplier<T> factory, UUID ehrId, UUID ehrStatusId, int version, String accept, List<String> headerList) {
// create either EhrStatusResponseData or null (means no body, only headers incl. link to resource), via lambda request
T minimalOrRepresentation = factory.get();
// check for valid format header to produce content accordingly
MediaType contentType = resolveContentType(// to prepare header input if this header is needed later
accept);
Optional<OriginalVersion<EhrStatus>> ehrStatus = ehrService.getEhrStatusAtVersion(ehrId, ehrStatusId, version);
if (minimalOrRepresentation != null) {
// when this "if" is true the following casting can be executed and data manipulated by reference (handled by temporary variable)
EhrStatusResponseData objByReference = minimalOrRepresentation;
if (ehrStatus.isPresent()) {
objByReference.setArchetypeNodeId(ehrStatus.get().getData().getArchetypeNodeId());
objByReference.setName(ehrStatus.get().getData().getName());
objByReference.setUid(ehrStatus.get().getUid());
objByReference.setSubject(ehrStatus.get().getData().getSubject());
objByReference.setOtherDetails(ehrStatus.get().getData().getOtherDetails());
objByReference.setModifiable(ehrStatus.get().getData().isModifiable());
objByReference.setQueryable(ehrStatus.get().getData().isQueryable());
} else {
return Optional.empty();
}
}
// create and supplement headers with data depending on which headers are requested
HttpHeaders respHeaders = new HttpHeaders();
for (String header : headerList) {
switch(header) {
case CONTENT_TYPE:
respHeaders.setContentType(contentType);
break;
case LOCATION:
try {
URI url = new URI(getBaseEnvLinkURL() + "/rest/openehr/v1/ehr/" + ehrId + "/ehr_status/" + ehrStatusId + "::" + ehrService.getServerConfig().getNodename() + "::" + version);
respHeaders.setLocation(url);
} catch (Exception e) {
throw new InternalServerException(e.getMessage());
}
break;
case ETAG:
respHeaders.setETag("\"" + ehrStatusId + "::" + ehrService.getServerConfig().getNodename() + "::" + version + "\"");
break;
case LAST_MODIFIED:
ehrStatus.ifPresent(ehrStatusOriginalVersion -> respHeaders.setLastModified(ehrStatusOriginalVersion.getCommitAudit().getTimeCommitted().getMagnitude()));
break;
default:
}
}
return Optional.of(new InternalResponse<>(minimalOrRepresentation, respHeaders));
}
Aggregations