001package ca.uhn.fhir.context;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
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<>();
053                        myResourceTypes.add(IBaseResource.class);
054                }
055        }
056
057        @Override
058        public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
059                if (IBaseReference.class.isAssignableFrom(theDatatype)) {
060                        return getElementName();
061                }
062                return null;
063        }
064
065        @Override
066        public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) {
067                if (IBaseReference.class.isAssignableFrom(theDatatype)) {
068                        return myRuntimeDef;
069                }
070                return null;
071        }
072
073        @Override
074        public Set<String> getValidChildNames() {
075                return myValidChildNames;
076        }
077
078        @Override
079        public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
080                return myRuntimeDef;
081        }
082
083        @Override
084        void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
085                myRuntimeDef = findResourceReferenceDefinition(theClassToElementDefinitions);
086
087                myValidChildNames = new HashSet<String>();
088                myValidChildNames.add(getElementName());
089                
090                /*
091                 * [elementName]Resource is not actually valid FHIR but we've encountered it in the wild
092                 * so we'll accept it just to be nice
093                 */
094                myValidChildNames.add(getElementName() + "Resource");
095
096                /*
097                 * Below has been disabled- We used to allow field names to contain the name of the resource
098                 * that they accepted. This wasn't valid but we accepted it just to be flexible because there
099                 * were some bad examples containing this. This causes conflicts with actual field names in 
100                 * recent definitions though, so it has been disabled as of HAPI 0.9 
101                 */
102//              for (Class<? extends IBaseResource> next : myResourceTypes) {
103//                      if (next == IResource.class) {
104//                              for (Entry<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> nextEntry : theClassToElementDefinitions.entrySet()) {
105//                                      if (IResource.class.isAssignableFrom(nextEntry.getKey())) {
106//                                              RuntimeResourceDefinition nextDef = (RuntimeResourceDefinition) nextEntry.getValue();
107//                                              myValidChildNames.add(getElementName() + nextDef.getName());
108//                                      }
109//                              }
110//                      } 
111//                      else {
112//                              RuntimeResourceDefinition nextDef = (RuntimeResourceDefinition) theClassToElementDefinitions.get(next);
113//                              if (nextDef == null) {
114//                                      throw new ConfigurationException("Can't find child of type: " + next.getCanonicalName() + " in " + getField().getDeclaringClass());
115//                              }
116//                              myValidChildNames.add(getElementName() + nextDef.getName());
117//                      }
118//              }
119
120                myResourceTypes = Collections.unmodifiableList(myResourceTypes);
121                myValidChildNames = Collections.unmodifiableSet(myValidChildNames);
122        }
123
124        public List<Class<? extends IBaseResource>> getResourceTypes() {
125                return myResourceTypes;
126        }
127
128        @Override
129        public String toString() {
130                return getClass().getSimpleName() + "[" + getElementName() + "]";
131        }
132}