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  
23  import java.lang.reflect.Field;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Comparator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.hl7.fhir.instance.model.api.IBase;
31  import org.hl7.fhir.instance.model.api.IBaseDatatype;
32  import org.hl7.fhir.instance.model.api.IBaseReference;
33  
34  import ca.uhn.fhir.model.api.IDatatype;
35  import ca.uhn.fhir.model.api.IResource;
36  import ca.uhn.fhir.model.api.annotation.Child;
37  import ca.uhn.fhir.model.api.annotation.Description;
38  import ca.uhn.fhir.model.primitive.XhtmlDt;
39  
40  public class RuntimeChildAny extends RuntimeChildChoiceDefinition {
41  
42  	public RuntimeChildAny(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation) {
43  		super(theField, theElementName, theChildAnnotation, theDescriptionAnnotation);
44  	}
45  
46  	@Override
47  	void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
48  		List<Class<? extends IBase>> choiceTypes = new ArrayList<Class<? extends IBase>>();
49  		
50  		for (Class<? extends IBase> next : theClassToElementDefinitions.keySet()) {
51  			if (next.equals(XhtmlDt.class)) {
52  				continue;
53  			}
54  			
55  			BaseRuntimeElementDefinition<?> nextDef = theClassToElementDefinitions.get(next);
56  			if (nextDef instanceof IRuntimeDatatypeDefinition) {
57  				if (((IRuntimeDatatypeDefinition) nextDef).isSpecialization()) {
58  					/*
59  					 * Things like BoundCodeDt shoudn't be considered as valid options for an "any" choice, since 
60  					 * we'll already have CodeDt as an option 
61  					 */
62  					continue;
63  				}
64  			}
65  			
66  			if (IResource.class.isAssignableFrom(next) || IDatatype.class.isAssignableFrom(next) || IBaseDatatype.class.isAssignableFrom(next) || IBaseReference.class.isAssignableFrom(next)) {
67  				choiceTypes.add(next);
68  			}
69  		}
70  		Collections.sort(choiceTypes,new Comparator<Class<?>>(){
71  			@Override
72  			public int compare(Class<?> theO1, Class<?> theO2) {
73  				boolean o1res = IResource.class.isAssignableFrom(theO1);
74  				boolean o2res = IResource.class.isAssignableFrom(theO2);
75  				if (o1res && o2res) {
76  					return theO1.getSimpleName().compareTo(theO2.getSimpleName());
77  				} else if (o1res) {
78  					return -1;
79  				} else if (o1res == false && o2res == false) {
80  					return 0;
81  				}else {
82  					return 1;
83  				}
84  			}});
85  		
86  		setChoiceTypes(choiceTypes);
87  		
88  		super.sealAndInitialize(theContext, theClassToElementDefinitions);
89  	}
90  
91  	
92  	
93  }