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