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