Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the possibility to search for reference using OR search #451

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ca.uhn.fhir.rest.gclient;

import java.util.Collection;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.primitive.IdDt;

Expand Down Expand Up @@ -57,6 +59,17 @@ public ICriterion<ReferenceClientParam> hasId(String theId) {
return new StringCriterion<ReferenceClientParam>(getParamName(), theId);
}

/**
* Match the referenced resource if the resource has ANY of the given IDs
* (this is an OR search, not an AND search), (this can be the logical ID or
* the absolute URL of the resource). Note that to specify an AND search,
* simply add a subsequent {@link IQuery#where(ICriterion) where} criteria
* with the same parameter.
*/
public ICriterion<ReferenceClientParam> ids(Collection<String> theIds) {
return new StringCriterion<ReferenceClientParam>(getParamName(), theIds);
}

private static class ReferenceChainCriterion implements ICriterion<ReferenceClientParam>, ICriterionInternal {

private String myParamName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.apache.commons.lang3.StringUtils.isBlank;

import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -47,7 +48,7 @@ public StringCriterion(String theName, ParamPrefixEnum thePrefix, String theValu
myValue = ParameterUtil.escapeWithDefault(theValue);
}

public StringCriterion(String theName, List<String> theValue) {
public StringCriterion(String theName, Collection<String> theValue) {
myName=theName;
StringBuilder b = new StringBuilder();
for (String next : theValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -47,6 +50,7 @@
import org.junit.Test;

import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.BundleEntry;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
Expand Down Expand Up @@ -1556,6 +1560,43 @@ public void testSearchByResourceChain() {
assertEquals(p1Id.getIdPart(), actual.getEntries().get(0).getResource().getId().getIdPart());

}

@Test
public void testSearchByReferenceIds() {
Organization o1 = new Organization();
o1.setName("testSearchByResourceChainName01");
IdDt o1id = (IdDt) ourClient.create().resource(o1).execute().getId().toUnqualifiedVersionless();
Organization o2 = new Organization();
o2.setName("testSearchByResourceChainName02");
IdDt o2id = (IdDt) ourClient.create().resource(o2).execute().getId().toUnqualifiedVersionless();

Patient p1 = new Patient();
p1.addIdentifier().setSystem("urn:system").setValue("testSearchByReferenceIds01");
p1.addName().addFamily("testSearchByReferenceIdsFamily01").addGiven("testSearchByReferenceIdsGiven01");
p1.setManagingOrganization(new ResourceReferenceDt(o1id.toUnqualifiedVersionless()));
IdDt p1Id = (IdDt) ourClient.create().resource(p1).execute().getId();

Patient p2 = new Patient();
p2.addIdentifier().setSystem("urn:system").setValue("testSearchByReferenceIds02");
p2.addName().addFamily("testSearchByReferenceIdsFamily02").addGiven("testSearchByReferenceIdsGiven02");
p2.setManagingOrganization(new ResourceReferenceDt(o2id.toUnqualifiedVersionless()));
IdDt p2Id = (IdDt) ourClient.create().resource(p2).execute().getId();

//@formatter:off
Bundle actual = ourClient.search()
.forResource(Patient.class)
.where(Patient.ORGANIZATION.ids(Arrays.asList(o1id.getIdPart(), o2id.getIdPart())))
.encodedJson().prettyPrint().execute();
//@formatter:on
Set<String> expectedIds = new HashSet<String>();
expectedIds.add(p1Id.getIdPart());
expectedIds.add(p2Id.getIdPart());
Set<String> actualIds = new HashSet<String>();
for (BundleEntry ele : actual.getEntries()) {
actualIds.add(ele.getResource().getId().getIdPart());
}
assertEquals("Expects to retrieve the 2 patients which reference the two different organizations", expectedIds, actualIds);
}

@Test
public void testSearchLastUpdatedParamRp() throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -96,11 +99,9 @@
import org.junit.AfterClass;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.NestedExceptionUtils;

import com.google.common.collect.Lists;

import ca.uhn.fhir.jpa.dao.SearchParameterMap;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.model.primitive.UriDt;
import ca.uhn.fhir.parser.IParser;
Expand Down Expand Up @@ -134,12 +135,11 @@ public static void afterClassClearContext() {
@Test
public void testSearchPagingKeepsOldSearches() throws Exception {
String methodName = "testSearchPagingKeepsOldSearches";
IIdType pid1;
{
Patient patient = new Patient();
patient.addIdentifier().setSystem("urn:system").setValue("0");
patient.addName().addFamily(methodName).addGiven("Joe");
pid1 = myPatientDao.create(patient, mySrd).getId().toUnqualifiedVersionless();
myPatientDao.create(patient, mySrd).getId().toUnqualifiedVersionless();
}

for (int i = 1; i <= 20; i++) {
Expand Down Expand Up @@ -1971,6 +1971,43 @@ public void testSearchByResourceChain() {
assertEquals(p1Id.getIdPart(), actual.getEntry().get(0).getResource().getIdElement().getIdPart());

}

@Test
public void testSearchByReferenceIds() {
Organization o1 = new Organization();
o1.setName("testSearchByResourceChainName01");
IIdType o1id = ourClient.create().resource(o1).execute().getId().toUnqualifiedVersionless();
Organization o2 = new Organization();
o2.setName("testSearchByResourceChainName02");
IIdType o2id = ourClient.create().resource(o2).execute().getId().toUnqualifiedVersionless();

Patient p1 = new Patient();
p1.addIdentifier().setSystem("urn:system").setValue("testSearchByReferenceIds01");
p1.addName().addFamily("testSearchByReferenceIdsFamily01").addGiven("testSearchByReferenceIdsGiven01");
p1.setManagingOrganization(new Reference(o1id.toUnqualifiedVersionless()));
IIdType p1Id = ourClient.create().resource(p1).execute().getId();

Patient p2 = new Patient();
p2.addIdentifier().setSystem("urn:system").setValue("testSearchByReferenceIds02");
p2.addName().addFamily("testSearchByReferenceIdsFamily02").addGiven("testSearchByReferenceIdsGiven02");
p2.setManagingOrganization(new Reference(o2id.toUnqualifiedVersionless()));
IIdType p2Id = ourClient.create().resource(p2).execute().getId();

//@formatter:off
Bundle actual = ourClient.search()
.forResource(Patient.class)
.where(Patient.ORGANIZATION.ids(Arrays.asList(o1id.getIdPart(), o2id.getIdPart())))
.encodedJson().prettyPrint().returnBundle(Bundle.class).execute();
//@formatter:on
Set<String> expectedIds = new HashSet<String>();
expectedIds.add(p1Id.getIdPart());
expectedIds.add(p2Id.getIdPart());
Set<String> actualIds = new HashSet<String>();
for (BundleEntryComponent ele : actual.getEntry()) {
actualIds.add(ele.getResource().getIdElement().getIdPart());
}
assertEquals("Expects to retrieve the 2 patients which reference the two different organizations", expectedIds, actualIds);
}

@Test
public void testSearchLastUpdatedParamRp() throws InterruptedException {
Expand Down