use of org.monarchinitiative.loinc2hpo.exception.SubjectNotFoundException in project loinc2hpo by monarch-initiative.
the class FhirResourceRetriever method retrievePatientFromServer.
/**
* Retrieve a patient from the reference field of an observation
* @param subject
* @return
*/
public static Patient retrievePatientFromServer(Reference subject) throws SubjectNotFoundException, AmbiguousSubjectException {
List<Patient> patients = new ArrayList<>();
if (subject.hasReference()) {
String ref = subject.getReference();
if (!ref.startsWith(BASEURL) && ref.startsWith("Patient")) {
ref = BASEURL + "/" + ref;
}
Bundle patientBundle = client.search().byUrl(ref).returnBundle(Bundle.class).execute();
while (true) {
patientBundle.getEntry().forEach(p -> patients.add((Patient) p.getResource()));
if (patientBundle.getLink(IBaseBundle.LINK_NEXT) != null) {
patientBundle = client.loadPage().next(patientBundle).execute();
} else {
break;
}
}
} else if (subject.hasIdentifier()) {
Identifier identifier = subject.getIdentifier();
// TODO: find patient through the identifier
}
if (patients.size() == 1) {
return patients.iterator().next();
} else if (patients.isEmpty()) {
throw new SubjectNotFoundException("Expect one subject, but found none");
} else {
throw new AmbiguousSubjectException("Except one subject, but found multiple");
}
}
Aggregations