Skip to content

Commit

Permalink
yay it works!
Browse files Browse the repository at this point in the history
  • Loading branch information
fil512 committed Feb 21, 2019
1 parent 6b85337 commit dcd91ac
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 29 deletions.
Expand Up @@ -38,7 +38,7 @@ public void generateNarrative(FhirContext theFhirContext, IBaseResource theResou
if (name == null) return;

try {
String result = processTemplate(theFhirContext, name, theResource);
String result = processNamedTemplate(theFhirContext, name, theResource);

if (isCleanWhitespace()) {
ourLog.trace("Pre-whitespace cleaning: ", result);
Expand Down Expand Up @@ -68,7 +68,7 @@ public void generateNarrative(FhirContext theFhirContext, IBaseResource theResou
}
}

protected abstract String processTemplate(FhirContext theFhirContext, String theName, IBaseResource theResource) throws Exception;
protected abstract String processNamedTemplate(FhirContext theFhirContext, String theName, IBaseResource theResource) throws Exception;

protected String getName(FhirContext theContext, IBaseResource theResource) {
if (!myInitialized) {
Expand Down
Expand Up @@ -59,7 +59,7 @@ public abstract class BaseThymeleafNarrativeGenerator extends BaseNarrativeGener
private IMessageResolver resolver;

@Override
protected String processTemplate(FhirContext theContext, String theName, IBaseResource theResource) {
protected String processNamedTemplate(FhirContext theContext, String theName, IBaseResource theResource) {
Context context = new Context();
context.setVariable("resource", theResource);
context.setVariable("fhirVersion", theContext.getVersion().getVersion().name());
Expand Down
@@ -1 +1,5 @@
{% if %"FHIR_VERSION" = 'DSTU1' %}
<div class="hapiHeaderText">{{ Medication.name.text }}</div>
{% else %}
<div class="hapiHeaderText">{{ Medication.code.text }}</div>
{% endif %}

This file was deleted.

Expand Up @@ -19,27 +19,29 @@ public class DefaultLiquidNarrativeGenerator extends BaseNarrativeGenerator {

public static final String NARRATIVES_PROPERTIES = "classpath:ca/uhn/fhir/narrative/liquid/narratives.properties";
static final String HAPISERVER_NARRATIVES_PROPERTIES = "classpath:ca/uhn/fhir/narrative/liquid/narratives-hapiserver.properties";
private HapiWorkerContext myWorkerContext;

private boolean myUseHapiServerConformanceNarrative;

private LiquidEngine myLiquidEngine;

@Override
protected void initializeNarrativeEngine(FhirContext theFhirContext) {
// FIXME KHS
// MapEvaluationContext myHostServices = null;
// MapEvaluationContext myHostServices = new MapEvaluationContext();
// myHostServices.put("FHIR_VERSION", theFhirContext.getVersion().getVersion().name());
// myLiquidEngine = new LiquidEngine(new HapiWorkerContext(theFhirContext, new DefaultProfileValidationSupport()), myHostServices);
myLiquidEngine = new LiquidEngine(new HapiWorkerContext(theFhirContext, new DefaultProfileValidationSupport()), null);
LiquidEnvironmentVariables liquidEnvironmentVariables = new LiquidEnvironmentVariables();
liquidEnvironmentVariables.put("FHIR_VERSION", theFhirContext.getVersion().getVersion().name());
myWorkerContext = new HapiWorkerContext(theFhirContext, new DefaultProfileValidationSupport());
myLiquidEngine = new LiquidEngine(myWorkerContext, liquidEnvironmentVariables);
}

@Override
protected String processTemplate(FhirContext theFhirContext, String theName, IBaseResource theResource) throws Exception {
protected String processNamedTemplate(FhirContext theFhirContext, String theName, IBaseResource theResource) throws Exception {
String template = getNarrativeTemplate(theName);
// TODO How to make FhirContext available to namespace?
LiquidEngine.LiquidDocument doc = myLiquidEngine.parse(template, theName);
return myLiquidEngine.evaluate(doc, (Resource) theResource, null);
return processTemplate(theFhirContext, theName, (Resource) theResource, template);
}

private String processTemplate(FhirContext theFhirContext, String theName, Resource theResource, String theTemplate) throws Exception {
LiquidEngine.LiquidDocument doc = myLiquidEngine.parse(theTemplate, theName);
return myLiquidEngine.evaluate(doc, theResource, null);
}

@Override
Expand Down
@@ -0,0 +1,68 @@
package ca.uhn.fhir.r4.narrative;

import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.PathEngineException;
import org.hl7.fhir.r4.model.Base;
import org.hl7.fhir.r4.model.StringType;
import org.hl7.fhir.r4.model.TypeDetails;
import org.hl7.fhir.r4.utils.FHIRPathEngine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LiquidEnvironmentVariables implements FHIRPathEngine.IEvaluationContext {
private Map<String, String> constantMap = new HashMap<>();

void put(String key, String value) {
constantMap.put(key, value);
if (!key.startsWith("\"")) {
// Support both quoted and unquoted version of constant
constantMap.put("\"" + key + "\"", value);
}
}

@Override
public Base resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
String value = constantMap.get(name);
if (value == null) {
return null;
}
return new StringType(constantMap.get(name));
}

@Override
public TypeDetails resolveConstantType(Object appContext, String name) throws PathEngineException {
return null;
}

@Override
public boolean log(String argument, List<Base> focus) {
return false;
}

@Override
public FunctionDetails resolveFunction(String functionName) {
return null;
}

@Override
public TypeDetails checkFunction(Object appContext, String functionName, List<TypeDetails> parameters) throws PathEngineException {
return null;
}

@Override
public List<Base> executeFunction(Object appContext, String functionName, List<List<Base>> parameters) {
return null;
}

@Override
public Base resolveReference(Object appContext, String url) throws FHIRException {
return null;
}

@Override
public boolean conformsToProfile(Object appContext, Base item, String url) throws FHIRException {
return false;
}
}
Expand Up @@ -27,14 +27,14 @@
import static org.junit.Assert.assertTrue;

public class DefaultLiquidNarrativeGeneratorR4Test {
private static FhirContext ourCtx = FhirContext.forR4();
private static FhirContext ourFhirContext = FhirContext.forR4();
private static final Logger ourLog = LoggerFactory.getLogger(DefaultLiquidNarrativeGeneratorR4Test.class);
private DefaultLiquidNarrativeGenerator myNarrativeGenerator;

@Before
public void before() {
myNarrativeGenerator = new DefaultLiquidNarrativeGenerator();
ourCtx.setNarrativeGenerator(myNarrativeGenerator);
ourFhirContext.setNarrativeGenerator(myNarrativeGenerator);
}


Expand All @@ -60,7 +60,7 @@ public void testGeneratePatient() throws DataFormatException {
value.setBirthDate(new Date());

Narrative narrative = new Narrative();
myNarrativeGenerator.generateNarrative(ourCtx, value, narrative);
myNarrativeGenerator.generateNarrative(ourFhirContext, value, narrative);
String output = narrative.getDiv().getValueAsString();
ourLog.info(output);
assertThat(output, StringContains.containsString("<div class=\"hapiHeaderText\">joe john <b>BLOW </b></div>"));
Expand Down Expand Up @@ -126,7 +126,7 @@ public void testGenerateDiagnosticReport() throws DataFormatException {
value.addResult().setReference("Observation/3");

Narrative narrative = new Narrative();
myNarrativeGenerator.generateNarrative(ourCtx, value, narrative);
myNarrativeGenerator.generateNarrative(ourFhirContext, value, narrative);
String output = narrative.getDiv().getValueAsString();

ourLog.info(output);
Expand All @@ -149,10 +149,10 @@ public void testGenerateOperationOutcome() {
"</OperationOutcome>";
//@formatter:on

OperationOutcome oo = ourCtx.newXmlParser().parseResource(OperationOutcome.class, parse);
OperationOutcome oo = ourFhirContext.newXmlParser().parseResource(OperationOutcome.class, parse);

Narrative narrative = new Narrative();
myNarrativeGenerator.generateNarrative(ourCtx, oo, narrative);
myNarrativeGenerator.generateNarrative(ourFhirContext, oo, narrative);
String output = narrative.getDiv().getValueAsString();

ourLog.info(output);
Expand Down Expand Up @@ -192,7 +192,7 @@ public void testGenerateDiagnosticReportWithObservations() throws DataFormatExce
}

Narrative narrative = new Narrative();
myNarrativeGenerator.generateNarrative(ourCtx, value, narrative);
myNarrativeGenerator.generateNarrative(ourFhirContext, value, narrative);
String output = narrative.getDiv().getValueAsString();

ourLog.info(output);
Expand All @@ -213,7 +213,7 @@ public void testGenerateMedicationPrescription() {
mp.setAuthoredOnElement(new DateTimeType("2014-09-01"));

Narrative narrative = new Narrative();
myNarrativeGenerator.generateNarrative(ourCtx, mp, narrative);
myNarrativeGenerator.generateNarrative(ourFhirContext, mp, narrative);

assertTrue("Expected medication name of ciprofloaxin within narrative: " + narrative.getDiv().toString(), narrative.getDiv().toString().indexOf("ciprofloaxin") > -1);
assertTrue("Expected string status of ACTIVE within narrative: " + narrative.getDiv().toString(), narrative.getDiv().toString().indexOf("ACTIVE") > -1);
Expand All @@ -226,11 +226,9 @@ public void testGenerateMedication() {
med.getCode().setText("ciproflaxin");

Narrative narrative = new Narrative();
myNarrativeGenerator.generateNarrative(ourCtx, med, narrative);
myNarrativeGenerator.generateNarrative(ourFhirContext, med, narrative);

String string = narrative.getDiv().getValueAsString();
assertThat(string, containsString("ciproflaxin"));

}

}

0 comments on commit dcd91ac

Please sign in to comment.