use of au.csiro.redmatch.compiler.Attribute in project redmatch by aehrc.
the class HapiReflectionHelper method getElementToSet.
/**
* Finds or creates the element where we are going to set a value.
*
* @param resource The resource that contains the element.
* @param attributes The list of attributes that point at the element where a value is going to be set.
* @param fhirPackage The FHIR package specified in the rules document. Needed to replace extension names.
* @param originalResourceType The resource type in the rules. This can be a profile name, so it can be different from
* * the actual FHIR resource type.
* @return The element.
*/
public Base getElementToSet(DomainResource resource, List<Attribute> attributes, VersionedFhirPackage fhirPackage, String originalResourceType) throws NoSuchFieldException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
// Used to get information about the attributes from the terminology service
StringBuilder sb = new StringBuilder();
sb.append(originalResourceType);
Base theElement = resource;
for (Attribute att : attributes) {
final String attName = att.getName();
final boolean isList = att.isList();
// Check the current attribute path to see if it is an extension - we need to treat extensions differently
sb.append(".");
sb.append(attName);
CodeInfo codeInfo = terminologyService.lookup(fhirPackage, sb.toString());
String extensionUrl = codeInfo.getExtensionUrl();
if (extensionUrl != null) {
// This is a profiled extension, so we set the url attribute based on the information in the profile
List<?> list = (List<?>) invokeGetter(theElement, "extension");
if (list.size() > 0) {
theElement = (Base) list.get(0);
} else {
theElement = invokeAdder(theElement, "extension");
}
((Extension) theElement).setUrl(extensionUrl);
} else {
if (!isList) {
// If attribute is not a list then we just need to get it because HAPI auto-creates instances
theElement = (Base) invokeGetter(theElement, attName);
} else {
if (!att.hasAttributeIndex()) {
List<?> list = (List<?>) invokeGetter(theElement, attName);
if (list.size() > 0) {
theElement = (Base) list.get(0);
} else {
theElement = invokeAdder(theElement, attName);
}
} else {
// Find the element in the list or create the necessary elements
int index = att.getAttributeIndex();
List<?> list = (List<?>) invokeGetter(theElement, attName);
int num = index - list.size() + 1;
if (num > 0) {
final Method add = getAddMethod(theElement.getClass(), attName);
Object elem = null;
for (int j = 0; j < num; j++) {
elem = add.invoke(theElement);
}
theElement = (Base) elem;
} else {
theElement = (Base) list.get(index);
}
}
}
}
}
return theElement;
}