Add ability to generate named parametrrs in a version independent way

This commit is contained in:
James Agnew 2019-02-15 16:25:44 -05:00
parent 1311ded7de
commit 87ed00a002
3 changed files with 40 additions and 4 deletions

View File

@ -20,10 +20,7 @@ package ca.uhn.fhir.util;
* #L% * #L%
*/ */
import ca.uhn.fhir.context.BaseRuntimeChildDefinition; import ca.uhn.fhir.context.*;
import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeResourceDefinition;
import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.model.primitive.StringDt;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
@ -107,6 +104,25 @@ public class ParametersUtil {
addClientParameter(theContext, theValue, theParameters, paramChild, paramChildElem, theName); addClientParameter(theContext, theValue, theParameters, paramChild, paramChildElem, theName);
} }
/**
* Add a paratemer value to a Parameters resource
*
* @param theContext The FhirContext
* @param theParameters The Parameters resource
* @param theName The parameter name
* @param thePrimitiveDatatype The datatype, e.g. "string", or "uri"
* @param theValue The value
*/
public static void addParameterToParameters(FhirContext theContext, IBaseParameters theParameters, String theName, String thePrimitiveDatatype, String theValue) {
Validate.notBlank(thePrimitiveDatatype, "thePrimitiveDatatype must not be null or empty");
BaseRuntimeElementDefinition<?> datatypeDef = theContext.getElementDefinition(thePrimitiveDatatype);
IPrimitiveType<?> value = (IPrimitiveType<?>) datatypeDef.newInstance();
value.setValueAsString(theValue);
addParameterToParameters(theContext, theParameters, theName, value);
}
private static IBase createParameterRepetition(FhirContext theContext, IBaseResource theTargetResource, BaseRuntimeChildDefinition paramChild, BaseRuntimeElementCompositeDefinition<?> paramChildElem, String theName) { private static IBase createParameterRepetition(FhirContext theContext, IBaseResource theTargetResource, BaseRuntimeChildDefinition paramChild, BaseRuntimeElementCompositeDefinition<?> paramChildElem, String theName) {
IBase parameter = paramChildElem.newInstance(); IBase parameter = paramChildElem.newInstance();
paramChild.getMutator().addValue(theTargetResource, parameter); paramChild.getMutator().addValue(theTargetResource, parameter);

View File

@ -3,14 +3,29 @@ package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import org.hamcrest.MatcherAssert; import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.hl7.fhir.instance.model.api.IBaseParameters;
import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.Parameters;
import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.StringType;
import org.junit.Test; import org.junit.Test;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals;
public class ParametersUtilR4Test { public class ParametersUtilR4Test {
@Test
public void testCreateParameters() {
FhirContext ctx = FhirContext.forR4();
IBaseParameters parameters = ParametersUtil.newInstance(ctx);
ParametersUtil.addParameterToParameters(ctx, parameters, "someString", "string", "someStringValue");
ParametersUtil.addParameterToParameters(ctx, parameters, "someDate", "date", "2019");
String encoded = ctx.newJsonParser().encodeResourceToString(parameters);
assertEquals("{\"resourceType\":\"Parameters\",\"parameter\":[{\"name\":\"someString\",\"valueString\":\"someStringValue\"},{\"name\":\"someDate\",\"valueDate\":\"2019\"}]}", encoded);
}
@Test @Test
public void testGetValues(){ public void testGetValues(){
Parameters p = new Parameters(); Parameters p = new Parameters();

View File

@ -43,6 +43,11 @@
A Google Analytics script fragment was leftover in the hapi-fhir-jpaserver A Google Analytics script fragment was leftover in the hapi-fhir-jpaserver
example and starter projects. Thanks to Patrick Werner for removing these! example and starter projects. Thanks to Patrick Werner for removing these!
</action> </action>
<action type="add">
ParametersUtil now has a utility method that can be used to add parameter values
using the string name of the datatype (e.g. "dateTime") in order to help
building Parameters resources in a version-independent way.
</action>
</release> </release>
<release version="3.7.0" date="2019-02-06" description="Gale"> <release version="3.7.0" date="2019-02-06" description="Gale">
<action type="add"> <action type="add">