001package ca.uhn.fhir.context; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2021 Smile CDR, Inc. 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.lang.reflect.Field; 024import java.util.*; 025 026import org.apache.commons.lang3.StringUtils; 027import org.hl7.fhir.instance.model.api.*; 028 029import ca.uhn.fhir.model.api.annotation.Child; 030import ca.uhn.fhir.model.api.annotation.Description; 031 032public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefinition { 033 034 private List<Class<? extends IBase>> myChoiceTypes; 035 private Map<String, BaseRuntimeElementDefinition<?>> myNameToChildDefinition; 036 private Map<Class<? extends IBase>, String> myDatatypeToElementName; 037 private Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> myDatatypeToElementDefinition; 038 private String myReferenceSuffix; 039 private List<Class<? extends IBaseResource>> myResourceTypes; 040 041 /** 042 * Constructor 043 */ 044 public RuntimeChildChoiceDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation, List<Class<? extends IBase>> theChoiceTypes) { 045 super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName); 046 047 myChoiceTypes = Collections.unmodifiableList(theChoiceTypes); 048 } 049 050 /** 051 * Constructor 052 * 053 * For extension, if myChoiceTypes will be set some other way 054 */ 055 RuntimeChildChoiceDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation) { 056 super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName); 057 } 058 059 void setChoiceTypes(List<Class<? extends IBase>> theChoiceTypes) { 060 myChoiceTypes = Collections.unmodifiableList(theChoiceTypes); 061 } 062 063 public List<Class<? extends IBase>> getChoices() { 064 return myChoiceTypes; 065 } 066 067 @Override 068 public Set<String> getValidChildNames() { 069 return myNameToChildDefinition.keySet(); 070 } 071 072 @Override 073 public BaseRuntimeElementDefinition<?> getChildByName(String theName) { 074 assert myNameToChildDefinition.containsKey(theName); 075 076 return myNameToChildDefinition.get(theName); 077 } 078 079 @SuppressWarnings("unchecked") 080 @Override 081 void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) { 082 myNameToChildDefinition = new HashMap<String, BaseRuntimeElementDefinition<?>>(); 083 myDatatypeToElementName = new HashMap<Class<? extends IBase>, String>(); 084 myDatatypeToElementDefinition = new HashMap<Class<? extends IBase>, BaseRuntimeElementDefinition<?>>(); 085 myResourceTypes = new ArrayList<Class<? extends IBaseResource>>(); 086 087 myReferenceSuffix = "Reference"; 088 089 for (Class<? extends IBase> next : myChoiceTypes) { 090 091 String elementName = null; 092 BaseRuntimeElementDefinition<?> nextDef; 093 boolean nonPreferred = false; 094 if (IBaseResource.class.isAssignableFrom(next)) { 095 elementName = getElementName() + StringUtils.capitalize(next.getSimpleName()); 096 nextDef = findResourceReferenceDefinition(theClassToElementDefinitions); 097 098 myNameToChildDefinition.put(getElementName() + "Reference", nextDef); 099 myNameToChildDefinition.put(getElementName() + "Resource", nextDef); 100 101 myResourceTypes.add((Class<? extends IBaseResource>) next); 102 103 } else { 104 nextDef = theClassToElementDefinitions.get(next); 105 BaseRuntimeElementDefinition<?> nextDefForChoice = nextDef; 106 107 /* 108 * In HAPI 1.3 the following applied: 109 * Elements which are called foo[x] and have a choice which is a profiled datatype must use the 110 * unprofiled datatype as the element name. E.g. if foo[x] allows markdown as a datatype, it calls the 111 * element fooString when encoded, because markdown is a profile of string. This is according to the 112 * FHIR spec 113 * 114 * Note that as of HAPI 1.4 this applies only to non-primitive datatypes after discussion 115 * with Grahame. 116 */ 117 if (nextDef instanceof IRuntimeDatatypeDefinition) { 118 IRuntimeDatatypeDefinition nextDefDatatype = (IRuntimeDatatypeDefinition) nextDef; 119 if (nextDefDatatype.getProfileOf() != null && !IPrimitiveType.class.isAssignableFrom(next)) { 120 nextDefForChoice = null; 121 nonPreferred = true; 122 Class<? extends IBaseDatatype> profileType = nextDefDatatype.getProfileOf(); 123 BaseRuntimeElementDefinition<?> elementDef = theClassToElementDefinitions.get(profileType); 124 elementName = getElementName() + StringUtils.capitalize(elementDef.getName()); 125 } 126 } 127 if (nextDefForChoice != null) { 128 elementName = getElementName() + StringUtils.capitalize(nextDefForChoice.getName()); 129 } 130 } 131 132 // I don't see how elementName could be null here, but eclipse complains.. 133 if (elementName != null) { 134 if (myNameToChildDefinition.containsKey(elementName) == false || !nonPreferred) { 135 myNameToChildDefinition.put(elementName, nextDef); 136 } 137 } 138 139 /* 140 * If this is a resource reference, the element name is "fooNameReference" 141 */ 142 if (IBaseResource.class.isAssignableFrom(next) || IBaseReference.class.isAssignableFrom(next)) { 143 next = theContext.getVersion().getResourceReferenceType(); 144 elementName = getElementName() + myReferenceSuffix; 145 myNameToChildDefinition.put(elementName, nextDef); 146 } 147 148 myDatatypeToElementDefinition.put(next, nextDef); 149 150 if (myDatatypeToElementName.containsKey(next)) { 151 String existing = myDatatypeToElementName.get(next); 152 if (!existing.equals(elementName)) { 153 throw new ConfigurationException("Already have element name " + existing + " for datatype " + next.getSimpleName() + " in " + getElementName() + ", cannot add " + elementName); 154 } 155 } else { 156 myDatatypeToElementName.put(next, elementName); 157 } 158 } 159 160 myNameToChildDefinition = Collections.unmodifiableMap(myNameToChildDefinition); 161 myDatatypeToElementName = Collections.unmodifiableMap(myDatatypeToElementName); 162 myDatatypeToElementDefinition = Collections.unmodifiableMap(myDatatypeToElementDefinition); 163 myResourceTypes = Collections.unmodifiableList(myResourceTypes); 164 } 165 166 167 public List<Class<? extends IBaseResource>> getResourceTypes() { 168 return myResourceTypes; 169 } 170 171 @Override 172 public String getChildNameByDatatype(Class<? extends IBase> theDatatype) { 173 String retVal = myDatatypeToElementName.get(theDatatype); 174 return retVal; 175 } 176 177 @Override 178 public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) { 179 return myDatatypeToElementDefinition.get(theDatatype); 180 } 181 182 public Set<Class<? extends IBase>> getValidChildTypes() { 183 return Collections.unmodifiableSet((myDatatypeToElementDefinition.keySet())); 184 } 185 186}