javahl7-fhirhapihapi-fhir

Hapi FHIR - Appointment with patientId and list of service category name


I am new to HAPI FHIR api, But I am trying to get Appointment resources for a patientID with service category and service type code.

I can find appointments for patient with withIDAndCompartment but not sure how to add the search criteria.

Bundle bundle = fhirClient.search().forResource(Patient.class).withIdAndCompartment(patientId, ResourceType.Appointment.name()).returnBundle(Bunle.class).execute();

List<Appointment> appointments = BundleUtil.toListOfResourcesOfType(fhirClient.getFhirContext(), bundle, Appointment.class);

will return patientId's appointments.

I am not sure how to use TokenClientParam, Appointment.SERVICE_CATEGORY or Appointment.SERVICE_TYPE, to search the appointment type with the input service category or service type code.

Any help will be appreciated


Solution

  • The fields you want to search have all been added to the Search parameter by HAPI FHIR, you can refer to my code below

        public List<Appointment> findByPatientIdAndServiceTypeAndServiceCategory(
                final String patientId,
                final String serviceType,
                final String serviceCategory,
                final int offset,
                final int limit,
                final Collection<Include> includes) {
            final IQuery<?> theQuery = fhirClient.search().forResource(Appointment.class)
                .totalMode(SearchTotalModeEnum.ACCURATE)
                .offset(offset)
                .count(limit);
    
            if (StringUtils.isNotBlank(patientId)) {
                theQuery.and(Appointment.PATIENT.hasId(patientId));
            }
    
            if (StringUtils.isNotBlank(serviceType)) {
                theQuery.and(Appointment.SERVICE_TYPE.exactly().code(serviceType));
            }
    
            if (StringUtils.isNotBlank(serviceCategory)) {
                theQuery.and(Appointment.SERVICE_CATEGORY.exactly().code(serviceCategory));
            }
    
            CollectionUtils.emptyIfNull(includes).forEach(theQuery::include);
    
            theQuery.returnBundle(Bundle.class).execute();
            Bundle result = theQuery.returnBundle(Bundle.class).execute();
    
            if (result.getEntry().isEmpty()) {
                return Collections.emptyList();
            }
    
            return result.getEntry()
                .stream()
                .filter(entryComponent -> entryComponent.getResource() instanceof Appointment)
                .map(entry -> (Appointment) entry.getResource())
                .collect(Collectors.toList());
        }