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.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import ca.uhn.fhir.model.api.IElement;
28  import ca.uhn.fhir.model.api.IResource;
29  import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
30  
31  public class RuntimeResourceReferenceDefinition extends BaseRuntimeElementDefinition<BaseResourceReferenceDt> {
32  
33  	private final List<Class<? extends IResource>> myResourceTypes;
34  	private HashMap<Class<? extends IResource>, RuntimeResourceDefinition> myResourceTypeToDefinition;
35  
36  	public RuntimeResourceReferenceDefinition(String theName, List<Class<? extends IResource>> theResourceTypes) {
37  		super(theName, BaseResourceReferenceDt.class);
38  		if (theResourceTypes == null || theResourceTypes.isEmpty()) {
39  			throw new ConfigurationException("Element '" + theName + "' has no resource types noted");
40  		}
41  		myResourceTypes = theResourceTypes;
42  	}
43  
44  	public List<Class<? extends IResource>> getResourceTypes() {
45  		return myResourceTypes;
46  	}
47  
48  	@Override
49  	void sealAndInitialize(Map<Class<? extends IElement>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
50  		myResourceTypeToDefinition = new HashMap<Class<? extends IResource>, RuntimeResourceDefinition>();
51  		for (Class<? extends IResource> next : myResourceTypes) {
52  			if (next.equals(IResource.class)) {
53  				continue;
54  			}
55  			RuntimeResourceDefinition definition = (RuntimeResourceDefinition) theClassToElementDefinitions.get(next);
56  			if (definition == null) {
57  				throw new ConfigurationException("Couldn't find definition for: " + next.getCanonicalName());
58  			}
59  			myResourceTypeToDefinition.put(next, definition);
60  		}
61  	}
62  
63  	@Override
64  	public ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum getChildType() {
65  		return ChildTypeEnum.RESOURCE_REF;
66  	}
67  
68  	public RuntimeResourceDefinition getDefinitionForResourceType(Class<? extends IResource> theType) {
69  		RuntimeResourceDefinition retVal = myResourceTypeToDefinition.get(theType);
70  		if (retVal == null) {
71  			throw new ConfigurationException("Unknown type:  " + theType.getCanonicalName());
72  		}
73  		return retVal;
74  	}
75  
76  }