Added support for additional types
This commit is contained in:
parent
ef97eb22fb
commit
f9f703abe2
|
@ -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.IBaseHasExtensions;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
IBaseHasExtensions baseHasExtensions = validateExtensionSupport(theBase);
|
||||
IBaseExtension extension = getExtensionByUrl(baseHasExtensions, theUrl);
|
||||
IBaseExtension extension = getExtension(baseHasExtensions, theUrl);
|
||||
if (extension == null) {
|
||||
extension = baseHasExtensions.addExtension();
|
||||
extension.setUrl(theUrl);
|
||||
|
@ -72,7 +75,7 @@ public class ExtensionUtil {
|
|||
return false;
|
||||
}
|
||||
|
||||
return getExtensionByUrl(baseHasExtensions, theExtensionUrl) != null;
|
||||
return getExtension(baseHasExtensions, theExtensionUrl) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,14 +89,21 @@ public class ExtensionUtil {
|
|||
if (!hasExtension(theBase, theExtensionUrl)) {
|
||||
return false;
|
||||
}
|
||||
IBaseDatatype value = getExtensionByUrl((IBaseHasExtensions) theBase, theExtensionUrl).getValue();
|
||||
IBaseDatatype value = getExtension((IBaseHasExtensions) theBase, theExtensionUrl).getValue();
|
||||
if (value == null) {
|
||||
return theExtensionValue == null;
|
||||
}
|
||||
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()
|
||||
.stream()
|
||||
.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 theValue The value to set
|
||||
* @param theFhirContext The context containing FHIR resource definitions
|
||||
*/
|
||||
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 theUrl Extension URL
|
||||
|
@ -125,4 +161,18 @@ public class ExtensionUtil {
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -287,7 +287,7 @@ public final class TerserUtil {
|
|||
public static void setFieldByFhirPath(FhirTerser theTerser, String theFhirPath, IBaseResource theResource, IBase theValue) {
|
||||
List<IBase> theFromFieldValues = theTerser.getValues(theResource, theFhirPath, true, false);
|
||||
for (IBase theFromFieldValue : theFromFieldValues) {
|
||||
theTerser.cloneInto(theFromFieldValue, theValue, true);
|
||||
theTerser.cloneInto(theValue, theFromFieldValue, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -85,8 +85,25 @@ public class TerserUtilHelper {
|
|||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package ca.uhn.fhir.util;
|
||||
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
import org.hl7.fhir.instance.model.api.IBaseDatatype;
|
||||
import org.hl7.fhir.r4.model.Patient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -21,4 +22,17 @@ class ExtensionUtilTest {
|
|||
ExtensionUtil.setExtension(ourFhirContext, p1, EXT_URL, "value");
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,11 +3,15 @@ package ca.uhn.fhir.util;
|
|||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.context.RuntimeResourceDefinition;
|
||||
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.Identifier;
|
||||
import org.hl7.fhir.r4.model.Patient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -53,6 +57,21 @@ class TerserUtilTest {
|
|||
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
|
||||
void testFieldExists() {
|
||||
assertTrue(TerserUtil.fieldExists(ourFhirContext, "identifier", TerserUtil.newResource(ourFhirContext, "Patient")));
|
||||
|
|
Loading…
Reference in New Issue