Merge pull request #1290 from jamesagnew/onlyOnce

minor BundleUtil Improvement
This commit is contained in:
Patrick Werner 2019-04-26 14:24:17 +02:00 committed by GitHub
commit 11590c0e08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 31 deletions

View File

@ -24,6 +24,7 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.hl7.fhir.instance.model.api.*; import org.hl7.fhir.instance.model.api.*;
@ -50,7 +51,7 @@ public class BundleUtil {
BaseRuntimeChildDefinition relChild = relDef.getChildByName("relation"); BaseRuntimeChildDefinition relChild = relDef.getChildByName("relation");
List<IBase> relValues = relChild.getAccessor().getValues(nextLink); List<IBase> relValues = relChild.getAccessor().getValues(nextLink);
for (IBase next : relValues) { for (IBase next : relValues) {
IPrimitiveType<?> nextValue = (IPrimitiveType<?>)next; IPrimitiveType<?> nextValue = (IPrimitiveType<?>) next;
if (theLinkRelation.equals(nextValue.getValueAsString())) { if (theLinkRelation.equals(nextValue.getValueAsString())) {
isRightRel = true; isRightRel = true;
} }
@ -64,7 +65,7 @@ public class BundleUtil {
BaseRuntimeChildDefinition urlChild = linkDef.getChildByName("url"); BaseRuntimeChildDefinition urlChild = linkDef.getChildByName("url");
List<IBase> values = urlChild.getAccessor().getValues(nextLink); List<IBase> values = urlChild.getAccessor().getValues(nextLink);
for (IBase nextUrl : values) { for (IBase nextUrl : values) {
IPrimitiveType<?> nextValue = (IPrimitiveType<?>)nextUrl; IPrimitiveType<?> nextValue = (IPrimitiveType<?>) nextUrl;
if (isNotBlank(nextValue.getValueAsString())) { if (isNotBlank(nextValue.getValueAsString())) {
return nextValue.getValueAsString(); return nextValue.getValueAsString();
} }
@ -97,7 +98,7 @@ public class BundleUtil {
for (IBase nextEntryValue : requestChild.getAccessor().getValues(nextEntry)) { for (IBase nextEntryValue : requestChild.getAccessor().getValues(nextEntry)) {
for (IBase nextUrlValue : urlChild.getAccessor().getValues(nextEntryValue)) { for (IBase nextUrlValue : urlChild.getAccessor().getValues(nextEntryValue)) {
url = ((IPrimitiveType<String>)nextUrlValue).getValue(); url = ((IPrimitiveType<String>) nextUrlValue).getValue();
} }
} }
@ -150,7 +151,7 @@ public class BundleUtil {
BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource"); BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource");
BaseRuntimeChildDefinition requestChild = entryChildElem.getChildByName("request"); BaseRuntimeChildDefinition requestChild = entryChildElem.getChildByName("request");
BaseRuntimeElementCompositeDefinition<?> requestElem = (BaseRuntimeElementCompositeDefinition<?>) requestChild.getChildByName("request"); BaseRuntimeElementCompositeDefinition<?> requestElem = (BaseRuntimeElementCompositeDefinition<?>) requestChild.getChildByName("request");
BaseRuntimeChildDefinition urlChild = requestElem.getChildByName("url"); BaseRuntimeChildDefinition urlChild = requestElem.getChildByName("url");
BaseRuntimeChildDefinition methodChild = requestElem.getChildByName("method"); BaseRuntimeChildDefinition methodChild = requestElem.getChildByName("method");
@ -164,10 +165,10 @@ public class BundleUtil {
} }
for (IBase nextRequest : requestChild.getAccessor().getValues(nextEntry)) { for (IBase nextRequest : requestChild.getAccessor().getValues(nextEntry)) {
for (IBase nextUrl : urlChild.getAccessor().getValues(nextRequest)) { for (IBase nextUrl : urlChild.getAccessor().getValues(nextRequest)) {
url = ((IPrimitiveType<?>)nextUrl).getValueAsString(); url = ((IPrimitiveType<?>) nextUrl).getValueAsString();
} }
for (IBase nextUrl : methodChild.getAccessor().getValues(nextRequest)) { for (IBase nextUrl : methodChild.getAccessor().getValues(nextRequest)) {
String methodString = ((IPrimitiveType<?>)nextUrl).getValueAsString(); String methodString = ((IPrimitiveType<?>) nextUrl).getValueAsString();
if (isNotBlank(methodString)) { if (isNotBlank(methodString)) {
requestType = RequestTypeEnum.valueOf(methodString); requestType = RequestTypeEnum.valueOf(methodString);
} }
@ -189,7 +190,7 @@ public class BundleUtil {
* Extract all of the resources from a given bundle * Extract all of the resources from a given bundle
*/ */
public static List<IBaseResource> toListOfResources(FhirContext theContext, IBaseBundle theBundle) { public static List<IBaseResource> toListOfResources(FhirContext theContext, IBaseBundle theBundle) {
return toListOfResourcesOfType(theContext, theBundle, null); return toListOfResourcesOfType(theContext, theBundle, IBaseResource.class);
} }
/** /**
@ -197,6 +198,7 @@ public class BundleUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T extends IBaseResource> List<T> toListOfResourcesOfType(FhirContext theContext, IBaseBundle theBundle, Class<T> theTypeToInclude) { public static <T extends IBaseResource> List<T> toListOfResourcesOfType(FhirContext theContext, IBaseBundle theBundle, Class<T> theTypeToInclude) {
Objects.requireNonNull(theTypeToInclude, "ResourceType must not be null");
List<T> retVal = new ArrayList<>(); List<T> retVal = new ArrayList<>();
RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle); RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle);
@ -207,36 +209,36 @@ public class BundleUtil {
BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource"); BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource");
for (IBase nextEntry : entries) { for (IBase nextEntry : entries) {
for (IBase next : resourceChild.getAccessor().getValues(nextEntry)) { for (IBase next : resourceChild.getAccessor().getValues(nextEntry)) {
if (theTypeToInclude != null && !theTypeToInclude.isAssignableFrom(next.getClass())) { if (theTypeToInclude.isAssignableFrom(next.getClass())) {
continue; retVal.add((T) next);
} }
retVal.add((T) next);
} }
} }
return retVal; return retVal;
} }
public static class BundleEntryParts public static class BundleEntryParts {
{
private final RequestTypeEnum myRequestType; private final RequestTypeEnum myRequestType;
private final IBaseResource myResource; private final IBaseResource myResource;
private final String myUrl; private final String myUrl;
BundleEntryParts(RequestTypeEnum theRequestType, String theUrl, IBaseResource theResource) { BundleEntryParts(RequestTypeEnum theRequestType, String theUrl, IBaseResource theResource) {
super(); super();
myRequestType = theRequestType; myRequestType = theRequestType;
myUrl = theUrl; myUrl = theUrl;
myResource = theResource; myResource = theResource;
} }
public RequestTypeEnum getRequestType() { public RequestTypeEnum getRequestType() {
return myRequestType; return myRequestType;
} }
public IBaseResource getResource() { public IBaseResource getResource() {
return myResource; return myResource;
} }
public String getUrl() { public String getUrl() {
return myUrl; return myUrl;
} }
} }
} }

View File

@ -2,10 +2,13 @@ package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Patient;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.List;
public class BundleUtilTest { public class BundleUtilTest {
private static FhirContext ourCtx = FhirContext.forR4(); private static FhirContext ourCtx = FhirContext.forR4();
@ -38,6 +41,17 @@ public class BundleUtilTest {
Assert.assertEquals(null, BundleUtil.getTotal(ourCtx, b)); 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<Patient> list = BundleUtil.toListOfResourcesOfType(ourCtx, bundle, Patient.class);
Assert.assertEquals(5, list.size());
}
@AfterClass @AfterClass
public static void afterClassClearContext() { public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest(); TestUtil.clearAllStaticFieldsForUnitTest();