Skip to content

Commit

Permalink
FM2-646: Practitioner: support limiting search to users or providers
Browse files Browse the repository at this point in the history
  • Loading branch information
mogoodrich committed Sep 26, 2024
1 parent 6842295 commit a304967
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 139 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ private FhirConstants() {

public static final String OPENMRS_FHIR_EXT_ENCOUNTER_TAG = OPENMRS_FHIR_EXT_PREFIX + "/encounter-tag";

public static final String OPENMRS_FHIR_EXT_PRACTITIONER_TAG = OPENMRS_FHIR_EXT_PREFIX + "/practitioner-tag";

public static final String OPENMRS_FHIR_EXT_OBSERVATION_REFERENCE_RANGE = OPENMRS_FHIR_EXT_PREFIX
+ "/obs/reference-range";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

import javax.annotation.Nonnull;

import java.util.Collections;

import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.InternalCodingDt;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
Expand All @@ -21,6 +25,7 @@
import org.hl7.fhir.r4.model.Practitioner;
import org.openmrs.Provider;
import org.openmrs.User;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirGlobalPropertyService;
import org.openmrs.module.fhir2.api.FhirPractitionerService;
import org.openmrs.module.fhir2.api.FhirUserService;
Expand Down Expand Up @@ -92,17 +97,36 @@ public Practitioner create(@Nonnull Practitioner newResource) {
@Override
@Transactional(readOnly = true)
public IBundleProvider searchForPractitioners(PractitionerSearchParams practitionerSearchParams) {
IBundleProvider providerBundle = searchQuery.getQueryResults(practitionerSearchParams.toSearchParameterMap(), dao,
translator, searchQueryInclude);
SearchParameterMap theParams = new SearchParameterMap();
IBundleProvider userBundle = userService.searchForUsers(theParams);

if (!providerBundle.isEmpty() && !userBundle.isEmpty()) {
IBundleProvider providerBundle = null;
IBundleProvider userBundle = null;

if (shouldSearchExplicitlyFor(practitionerSearchParams.getTag(), "provider")) {
providerBundle = searchQuery.getQueryResults(practitionerSearchParams.toSearchParameterMap(), dao, translator,
searchQueryInclude);
}

if (shouldSearchExplicitlyFor(practitionerSearchParams.getTag(), "user")) {
SearchParameterMap theParams = new SearchParameterMap();
userBundle = userService.searchForUsers(theParams);
}

if (providerBundle != null && userBundle != null) {
return new TwoSearchQueryBundleProvider(providerBundle, userBundle, globalPropertyService);
} else if (providerBundle.isEmpty() && !userBundle.isEmpty()) {
} else if (providerBundle == null && userBundle != null) {
return userBundle;
}

return providerBundle;
}

protected boolean shouldSearchExplicitlyFor(TokenAndListParam tokenAndListParam, @Nonnull String valueToCheck) {
if (tokenAndListParam == null || tokenAndListParam.size() == 0 || valueToCheck.isEmpty()) {
return true;
}

return tokenAndListParam.getValuesAsQueryTokens().stream().anyMatch(
tokenOrListParam -> tokenOrListParam.doesCodingListMatch(Collections
.singletonList(new InternalCodingDt(FhirConstants.OPENMRS_FHIR_EXT_PRACTITIONER_TAG, valueToCheck))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ public class PractitionerSearchParams extends BaseResourceSearchParams {

private StringAndListParam country;

private TokenAndListParam tag;

@Builder
public PractitionerSearchParams(TokenAndListParam identifier, StringAndListParam name, StringAndListParam given,
StringAndListParam family, StringAndListParam city, StringAndListParam state, StringAndListParam postalCode,
StringAndListParam country, TokenAndListParam id, DateRangeParam lastUpdated, HashSet<Include> revIncludes) {
StringAndListParam country, TokenAndListParam id, TokenAndListParam tag, DateRangeParam lastUpdated,
HashSet<Include> revIncludes) {

super(id, lastUpdated, null, null, revIncludes);

Expand All @@ -57,6 +60,7 @@ public PractitionerSearchParams(TokenAndListParam identifier, StringAndListParam
this.state = state;
this.postalCode = postalCode;
this.country = country;
this.tag = tag;
}

@Override
Expand All @@ -68,6 +72,7 @@ public SearchParameterMap toSearchParameterMap() {
.addParameter(FhirConstants.CITY_SEARCH_HANDLER, getCity())
.addParameter(FhirConstants.STATE_SEARCH_HANDLER, getState())
.addParameter(FhirConstants.POSTALCODE_SEARCH_HANDLER, getPostalCode())
.addParameter(FhirConstants.COUNTRY_SEARCH_HANDLER, getCountry());
.addParameter(FhirConstants.COUNTRY_SEARCH_HANDLER, getCountry())
.addParameter(FhirConstants.TAG_SEARCH_HANDLER, getTag());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public IBundleProvider searchForPractitioners(@OptionalParam(name = Practitioner
@OptionalParam(name = Practitioner.SP_ADDRESS_POSTALCODE) StringAndListParam postalCode,
@OptionalParam(name = Practitioner.SP_ADDRESS_COUNTRY) StringAndListParam country,
@OptionalParam(name = Practitioner.SP_RES_ID) TokenAndListParam id,
@OptionalParam(name = "_tag") TokenAndListParam tag,
@OptionalParam(name = "_lastUpdated") DateRangeParam lastUpdated,
@IncludeParam(reverse = true, allow = { "Encounter:" + Encounter.SP_PARTICIPANT,
"MedicationRequest:" + MedicationRequest.SP_REQUESTER,
Expand All @@ -124,7 +125,7 @@ public IBundleProvider searchForPractitioners(@OptionalParam(name = Practitioner

return new SearchQueryBundleProviderR3Wrapper(
practitionerService.searchForPractitioners(new PractitionerSearchParams(identifier, name, given, family,
city, state, postalCode, country, id, lastUpdated, revIncludes)));
city, state, postalCode, country, id, tag, lastUpdated, revIncludes)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public IBundleProvider searchForPractitioners(@OptionalParam(name = Practitioner
@OptionalParam(name = Practitioner.SP_ADDRESS_POSTALCODE) StringAndListParam postalCode,
@OptionalParam(name = Practitioner.SP_ADDRESS_COUNTRY) StringAndListParam country,
@OptionalParam(name = Practitioner.SP_RES_ID) TokenAndListParam id,
@OptionalParam(name = "_tag") TokenAndListParam tag,
@OptionalParam(name = "_lastUpdated") DateRangeParam lastUpdated,
@IncludeParam(reverse = true, allow = { "Encounter:" + Encounter.SP_PARTICIPANT,
"MedicationRequest:" + MedicationRequest.SP_REQUESTER, "ServiceRequest:" + ServiceRequest.SP_REQUESTER,
Expand All @@ -131,6 +132,6 @@ public IBundleProvider searchForPractitioners(@OptionalParam(name = Practitioner
}

return practitionerService.searchForPractitioners(new PractitionerSearchParams(identifier, name, given, family, city,
state, postalCode, country, id, lastUpdated, revIncludes));
state, postalCode, country, id, tag, lastUpdated, revIncludes));
}
}
Loading

0 comments on commit a304967

Please sign in to comment.