001package ca.uhn.fhir.util; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2019 University Health Network 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import ca.uhn.fhir.context.*; 024import ca.uhn.fhir.model.primitive.StringDt; 025import org.apache.commons.lang3.StringUtils; 026import org.apache.commons.lang3.Validate; 027import org.hl7.fhir.instance.model.api.*; 028 029import java.util.ArrayList; 030import java.util.Collection; 031import java.util.List; 032import java.util.Optional; 033 034/** 035 * Utilities for dealing with parameters resources in a version indepenedent way 036 */ 037public class ParametersUtil { 038 039 public static List<String> getNamedParameterValuesAsString(FhirContext theCtx, IBaseParameters theParameters, String theParameterName) { 040 Validate.notNull(theParameters, "theParameters must not be null"); 041 RuntimeResourceDefinition resDef = theCtx.getResourceDefinition(theParameters.getClass()); 042 BaseRuntimeChildDefinition parameterChild = resDef.getChildByName("parameter"); 043 List<IBase> parameterReps = parameterChild.getAccessor().getValues(theParameters); 044 045 List<String> retVal = new ArrayList<>(); 046 047 for (IBase nextParameter : parameterReps) { 048 BaseRuntimeElementCompositeDefinition<?> nextParameterDef = (BaseRuntimeElementCompositeDefinition<?>) theCtx.getElementDefinition(nextParameter.getClass()); 049 BaseRuntimeChildDefinition nameChild = nextParameterDef.getChildByName("name"); 050 List<IBase> nameValues = nameChild.getAccessor().getValues(nextParameter); 051 Optional<? extends IPrimitiveType<?>> nameValue = nameValues 052 .stream() 053 .filter(t -> t instanceof IPrimitiveType<?>) 054 .map(t -> ((IPrimitiveType<?>) t)) 055 .findFirst(); 056 if (!nameValue.isPresent() || !theParameterName.equals(nameValue.get().getValueAsString())) { 057 continue; 058 } 059 060 BaseRuntimeChildDefinition valueChild = nextParameterDef.getChildByName("value[x]"); 061 List<IBase> valueValues = valueChild.getAccessor().getValues(nextParameter); 062 valueValues 063 .stream() 064 .filter(t -> t instanceof IPrimitiveType<?>) 065 .map(t -> ((IPrimitiveType<?>) t).getValueAsString()) 066 .filter(StringUtils::isNotBlank) 067 .forEach(retVal::add); 068 069 } 070 071 return retVal; 072 } 073 074 private static void addClientParameter(FhirContext theContext, Object theValue, IBaseResource theTargetResource, BaseRuntimeChildDefinition paramChild, BaseRuntimeElementCompositeDefinition<?> paramChildElem, String theName) { 075 if (theValue instanceof IBaseResource) { 076 IBase parameter = createParameterRepetition(theContext, theTargetResource, paramChild, paramChildElem, theName); 077 paramChildElem.getChildByName("resource").getMutator().addValue(parameter, (IBaseResource) theValue); 078 } else if (theValue instanceof IBaseDatatype) { 079 IBase parameter = createParameterRepetition(theContext, theTargetResource, paramChild, paramChildElem, theName); 080 paramChildElem.getChildByName("value[x]").getMutator().addValue(parameter, (IBaseDatatype) theValue); 081 } else if (theValue instanceof Collection) { 082 Collection<?> collection = (Collection<?>) theValue; 083 for (Object next : collection) { 084 addClientParameter(theContext, next, theTargetResource, paramChild, paramChildElem, theName); 085 } 086 } else { 087 throw new IllegalArgumentException("Don't know how to handle value of type " + theValue.getClass() + " for parameter " + theName); 088 } 089 } 090 091 /** 092 * Add a paratemer value to a Parameters resource 093 * 094 * @param theContext The FhirContext 095 * @param theParameters The Parameters resource 096 * @param theName The parametr name 097 * @param theValue The parameter value (can be a {@link IBaseResource resource} or a {@link IBaseDatatype datatype}) 098 */ 099 public static void addParameterToParameters(FhirContext theContext, IBaseParameters theParameters, String theName, Object theValue) { 100 RuntimeResourceDefinition def = theContext.getResourceDefinition(theParameters); 101 BaseRuntimeChildDefinition paramChild = def.getChildByName("parameter"); 102 BaseRuntimeElementCompositeDefinition<?> paramChildElem = (BaseRuntimeElementCompositeDefinition<?>) paramChild.getChildByName("parameter"); 103 104 addClientParameter(theContext, theValue, theParameters, paramChild, paramChildElem, theName); 105 } 106 107 /** 108 * Add a paratemer value to a Parameters resource 109 * 110 * @param theContext The FhirContext 111 * @param theParameters The Parameters resource 112 * @param theName The parameter name 113 * @param thePrimitiveDatatype The datatype, e.g. "string", or "uri" 114 * @param theValue The value 115 */ 116 public static void addParameterToParameters(FhirContext theContext, IBaseParameters theParameters, String theName, String thePrimitiveDatatype, String theValue) { 117 Validate.notBlank(thePrimitiveDatatype, "thePrimitiveDatatype must not be null or empty"); 118 119 BaseRuntimeElementDefinition<?> datatypeDef = theContext.getElementDefinition(thePrimitiveDatatype); 120 IPrimitiveType<?> value = (IPrimitiveType<?>) datatypeDef.newInstance(); 121 value.setValueAsString(theValue); 122 123 addParameterToParameters(theContext, theParameters, theName, value); 124 } 125 126 private static IBase createParameterRepetition(FhirContext theContext, IBaseResource theTargetResource, BaseRuntimeChildDefinition paramChild, BaseRuntimeElementCompositeDefinition<?> paramChildElem, String theName) { 127 IBase parameter = paramChildElem.newInstance(); 128 paramChild.getMutator().addValue(theTargetResource, parameter); 129 IPrimitiveType<?> value; 130 value = createString(theContext, theName); 131 paramChildElem.getChildByName("name").getMutator().addValue(parameter, value); 132 return parameter; 133 } 134 135 public static IPrimitiveType<?> createString(FhirContext theContext, String theValue) { 136 IPrimitiveType<?> value; 137 if (theContext.getVersion().getVersion().isRi()) { 138 value = (IPrimitiveType<?>) theContext.getElementDefinition("string").newInstance(theValue); 139 } else { 140 value = new StringDt(theValue); 141 } 142 return value; 143 } 144 145 public static IPrimitiveType<?> createUri(FhirContext theContext, String theValue) { 146 IPrimitiveType<?> value = (IPrimitiveType<?>) theContext.getElementDefinition("uri").newInstance(theValue); 147 return value; 148 } 149 150 public static IPrimitiveType<?> createCode(FhirContext theContext, String theValue) { 151 IPrimitiveType<?> value = (IPrimitiveType<?>) theContext.getElementDefinition("code").newInstance(theValue); 152 return value; 153 } 154 155 public static IBaseParameters newInstance(FhirContext theContext) { 156 Validate.notNull(theContext, "theContext must not be null"); 157 return (IBaseParameters) theContext.getResourceDefinition("Parameters").newInstance(); 158 } 159 160 @SuppressWarnings("unchecked") 161 public static void addParameterToParametersBoolean(FhirContext theCtx, IBaseParameters theParameters, String theName, boolean theValue) { 162 IPrimitiveType<Boolean> value = (IPrimitiveType<Boolean>) theCtx.getElementDefinition("boolean").newInstance(); 163 value.setValue(theValue); 164 addParameterToParameters(theCtx, theParameters, theName, value); 165 166 } 167 168 @SuppressWarnings("unchecked") 169 public static void addParameterToParametersCode(FhirContext theCtx, IBaseParameters theParameters, String theName, String theValue) { 170 IPrimitiveType<String> value = (IPrimitiveType<String>) theCtx.getElementDefinition("code").newInstance(); 171 value.setValue(theValue); 172 addParameterToParameters(theCtx, theParameters, theName, value); 173 } 174 175 @SuppressWarnings("unchecked") 176 public static void addParameterToParametersInteger(FhirContext theCtx, IBaseParameters theParameters, String theName, int theValue) { 177 IPrimitiveType<Integer> count = (IPrimitiveType<Integer>) theCtx.getElementDefinition("integer").newInstance(); 178 count.setValue(theValue); 179 addParameterToParameters(theCtx, theParameters, theName, count); 180 181 } 182 183 public static void addParameterToParametersReference(FhirContext theCtx, IBaseParameters theParameters, String theName, String theReference) { 184 IBaseReference target = (IBaseReference) theCtx.getElementDefinition("reference").newInstance(); 185 target.setReference(theReference); 186 addParameterToParameters(theCtx, theParameters, theName, target); 187 } 188 189 @SuppressWarnings("unchecked") 190 public static void addParameterToParametersString(FhirContext theCtx, IBaseParameters theParameters, String theName, String theValue) { 191 IPrimitiveType<String> value = (IPrimitiveType<String>) theCtx.getElementDefinition("string").newInstance(); 192 value.setValue(theValue); 193 addParameterToParameters(theCtx, theParameters, theName, value); 194 } 195 196 @SuppressWarnings("unchecked") 197 public static void addParameterToParametersUri(FhirContext theCtx, IBaseParameters theParameters, String theName, String theValue) { 198 IPrimitiveType<String> value = (IPrimitiveType<String>) theCtx.getElementDefinition("uri").newInstance(); 199 value.setValue(theValue); 200 addParameterToParameters(theCtx, theParameters, theName, value); 201 202 } 203 204 /** 205 * Add a parameter with no value (typically because we'll be adding sub-parameters) 206 */ 207 public static IBase addParameterToParameters(FhirContext theContext, IBaseParameters theParameters, String theName) { 208 RuntimeResourceDefinition def = theContext.getResourceDefinition(theParameters); 209 BaseRuntimeChildDefinition paramChild = def.getChildByName("parameter"); 210 BaseRuntimeElementCompositeDefinition<?> paramChildElem = (BaseRuntimeElementCompositeDefinition<?>) paramChild.getChildByName("parameter"); 211 212 return createParameterRepetition(theContext, theParameters, paramChild, paramChildElem, theName); 213 } 214 215 public static void addPartCode(FhirContext theContext, IBase theParameter, String theName, String theCode) { 216 IPrimitiveType<String> value = (IPrimitiveType<String>) theContext.getElementDefinition("code").newInstance(); 217 value.setValue(theCode); 218 219 addPart(theContext, theParameter, theName, value); 220 } 221 222 public static void addPartString(FhirContext theContext, IBase theParameter, String theName, String theValue) { 223 IPrimitiveType<String> value = (IPrimitiveType<String>) theContext.getElementDefinition("string").newInstance(); 224 value.setValue(theValue); 225 226 addPart(theContext, theParameter, theName, value); 227 } 228 229 public static void addPartCoding(FhirContext theContext, IBase theParameter, String theName, String theSystem, String theCode, String theDisplay) { 230 IBase coding = theContext.getElementDefinition("coding").newInstance(); 231 232 BaseRuntimeElementCompositeDefinition<?> codingDef = (BaseRuntimeElementCompositeDefinition<?>) theContext.getElementDefinition(coding.getClass()); 233 codingDef.getChildByName("system").getMutator().addValue(coding, createUri(theContext, theSystem)); 234 codingDef.getChildByName("code").getMutator().addValue(coding, createCode(theContext, theCode)); 235 codingDef.getChildByName("display").getMutator().addValue(coding, createString(theContext, theDisplay)); 236 237 addPart(theContext, theParameter, theName, coding); 238 } 239 240 private static void addPart(FhirContext theContext, IBase theParameter, String theName, IBase theValue) { 241 BaseRuntimeElementCompositeDefinition<?> def = (BaseRuntimeElementCompositeDefinition<?>) theContext.getElementDefinition(theParameter.getClass()); 242 BaseRuntimeChildDefinition partChild = def.getChildByName("part"); 243 244 BaseRuntimeElementCompositeDefinition<?> partChildElem = (BaseRuntimeElementCompositeDefinition<?>) partChild.getChildByName("part"); 245 IBase part = partChildElem.newInstance(); 246 partChild.getMutator().addValue(theParameter, part); 247 248 IPrimitiveType<String> name = (IPrimitiveType<String>) theContext.getElementDefinition("string").newInstance(); 249 name.setValue(theName); 250 partChildElem.getChildByName("name").getMutator().addValue(part, name); 251 252 partChildElem.getChildByName("value[x]").getMutator().addValue(part, theValue); 253 } 254}