Work on extensions

This commit is contained in:
James 2017-01-26 07:27:25 -05:00
parent 234b4a0c1c
commit 7e573c2299
1 changed files with 85 additions and 32 deletions

View File

@ -10,7 +10,7 @@ package ca.uhn.fhir.context;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@ -26,6 +26,7 @@ import java.lang.reflect.Modifier;
import java.util.*;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseResource;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.annotation.Child;
@ -43,16 +44,18 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
private Class<?> myEnumerationType;
private Class<? extends IBase> myChildType;
private RuntimeResourceBlockDefinition myChildResourceBlock;
private BaseRuntimeElementDefinition<?> myChildDef;
/**
* @param theBoundTypeBinder
* If the child is of a type that requires a constructor argument to instantiate, this is the argument to
* use
* If the child is of a type that requires a constructor argument to instantiate, this is the argument to
* use
* @param theDefinedLocally
* See {@link Extension#definedLocally()}
* See {@link Extension#definedLocally()}
*/
@SuppressWarnings("unchecked")
RuntimeChildDeclaredExtensionDefinition(Field theField, Child theChild, Description theDescriptionAnnotation, Extension theExtension, String theElementName, String theExtensionUrl, Class<? extends IBase> theChildType, Object theBoundTypeBinder)
RuntimeChildDeclaredExtensionDefinition(Field theField, Child theChild, Description theDescriptionAnnotation, Extension theExtension, String theElementName, String theExtensionUrl,
Class<? extends IBase> theChildType, Object theBoundTypeBinder)
throws ConfigurationException {
super(theField, theElementName, theChild, theDescriptionAnnotation);
assert isNotBlank(theExtensionUrl);
@ -61,17 +64,22 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
myDefinedLocally = theExtension.definedLocally();
myModifier = theExtension.isModifier();
myInstanceConstructorArguments = theBoundTypeBinder;
List<Class<? extends IBase>> choiceTypes = new ArrayList<Class<? extends IBase>>();
for (Class<? extends IElement> next : theChild.type()) {
choiceTypes.add(next);
}
if (Modifier.isAbstract(theField.getType().getModifiers()) == false) {
Class<?> type = theField.getType();
if (type.getAnnotation(DatatypeDef.class) != null) {
choiceTypes.add((Class<? extends IBase>) type);
}
if (Modifier.isAbstract(theChildType.getModifiers()) == false) {
choiceTypes.add(theChildType);
}
// if (Modifier.isAbstract(theField.getType().getModifiers()) == false) {
// Class<?> type = theField.getType();
// if (type.getAnnotation(DatatypeDef.class) != null) {
// choiceTypes.add((Class<? extends IBase>) type);
// }
// }
setChoiceTypes(choiceTypes);
}
@ -96,17 +104,54 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
@Override
public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
String retVal = super.getChildNameByDatatype(theDatatype);
if (retVal != null) {
BaseRuntimeElementDefinition<?> childDef = super.getChildElementDefinitionByDatatype(theDatatype);
if (childDef instanceof RuntimeResourceBlockDefinition) {
// Child is a newted extension
retVal = null;
}
}
if (retVal == null) {
if (myModifier) {
return "modifierExtension";
}else {
} else {
return "extension";
}
}
return retVal;
}
@Override
public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
// if (myDatatypeChildName != null) {
// if (myDatatypeChildName.equals(theName)) {
// return myChildDef;
// } else {
// return null;
// }
// } else {
// return null;
// }
if ("extension".equals(theName)||"modifierExtension".equals(theName)) {
if (myChildResourceBlock != null) {
return myChildResourceBlock;
}
if (myChildDef != null) {
return myChildDef;
}
}
return null;
//
//
// return super.getChildByName(theName);
}
@Override
public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) {
if (myChildResourceBlock != null) {
@ -121,24 +166,22 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
return myUrlToChildExtension.get(theUrl);
}
// @Override
// public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
// String retVal = super.getChildNameByDatatype(theDatatype);
// if (retVal )
// if (myChildType.equals(theDatatype) && myDatatypeChildName != null) {
// return myDatatypeChildName;
// } else {
// return "extension";
// }
// }
// @Override
// public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
// String retVal = super.getChildNameByDatatype(theDatatype);
// if (retVal )
// if (myChildType.equals(theDatatype) && myDatatypeChildName != null) {
// return myDatatypeChildName;
// } else {
// return "extension";
// }
// }
@Override
public String getExtensionUrl() {
return myExtensionUrl;
}
public boolean isDefinedLocally() {
return myDefinedLocally;
}
@ -148,15 +191,14 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
return myModifier;
}
@Override
void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
myUrlToChildExtension = new HashMap<String, RuntimeChildDeclaredExtensionDefinition>();
BaseRuntimeElementDefinition<?> elementDef = theClassToElementDefinitions.get(myChildType);
/*
* This will happen for any type that isn't defined in the base set of
* This will happen for any type that isn't defined in the base set of
* built-in types, e.g. custom structures or custom extensions
*/
if (elementDef == null) {
@ -164,17 +206,28 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
elementDef = theContext.getElementDefinition(myChildType);
}
}
if (elementDef instanceof RuntimeResourceBlockDefinition) {
if (elementDef instanceof RuntimePrimitiveDatatypeDefinition || elementDef instanceof RuntimeCompositeDatatypeDefinition) {
// myDatatypeChildName = "value" + elementDef.getName().substring(0, 1).toUpperCase() + elementDef.getName().substring(1);
// if ("valueResourceReference".equals(myDatatypeChildName)) {
// Per one of the examples here: http://hl7.org/implement/standards/fhir/extensibility.html#extension
// myDatatypeChildName = "valueResource";
// List<Class<? extends IBaseResource>> types = new ArrayList<Class<? extends IBaseResource>>();
// types.add(IBaseResource.class);
// myChildDef = findResourceReferenceDefinition(theClassToElementDefinitions);
// } else {
myChildDef = elementDef;
// }
} else if (elementDef instanceof RuntimeResourceBlockDefinition) {
RuntimeResourceBlockDefinition extDef = ((RuntimeResourceBlockDefinition) elementDef);
for (RuntimeChildDeclaredExtensionDefinition next : extDef.getExtensions()) {
myUrlToChildExtension.put(next.getExtensionUrl(), next);
}
myChildResourceBlock = (RuntimeResourceBlockDefinition)elementDef;
myChildResourceBlock = (RuntimeResourceBlockDefinition) elementDef;
}
myUrlToChildExtension = Collections.unmodifiableMap(myUrlToChildExtension);
super.sealAndInitialize(theContext, theClassToElementDefinitions);
}