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 not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -26,6 +26,7 @@ import java.lang.reflect.Modifier;
import java.util.*; import java.util.*;
import org.hl7.fhir.instance.model.api.IBase; 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.IElement;
import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.api.annotation.Child;
@ -43,16 +44,18 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
private Class<?> myEnumerationType; private Class<?> myEnumerationType;
private Class<? extends IBase> myChildType; private Class<? extends IBase> myChildType;
private RuntimeResourceBlockDefinition myChildResourceBlock; private RuntimeResourceBlockDefinition myChildResourceBlock;
private BaseRuntimeElementDefinition<?> myChildDef;
/** /**
* @param theBoundTypeBinder * @param theBoundTypeBinder
* If the child is of a type that requires a constructor argument to instantiate, this is the argument to * If the child is of a type that requires a constructor argument to instantiate, this is the argument to
* use * use
* @param theDefinedLocally * @param theDefinedLocally
* See {@link Extension#definedLocally()} * See {@link Extension#definedLocally()}
*/ */
@SuppressWarnings("unchecked") @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 { throws ConfigurationException {
super(theField, theElementName, theChild, theDescriptionAnnotation); super(theField, theElementName, theChild, theDescriptionAnnotation);
assert isNotBlank(theExtensionUrl); assert isNotBlank(theExtensionUrl);
@ -66,12 +69,17 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
for (Class<? extends IElement> next : theChild.type()) { for (Class<? extends IElement> next : theChild.type()) {
choiceTypes.add(next); choiceTypes.add(next);
} }
if (Modifier.isAbstract(theField.getType().getModifiers()) == false) {
Class<?> type = theField.getType(); if (Modifier.isAbstract(theChildType.getModifiers()) == false) {
if (type.getAnnotation(DatatypeDef.class) != null) { choiceTypes.add(theChildType);
choiceTypes.add((Class<? extends IBase>) type);
}
} }
// 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); setChoiceTypes(choiceTypes);
} }
@ -96,17 +104,54 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
@Override @Override
public String getChildNameByDatatype(Class<? extends IBase> theDatatype) { public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
String retVal = super.getChildNameByDatatype(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 (retVal == null) {
if (myModifier) { if (myModifier) {
return "modifierExtension"; return "modifierExtension";
}else { } else {
return "extension"; return "extension";
} }
} }
return retVal; 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 @Override
public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) { public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) {
if (myChildResourceBlock != null) { if (myChildResourceBlock != null) {
@ -121,24 +166,22 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
return myUrlToChildExtension.get(theUrl); return myUrlToChildExtension.get(theUrl);
} }
// @Override // @Override
// public String getChildNameByDatatype(Class<? extends IBase> theDatatype) { // public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
// String retVal = super.getChildNameByDatatype(theDatatype); // String retVal = super.getChildNameByDatatype(theDatatype);
// if (retVal ) // if (retVal )
// if (myChildType.equals(theDatatype) && myDatatypeChildName != null) { // if (myChildType.equals(theDatatype) && myDatatypeChildName != null) {
// return myDatatypeChildName; // return myDatatypeChildName;
// } else { // } else {
// return "extension"; // return "extension";
// } // }
// } // }
@Override @Override
public String getExtensionUrl() { public String getExtensionUrl() {
return myExtensionUrl; return myExtensionUrl;
} }
public boolean isDefinedLocally() { public boolean isDefinedLocally() {
return myDefinedLocally; return myDefinedLocally;
} }
@ -148,7 +191,6 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
return myModifier; return myModifier;
} }
@Override @Override
void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) { void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
myUrlToChildExtension = new HashMap<String, RuntimeChildDeclaredExtensionDefinition>(); myUrlToChildExtension = new HashMap<String, RuntimeChildDeclaredExtensionDefinition>();
@ -165,12 +207,23 @@ public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceD
} }
} }
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); RuntimeResourceBlockDefinition extDef = ((RuntimeResourceBlockDefinition) elementDef);
for (RuntimeChildDeclaredExtensionDefinition next : extDef.getExtensions()) { for (RuntimeChildDeclaredExtensionDefinition next : extDef.getExtensions()) {
myUrlToChildExtension.put(next.getExtensionUrl(), next); myUrlToChildExtension.put(next.getExtensionUrl(), next);
} }
myChildResourceBlock = (RuntimeResourceBlockDefinition)elementDef; myChildResourceBlock = (RuntimeResourceBlockDefinition) elementDef;
} }
myUrlToChildExtension = Collections.unmodifiableMap(myUrlToChildExtension); myUrlToChildExtension = Collections.unmodifiableMap(myUrlToChildExtension);