IGenericClient (optional) Chained Searching

98 views
Skip to first unread message

granada Coder

unread,
Feb 24, 2021, 4:31:33 PM2/24/21
to HAPI FHIR

Is there is a way to "optionally" chain together filters for IGenericClient?


This code works:


import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.gclient.ICriterion;

Optional<String> optFamilyName = Optional.of("Smith");
Optional<String> optGivenName = Optional.of("Peggy");

//optGivenName = Optional.empty();

ICriterion<StringClientParam> familyNameCriteria = null;
if (optFamilyName.isPresent()) {
familyNameCriteria = Patient.FAMILY.matches().value(optFamilyName.get());
}
ICriterion<StringClientParam> givenNameCriteria = null;

if (optGivenName.isPresent()) {
givenNameCriteria = Patient.GIVEN.matches().value(optGivenName.get());
}

// Perform a search
Bundle bundleSearchResult = client
.search()
.forResource(Patient.class)
.where(familyNameCriteria)
.and(givenNameCriteria)
.returnBundle(Bundle.class)
.execute();



But if I uncomment out 

optGivenName = Optional.empty();

it will fail.

I understand why it is failing...that isn't the main point.

I'm trying to add items to the .where (and ? .add)... but when they are provided, not all the time.

Aka, adding "optional chaining" to the search method.


This below code does not work (will not compile), but kinda shows the intention:


Collection<ICriterion> allCriterias = new ArrayList<>();

ICriterion<StringClientParam> familyNameCriteria = null;
if (optFamilyName.isPresent()) {
familyNameCriteria = Patient.FAMILY.matches().value(optFamilyName.get());
allCriterias.add(familyNameCriteria);
}
ICriterion<StringClientParam> givenNameCriteria = null;

if (optGivenName.isPresent()) {
givenNameCriteria = Patient.GIVEN.matches().value(optGivenName.get());
allCriterias.add(familyNameCriteria);
}

// Perform a search
Bundle bundleSearchResult = client
.search()
.forResource(Patient.class)
.where(allCriterias)  <<<<<<<<<< the items exist or do not exist i this List (this does NOT compile but shows the idea 
.returnBundle(Bundle.class)
.execute();



implementation group: 'ca.uhn.hapi.fhir', name: 'hapi-fhir-structures-r4', version: hapiFhirVersion
implementation group: 'ca.uhn.hapi.fhir', name: 'hapi-fhir-client-okhttp', version: hapiFhirVersion


hapiFhirVersion = '5.2.0'

granada Coder

unread,
Feb 24, 2021, 4:33:41 PM2/24/21
to HAPI FHIR
Hmmm, I found the below, but I cannot find any examples how to actually code up the items for:

Map<String, List<IQueryParameterType>> theCriterion

public interface IBaseQuery<T extends IBaseQuery<?>> {
/**
* Add a set of search parameters to the query.
*/
T where(Map<String, List<IQueryParameterType>> theCriterion);

granada Coder

unread,
Mar 1, 2021, 12:57:41 PM3/1/21
to HAPI FHIR
So I was on the right track.

Here is a sampling:


Map<String, List<IQueryParameterType>> searchParameterMap = new HashMap<>();

// START FAMILY NAME

if (true == true) // your optional check here
{
List<IQueryParameterType> familyNameChainedFilters = new ArrayList<>();

StringParam sp1 = new StringParam("Smith", true);


familyNameChainedFilters.add(sp1);
searchParameterMap.put(Patient.FAMILY.getParamName(), familyNameChainedFilters);
}

// START DOB

if (true == true) // your optional check here
{
List<IQueryParameterType> dateOfBirthChainedFilters = new ArrayList<>();

SimpleDateFormat format1 = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
String dateAsString = format1.format(args.getDateOfBirth().get());
DateParam dp1 = new DateParam(ParamPrefixEnum.EQUAL, dateAsString);
dateOfBirthChainedFilters.add(dp1);

searchParameterMap.put(Patient.BIRTHDATE.getParamName(), dateOfBirthChainedFilters);
}
//START GENDER


if (true == true) // your optional check here
{
List<IQueryParameterType> genderChainedFilters = new ArrayList<>();

StringParam sp1 = new StringParam(AdministrativeGender.FEMALE.toCode());

genderChainedFilters.add(sp1);
searchParameterMap.put(Patient.GENDER.getParamName(), genderChainedFilters);
}


and now you can use the searchParameterMap, in the .where clause.
Reply all
Reply to author
Forward
0 new messages