Search in sources :

Example 1 with ContributionDto

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));
}
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 ContributionDto

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);
}
Also used : OriginalVersion(com.nedap.archie.rm.changecontrol.OriginalVersion) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) ContributionService(org.ehrbase.api.service.ContributionService) Composition(com.nedap.archie.rm.composition.Composition) LocalDateTime(java.time.LocalDateTime) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) EhrService(org.ehrbase.api.service.EhrService) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) VersionedComposition(com.nedap.archie.rm.ehr.VersionedComposition) VersionedCompositionApiSpecification(org.ehrbase.rest.openehr.specification.VersionedCompositionApiSpecification) RevisionHistoryResponseData(org.ehrbase.response.openehr.RevisionHistoryResponseData) GetMapping(org.springframework.web.bind.annotation.GetMapping) ObjectVersionId(com.nedap.archie.rm.support.identification.ObjectVersionId) HttpHeaders(org.springframework.http.HttpHeaders) VersionedObjectResponseData(org.ehrbase.response.openehr.VersionedObjectResponseData) MediaType(org.springframework.http.MediaType) PostAuthorize(org.springframework.security.access.prepost.PostAuthorize) UUID(java.util.UUID) RestController(org.springframework.web.bind.annotation.RestController) BaseController(org.ehrbase.rest.BaseController) Objects(java.util.Objects) ContributionDto(org.ehrbase.response.ehrscape.ContributionDto) CompositionService(org.ehrbase.api.service.CompositionService) InternalServerException(org.ehrbase.api.exception.InternalServerException) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) Optional(java.util.Optional) RevisionHistory(com.nedap.archie.rm.generic.RevisionHistory) ResponseEntity(org.springframework.http.ResponseEntity) RequestHeader(org.springframework.web.bind.annotation.RequestHeader) OriginalVersion(com.nedap.archie.rm.changecontrol.OriginalVersion) OriginalVersionResponseData(org.ehrbase.response.openehr.OriginalVersionResponseData) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) HttpHeaders(org.springframework.http.HttpHeaders) Composition(com.nedap.archie.rm.composition.Composition) VersionedComposition(com.nedap.archie.rm.ehr.VersionedComposition) InternalServerException(org.ehrbase.api.exception.InternalServerException) ContributionDto(org.ehrbase.response.ehrscape.ContributionDto) UUID(java.util.UUID) OriginalVersionResponseData(org.ehrbase.response.openehr.OriginalVersionResponseData)

Example 3 with ContributionDto

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);
}
Also used : InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) OriginalVersion(com.nedap.archie.rm.changecontrol.OriginalVersion) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) ContributionService(org.ehrbase.api.service.ContributionService) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) LocalDateTime(java.time.LocalDateTime) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) EhrService(org.ehrbase.api.service.EhrService) VersionedEhrStatusApiSpecification(org.ehrbase.rest.openehr.specification.VersionedEhrStatusApiSpecification) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) VersionedEhrStatus(com.nedap.archie.rm.ehr.VersionedEhrStatus) RevisionHistoryResponseData(org.ehrbase.response.openehr.RevisionHistoryResponseData) GetMapping(org.springframework.web.bind.annotation.GetMapping) HttpHeaders(org.springframework.http.HttpHeaders) Timestamp(java.sql.Timestamp) VersionedObjectResponseData(org.ehrbase.response.openehr.VersionedObjectResponseData) MediaType(org.springframework.http.MediaType) UUID(java.util.UUID) RestController(org.springframework.web.bind.annotation.RestController) BaseController(org.ehrbase.rest.BaseController) Objects(java.util.Objects) ContributionDto(org.ehrbase.response.ehrscape.ContributionDto) InternalServerException(org.ehrbase.api.exception.InternalServerException) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) Optional(java.util.Optional) RevisionHistory(com.nedap.archie.rm.generic.RevisionHistory) ResponseEntity(org.springframework.http.ResponseEntity) RequestHeader(org.springframework.web.bind.annotation.RequestHeader) OriginalVersion(com.nedap.archie.rm.changecontrol.OriginalVersion) EhrStatus(com.nedap.archie.rm.ehr.EhrStatus) OriginalVersionResponseData(org.ehrbase.response.openehr.OriginalVersionResponseData) HttpHeaders(org.springframework.http.HttpHeaders) VersionedEhrStatus(com.nedap.archie.rm.ehr.VersionedEhrStatus) EhrStatus(com.nedap.archie.rm.ehr.EhrStatus) InternalServerException(org.ehrbase.api.exception.InternalServerException) ContributionDto(org.ehrbase.response.ehrscape.ContributionDto) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) InternalServerException(org.ehrbase.api.exception.InternalServerException) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) OriginalVersionResponseData(org.ehrbase.response.openehr.OriginalVersionResponseData) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) UUID(java.util.UUID) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with ContributionDto

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);
}
Also used : OriginalVersion(com.nedap.archie.rm.changecontrol.OriginalVersion) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) ContributionService(org.ehrbase.api.service.ContributionService) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) LocalDateTime(java.time.LocalDateTime) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) EhrService(org.ehrbase.api.service.EhrService) VersionedEhrStatusApiSpecification(org.ehrbase.rest.openehr.specification.VersionedEhrStatusApiSpecification) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) VersionedEhrStatus(com.nedap.archie.rm.ehr.VersionedEhrStatus) RevisionHistoryResponseData(org.ehrbase.response.openehr.RevisionHistoryResponseData) GetMapping(org.springframework.web.bind.annotation.GetMapping) HttpHeaders(org.springframework.http.HttpHeaders) Timestamp(java.sql.Timestamp) VersionedObjectResponseData(org.ehrbase.response.openehr.VersionedObjectResponseData) MediaType(org.springframework.http.MediaType) UUID(java.util.UUID) RestController(org.springframework.web.bind.annotation.RestController) BaseController(org.ehrbase.rest.BaseController) Objects(java.util.Objects) ContributionDto(org.ehrbase.response.ehrscape.ContributionDto) InternalServerException(org.ehrbase.api.exception.InternalServerException) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) Optional(java.util.Optional) RevisionHistory(com.nedap.archie.rm.generic.RevisionHistory) ResponseEntity(org.springframework.http.ResponseEntity) RequestHeader(org.springframework.web.bind.annotation.RequestHeader) OriginalVersion(com.nedap.archie.rm.changecontrol.OriginalVersion) EhrStatus(com.nedap.archie.rm.ehr.EhrStatus) OriginalVersionResponseData(org.ehrbase.response.openehr.OriginalVersionResponseData) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) HttpHeaders(org.springframework.http.HttpHeaders) VersionedEhrStatus(com.nedap.archie.rm.ehr.VersionedEhrStatus) EhrStatus(com.nedap.archie.rm.ehr.EhrStatus) InternalServerException(org.ehrbase.api.exception.InternalServerException) ContributionDto(org.ehrbase.response.ehrscape.ContributionDto) OriginalVersionResponseData(org.ehrbase.response.openehr.OriginalVersionResponseData) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) UUID(java.util.UUID) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

ContributionDto (org.ehrbase.response.ehrscape.ContributionDto)4 HttpHeaders (org.springframework.http.HttpHeaders)4 OriginalVersion (com.nedap.archie.rm.changecontrol.OriginalVersion)3 RevisionHistory (com.nedap.archie.rm.generic.RevisionHistory)3 LocalDateTime (java.time.LocalDateTime)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 UUID (java.util.UUID)3 InternalServerException (org.ehrbase.api.exception.InternalServerException)3 InvalidApiParameterException (org.ehrbase.api.exception.InvalidApiParameterException)3 ObjectNotFoundException (org.ehrbase.api.exception.ObjectNotFoundException)3 ContributionService (org.ehrbase.api.service.ContributionService)3 EhrService (org.ehrbase.api.service.EhrService)3 OriginalVersionResponseData (org.ehrbase.response.openehr.OriginalVersionResponseData)3 RevisionHistoryResponseData (org.ehrbase.response.openehr.RevisionHistoryResponseData)3 VersionedObjectResponseData (org.ehrbase.response.openehr.VersionedObjectResponseData)3 BaseController (org.ehrbase.rest.BaseController)3 DateTimeFormat (org.springframework.format.annotation.DateTimeFormat)3 MediaType (org.springframework.http.MediaType)3 ResponseEntity (org.springframework.http.ResponseEntity)3