From ef88eb9a9753bb37bc8cedd7b56777cc485cf3aa Mon Sep 17 00:00:00 2001 From: James Agnew Date: Sun, 22 Mar 2015 17:26:25 +0100 Subject: [PATCH] Get reverse includes working --- .../java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java | 2 +- .../uhn/fhir/jpa/dao/BaseFhirResourceDao.java | 14 +++++------ .../jpa/dao/FhirResourceDaoDstu2Test.java | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java index bfde2b15321..6be38af1b0a 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java @@ -855,7 +855,7 @@ public abstract class BaseFhirDao implements IDao { RuntimeResourceDefinition type = myContext.getResourceDefinition(theEntity.getResourceType()); return toResource(type.getImplementingClass(), theEntity); } - + protected T toResource(Class theResourceType, BaseHasResource theEntity) { String resourceText = null; switch (theEntity.getEncoding()) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirResourceDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirResourceDao.java index 2d4eeb18a5d..17b72835bd1 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirResourceDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirResourceDao.java @@ -1122,14 +1122,13 @@ public abstract class BaseFhirResourceDao extends BaseFhirD CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery cq = builder.createQuery(ResourceTable.class); Root from = cq.from(ResourceTable.class); - cq.where(builder.equal(from.get("myResourceType"), getContext().getResourceDefinition(myResourceType).getName())); - if (theIncludePids != null) { - cq.where(from.get("myId").in(theIncludePids)); - } +// cq.where(builder.equal(from.get("myResourceType"), getContext().getResourceDefinition(myResourceType).getName())); + cq.where(from.get("myId").in(theIncludePids)); TypedQuery q = myEntityManager.createQuery(cq); for (ResourceTable next : q.getResultList()) { - T resource = toResource(myResourceType, next); + Class resourceType = getContext().getResourceDefinition(next.getResourceType()).getImplementingClass(); + IResource resource = (IResource) toResource(resourceType, next); Integer index = position.get(next.getId()); if (index == null) { ourLog.warn("Got back unexpected resource PID {}", next.getId()); @@ -1442,7 +1441,6 @@ public abstract class BaseFhirResourceDao extends BaseFhirD return; } - Long[] matchesArray = theMatches.toArray(new Long[theMatches.size()]); Set pidsToInclude = new HashSet(); for (Include nextInclude : theRevIncludes) { @@ -1465,10 +1463,10 @@ public abstract class BaseFhirResourceDao extends BaseFhirD } for (String nextPath : param.getPathsSplit()) { - String sql = "SELECT r FROM ResourceLink r WHERE r.mySourcePath = :src_path AND r.myTargetResourcePid IN :target_pids"; + String sql = "SELECT r FROM ResourceLink r WHERE r.mySourcePath = :src_path AND r.myTargetResourcePid IN (:target_pids)"; TypedQuery q = myEntityManager.createQuery(sql, ResourceLink.class); q.setParameter("src_path", nextPath); - q.setParameter("target_pids", matchesArray); + q.setParameter("target_pids", theMatches); List results = q.getResultList(); for (ResourceLink resourceLink : results) { pidsToInclude.add(resourceLink.getSourceResourcePid()); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoDstu2Test.java index 1e0f7da517e..942705d25f1 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoDstu2Test.java @@ -18,6 +18,7 @@ import static org.junit.Assert.fail; import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -846,6 +847,29 @@ public class FhirResourceDaoDstu2Test { } + @Test + public void testReverseIncludes() { + String methodName = "testReverseIncludes"; + Organization org = new Organization(); + org.setName("X"+methodName+"X"); + IdDt orgId = ourOrganizationDao.create(org).getId(); + + Patient pat = new Patient(); + pat.addName().addFamily("X"+methodName+"X"); + pat.getManagingOrganization().setReference(orgId.toUnqualifiedVersionless()); + ourPatientDao.create(pat); + + SearchParameterMap map = new SearchParameterMap(); + map.add(Organization.SP_NAME, new StringParam("X"+methodName+"X")); + map.setRevIncludes(Collections.singleton(Patient.INCLUDE_ORGANIZATION)); + IBundleProvider resultsP = ourOrganizationDao.search(map); + assertEquals(1, resultsP.size()); + List results = resultsP.getResources(0, resultsP.size()); + assertEquals(2, results.size()); + assertEquals(Organization.class, results.get(0).getClass()); + assertEquals(Patient.class, results.get(1).getClass()); + } + @Test public void testResourceInstanceMetaOperation() { deleteEverything();