Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Parameters utility for primitive types #613

Merged
merged 9 commits into from
Sep 26, 2022
193 changes: 193 additions & 0 deletions core/src/main/java/org/opencds/cqf/ruler/utility/Parameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package org.opencds.cqf.ruler.utility;

import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.util.ParametersUtil;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseDatatype;
import org.hl7.fhir.instance.model.api.IBaseParameters;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;

import java.util.List;
import java.util.Objects;

import static com.google.common.base.Preconditions.checkNotNull;

public class Parameters {

private Parameters() {
}

private static BaseRuntimeChildDefinition getParameterChild(FhirContext fhirContext) {
return fhirContext.getResourceDefinition("Parameters").getChildByName("parameter");
}

private static BaseRuntimeElementDefinition<?> getParameterElement(FhirContext fhirContext) {
return getParameterChild(fhirContext).getChildByName("parameter");
}

private static BaseRuntimeChildDefinition.IMutator getValueMutator(FhirContext fhirContext) {
return getParameterElement(fhirContext)
.getChildByName("value[x]").getMutator();
}

private static void validateNameAndValue(String name, Object value) {
checkNotNull(name);
checkNotNull(value);
}

public static IBaseParameters newParameters(FhirContext fhirContext, IIdType theId, IBase... parts) {
checkNotNull(theId);
IBaseParameters newParameters = ParametersUtil.newInstance(fhirContext);
newParameters.setId(theId);
BaseRuntimeChildDefinition.IMutator mutator = getParameterChild(fhirContext).getMutator();
for (IBase part : parts) {
mutator.addValue(newParameters, part);
}
return newParameters;
}

public static IBaseParameters newParameters(FhirContext fhirContext, String theId, IBase... parts) {
checkNotNull(theId);
IIdType id = (IIdType) Objects.requireNonNull(fhirContext.getElementDefinition("id")).newInstance();
id.setValue(theId);
return newParameters(fhirContext, id, parts);
}

public static IBaseParameters newParameters(FhirContext fhirContext, IBase... parts) {
IBaseParameters newParameters = ParametersUtil.newInstance(fhirContext);
BaseRuntimeChildDefinition.IMutator mutator = getParameterChild(fhirContext).getMutator();
for (IBase part : parts) {
mutator.addValue(newParameters, part);
}
return newParameters;
}

public static IBase newPart(FhirContext fhirContext, String name, IBase... parts) {
checkNotNull(name);
BaseRuntimeChildDefinition.IMutator nameMutator = getParameterElement(fhirContext)
.getChildByName("name").getMutator();
BaseRuntimeChildDefinition.IMutator partMutator = getParameterElement(fhirContext)
.getChildByName("part").getMutator();
IBase parameterBase = getParameterElement(fhirContext).newInstance();
IBase theName = Objects.requireNonNull(fhirContext.getElementDefinition("string")).newInstance(name);
nameMutator.setValue(parameterBase, theName);
for (IBase part : parts) {
partMutator.addValue(parameterBase, part);
}
return parameterBase;
}

public static <T extends IBaseDatatype> IBase newPart(FhirContext fhirContext, Class<T> type,
String name, Object value, IBase... parts) {
validateNameAndValue(name, value);
IBase newPpc = newPart(fhirContext, name, parts);
IBase typeValue = Objects.requireNonNull(fhirContext.getElementDefinition(type)).newInstance(value);
getValueMutator(fhirContext).setValue(newPpc, typeValue);
return newPpc;
}

public static IBase newPart(FhirContext fhirContext, String typeName,
String name, Object value, IBase... parts) {
validateNameAndValue(name, value);
IBase newPpc = newPart(fhirContext, name, parts);
IBase typeValue = Objects.requireNonNull(fhirContext.getElementDefinition(typeName)).newInstance(value.toString());
getValueMutator(fhirContext).setValue(newPpc, typeValue);
return newPpc;
}

public static IBase newPart(FhirContext fhirContext, String name, IBaseResource value, IBase... parts) {
validateNameAndValue(name, value);
IBase newPpc = newPart(fhirContext, name, parts);
getParameterElement(fhirContext).getChildByName("resource").getMutator().setValue(newPpc, value);
return newPpc;
}

public static List<IBase> getPartsByName(FhirContext fhirContext, IBaseResource parameters, String name) {
checkNotNull(parameters);
checkNotNull(name);
return ParametersUtil.getNamedParameters(fhirContext, parameters, name);
}

public static IBase newBase64BinaryPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "base64binary", name, value, parts);
}

public static IBase newBooleanPart(FhirContext fhirContext, String name, boolean value, IBase... parts) {
return newPart(fhirContext, "boolean", name, value, parts);
}

public static IBase newCanonicalPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "canonical", name, value, parts);
}

public static IBase newCodePart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "code", name, value, parts);
}

public static IBase newDatePart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "date", name, value, parts);
}

public static IBase newDateTimePart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "datetime", name, value, parts);
}

public static IBase newDecimalPart(FhirContext fhirContext, String name, double value, IBase... parts) {
return newPart(fhirContext, "decimal", name, value, parts);
}

public static IBase newIdPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "id", name, value, parts);
}

public static IBase newInstantPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "instant", name, value, parts);
}

public static IBase newIntegerPart(FhirContext fhirContext, String name, int value, IBase... parts) {
return newPart(fhirContext, "integer", name, value, parts);
}

public static IBase newInteger64Part(FhirContext fhirContext, String name, long value, IBase... parts) {
return newPart(fhirContext, "integer64", name, value, parts);
}

public static IBase newMarkdownPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "markdown", name, value, parts);
}

public static IBase newOidPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "oid", name, value, parts);
}

public static IBase newPositiveIntPart(FhirContext fhirContext, String name, int value, IBase... parts) {
return newPart(fhirContext, "positiveint", name, value, parts);
}

public static IBase newStringPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "string", name, value, parts);
}

public static IBase newTimePart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "time", name, value, parts);
}

public static IBase newUnsignedIntPart(FhirContext fhirContext, String name, int value, IBase... parts) {
return newPart(fhirContext, "unsignedint", name, value, parts);
}

public static IBase newUriPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "uri", name, value, parts);
}

public static IBase newUrlPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "url", name, value, parts);
}

public static IBase newUuidPart(FhirContext fhirContext, String name, String value, IBase... parts) {
return newPart(fhirContext, "uuid", name, value, parts);
}
}
133 changes: 106 additions & 27 deletions core/src/main/java/org/opencds/cqf/ruler/utility/dstu3/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,87 @@
import java.util.List;
import java.util.stream.Collectors;

import org.hl7.fhir.dstu3.model.Base64BinaryType;
import org.hl7.fhir.dstu3.model.BooleanType;
import org.hl7.fhir.dstu3.model.CodeType;
import org.hl7.fhir.dstu3.model.DateTimeType;
import org.hl7.fhir.dstu3.model.DateType;
import org.hl7.fhir.dstu3.model.DecimalType;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.InstantType;
import org.hl7.fhir.dstu3.model.IntegerType;
import org.hl7.fhir.dstu3.model.OidType;
import org.hl7.fhir.dstu3.model.PositiveIntType;
import org.hl7.fhir.dstu3.model.StringType;
import org.hl7.fhir.dstu3.model.TimeType;
import org.hl7.fhir.dstu3.model.UnsignedIntType;
import org.hl7.fhir.dstu3.model.UriType;
import org.opencds.cqf.ruler.utility.Ids;

public class Parameters {

private Parameters() {
}

public static org.hl7.fhir.dstu3.model.Parameters newParameters(IdType theId,
org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
public static org.hl7.fhir.dstu3.model.Parameters parameters(
IdType theId, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
checkNotNull(theId);

org.hl7.fhir.dstu3.model.Parameters p = newParameters(parts);
org.hl7.fhir.dstu3.model.Parameters p = parameters(parts);
p.setId(theId);
return p;
}

public static org.hl7.fhir.dstu3.model.Parameters newParameters(String theIdPart,
org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
public static org.hl7.fhir.dstu3.model.Parameters parameters(
String theIdPart, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
checkNotNull(theIdPart);

return newParameters((IdType) Ids.newId(org.hl7.fhir.dstu3.model.Parameters.class, theIdPart), parts);
return parameters((IdType) Ids.newId(org.hl7.fhir.dstu3.model.Parameters.class, theIdPart), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters newParameters(
public static org.hl7.fhir.dstu3.model.Parameters parameters(
org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
org.hl7.fhir.dstu3.model.Parameters p = new org.hl7.fhir.dstu3.model.Parameters();
for (org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent c : parts) {
p.addParameter(c);
}

return p;
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent newPart(String name,
String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent part(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
checkNotNull(name);
checkNotNull(value);

org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent c = newPart(name, parts);
org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent c = part(name, parts);
c.setValue(new StringType(value));
return c;
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent newPart(String name,
org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent part(
String name, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
checkNotNull(name);

org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent c = new org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent();
c.setName(name);
for (org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent p : parts) {
c.addPart(p);
}

return c;
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent newPart(String name,
org.hl7.fhir.dstu3.model.Type value,
public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent part(
String name, org.hl7.fhir.dstu3.model.Type value,
org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
checkNotNull(name);
checkNotNull(value);

org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent c = newPart(name, parts);
org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent c = part(name, parts);
c.setValue(value);
return c;
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent newPart(String name,
org.hl7.fhir.dstu3.model.Resource resource,
public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent part(
String name, org.hl7.fhir.dstu3.model.Resource resource,
org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
checkNotNull(name);
checkNotNull(resource);

org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent c = newPart(name, parts);
org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent c = part(name, parts);
c.setResource(resource);
return c;
}
Expand All @@ -89,7 +94,81 @@ public static List<org.hl7.fhir.dstu3.model.Parameters.ParametersParameterCompon
org.hl7.fhir.dstu3.model.Parameters parameters, String name) {
checkNotNull(parameters);
checkNotNull(name);

return parameters.getParameter().stream().filter(x -> name.equals(x.getName())).collect(Collectors.toList());
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent base64BinaryPart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new Base64BinaryType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent booleanPart(
String name, boolean value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new BooleanType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent codePart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new CodeType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent datePart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new DateType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent dateTimePart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new DateTimeType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent decimalPart(
String name, double value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new DecimalType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent idPart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new IdType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent instantPart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new InstantType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent integerPart(
String name, int value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new IntegerType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent oidPart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new OidType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent positiveIntPart(
String name, int value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new PositiveIntType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent stringPart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new StringType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent timePart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new TimeType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent unsignedIntPart(
String name, int value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new UnsignedIntType(value), parts);
}

public static org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent uriPart(
String name, String value, org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent... parts) {
return part(name, new UriType(value), parts);
}
}
Loading