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 IBaseParameters newInstance(FhirContext theContext) { 146 Validate.notNull(theContext, "theContext must not be null"); 147 return (IBaseParameters) theContext.getResourceDefinition("Parameters").newInstance(); 148 } 149 150}