Search in sources :

Example 1 with FhirValidationException

use of uk.nhs.adaptors.common.exception.FhirValidationException in project nia-patient-switching-standard-adaptor by NHSDigital.

the class SDSService method parsePersistDuration.

private Duration parsePersistDuration(String sdsResponse) throws SdsRetrievalException {
    Bundle bundle;
    try {
        bundle = fhirParser.parseResource(sdsResponse, Bundle.class);
    } catch (FhirValidationException e) {
        throw new SdsRetrievalException(e.getMessage());
    }
    List<Bundle.BundleEntryComponent> entries = bundle.getEntry();
    if (entries.isEmpty()) {
        throw new SdsRetrievalException("sds response doesn't contain any results");
    }
    Resource resource = entries.get(0).getResource();
    Property matchingChildren = resource.getChildByName(EXTENSION_KEY_VALUE);
    Optional<Extension> extensions = matchingChildren.getValues().stream().map(child -> (Extension) child).findFirst();
    Optional<Extension> persistDuration = extensions.orElseThrow(() -> new SdsRetrievalException("Error parsing persist duration extension")).getExtensionsByUrl(PERSIST_DURATION_URL).stream().findFirst();
    String isoDuration = persistDuration.orElseThrow(() -> new SdsRetrievalException("Error parsing persist duration value")).getValue().toString();
    return Duration.parse(isoDuration);
}
Also used : Extension(org.hl7.fhir.dstu3.model.Extension) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) Autowired(org.springframework.beans.factory.annotation.Autowired) Extension(org.hl7.fhir.dstu3.model.Extension) FhirParser(uk.nhs.adaptors.common.util.fhir.FhirParser) SdsRequestBuilder(uk.nhs.adaptors.pss.translator.sds.SdsRequestBuilder) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Service(org.springframework.stereotype.Service) Duration(java.time.Duration) FhirValidationException(uk.nhs.adaptors.common.exception.FhirValidationException) Optional(java.util.Optional) WebClientResponseException(org.springframework.web.reactive.function.client.WebClientResponseException) AllArgsConstructor(lombok.AllArgsConstructor) Property(org.hl7.fhir.dstu3.model.Property) SdsRetrievalException(uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) SdsRetrievalException(uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException) FhirValidationException(uk.nhs.adaptors.common.exception.FhirValidationException) Property(org.hl7.fhir.dstu3.model.Property)

Aggregations

Duration (java.time.Duration)1 List (java.util.List)1 Optional (java.util.Optional)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Bundle (org.hl7.fhir.dstu3.model.Bundle)1 Extension (org.hl7.fhir.dstu3.model.Extension)1 Property (org.hl7.fhir.dstu3.model.Property)1 Resource (org.hl7.fhir.dstu3.model.Resource)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Service (org.springframework.stereotype.Service)1 WebClientResponseException (org.springframework.web.reactive.function.client.WebClientResponseException)1 FhirValidationException (uk.nhs.adaptors.common.exception.FhirValidationException)1 FhirParser (uk.nhs.adaptors.common.util.fhir.FhirParser)1 SdsRetrievalException (uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException)1 SdsRequestBuilder (uk.nhs.adaptors.pss.translator.sds.SdsRequestBuilder)1