2

I am very new to the HAPI FHIR client, and have been endlessly browsing tutorials and documentation to try and assist me with my program that I am trying to develop, but I am now stuck when it comes to querying a server to search for a list of Patients that are all being treated by the same practitioner. In other words, I want a user to enter a Practitioner ID number and for the system to return a list of patients with their details. I was able to query the server to get all the Encounters when entering the ID number of the Practitioner, which were all placed into a Bundle, but I have no idea where to go from there. I have placed the line of code I used to get the Bundle and can verify that it works, because when couting the number of resources in the bundle, I get the correct number. pracID is the user-entered integer.

Bundle response = client.search().forResource(Encounter.class).where(Encounter.PRACTITIONER.hasId(Integer.toString(pracID))).returnBundle(Bundle.class).execute();

I am also coding this in Java on IntelliJ, using the R4 version of the Context client. I will highly appreciate any help. Thanks in advance

James Foster
  • 71
  • 1
  • 7

1 Answers1

2

I think there are many roads leading to Rome, but my first naive approach would be the following: You could try to find the patient data recursively using the search parameter _include. This search parameter searches all encounter objects for the corresponding Subject:

Bundle response = 
   client.search()
   .forResource(Encounter.class)
   .where(Encounter.PRACTITIONER.hasId(Integer.toString(pracID)))
   .include(Patient.INCLUDE_ALL.asRecursive())
   .returnBundle(Bundle.class).execute(); 

If links are available, the bundle should now contain not only Encounter objects but also Patient objects, which could be retrieved for further processing as follows:

List<Patient> listPatients = new ArrayList<Patient>();
response.getEntry().forEach(entry -> { 
      if (entry.getResource() instanceof Patient) {
        listPatients.add((Patient) entry.getResource());
      }
});
MavidDeyers
  • 180
  • 1
  • 14
  • This has been a big help thank you. I know it's a late reply, just been busy lately is all. I was playing around with the query you provided and it work beautifully, but I noticed that by using the ID, it does not return all instances of that Practitioner as I saw that they can have multiple IDs, but only one Identifier. I can use the TokenClientParam in the where clause of the statement, but that only returns a specific resource, is there a way to do something like you coded, but rather using the identifier? – James Foster May 18 '20 at 06:43
  • Do you know all Ids in advance and can you include them in the query? Then put them all in a list and use the _hasAnyOfIds_ method instead of _hasId_. If not, how do you identify the Practitioner? – MavidDeyers May 18 '20 at 08:48
  • 1
    I found a way to get all the IDs using the identifier value, with that I can then use the _hasAnyOfIds_ method to then get all the resources in need. Thanks for the help – James Foster May 19 '20 at 09:17