Search in sources :

Example 1 with AmbiguousSubjectException

use of org.monarchinitiative.loinc2hpo.exception.AmbiguousSubjectException 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");
    }
}
Also used : SubjectNotFoundException(org.monarchinitiative.loinc2hpo.exception.SubjectNotFoundException) AmbiguousSubjectException(org.monarchinitiative.loinc2hpo.exception.AmbiguousSubjectException) IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 IBaseBundle (org.hl7.fhir.instance.model.api.IBaseBundle)1 AmbiguousSubjectException (org.monarchinitiative.loinc2hpo.exception.AmbiguousSubjectException)1 SubjectNotFoundException (org.monarchinitiative.loinc2hpo.exception.SubjectNotFoundException)1