View Javadoc
1   package ca.uhn.fhir.context;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  import static org.apache.commons.lang3.StringUtils.isBlank;
23  
24  import java.util.Map;
25  
26  import org.hl7.fhir.instance.model.api.IBase;
27  import org.hl7.fhir.instance.model.api.IBaseDatatype;
28  import org.hl7.fhir.instance.model.api.ICompositeType;
29  
30  import ca.uhn.fhir.model.api.annotation.DatatypeDef;
31  import ca.uhn.fhir.model.api.annotation.ResourceDef;
32  
33  public class RuntimeCompositeDatatypeDefinition extends BaseRuntimeElementCompositeDefinition<ICompositeType> implements IRuntimeDatatypeDefinition {
34  
35  	private boolean mySpecialization;
36  	private Class<? extends IBaseDatatype> myProfileOfType;
37  	private BaseRuntimeElementDefinition<?> myProfileOf;
38  
39  	public RuntimeCompositeDatatypeDefinition(DatatypeDef theDef, Class<? extends ICompositeType> theImplementingClass, boolean theStandardType, FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
40  		super(theDef.name(), theImplementingClass, theStandardType, theContext, theClassToElementDefinitions);
41  		
42  		String resourceName = theDef.name();
43  		if (isBlank(resourceName)) {
44  			throw new ConfigurationException("Resource type @" + ResourceDef.class.getSimpleName() + " annotation contains no resource name: " + theImplementingClass.getCanonicalName());
45  		}
46  		
47  		mySpecialization = theDef.isSpecialization();
48  		myProfileOfType = theDef.profileOf();
49  		if (myProfileOfType.equals(IBaseDatatype.class)) {
50  			myProfileOfType = null;
51  		}
52  
53  	}
54  
55  	@Override
56  	public void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
57  		super.sealAndInitialize(theContext, theClassToElementDefinitions);
58  		
59  		if (myProfileOfType != null) {
60  			myProfileOf = theClassToElementDefinitions.get(myProfileOfType);
61  			if (myProfileOf == null) {
62  				throw new ConfigurationException("Unknown profileOf value: " + myProfileOfType);
63  			}
64  		}
65  	}
66  
67  	@Override
68  	public Class<? extends IBaseDatatype> getProfileOf() {
69  		return myProfileOfType;
70  	}
71  
72  	@Override
73  	public boolean isSpecialization() {
74  		return mySpecialization;
75  	}
76  
77  	@Override
78  	public ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum getChildType() {
79  		return ChildTypeEnum.COMPOSITE_DATATYPE;
80  	}
81  
82  	@Override
83  	public boolean isProfileOf(Class<? extends IBaseDatatype> theType) {
84  		validateSealed();
85  		if (myProfileOfType != null) {
86  			if (myProfileOfType.equals(theType)) {
87  				return true;
88  			} else if (myProfileOf instanceof IRuntimeDatatypeDefinition) {
89  				return ((IRuntimeDatatypeDefinition) myProfileOf).isProfileOf(theType);
90  			}
91  		}
92  		return false;
93  	}
94  
95  
96  }