From a5e7ea16aecc719f9579a6191d64697280fe5b89 Mon Sep 17 00:00:00 2001 From: patrick-werner Date: Fri, 26 Apr 2019 10:47:58 +0200 Subject: [PATCH 1/2] added all fetchValueSet Implementations --- hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java index e6284b22148..9c29eb20df3 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java @@ -24,6 +24,7 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import org.apache.commons.lang3.tuple.Pair; import org.hl7.fhir.instance.model.api.*; @@ -197,6 +198,7 @@ public class BundleUtil { */ @SuppressWarnings("unchecked") public static List toListOfResourcesOfType(FhirContext theContext, IBaseBundle theBundle, Class theTypeToInclude) { + Objects.requireNonNull(theTypeToInclude, "ResourceType must not be null"); List retVal = new ArrayList<>(); RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle); @@ -207,7 +209,7 @@ public class BundleUtil { BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource"); for (IBase nextEntry : entries) { for (IBase next : resourceChild.getAccessor().getValues(nextEntry)) { - if (theTypeToInclude != null && !theTypeToInclude.isAssignableFrom(next.getClass())) { + if (!theTypeToInclude.isAssignableFrom(next.getClass())) { continue; } retVal.add((T) next); From 7f45660a150dd3a26527d55f002e188f4ef66037 Mon Sep 17 00:00:00 2001 From: patrick-werner Date: Fri, 26 Apr 2019 12:55:18 +0200 Subject: [PATCH 2/2] changed toListOfResources to send IBaseResource as Type instead of null --- .../java/ca/uhn/fhir/util/BundleUtil.java | 62 +++++++++---------- .../java/ca/uhn/fhir/util/BundleUtilTest.java | 14 +++++ 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java index 9c29eb20df3..3e39a96ba8e 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java @@ -11,9 +11,9 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank; * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -51,7 +51,7 @@ public class BundleUtil { BaseRuntimeChildDefinition relChild = relDef.getChildByName("relation"); List relValues = relChild.getAccessor().getValues(nextLink); for (IBase next : relValues) { - IPrimitiveType nextValue = (IPrimitiveType)next; + IPrimitiveType nextValue = (IPrimitiveType) next; if (theLinkRelation.equals(nextValue.getValueAsString())) { isRightRel = true; } @@ -65,7 +65,7 @@ public class BundleUtil { BaseRuntimeChildDefinition urlChild = linkDef.getChildByName("url"); List values = urlChild.getAccessor().getValues(nextLink); for (IBase nextUrl : values) { - IPrimitiveType nextValue = (IPrimitiveType)nextUrl; + IPrimitiveType nextValue = (IPrimitiveType) nextUrl; if (isNotBlank(nextValue.getValueAsString())) { return nextValue.getValueAsString(); } @@ -84,35 +84,35 @@ public class BundleUtil { BaseRuntimeElementCompositeDefinition entryChildElem = (BaseRuntimeElementCompositeDefinition) entryChild.getChildByName("entry"); BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource"); - + BaseRuntimeChildDefinition requestChild = entryChildElem.getChildByName("request"); BaseRuntimeElementCompositeDefinition requestDef = (BaseRuntimeElementCompositeDefinition) requestChild.getChildByName("request"); - + BaseRuntimeChildDefinition urlChild = requestDef.getChildByName("url"); List> retVal = new ArrayList<>(entries.size()); for (IBase nextEntry : entries) { - + String url = null; IBaseResource resource = null; - + for (IBase nextEntryValue : requestChild.getAccessor().getValues(nextEntry)) { for (IBase nextUrlValue : urlChild.getAccessor().getValues(nextEntryValue)) { - url = ((IPrimitiveType)nextUrlValue).getValue(); + url = ((IPrimitiveType) nextUrlValue).getValue(); } } - + // Should return 0..1 only for (IBase nextValue : resourceChild.getAccessor().getValues(nextEntry)) { resource = (IBaseResource) nextValue; } - + retVal.add(Pair.of(url, resource)); } - - return retVal; + + return retVal; } - + public static String getBundleType(FhirContext theContext, IBaseBundle theBundle) { RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle); BaseRuntimeChildDefinition entryChild = def.getChildByName("type"); @@ -148,13 +148,13 @@ public class BundleUtil { List entries = entryChild.getAccessor().getValues(theBundle); BaseRuntimeElementCompositeDefinition entryChildElem = (BaseRuntimeElementCompositeDefinition) entryChild.getChildByName("entry"); - + BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource"); BaseRuntimeChildDefinition requestChild = entryChildElem.getChildByName("request"); - BaseRuntimeElementCompositeDefinition requestElem = (BaseRuntimeElementCompositeDefinition) requestChild.getChildByName("request"); + BaseRuntimeElementCompositeDefinition requestElem = (BaseRuntimeElementCompositeDefinition) requestChild.getChildByName("request"); BaseRuntimeChildDefinition urlChild = requestElem.getChildByName("url"); BaseRuntimeChildDefinition methodChild = requestElem.getChildByName("method"); - + for (IBase nextEntry : entries) { IBaseResource resource = null; String url = null; @@ -165,36 +165,36 @@ public class BundleUtil { } for (IBase nextRequest : requestChild.getAccessor().getValues(nextEntry)) { for (IBase nextUrl : urlChild.getAccessor().getValues(nextRequest)) { - url = ((IPrimitiveType)nextUrl).getValueAsString(); + url = ((IPrimitiveType) nextUrl).getValueAsString(); } for (IBase nextUrl : methodChild.getAccessor().getValues(nextRequest)) { - String methodString = ((IPrimitiveType)nextUrl).getValueAsString(); + String methodString = ((IPrimitiveType) nextUrl).getValueAsString(); if (isNotBlank(methodString)) { requestType = RequestTypeEnum.valueOf(methodString); } } } - /* + /* * All 3 might be null - That's ok because we still want to know the * order in the original bundle. */ retVal.add(new BundleEntryParts(requestType, url, resource)); } - + return retVal; } - + /** * Extract all of the resources from a given bundle */ public static List toListOfResources(FhirContext theContext, IBaseBundle theBundle) { - return toListOfResourcesOfType(theContext, theBundle, null); + return toListOfResourcesOfType(theContext, theBundle, IBaseResource.class); } /** - * Extract all of the resources of a given type from a given bundle + * Extract all of the resources of a given type from a given bundle */ @SuppressWarnings("unchecked") public static List toListOfResourcesOfType(FhirContext theContext, IBaseBundle theBundle, Class theTypeToInclude) { @@ -209,36 +209,36 @@ public class BundleUtil { BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource"); for (IBase nextEntry : entries) { for (IBase next : resourceChild.getAccessor().getValues(nextEntry)) { - if (!theTypeToInclude.isAssignableFrom(next.getClass())) { - continue; + if (theTypeToInclude.isAssignableFrom(next.getClass())) { + retVal.add((T) next); } - retVal.add((T) next); } } - return retVal; } - public static class BundleEntryParts - { + public static class BundleEntryParts { private final RequestTypeEnum myRequestType; private final IBaseResource myResource; private final String myUrl; + BundleEntryParts(RequestTypeEnum theRequestType, String theUrl, IBaseResource theResource) { super(); myRequestType = theRequestType; myUrl = theUrl; myResource = theResource; } + public RequestTypeEnum getRequestType() { return myRequestType; } + public IBaseResource getResource() { return myResource; } + public String getUrl() { return myUrl; } } - } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/BundleUtilTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/BundleUtilTest.java index b203eef8fdb..278889043dc 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/BundleUtilTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/BundleUtilTest.java @@ -2,10 +2,13 @@ package ca.uhn.fhir.util; import ca.uhn.fhir.context.FhirContext; import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.Patient; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Test; +import java.util.List; + public class BundleUtilTest { private static FhirContext ourCtx = FhirContext.forR4(); @@ -38,6 +41,17 @@ public class BundleUtilTest { Assert.assertEquals(null, BundleUtil.getTotal(ourCtx, b)); } + @Test + public void toListOfResourcesOfTypeTest() { + Bundle bundle = new Bundle(); + for (int i = 0; i < 5; i++) { + bundle.addEntry(new Bundle.BundleEntryComponent().setResource(new Patient())); + } + List list = BundleUtil.toListOfResourcesOfType(ourCtx, bundle, Patient.class); + Assert.assertEquals(5, list.size()); + } + + @AfterClass public static void afterClassClearContext() { TestUtil.clearAllStaticFieldsForUnitTest();