001package ca.uhn.fhir.context; 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.lang.reflect.Field; 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.HashSet; 027import java.util.List; 028import java.util.Map; 029import java.util.Set; 030 031import org.hl7.fhir.instance.model.api.IBase; 032import org.hl7.fhir.instance.model.api.IBaseReference; 033import org.hl7.fhir.instance.model.api.IBaseResource; 034 035import ca.uhn.fhir.model.api.annotation.Child; 036import ca.uhn.fhir.model.api.annotation.Description; 037 038public class RuntimeChildResourceDefinition extends BaseRuntimeDeclaredChildDefinition { 039 040 private BaseRuntimeElementDefinition<?> myRuntimeDef; 041 private List<Class<? extends IBaseResource>> myResourceTypes; 042 private Set<String> myValidChildNames; 043 044 /** 045 * Constructor 046 */ 047 public RuntimeChildResourceDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation, List<Class<? extends IBaseResource>> theResourceTypes) { 048 super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName); 049 myResourceTypes = theResourceTypes; 050 051 if (theResourceTypes == null || theResourceTypes.isEmpty()) { 052 myResourceTypes = new ArrayList<Class<? extends IBaseResource>>(); 053 myResourceTypes.add(IBaseResource.class); 054// throw new ConfigurationException("Field '" + theField.getName() + "' on type '" + theField.getDeclaringClass().getCanonicalName() + "' has no resource types noted"); 055 } 056 } 057 058 @Override 059 public String getChildNameByDatatype(Class<? extends IBase> theDatatype) { 060 if (IBaseReference.class.isAssignableFrom(theDatatype)) { 061 return getElementName(); 062 } 063 return null; 064 } 065 066 @Override 067 public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) { 068 if (IBaseReference.class.isAssignableFrom(theDatatype)) { 069 return myRuntimeDef; 070 } 071 return null; 072 } 073 074 @Override 075 public Set<String> getValidChildNames() { 076 return myValidChildNames; 077 } 078 079 @Override 080 public BaseRuntimeElementDefinition<?> getChildByName(String theName) { 081 return myRuntimeDef; 082 } 083 084 @Override 085 void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) { 086 myRuntimeDef = findResourceReferenceDefinition(theClassToElementDefinitions); 087 088 myValidChildNames = new HashSet<String>(); 089 myValidChildNames.add(getElementName()); 090 091 /* 092 * [elementName]Resource is not actually valid FHIR but we've encountered it in the wild 093 * so we'll accept it just to be nice 094 */ 095 myValidChildNames.add(getElementName() + "Resource"); 096 097 /* 098 * Below has been disabled- We used to allow field names to contain the name of the resource 099 * that they accepted. This wasn't valid but we accepted it just to be flexible because there 100 * were some bad examples containing this. This causes conflicts with actual field names in 101 * recent definitions though, so it has been disabled as of HAPI 0.9 102 */ 103// for (Class<? extends IBaseResource> next : myResourceTypes) { 104// if (next == IResource.class) { 105// for (Entry<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> nextEntry : theClassToElementDefinitions.entrySet()) { 106// if (IResource.class.isAssignableFrom(nextEntry.getKey())) { 107// RuntimeResourceDefinition nextDef = (RuntimeResourceDefinition) nextEntry.getValue(); 108// myValidChildNames.add(getElementName() + nextDef.getName()); 109// } 110// } 111// } 112// else { 113// RuntimeResourceDefinition nextDef = (RuntimeResourceDefinition) theClassToElementDefinitions.get(next); 114// if (nextDef == null) { 115// throw new ConfigurationException("Can't find child of type: " + next.getCanonicalName() + " in " + getField().getDeclaringClass()); 116// } 117// myValidChildNames.add(getElementName() + nextDef.getName()); 118// } 119// } 120 121 myResourceTypes = Collections.unmodifiableList(myResourceTypes); 122 myValidChildNames = Collections.unmodifiableSet(myValidChildNames); 123 } 124 125 public List<Class<? extends IBaseResource>> getResourceTypes() { 126 return myResourceTypes; 127 } 128 129 @Override 130 public String toString() { 131 return getClass().getSimpleName() + "[" + getElementName() + "]"; 132 } 133}