Skip to content

Commit 00a3a3b

Browse files
FM2-479: Support querying by encounter type as a chained parameter (#419)
1 parent 126738b commit 00a3a3b

File tree

11 files changed

+106
-19
lines changed

11 files changed

+106
-19
lines changed

api-2.6/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationDispenseDaoImpl_2_6.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams
7474
.forEach(param -> handlePatientReference(criteria, (ReferenceAndListParam) param.getParam()));
7575
break;
7676
case FhirConstants.ENCOUNTER_REFERENCE_SEARCH_HANDLER:
77-
entry.getValue().forEach(e -> handleEncounterReference("e", (ReferenceAndListParam) e.getParam())
78-
.ifPresent(c -> createAlias(criteria, "encounter", "e").add(c)));
77+
entry.getValue()
78+
.forEach(e -> handleEncounterReference(criteria, (ReferenceAndListParam) e.getParam(), "e"));
7979
break;
8080
case FhirConstants.MEDICATION_REQUEST_REFERENCE_SEARCH_HANDLER:
8181
entry.getValue()

api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import org.hibernate.criterion.MatchMode;
8181
import org.hibernate.criterion.Order;
8282
import org.hibernate.internal.CriteriaImpl;
83+
import org.hl7.fhir.dstu3.model.Encounter;
8384
import org.hl7.fhir.exceptions.FHIRException;
8485
import org.hl7.fhir.r4.model.Location;
8586
import org.hl7.fhir.r4.model.Patient;
@@ -493,14 +494,36 @@ protected Optional<Criterion> handleQuantity(@Nonnull String propertyName, Quant
493494
return handleAndListParam(quantityAndListParam, quantityParam -> handleQuantity(propertyName, quantityParam));
494495
}
495496

496-
protected Optional<Criterion> handleEncounterReference(@Nonnull String encounterAlias,
497-
ReferenceAndListParam encounterReference) {
497+
protected void handleEncounterReference(Criteria criteria, ReferenceAndListParam encounterReference,
498+
@Nonnull String encounterAlias) {
499+
handleEncounterReference(criteria, encounterReference, encounterAlias, "encounter");
500+
}
501+
502+
protected void handleEncounterReference(Criteria criteria, ReferenceAndListParam encounterReference,
503+
@Nonnull String encounterAlias, @Nonnull String associationPath) {
498504
if (encounterReference == null) {
499-
return Optional.empty();
505+
return;
500506
}
501507

502-
return handleAndListParam(encounterReference,
503-
token -> Optional.of(eq(String.format("%s.uuid", encounterAlias), token.getIdPart())));
508+
if (lacksAlias(criteria, encounterAlias)) {
509+
criteria.createAlias(associationPath, encounterAlias);
510+
}
511+
512+
handleAndListParam(encounterReference, token -> {
513+
if (token.getChain() != null) {
514+
switch (token.getChain()) {
515+
case Encounter.SP_TYPE:
516+
if (lacksAlias(criteria, "et")) {
517+
criteria.createAlias(String.format("%s.encounterType", encounterAlias), "et");
518+
}
519+
return propertyLike("et.uuid", new StringParam(token.getValue(), true));
520+
}
521+
} else {
522+
return Optional.of(eq(String.format("%s.uuid", encounterAlias), token.getIdPart()));
523+
}
524+
525+
return Optional.empty();
526+
}).ifPresent(criteria::add);
504527
}
505528

506529
protected Optional<Criterion> handleGender(@Nonnull String propertyName, TokenAndListParam gender) {

api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirDiagnosticReportDaoImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams
3737
theParams.getParameters().forEach(entry -> {
3838
switch (entry.getKey()) {
3939
case FhirConstants.ENCOUNTER_REFERENCE_SEARCH_HANDLER:
40-
entry.getValue().forEach(param -> handleEncounterReference("e", (ReferenceAndListParam) param.getParam())
41-
.ifPresent(c -> criteria.createAlias("encounter", "e").add(c)));
40+
entry.getValue().forEach(
41+
param -> handleEncounterReference(criteria, (ReferenceAndListParam) param.getParam(), "e"));
4242
break;
4343
case FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER:
4444
entry.getValue().forEach(

api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationRequestDaoImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams
2929
theParams.getParameters().forEach(entry -> {
3030
switch (entry.getKey()) {
3131
case FhirConstants.ENCOUNTER_REFERENCE_SEARCH_HANDLER:
32-
entry.getValue().forEach(e -> handleEncounterReference("e", (ReferenceAndListParam) e.getParam())
33-
.ifPresent(c -> criteria.createAlias("encounter", "e").add(c)));
32+
entry.getValue()
33+
.forEach(e -> handleEncounterReference(criteria, (ReferenceAndListParam) e.getParam(), "e"));
3434
break;
3535
case FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER:
3636
entry.getValue().forEach(patientReference -> handlePatientReference(criteria,

api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirObservationDaoImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public class FhirObservationDaoImpl extends BaseFhirDao<Obs> implements FhirObse
6060
@Override
6161
public List<String> getSearchResultUuids(@Nonnull SearchParameterMap theParams) {
6262
if (!theParams.getParameters(FhirConstants.LASTN_OBSERVATION_SEARCH_HANDLER).isEmpty()) {
63-
6463
Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(typeToken.getRawType());
6564

6665
setupSearchParams(criteria, theParams);
@@ -99,8 +98,8 @@ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams
9998
theParams.getParameters().forEach(entry -> {
10099
switch (entry.getKey()) {
101100
case FhirConstants.ENCOUNTER_REFERENCE_SEARCH_HANDLER:
102-
entry.getValue().forEach(p -> handleEncounterReference("e", (ReferenceAndListParam) p.getParam())
103-
.ifPresent(c -> criteria.createAlias("encounter", "e").add(c)));
101+
entry.getValue()
102+
.forEach(p -> handleEncounterReference(criteria, (ReferenceAndListParam) p.getParam(), "e"));
104103
break;
105104
case FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER:
106105
entry.getValue().forEach(patientReference -> handlePatientReference(criteria,

api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirServiceRequestDaoImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams
3737
theParams.getParameters().forEach(entry -> {
3838
switch (entry.getKey()) {
3939
case FhirConstants.ENCOUNTER_REFERENCE_SEARCH_HANDLER:
40-
entry.getValue().forEach(param -> handleEncounterReference("e", (ReferenceAndListParam) param.getParam())
41-
.ifPresent(c -> criteria.createAlias("encounter", "e").add(c)));
40+
entry.getValue().forEach(
41+
param -> handleEncounterReference(criteria, (ReferenceAndListParam) param.getParam(), "e"));
4242
break;
4343
case FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER:
4444
entry.getValue().forEach(patientReference -> handlePatientReference(criteria,

api/src/main/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.apache.commons.collections.CollectionUtils;
4444
import org.hl7.fhir.convertors.conv30_40.Observation30_40;
4545
import org.hl7.fhir.dstu3.model.DiagnosticReport;
46+
import org.hl7.fhir.dstu3.model.Encounter;
4647
import org.hl7.fhir.dstu3.model.IdType;
4748
import org.hl7.fhir.dstu3.model.Observation;
4849
import org.hl7.fhir.dstu3.model.OperationOutcome;
@@ -94,7 +95,8 @@ public OperationOutcome deleteObservationResource(@IdParam @Nonnull IdType id) {
9495

9596
@Search
9697
public IBundleProvider searchObservations(
97-
@OptionalParam(name = Observation.SP_ENCOUNTER) ReferenceAndListParam encounterReference,
98+
@OptionalParam(name = Observation.SP_ENCOUNTER, chainWhitelist = { "",
99+
Encounter.SP_TYPE }, targetTypes = Encounter.class) ReferenceAndListParam encounterReference,
98100
@OptionalParam(name = Observation.SP_SUBJECT, chainWhitelist = { "", Patient.SP_IDENTIFIER, Patient.SP_GIVEN,
99101
Patient.SP_FAMILY,
100102
Patient.SP_NAME }, targetTypes = Patient.class) ReferenceAndListParam patientReference,

api/src/main/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public OperationOutcome deleteObservationResource(@IdParam @Nonnull IdType id) {
9191

9292
@Search
9393
public IBundleProvider searchObservations(
94-
@OptionalParam(name = Observation.SP_ENCOUNTER, chainWhitelist = {
95-
"" }, targetTypes = Encounter.class) ReferenceAndListParam encounterReference,
94+
@OptionalParam(name = Observation.SP_ENCOUNTER, chainWhitelist = { "",
95+
Encounter.SP_TYPE }, targetTypes = Encounter.class) ReferenceAndListParam encounterReference,
9696
@OptionalParam(name = Observation.SP_SUBJECT, chainWhitelist = { "", Patient.SP_IDENTIFIER, Patient.SP_GIVEN,
9797
Patient.SP_FAMILY,
9898
Patient.SP_NAME }, targetTypes = Patient.class) ReferenceAndListParam patientReference,

api/src/test/java/org/openmrs/module/fhir2/api/search/ObservationSearchQueryTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public class ObservationSearchQueryTest extends BaseModuleContextSensitiveTest {
131131

132132
private static final String ENCOUNTER_UUID_TWO = "6519d653-393b-4118-9c83-a3715b82d4ac";
133133

134+
private static final String ENCOUNTER_TYPE_UUID = "07000be2-26b6-4cce-8b40-866d8435b613";
135+
134136
private static final String MEMBER_UUID = "744b91f8-bdbc-4950-833b-002244e9fa2b";
135137

136138
private static final String OBS_SNOMED_CODE = "2332523";
@@ -667,6 +669,25 @@ public void searchForObs_shouldReturnObsByEncounter() {
667669
assertThat(resultList.size(), equalTo(10));
668670
}
669671

672+
@Test
673+
public void searchForObs_shouldReturnObsByEncounterType() {
674+
ReferenceAndListParam encounterReference = new ReferenceAndListParam()
675+
.addAnd(new ReferenceOrListParam().add(new ReferenceParam().setValue(ENCOUNTER_TYPE_UUID).setChain("type")));
676+
677+
SearchParameterMap theParams = new SearchParameterMap();
678+
theParams.addParameter(FhirConstants.ENCOUNTER_REFERENCE_SEARCH_HANDLER, encounterReference);
679+
680+
IBundleProvider results = search(theParams);
681+
682+
assertThat(results, notNullValue());
683+
assertThat(results.size(), equalTo(14));
684+
685+
List<IBaseResource> resultList = get(results);
686+
687+
assertThat(resultList, notNullValue());
688+
assertThat(resultList.size(), equalTo(10));
689+
}
690+
670691
@Test
671692
public void searchForObs_shouldReturnObsByCategory() {
672693
TokenAndListParam categories = new TokenAndListParam().addAnd(new TokenParam().setValue("laboratory"));

api/src/test/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProviderTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.hl7.fhir.dstu3.model.Patient;
5353
import org.hl7.fhir.instance.model.api.IBaseResource;
5454
import org.hl7.fhir.r4.model.DiagnosticReport;
55+
import org.hl7.fhir.r4.model.Encounter;
5556
import org.junit.Before;
5657
import org.junit.Test;
5758
import org.junit.runner.RunWith;
@@ -175,6 +176,26 @@ public void searchObservations_shouldReturnMatchingObservationsWhenPatientParamI
175176
assertThat(resultList.get(0).getIdElement().getIdPart(), equalTo(OBSERVATION_UUID));
176177
}
177178

179+
@Test
180+
public void searchObservations_shouldReturnMatchingObservationsWhenEncounterParamIsSpecified() {
181+
when(observationService.searchForObservations(any()))
182+
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(observation), 10, 1));
183+
184+
ReferenceAndListParam encounterParam = new ReferenceAndListParam();
185+
encounterParam.addValue(new ReferenceOrListParam().add(new ReferenceParam().setChain(Encounter.SP_TYPE)));
186+
187+
IBundleProvider results = resourceProvider.searchObservations(encounterParam, null, null, null, null, null, null,
188+
null, null, null, null, null, null, null, null, null);
189+
190+
List<IBaseResource> resultList = get(results, 1, 5);
191+
192+
assertThat(results, notNullValue());
193+
assertThat(resultList, hasSize(equalTo(1)));
194+
assertThat(resultList.get(0), notNullValue());
195+
assertThat(resultList.get(0).fhirType(), equalTo(FhirConstants.OBSERVATION));
196+
assertThat(resultList.get(0).getIdElement().getIdPart(), equalTo(OBSERVATION_UUID));
197+
}
198+
178199
@Test
179200
public void searchObservations_shouldAddRelatedResourcesWhenIncluded() {
180201
HashSet<Include> includes = new HashSet<>();

api/src/test/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProviderTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import lombok.Getter;
3939
import org.hl7.fhir.instance.model.api.IBaseResource;
4040
import org.hl7.fhir.r4.model.DiagnosticReport;
41+
import org.hl7.fhir.r4.model.Encounter;
4142
import org.hl7.fhir.r4.model.IdType;
4243
import org.hl7.fhir.r4.model.Observation;
4344
import org.hl7.fhir.r4.model.OperationOutcome;
@@ -170,6 +171,26 @@ public void searchObservations_shouldReturnMatchingObservationsWhenPatientParamI
170171
assertThat(resultList.get(0).getIdElement().getIdPart(), equalTo(OBSERVATION_UUID));
171172
}
172173

174+
@Test
175+
public void searchObservations_shouldReturnMatchingObservationsWhenEncounterParamIsSpecified() {
176+
when(observationService.searchForObservations(any()))
177+
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(observation), 10, 1));
178+
179+
ReferenceAndListParam encounterParam = new ReferenceAndListParam();
180+
encounterParam.addValue(new ReferenceOrListParam().add(new ReferenceParam().setChain(Encounter.SP_TYPE)));
181+
182+
IBundleProvider results = resourceProvider.searchObservations(encounterParam, null, null, null, null, null, null,
183+
null, null, null, null, null, null, null, null, null);
184+
185+
List<IBaseResource> resultList = get(results);
186+
187+
assertThat(results, notNullValue());
188+
assertThat(resultList, hasSize(equalTo(1)));
189+
assertThat(resultList.get(0), notNullValue());
190+
assertThat(resultList.get(0).fhirType(), equalTo(FhirConstants.OBSERVATION));
191+
assertThat(resultList.get(0).getIdElement().getIdPart(), equalTo(OBSERVATION_UUID));
192+
}
193+
173194
@Test
174195
public void searchObservations_shouldAddRelatedResourcesWhenIncluded() {
175196
HashSet<Include> includes = new HashSet<>();

0 commit comments

Comments
 (0)