001package ca.uhn.fhir.context; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2016 University Health Network 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import java.util.HashMap; 024import java.util.List; 025import java.util.Map; 026 027import org.hl7.fhir.instance.model.api.IAnyResource; 028import org.hl7.fhir.instance.model.api.IBase; 029import org.hl7.fhir.instance.model.api.IBaseResource; 030 031import ca.uhn.fhir.model.api.IResource; 032import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt; 033 034public class RuntimeResourceReferenceDefinition extends BaseRuntimeElementDefinition<BaseResourceReferenceDt> { 035 036 private final List<Class<? extends IBaseResource>> myResourceTypes; 037 private HashMap<Class<? extends IBaseResource>, RuntimeResourceDefinition> myResourceTypeToDefinition; 038 039 /** 040 * Constructor 041 * @param theStandardType 042 */ 043 public RuntimeResourceReferenceDefinition(String theName, List<Class<? extends IBaseResource>> theResourceTypes, boolean theStandardType) { 044 super(theName, BaseResourceReferenceDt.class, theStandardType); 045 if (theResourceTypes == null || theResourceTypes.isEmpty()) { 046 throw new ConfigurationException("Element '" + theName + "' has no resource types noted"); 047 } 048 myResourceTypes = theResourceTypes; 049 } 050 051 public List<Class<? extends IBaseResource>> getResourceTypes() { 052 return myResourceTypes; 053 } 054 055 @Override 056 void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) { 057 myResourceTypeToDefinition = new HashMap<Class<? extends IBaseResource>, RuntimeResourceDefinition>(); 058 for (Class<? extends IBaseResource> next : myResourceTypes) { 059 if (next.equals(IResource.class) || next.equals(IAnyResource.class) || next.equals(IBaseResource.class)) { 060 continue; 061 } 062 RuntimeResourceDefinition definition = (RuntimeResourceDefinition) theClassToElementDefinitions.get(next); 063 if (definition == null) { 064 throw new ConfigurationException("Couldn't find definition for: " + next.getCanonicalName()); 065 } 066 myResourceTypeToDefinition.put(next, definition); 067 } 068 } 069 070 @Override 071 public ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum getChildType() { 072 return ChildTypeEnum.RESOURCE_REF; 073 } 074 075 public RuntimeResourceDefinition getDefinitionForResourceType(Class<? extends IResource> theType) { 076 RuntimeResourceDefinition retVal = myResourceTypeToDefinition.get(theType); 077 if (retVal == null) { 078 throw new ConfigurationException("Unknown type: " + theType.getCanonicalName()); 079 } 080 return retVal; 081 } 082 083}