001/* 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.context; 021 022import ca.uhn.fhir.i18n.Msg; 023import ca.uhn.fhir.model.api.annotation.DatatypeDef; 024import ca.uhn.fhir.model.api.annotation.ResourceDef; 025import org.hl7.fhir.instance.model.api.IBase; 026import org.hl7.fhir.instance.model.api.IBaseDatatype; 027import org.hl7.fhir.instance.model.api.ICompositeType; 028 029import java.util.Map; 030 031import static org.apache.commons.lang3.StringUtils.isBlank; 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(Msg.code(1712) + "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(Msg.code(1713) + "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}