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 */
022import static org.apache.commons.lang3.StringUtils.isBlank;
023
024import java.util.Map;
025
026import org.hl7.fhir.instance.model.api.IBase;
027import org.hl7.fhir.instance.model.api.IBaseDatatype;
028import org.hl7.fhir.instance.model.api.ICompositeType;
029
030import ca.uhn.fhir.model.api.annotation.DatatypeDef;
031import ca.uhn.fhir.model.api.annotation.ResourceDef;
032
033public class RuntimeCompositeDatatypeDefinition extends BaseRuntimeElementCompositeDefinition<ICompositeType> implements IRuntimeDatatypeDefinition {
034
035        private boolean mySpecialization;
036        private Class<? extends IBaseDatatype> myProfileOfType;
037        private BaseRuntimeElementDefinition<?> myProfileOf;
038
039        public RuntimeCompositeDatatypeDefinition(DatatypeDef theDef, Class<? extends ICompositeType> theImplementingClass, boolean theStandardType, FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
040                super(theDef.name(), theImplementingClass, theStandardType, theContext, theClassToElementDefinitions);
041                
042                String resourceName = theDef.name();
043                if (isBlank(resourceName)) {
044                        throw new ConfigurationException("Resource type @" + ResourceDef.class.getSimpleName() + " annotation contains no resource name: " + theImplementingClass.getCanonicalName());
045                }
046                
047                mySpecialization = theDef.isSpecialization();
048                myProfileOfType = theDef.profileOf();
049                if (myProfileOfType.equals(IBaseDatatype.class)) {
050                        myProfileOfType = null;
051                }
052
053        }
054
055        @Override
056        public void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
057                super.sealAndInitialize(theContext, theClassToElementDefinitions);
058                
059                if (myProfileOfType != null) {
060                        myProfileOf = theClassToElementDefinitions.get(myProfileOfType);
061                        if (myProfileOf == null) {
062                                throw new ConfigurationException("Unknown profileOf value: " + myProfileOfType);
063                        }
064                }
065        }
066
067        @Override
068        public Class<? extends IBaseDatatype> getProfileOf() {
069                return myProfileOfType;
070        }
071
072        @Override
073        public boolean isSpecialization() {
074                return mySpecialization;
075        }
076
077        @Override
078        public ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum getChildType() {
079                return ChildTypeEnum.COMPOSITE_DATATYPE;
080        }
081
082        @Override
083        public boolean isProfileOf(Class<? extends IBaseDatatype> theType) {
084                validateSealed();
085                if (myProfileOfType != null) {
086                        if (myProfileOfType.equals(theType)) {
087                                return true;
088                        } else if (myProfileOf instanceof IRuntimeDatatypeDefinition) {
089                                return ((IRuntimeDatatypeDefinition) myProfileOf).isProfileOf(theType);
090                        }
091                }
092                return false;
093        }
094
095
096}