View Javadoc
1   package ca.uhn.fhir.context;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 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.util.ArrayList;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Map.Entry;
29  import java.util.Set;
30  
31  import org.apache.commons.lang3.text.WordUtils;
32  
33  import ca.uhn.fhir.model.api.ExtensionDt;
34  import ca.uhn.fhir.model.api.IDatatype;
35  import ca.uhn.fhir.model.api.IElement;
36  import ca.uhn.fhir.model.api.IResource;
37  import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
38  import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
39  
40  public class RuntimeChildUndeclaredExtensionDefinition extends BaseRuntimeChildDefinition {
41  
42  	private Map<String, BaseRuntimeElementDefinition<?>> myAttributeNameToDefinition;
43  	private Map<Class<? extends IElement>, String> myDatatypeToAttributeName;
44  	private Map<Class<? extends IElement>, BaseRuntimeElementDefinition<?>> myDatatypeToDefinition;
45  
46  	public RuntimeChildUndeclaredExtensionDefinition() {
47  		// nothing
48  	}
49  
50  	@Override
51  	public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
52  		return myAttributeNameToDefinition.get(theName);
53  	}
54  
55  	@Override
56  	public Set<String> getValidChildNames() {
57  		return myAttributeNameToDefinition.keySet();
58  	}
59  
60  	@Override
61  	public String getChildNameByDatatype(Class<? extends IElement> theDatatype) {
62  		return myDatatypeToAttributeName.get(theDatatype);
63  	}
64  
65  	@Override
66  	public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IElement> theType) {
67  		return myDatatypeToDefinition.get(theType);
68  	}
69  
70  	@Override
71  	void sealAndInitialize(Map<Class<? extends IElement>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
72  		Map<String, BaseRuntimeElementDefinition<?>> datatypeAttributeNameToDefinition = new HashMap<String, BaseRuntimeElementDefinition<?>>();
73  		myDatatypeToAttributeName = new HashMap<Class<? extends IElement>, String>();
74  
75  		for (BaseRuntimeElementDefinition<?> next : theClassToElementDefinitions.values()) {
76  			if (next instanceof IRuntimeDatatypeDefinition) {
77  //				if (next.getName().equals("CodeableConcept")) {
78  //					System.out.println();
79  //				}
80  				
81  				if (!((IRuntimeDatatypeDefinition) next).isSpecialization()) {
82  					String attrName = "value" + WordUtils.capitalize(next.getName());
83  					datatypeAttributeNameToDefinition.put(attrName, next);
84  					datatypeAttributeNameToDefinition.put(attrName.toLowerCase(), next);
85  					myDatatypeToAttributeName.put(next.getImplementingClass(), attrName);
86  				}
87  			}
88  		}
89  
90  		myAttributeNameToDefinition = datatypeAttributeNameToDefinition;
91  
92  		myDatatypeToDefinition = new HashMap<Class<? extends IElement>, BaseRuntimeElementDefinition<?>>();
93  
94  		for (Entry<String, BaseRuntimeElementDefinition<?>> next : myAttributeNameToDefinition.entrySet()) {
95  			@SuppressWarnings("unchecked")
96  			Class<? extends IDatatype> type = (Class<? extends IDatatype>) next.getValue().getImplementingClass();
97  			myDatatypeToDefinition.put(type, next.getValue());
98  		}
99  
100 		// Resource Reference
101 		myDatatypeToAttributeName.put(ResourceReferenceDt.class, "valueResource");
102 		List<Class<? extends IResource>> types = new ArrayList<Class<? extends IResource>>();
103 		types.add(IResource.class);
104 		RuntimeResourceReferenceDefinition def = new RuntimeResourceReferenceDefinition("valueResource", types);
105 		def.sealAndInitialize(theClassToElementDefinitions);
106 		myAttributeNameToDefinition.put("valueResource", def);
107 		myDatatypeToDefinition.put(BaseResourceReferenceDt.class, def);
108 		myDatatypeToDefinition.put(ResourceReferenceDt.class, def);
109 	}
110 
111 	@Override
112 	public IAccessor getAccessor() {
113 		return new IAccessor() {
114 			@Override
115 			public List<? extends IElement> getValues(Object theTarget) {
116 				ExtensionDt target = (ExtensionDt) theTarget;
117 				if (target.getValue() != null) {
118 					return Collections.singletonList(target.getValue());
119 				}
120 				return target.getUndeclaredExtensions();
121 			}
122 		};
123 	}
124 
125 	@Override
126 	public IMutator getMutator() {
127 		return new IMutator() {
128 			@Override
129 			public void addValue(Object theTarget, IElement theValue) {
130 				ExtensionDt target = (ExtensionDt) theTarget;
131 				if (theValue instanceof IDatatype) {
132 					target.setValue((IDatatype) theTarget);
133 				} else {
134 					target.getUndeclaredExtensions().add((ExtensionDt) theValue);
135 				}
136 			}
137 		};
138 	}
139 
140 	@Override
141 	public int getMax() {
142 		return 1;
143 	}
144 
145 	@Override
146 	public int getMin() {
147 		return 0;
148 	}
149 
150 	@Override
151 	public String getElementName() {
152 		return "extension";
153 	}
154 
155 }