001package ca.uhn.fhir.util; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2017 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 java.util.Collection; 024 025import org.apache.commons.lang3.Validate; 026import org.hl7.fhir.instance.model.api.IBase; 027import org.hl7.fhir.instance.model.api.IBaseDatatype; 028import org.hl7.fhir.instance.model.api.IBaseParameters; 029import org.hl7.fhir.instance.model.api.IBaseResource; 030import org.hl7.fhir.instance.model.api.IPrimitiveType; 031 032import ca.uhn.fhir.context.BaseRuntimeChildDefinition; 033import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition; 034import ca.uhn.fhir.context.FhirContext; 035import ca.uhn.fhir.context.RuntimeResourceDefinition; 036import ca.uhn.fhir.model.primitive.StringDt; 037 038/** 039 * Utilities for dealing with parameters resources 040 */ 041public class ParametersUtil { 042 043 public static void addParameterToParameters(FhirContext theContext, IBaseResource theTargetResource, Object sourceClientArgument, String theName) { 044 RuntimeResourceDefinition def = theContext.getResourceDefinition(theTargetResource); 045 BaseRuntimeChildDefinition paramChild = def.getChildByName("parameter"); 046 BaseRuntimeElementCompositeDefinition<?> paramChildElem = (BaseRuntimeElementCompositeDefinition<?>) paramChild.getChildByName("parameter"); 047 048 addClientParameter(theContext, sourceClientArgument, theTargetResource, paramChild, paramChildElem, theName); 049 } 050 051 private static void addClientParameter(FhirContext theContext, Object theSourceClientArgument, IBaseResource theTargetResource, BaseRuntimeChildDefinition paramChild, BaseRuntimeElementCompositeDefinition<?> paramChildElem, String theName) { 052 if (theSourceClientArgument instanceof IBaseResource) { 053 IBase parameter = createParameterRepetition(theContext, theTargetResource, paramChild, paramChildElem, theName); 054 paramChildElem.getChildByName("resource").getMutator().addValue(parameter, (IBaseResource) theSourceClientArgument); 055 } else if (theSourceClientArgument instanceof IBaseDatatype) { 056 IBase parameter = createParameterRepetition(theContext, theTargetResource, paramChild, paramChildElem, theName); 057 paramChildElem.getChildByName("value[x]").getMutator().addValue(parameter, (IBaseDatatype) theSourceClientArgument); 058 } else if (theSourceClientArgument instanceof Collection) { 059 Collection<?> collection = (Collection<?>) theSourceClientArgument; 060 for (Object next : collection) { 061 addClientParameter(theContext, next, theTargetResource, paramChild, paramChildElem, theName); 062 } 063 } else { 064 throw new IllegalArgumentException("Don't know how to handle value of type " + theSourceClientArgument.getClass() + " for paramater " + theName); 065 } 066 } 067 068 private static IBase createParameterRepetition(FhirContext theContext, IBaseResource theTargetResource, BaseRuntimeChildDefinition paramChild, BaseRuntimeElementCompositeDefinition<?> paramChildElem, String theName) { 069 IBase parameter = paramChildElem.newInstance(); 070 paramChild.getMutator().addValue(theTargetResource, parameter); 071 IPrimitiveType<?> value; 072 value = createString(theContext, theName); 073 paramChildElem.getChildByName("name").getMutator().addValue(parameter, value); 074 return parameter; 075 } 076 077 public static IPrimitiveType<?> createString(FhirContext theContext, String theValue) { 078 IPrimitiveType<?> value; 079 if (theContext.getVersion().getVersion().isRi()) { 080 value = (IPrimitiveType<?>) theContext.getElementDefinition("string").newInstance(theValue); 081 } else { 082 value = new StringDt(theValue); 083 } 084 return value; 085 } 086 087 public static IBaseParameters newInstance(FhirContext theContext) { 088 Validate.notNull(theContext, "theContext must not be null"); 089 return (IBaseParameters) theContext.getResourceDefinition("Parameters").newInstance(); 090 } 091}