Navigation Menu

Skip to content

Commit

Permalink
Fix #402 - Don't overwrite narrative sections
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Oct 1, 2019
1 parent 731be21 commit eaaf276
Show file tree
Hide file tree
Showing 9 changed files with 345 additions and 309 deletions.
Expand Up @@ -66,13 +66,17 @@ public String toString() {
}

public interface IAccessor {
List<IBase> getValues(Object theTarget);
List<IBase> getValues(IBase theTarget);

default IBase getFirstValueOrNull(IBase theTarget) {
return getValues(theTarget).stream().findFirst().orElse(null);
}
}

public interface IMutator {
void addValue(Object theTarget, IBase theValue);
void addValue(IBase theTarget, IBase theValue);

void setValue(Object theTarget, IBase theValue);
void setValue(IBase theTarget, IBase theValue);
}

BaseRuntimeElementDefinition<?> findResourceReferenceDefinition(Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
Expand Down
Expand Up @@ -20,34 +20,33 @@
* #L%
*/

import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.util.ValidateUtil;
import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.api.IBase;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.api.IBase;

import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.util.ValidateUtil;

public abstract class BaseRuntimeDeclaredChildDefinition extends BaseRuntimeChildDefinition {
private final IAccessor myAccessor;
private String myBindingValueSet;
private final String myElementName;
private final Field myField;
private final String myFormalDefinition;
private final int myMax;
private final int myMin;
private boolean myModifier;

private final IMutator myMutator;
private final String myShortDefinition;
private String myBindingValueSet;
private boolean myModifier;
private boolean mySummary;

BaseRuntimeDeclaredChildDefinition(Field theField, Child theChildAnnotation, Description theDescriptionAnnotation, String theElementName) throws ConfigurationException {
super();
Validate.notNull(theField, "No field speficied");
Validate.notNull(theField, "No field specified");
ValidateUtil.isGreaterThanOrEqualTo(theChildAnnotation.min(), 0, "Min must be >= 0");
Validate.isTrue(theChildAnnotation.max() == -1 || theChildAnnotation.max() >= theChildAnnotation.min(), "Max must be >= Min (unless it is -1 / unlimited)");
Validate.notBlank(theElementName, "Element name must not be blank");
Expand Down Expand Up @@ -87,6 +86,10 @@ public String getBindingValueSet() {
return myBindingValueSet;
}

void setBindingValueSet(String theBindingValueSet) {
myBindingValueSet = theBindingValueSet;
}

@Override
public String getElementName() {
return myElementName;
Expand Down Expand Up @@ -119,107 +122,99 @@ public String getShortDefinition() {
return myShortDefinition;
}

public BaseRuntimeElementDefinition<?> getSingleChildOrThrow() {
if (getValidChildNames().size() != 1) {
throw new IllegalStateException("This child has " + getValidChildNames().size() + " children, expected 1. This is a HAPI bug. Found: " + getValidChildNames());
}
return getChildByName(getValidChildNames().iterator().next());
}

public boolean isModifier() {
return myModifier;
}

protected void setModifier(boolean theModifier) {
myModifier = theModifier;
}

@Override
public boolean isSummary() {
return mySummary;
}

void setBindingValueSet(String theBindingValueSet) {
myBindingValueSet = theBindingValueSet;
}

protected void setModifier(boolean theModifier) {
myModifier = theModifier;
}

private final class FieldListAccessor implements IAccessor {
@SuppressWarnings("unchecked")
@Override
public List<IBase> getValues(Object theTarget) {
List<IBase> retVal;
try {
retVal = (List<IBase>) myField.get(theTarget);
} catch (Exception e) {
throw new ConfigurationException("Failed to get value", e);
}

public List<IBase> getValues(IBase theTarget) {
List<IBase> retVal = (List<IBase>) getFieldValue(theTarget, myField);
if (retVal == null) {
retVal = Collections.emptyList();
}
return retVal;
}

}

protected final class FieldListMutator implements IMutator {
@Override
public void addValue(Object theTarget, IBase theValue) {
public void addValue(IBase theTarget, IBase theValue) {
addValue(theTarget, theValue, false);
}

private void addValue(Object theTarget, IBase theValue, boolean theClear) {
try {
@SuppressWarnings("unchecked")
List<IBase> existingList = (List<IBase>) myField.get(theTarget);
if (existingList == null) {
existingList = new ArrayList<IBase>(2);
myField.set(theTarget, existingList);
}
if (theClear) {
existingList.clear();
}
existingList.add(theValue);
} catch (Exception e) {
throw new ConfigurationException("Failed to set value", e);
private void addValue(IBase theTarget, IBase theValue, boolean theClear) {
@SuppressWarnings("unchecked")
List<IBase> existingList = (List<IBase>) getFieldValue(theTarget, myField);
if (existingList == null) {
existingList = new ArrayList<>(2);
setFieldValue(theTarget, existingList, myField);
}
if (theClear) {
existingList.clear();
}
existingList.add(theValue);
}

@Override
public void setValue(Object theTarget, IBase theValue) {
public void setValue(IBase theTarget, IBase theValue) {
addValue(theTarget, theValue, true);
}
}

private final class FieldPlainAccessor implements IAccessor {
@Override
public List<IBase> getValues(Object theTarget) {
try {
Object values = myField.get(theTarget);
if (values == null) {
return Collections.emptyList();
}
List<IBase> retVal = Collections.singletonList((IBase) values);
return retVal;
} catch (Exception e) {
throw new ConfigurationException("Failed to get value", e);
public List<IBase> getValues(IBase theTarget) {
Object values = getFieldValue(theTarget, myField);
if (values == null) {
return Collections.emptyList();
}
return Collections.singletonList((IBase) values);
}

@Override
public IBase getFirstValueOrNull(IBase theTarget) {
return (IBase) getFieldValue(theTarget, myField);
}
}

protected final class FieldPlainMutator implements IMutator {
@Override
public void addValue(Object theTarget, IBase theValue) {
try {
myField.set(theTarget, theValue);
} catch (Exception e) {
throw new ConfigurationException("Failed to set value", e);
}
public void addValue(IBase theTarget, IBase theValue) {
setFieldValue(theTarget, theValue, myField);
}

@Override
public void setValue(Object theTarget, IBase theValue) {
public void setValue(IBase theTarget, IBase theValue) {
addValue(theTarget, theValue);
}
}

private static void setFieldValue(IBase theTarget, Object theValue, Field theField) {
try {
theField.set(theTarget, theValue);
} catch (IllegalAccessException e) {
throw new ConfigurationException("Failed to set value", e);
}
}

private static Object getFieldValue(IBase theTarget, Field theField) {
try {
return theField.get(theTarget);
} catch (IllegalAccessException e) {
throw new ConfigurationException("Failed to get value", e);
}
}

}
Expand Up @@ -68,14 +68,15 @@ private void addReferenceBinding(FhirContext theContext, Map<Class<? extends IBa
public IAccessor getAccessor() {
return new IAccessor() {
@Override
public List<IBase> getValues(Object theTarget) {
public List<IBase> getValues(IBase theTarget) {
ExtensionDt target = (ExtensionDt) theTarget;
if (target.getValue() != null) {
return Collections.singletonList((IBase) target.getValue());
}
ArrayList<IBase> retVal = new ArrayList<IBase>(target.getUndeclaredExtensions());
return retVal;
}

};
}

Expand Down Expand Up @@ -113,7 +114,7 @@ public int getMin() {
public IMutator getMutator() {
return new IMutator() {
@Override
public void addValue(Object theTarget, IBase theValue) {
public void addValue(IBase theTarget, IBase theValue) {
ExtensionDt target = (ExtensionDt) theTarget;
if (theValue instanceof IDatatype) {
target.setValue((IDatatype) theTarget);
Expand All @@ -123,7 +124,7 @@ public void addValue(Object theTarget, IBase theValue) {
}

@Override
public void setValue(Object theTarget, IBase theValue) {
public void setValue(IBase theTarget, IBase theValue) {
ExtensionDt target = (ExtensionDt) theTarget;
if (theValue instanceof IDatatype) {
target.setValue((IDatatype) theTarget);
Expand Down

0 comments on commit eaaf276

Please sign in to comment.