001package ca.uhn.fhir.context; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2017 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.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 if (theContext.getVersion().getVersion().equals(FhirVersionEnum.DSTU1)) { 088 myReferenceSuffix = "Resource"; 089 } else { 090 myReferenceSuffix = "Reference"; 091 } 092 093 for (Class<? extends IBase> next : myChoiceTypes) { 094 095 String elementName = null; 096 BaseRuntimeElementDefinition<?> nextDef; 097 boolean nonPreferred = false; 098 if (IBaseResource.class.isAssignableFrom(next)) { 099 elementName = getElementName() + StringUtils.capitalize(next.getSimpleName()); 100 List<Class<? extends IBaseResource>> types = new ArrayList<Class<? extends IBaseResource>>(); 101 types.add((Class<? extends IBaseResource>) next); 102 nextDef = findResourceReferenceDefinition(theClassToElementDefinitions); 103 104 myNameToChildDefinition.put(getElementName() + "Reference", nextDef); 105 myNameToChildDefinition.put(getElementName() + "Resource", nextDef); 106 107 myResourceTypes.add((Class<? extends IBaseResource>) next); 108 109 } else { 110 nextDef = theClassToElementDefinitions.get(next); 111 BaseRuntimeElementDefinition<?> nextDefForChoice = nextDef; 112 113 /* 114 * In HAPI 1.3 the following applied: 115 * Elements which are called foo[x] and have a choice which is a profiled datatype must use the 116 * unprofiled datatype as the element name. E.g. if foo[x] allows markdown as a datatype, it calls the 117 * element fooString when encoded, because markdown is a profile of string. This is according to the 118 * FHIR spec 119 * 120 * Note that as of HAPI 1.4 this applies only to non-primitive datatypes after discussion 121 * with Grahame. 122 */ 123 if (nextDef instanceof IRuntimeDatatypeDefinition) { 124 IRuntimeDatatypeDefinition nextDefDatatype = (IRuntimeDatatypeDefinition) nextDef; 125 if (nextDefDatatype.getProfileOf() != null && !IPrimitiveType.class.isAssignableFrom(next)) { 126 nextDefForChoice = null; 127 nonPreferred = true; 128 Class<? extends IBaseDatatype> profileType = nextDefDatatype.getProfileOf(); 129 BaseRuntimeElementDefinition<?> elementDef = theClassToElementDefinitions.get(profileType); 130 elementName = getElementName() + StringUtils.capitalize(elementDef.getName()); 131 } 132 } 133 if (nextDefForChoice != null) { 134 elementName = getElementName() + StringUtils.capitalize(nextDefForChoice.getName()); 135 } 136 } 137 138 // I don't see how elementName could be null here, but eclipse complains.. 139 if (elementName != null) { 140 if (myNameToChildDefinition.containsKey(elementName) == false || !nonPreferred) { 141 myNameToChildDefinition.put(elementName, nextDef); 142 } 143 } 144 145 /* 146 * If this is a resource reference, the element name is "fooNameReference" 147 */ 148 if (IBaseResource.class.isAssignableFrom(next) || IBaseReference.class.isAssignableFrom(next)) { 149 next = theContext.getVersion().getResourceReferenceType(); 150 elementName = getElementName() + myReferenceSuffix; 151 myNameToChildDefinition.put(elementName, nextDef); 152 } 153 154 myDatatypeToElementDefinition.put(next, nextDef); 155 156 if (myDatatypeToElementName.containsKey(next)) { 157 String existing = myDatatypeToElementName.get(next); 158 if (!existing.equals(elementName)) { 159 throw new ConfigurationException("Already have element name " + existing + " for datatype " + next.getSimpleName() + " in " + getElementName() + ", cannot add " + elementName); 160 } 161 } else { 162 myDatatypeToElementName.put(next, elementName); 163 } 164 } 165 166 myNameToChildDefinition = Collections.unmodifiableMap(myNameToChildDefinition); 167 myDatatypeToElementName = Collections.unmodifiableMap(myDatatypeToElementName); 168 myDatatypeToElementDefinition = Collections.unmodifiableMap(myDatatypeToElementDefinition); 169 myResourceTypes = Collections.unmodifiableList(myResourceTypes); 170 } 171 172 173 public List<Class<? extends IBaseResource>> getResourceTypes() { 174 return myResourceTypes; 175 } 176 177 @Override 178 public String getChildNameByDatatype(Class<? extends IBase> theDatatype) { 179 String retVal = myDatatypeToElementName.get(theDatatype); 180 return retVal; 181 } 182 183 @Override 184 public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) { 185 return myDatatypeToElementDefinition.get(theDatatype); 186 } 187 188 public Set<Class<? extends IBase>> getValidChildTypes() { 189 return Collections.unmodifiableSet((myDatatypeToElementDefinition.keySet())); 190 } 191 192}