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