001package ca.uhn.fhir.context;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 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.Collections;
025import java.util.Map;
026import java.util.Set;
027
028import org.hl7.fhir.instance.model.api.IBase;
029
030import ca.uhn.fhir.model.api.annotation.Child;
031import ca.uhn.fhir.model.api.annotation.Description;
032
033public class RuntimeChildResourceBlockDefinition extends BaseRuntimeDeclaredChildDefinition {
034
035//      private RuntimeResourceBlockDefinition myElementDef;
036        private Class<? extends IBase> myResourceBlockType;
037        private FhirContext myContext;
038
039        public RuntimeChildResourceBlockDefinition(FhirContext theContext, Field theField, Child theChildAnnotation, Description theDescriptionAnnotation, String theElementName, Class<? extends IBase> theResourceBlockType) throws ConfigurationException {
040                super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName);
041                myContext = theContext;
042                myResourceBlockType = theResourceBlockType;
043        }
044
045        @Override
046        public BaseRuntimeElementCompositeDefinition getChildByName(String theName) {
047                if (getElementName().equals(theName)) {
048                        return getDefinition();
049                }
050                return null;
051        }
052
053        private BaseRuntimeElementCompositeDefinition getDefinition() {
054                return (BaseRuntimeElementCompositeDefinition) myContext.getElementDefinition(myResourceBlockType);
055        }
056
057        @Override
058        public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
059                if (myResourceBlockType.equals(theDatatype)) {
060                        return getElementName();
061                }
062                return null;
063        }
064
065        @Override
066        public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) {
067                if (myResourceBlockType.equals(theDatatype)) {
068                        return getDefinition();
069                }
070                return null;
071        }
072
073        @Override
074        public Set<String> getValidChildNames() {
075                return Collections.singleton(getElementName());
076        }
077
078        @Override
079        void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
080//              myElementDef = (RuntimeResourceBlockDefinition) theClassToElementDefinitions.get(myResourceBlockType);
081        }
082
083}