Added support for additional types

This commit is contained in:
Nick Goupinets 2021-03-16 11:14:40 -04:00
parent ef97eb22fb
commit f9f703abe2
5 changed files with 109 additions and 9 deletions

View File

@ -27,6 +27,9 @@ import org.hl7.fhir.instance.model.api.IBaseDatatype;
import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* Utility for modifying with extensions in a FHIR version-independent approach. * Utility for modifying with extensions in a FHIR version-independent approach.
*/ */
@ -42,7 +45,7 @@ public class ExtensionUtil {
*/ */
public static IBaseExtension<?, ?> getOrCreateExtension(IBase theBase, String theUrl) { public static IBaseExtension<?, ?> getOrCreateExtension(IBase theBase, String theUrl) {
IBaseHasExtensions baseHasExtensions = validateExtensionSupport(theBase); IBaseHasExtensions baseHasExtensions = validateExtensionSupport(theBase);
IBaseExtension extension = getExtensionByUrl(baseHasExtensions, theUrl); IBaseExtension extension = getExtension(baseHasExtensions, theUrl);
if (extension == null) { if (extension == null) {
extension = baseHasExtensions.addExtension(); extension = baseHasExtensions.addExtension();
extension.setUrl(theUrl); extension.setUrl(theUrl);
@ -72,7 +75,7 @@ public class ExtensionUtil {
return false; return false;
} }
return getExtensionByUrl(baseHasExtensions, theExtensionUrl) != null; return getExtension(baseHasExtensions, theExtensionUrl) != null;
} }
/** /**
@ -86,14 +89,21 @@ public class ExtensionUtil {
if (!hasExtension(theBase, theExtensionUrl)) { if (!hasExtension(theBase, theExtensionUrl)) {
return false; return false;
} }
IBaseDatatype value = getExtensionByUrl((IBaseHasExtensions) theBase, theExtensionUrl).getValue(); IBaseDatatype value = getExtension((IBaseHasExtensions) theBase, theExtensionUrl).getValue();
if (value == null) { if (value == null) {
return theExtensionValue == null; return theExtensionValue == null;
} }
return value.toString().equals(theExtensionValue); return value.toString().equals(theExtensionValue);
} }
private static IBaseExtension<?, ?> getExtensionByUrl(IBaseHasExtensions theBase, String theExtensionUrl) { /**
* Gets the first extension with the specified URL
*
* @param theBase The resource to get the extension for
* @param theExtensionUrl URL of the extension to get. Must be non-null
* @return Returns the first available extension with the specified URL, or null if such extension doesn't exist
*/
public static IBaseExtension<?, ?> getExtension(IBaseHasExtensions theBase, String theExtensionUrl) {
return theBase.getExtension() return theBase.getExtension()
.stream() .stream()
.filter(e -> theExtensionUrl.equals(e.getUrl())) .filter(e -> theExtensionUrl.equals(e.getUrl()))
@ -102,18 +112,44 @@ public class ExtensionUtil {
} }
/** /**
* Sets value of the extension * Gets all extensions with the specified URL
*
* @param theBase The resource to get the extension for
* @param theExtensionUrl URL of the extension to get. Must be non-null
* @return Returns all extension with the specified URL, or an empty list if such extensions do not exist
*/
public static List<IBaseExtension<?, ?>> getExtensions(IBaseHasExtensions theBase, String theExtensionUrl) {
return theBase.getExtension()
.stream()
.filter(e -> theExtensionUrl.equals(e.getUrl()))
.collect(Collectors.toList());
}
/**
* Sets value of the extension as a string
* *
* @param theExtension The extension to set the value on * @param theExtension The extension to set the value on
* @param theValue The value to set * @param theValue The value to set
* @param theFhirContext The context containing FHIR resource definitions * @param theFhirContext The context containing FHIR resource definitions
*/ */
public static void setExtension(FhirContext theFhirContext, IBaseExtension theExtension, String theValue) { public static void setExtension(FhirContext theFhirContext, IBaseExtension theExtension, String theValue) {
theExtension.setValue(TerserUtil.newElement(theFhirContext, "string", theValue)); setExtension(theFhirContext, theExtension, "string", theValue);
} }
/** /**
* Sets or replaces existing extension with the specified value * Sets value of the extension
*
* @param theExtension The extension to set the value on
* @param theExtensionType Element type of the extension
* @param theValue The value to set
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBaseExtension theExtension, String theExtensionType, String theValue) {
theExtension.setValue(TerserUtil.newElement(theFhirContext, theExtensionType, theValue));
}
/**
* Sets or replaces existing extension with the specified value as a string
* *
* @param theBase The resource to update extension on * @param theBase The resource to update extension on
* @param theUrl Extension URL * @param theUrl Extension URL
@ -125,4 +161,18 @@ public class ExtensionUtil {
setExtension(theFhirContext, ext, theValue); setExtension(theFhirContext, ext, theValue);
} }
/**
* Sets or replaces existing extension with the specified value
*
* @param theBase The resource to update extension on
* @param theUrl Extension URL
* @param theValueType Type of the value to set in the extension
* @param theValue Extension value
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBase theBase, String theUrl, String theValueType, String theValue) {
IBaseExtension ext = getOrCreateExtension(theBase, theUrl);
setExtension(theFhirContext, ext, theValue);
}
} }

View File

@ -287,7 +287,7 @@ public final class TerserUtil {
public static void setFieldByFhirPath(FhirTerser theTerser, String theFhirPath, IBaseResource theResource, IBase theValue) { public static void setFieldByFhirPath(FhirTerser theTerser, String theFhirPath, IBaseResource theResource, IBase theValue) {
List<IBase> theFromFieldValues = theTerser.getValues(theResource, theFhirPath, true, false); List<IBase> theFromFieldValues = theTerser.getValues(theResource, theFhirPath, true, false);
for (IBase theFromFieldValue : theFromFieldValues) { for (IBase theFromFieldValue : theFromFieldValues) {
theTerser.cloneInto(theFromFieldValue, theValue, true); theTerser.cloneInto(theValue, theFromFieldValue, true);
} }
} }

View File

@ -85,8 +85,25 @@ public class TerserUtilHelper {
return this; return this;
} }
/**
* Sets field at the specified FHIR path
*
* @param theField The FHIR Path to set the values at
* @param theValue The string value to be set
* @return Returns current instance
*/
public TerserUtilHelper setField(String theField, String theFieldType, String theValue) {
IBase value = newElement(theFieldType, theValue);
TerserUtil.setFieldByFhirPath(getTerser(), theField, myResource, value);
return this;
}
protected IBase newStringElement(String theValue) { protected IBase newStringElement(String theValue) {
IBase value = TerserUtil.newElement(myContext, "string", theValue); return newElement("string", theValue);
}
protected IBase newElement(String theElementType, String theValue) {
IBase value = TerserUtil.newElement(myContext, theElementType, theValue);
return value; return value;
} }

View File

@ -1,6 +1,7 @@
package ca.uhn.fhir.util; package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.instance.model.api.IBaseDatatype;
import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Patient;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -21,4 +22,17 @@ class ExtensionUtilTest {
ExtensionUtil.setExtension(ourFhirContext, p1, EXT_URL, "value"); ExtensionUtil.setExtension(ourFhirContext, p1, EXT_URL, "value");
assertTrue(ExtensionUtil.hasExtension(p1, EXT_URL)); assertTrue(ExtensionUtil.hasExtension(p1, EXT_URL));
} }
@Test
void testExtensionTypesWork() {
Patient p1 = new Patient();
assertFalse(ExtensionUtil.hasExtension(p1, EXT_URL));
ExtensionUtil.setExtension(ourFhirContext, p1, EXT_URL, "integer", "1");
assertTrue(ExtensionUtil.hasExtension(p1, EXT_URL));
assertEquals(1, ExtensionUtil.getExtensions(p1, EXT_URL).size());
IBaseDatatype ext = ExtensionUtil.getExtension(p1, EXT_URL).getValue();
assertEquals("1", ext.toString());
}
} }

View File

@ -3,11 +3,15 @@ package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.context.RuntimeResourceDefinition;
import org.hl7.fhir.r4.model.DateTimeType; import org.hl7.fhir.r4.model.DateTimeType;
import org.hl7.fhir.r4.model.DateType;
import org.hl7.fhir.r4.model.Enumerations;
import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Patient;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.GregorianCalendar;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -53,6 +57,21 @@ class TerserUtilTest {
assertFalse(id1.equals(id2)); assertFalse(id1.equals(id2));
} }
@Test
void testSetFieldsViaHelper() {
TerserUtilHelper p1Helper = TerserUtilHelper.newHelper(ourFhirContext, "Patient");
p1Helper.setField("active", "boolean", "true");
p1Helper.setField("birthDate", "date", "1999-01-01");
p1Helper.setField("gender", "code", "male");
Patient p = p1Helper.getResource();
assertTrue(p.getActive());
assertEquals(Enumerations.AdministrativeGender.MALE, p.getGender());
DateType check = TerserUtil.newElement(ourFhirContext, "date", "1999-01-01");
assertEquals(check.getValue(), p.getBirthDate());
}
@Test @Test
void testFieldExists() { void testFieldExists() {
assertTrue(TerserUtil.fieldExists(ourFhirContext, "identifier", TerserUtil.newResource(ourFhirContext, "Patient"))); assertTrue(TerserUtil.fieldExists(ourFhirContext, "identifier", TerserUtil.newResource(ourFhirContext, "Patient")));