Search in sources :

Example 1 with EhrStatusResponseData

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());
}
Also used : InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) EhrService(org.ehrbase.api.service.EhrService) Supplier(java.util.function.Supplier) RequestBody(org.springframework.web.bind.annotation.RequestBody) PutMapping(org.springframework.web.bind.annotation.PutMapping) GetMapping(org.springframework.web.bind.annotation.GetMapping) URI(java.net.URI) PreconditionFailedException(org.ehrbase.api.exception.PreconditionFailedException) HttpHeaders(org.springframework.http.HttpHeaders) Timestamp(java.sql.Timestamp) MediaType(org.springframework.http.MediaType) UUID(java.util.UUID) RestController(org.springframework.web.bind.annotation.RestController) BaseController(org.ehrbase.rest.BaseController) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) InternalServerException(org.ehrbase.api.exception.InternalServerException) EhrStatusResponseData(org.ehrbase.response.openehr.EhrStatusResponseData) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) InternalResponse(org.ehrbase.rest.util.InternalResponse) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) RequestHeader(org.springframework.web.bind.annotation.RequestHeader) OriginalVersion(com.nedap.archie.rm.changecontrol.OriginalVersion) EhrStatusApiSpecification(org.ehrbase.rest.openehr.specification.EhrStatusApiSpecification) EhrStatus(com.nedap.archie.rm.ehr.EhrStatus) EhrStatus(com.nedap.archie.rm.ehr.EhrStatus) PreconditionFailedException(org.ehrbase.api.exception.PreconditionFailedException) InternalResponse(org.ehrbase.rest.util.InternalResponse) UUID(java.util.UUID) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with EhrStatusResponseData

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));
}
Also used : OriginalVersion(com.nedap.archie.rm.changecontrol.OriginalVersion) HttpHeaders(org.springframework.http.HttpHeaders) InternalServerException(org.ehrbase.api.exception.InternalServerException) EhrStatusResponseData(org.ehrbase.response.openehr.EhrStatusResponseData) MediaType(org.springframework.http.MediaType) URI(java.net.URI) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) PreconditionFailedException(org.ehrbase.api.exception.PreconditionFailedException) InternalServerException(org.ehrbase.api.exception.InternalServerException) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException)

Aggregations

OriginalVersion (com.nedap.archie.rm.changecontrol.OriginalVersion)2 URI (java.net.URI)2 InternalServerException (org.ehrbase.api.exception.InternalServerException)2 InvalidApiParameterException (org.ehrbase.api.exception.InvalidApiParameterException)2 ObjectNotFoundException (org.ehrbase.api.exception.ObjectNotFoundException)2 PreconditionFailedException (org.ehrbase.api.exception.PreconditionFailedException)2 EhrStatusResponseData (org.ehrbase.response.openehr.EhrStatusResponseData)2 HttpHeaders (org.springframework.http.HttpHeaders)2 MediaType (org.springframework.http.MediaType)2 EhrStatus (com.nedap.archie.rm.ehr.EhrStatus)1 Timestamp (java.sql.Timestamp)1 OffsetDateTime (java.time.OffsetDateTime)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Supplier (java.util.function.Supplier)1 EhrService (org.ehrbase.api.service.EhrService)1 BaseController (org.ehrbase.rest.BaseController)1 EhrStatusApiSpecification (org.ehrbase.rest.openehr.specification.EhrStatusApiSpecification)1