Skip to content

Commit

Permalink
changed toListOfResources to send IBaseResource as Type instead of null
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-werner committed Apr 26, 2019
1 parent a5e7ea1 commit 7f45660
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
62 changes: 31 additions & 31 deletions hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java
Expand Up @@ -11,9 +11,9 @@
* 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.
Expand Down Expand Up @@ -51,7 +51,7 @@ public static String getLinkUrlOfType(FhirContext theContext, IBaseBundle theBun
BaseRuntimeChildDefinition relChild = relDef.getChildByName("relation");
List<IBase> relValues = relChild.getAccessor().getValues(nextLink);
for (IBase next : relValues) {
IPrimitiveType<?> nextValue = (IPrimitiveType<?>)next;
IPrimitiveType<?> nextValue = (IPrimitiveType<?>) next;
if (theLinkRelation.equals(nextValue.getValueAsString())) {
isRightRel = true;
}
Expand All @@ -65,7 +65,7 @@ public static String getLinkUrlOfType(FhirContext theContext, IBaseBundle theBun
BaseRuntimeChildDefinition urlChild = linkDef.getChildByName("url");
List<IBase> values = urlChild.getAccessor().getValues(nextLink);
for (IBase nextUrl : values) {
IPrimitiveType<?> nextValue = (IPrimitiveType<?>)nextUrl;
IPrimitiveType<?> nextValue = (IPrimitiveType<?>) nextUrl;
if (isNotBlank(nextValue.getValueAsString())) {
return nextValue.getValueAsString();
}
Expand All @@ -84,35 +84,35 @@ public static List<Pair<String, IBaseResource>> getBundleEntryUrlsAndResources(F

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<Pair<String, IBaseResource>> 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<String>)nextUrlValue).getValue();
url = ((IPrimitiveType<String>) 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");
Expand Down Expand Up @@ -148,13 +148,13 @@ public static List<BundleEntryParts> toListOfEntries(FhirContext theContext, IBa
List<IBase> 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;
Expand All @@ -165,36 +165,36 @@ public static List<BundleEntryParts> toListOfEntries(FhirContext theContext, IBa
}
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<IBaseResource> 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 <T extends IBaseResource> List<T> toListOfResourcesOfType(FhirContext theContext, IBaseBundle theBundle, Class<T> theTypeToInclude) {
Expand All @@ -209,36 +209,36 @@ public static <T extends IBaseResource> List<T> toListOfResourcesOfType(FhirCont
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;
}
}

}
Expand Up @@ -2,10 +2,13 @@

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();
Expand Down Expand Up @@ -38,6 +41,17 @@ public void testGetTotalNull() {
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
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
Expand Down

0 comments on commit 7f45660

Please sign in to comment.