Skip to content

Commit

Permalink
Rename method from #451
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Oct 12, 2016
1 parent 80e303b commit 8c455d4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
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> hasAnyOf(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 @@ -1563,6 +1567,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.hasAnyOf(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 @@ -61,8 +61,6 @@

import com.google.common.collect.Lists;

import ca.uhn.fhir.jpa.dao.SearchParameterMap;
import ca.uhn.fhir.jpa.dao.SearchParameterMap.EverythingModeEnum;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.model.primitive.UriDt;
import ca.uhn.fhir.parser.IParser;
Expand Down Expand Up @@ -2254,6 +2252,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.hasAnyOf(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

0 comments on commit 8c455d4

Please sign in to comment.