Skip to content

Commit

Permalink
Bundle return (#4)
Browse files Browse the repository at this point in the history
* refactoring and adding new function to return Bundle

* refactor

Signed-off-by: ayushgarg0694 <ayushgarg0694@gmail.com>

* specific imports

Signed-off-by: ayushgarg0694 <ayushgarg0694@gmail.com>
  • Loading branch information
ayushgarg0694 authored and LisaWellman committed Jan 14, 2022
1 parent f74a9c4 commit 4339970
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package io.github.linuxforhealth.api;

import org.hl7.fhir.r4.model.Bundle;

import java.util.List;

/**
Expand All @@ -25,10 +27,10 @@ public interface MessageTemplate<T> {
*
* @param data - Input
* @param engine - {@link MessageEngine}
* @return String -JSON representation of FHIR bundle
* @return Bundle - {@link Bundle}
*
*/
String convert(T data, MessageEngine engine);
Bundle convert(T data, MessageEngine engine);

/**
* Name of the message
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/io/github/linuxforhealth/fhir/FHIRContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public static FhirValidator getValidator() {
return validator;
}

public String encodeResourceToString(Bundle bundle) {
public String encodeResourceToString(Bundle bundle){
return this.parser.encodeResourceToString(bundle);
}

public void validate(Bundle bundle) {
if (validateResource) {
ValidationResult result = getValidator().validateWithResult(bundle);
// The result object now contains the validation results
Expand All @@ -83,8 +87,9 @@ public String encodeResourceToString(Bundle bundle) {
}

}

}
return this.parser.encodeResourceToString(bundle);

}

private static void initValidator() {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/io/github/linuxforhealth/hl7/HL7ToFHIRConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,37 @@ public String convert(String hl7MessageData, ConverterOptions options) {
FHIRContext context = new FHIRContext(options.isPrettyPrint(), options.isValidateResource());
HL7MessageEngine engine = new HL7MessageEngine(context, options.getBundleType());

Message hl7message = getHl7Message(hl7MessageData);
if (hl7message != null) {
String messageType = HL7DataExtractor.getMessageType(hl7message);
HL7MessageModel hl7MessageTemplateModel = messagetemplates.get(messageType);
if (hl7MessageTemplateModel != null) {
Bundle bundle = hl7MessageTemplateModel.convert(hl7message, engine);
return engine.getFHIRContext().encodeResourceToString(bundle);
} else {
throw new UnsupportedOperationException("Message type not yet supported " + messageType);
}
} else {
throw new IllegalArgumentException("Parsed HL7 message was null.");
}
}

/**
* Converts the input HL7 message (String data) into FHIR bundle resource.
*
* @param hl7MessageData Message to convert
* @param options Options for conversion
*
* @return Bundle {@link Bundle} resource.
* @throws UnsupportedOperationException - if message type is not supported
*/
public Bundle convertToBundle(String hl7MessageData, ConverterOptions options) {
Preconditions.checkArgument(StringUtils.isNotBlank(hl7MessageData),
"Input HL7 message cannot be blank");
Preconditions.checkArgument(options != null, "options cannot be null.");
FHIRContext context = new FHIRContext(options.isPrettyPrint(), options.isValidateResource());
HL7MessageEngine engine = new HL7MessageEngine(context, options.getBundleType());

Message hl7message = getHl7Message(hl7MessageData);
if (hl7message != null) {
String messageType = HL7DataExtractor.getMessageType(hl7message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.r4.model.Bundle;
import com.fasterxml.jackson.annotation.JsonCreator;
Expand All @@ -21,97 +22,99 @@
import io.github.linuxforhealth.api.MessageTemplate;
import io.github.linuxforhealth.hl7.parsing.HL7DataExtractor;
import io.github.linuxforhealth.hl7.parsing.HL7HapiParser;
import org.apache.commons.lang3.exception.ExceptionUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HL7MessageModel implements MessageTemplate<Message> {

private List<FHIRResourceTemplate> resources;
private String messageName;
private static final Logger LOGGER = LoggerFactory.getLogger(HL7MessageModel.class);

@JsonCreator
public HL7MessageModel(@JsonProperty("messageName") String messageName,
@JsonProperty("resources") List<HL7FHIRResourceTemplate> resources) {
this.messageName = messageName;
this.resources = new ArrayList<>();
if (resources != null && !resources.isEmpty()) {
this.resources.addAll(resources);
}
private List<FHIRResourceTemplate> resources;
private String messageName;
private static final Logger LOGGER = LoggerFactory.getLogger(HL7MessageModel.class);

@JsonCreator
public HL7MessageModel(@JsonProperty("messageName") String messageName,
@JsonProperty("resources") List<HL7FHIRResourceTemplate> resources) {
this.messageName = messageName;
this.resources = new ArrayList<>();
if (resources != null && !resources.isEmpty()) {
this.resources.addAll(resources);
}

}
}

private void handleException(Exception e) {
StackTraceElement[] stackTrace = e.getStackTrace();
StringBuilder classAndStack = new StringBuilder();
classAndStack.append(e.getClass() + "\n");
for (int i = 0; i < stackTrace.length; i++) {
classAndStack.append(stackTrace[i] + "\n");
}
LOGGER.error("Error transforming HL7 message. {}", classAndStack);
}


public String convert(String message, MessageEngine engine) throws IOException {
Preconditions.checkArgument(StringUtils.isNotBlank(message),
"Input Hl7 message cannot be blank");
HL7HapiParser hparser = null;
try {
hparser = new HL7HapiParser();
public String convert(String message, MessageEngine engine) throws IOException {
Preconditions.checkArgument(StringUtils.isNotBlank(message),
"Input Hl7 message cannot be blank");
HL7HapiParser hparser = null;
try {
hparser = new HL7HapiParser();

Message hl7message = hparser.getParser().parse(message);
return convert(hl7message, engine);
Message hl7message = hparser.getParser().parse(message);
Bundle bundle = convert(hl7message, engine);
return engine.getFHIRContext().encodeResourceToString(bundle);

} catch (HL7Exception e) {
throw new IllegalArgumentException("Cannot parse the message.", e);
} finally {
if (hparser != null) {
hparser.getContext().close();
}
}

} catch (HL7Exception e) {
throw new IllegalArgumentException("Cannot parse the message.", e);
} finally {
if (hparser != null) {
hparser.getContext().close();
}
}

}

@Override
public Bundle convert(Message message, MessageEngine engine) {
Preconditions.checkArgument(message != null, "Input Hl7 message cannot be null");
Preconditions.checkArgument(engine != null, "MessageEngine cannot be null");

@Override
public String convert(Message message, MessageEngine engine) {
Preconditions.checkArgument(message != null, "Input Hl7 message cannot be null");
Preconditions.checkArgument(engine != null, "MessageEngine cannot be null");
HL7DataExtractor hl7DTE = new HL7DataExtractor(message);
HL7MessageData dataSource = new HL7MessageData(hl7DTE);

HL7DataExtractor hl7DTE = new HL7DataExtractor(message);
HL7MessageData dataSource = new HL7MessageData(hl7DTE);
Bundle bundle = null;

String result = null;
// Catch any exceptions and log them without the message.
// NOTE: We have seen PHI in these exception messages.
try {
bundle = engine.transform(dataSource, this.getResources(), new HashMap<>());
engine.getFHIRContext().validate(bundle);

// Catch any exceptions and log them without the message.
// NOTE: We have seen PHI in these exception messages.
try {
Bundle bundle = engine.transform(dataSource, this.getResources(), new HashMap<>());
result = engine.getFHIRContext().encodeResourceToString(bundle);
}
catch(Exception e) {
// Print stack class and trace without the error message.
StackTraceElement[] stackTrace = e.getStackTrace();
StringBuilder classAndStack = new StringBuilder();
classAndStack.append(e.getClass()+"\n");
for(int i=0; i < stackTrace.length; i++) {
classAndStack.append(stackTrace[i] +"\n");
} catch (Exception e) {
// Print stack class and trace without the error message.
handleException(e);
}
LOGGER.error("Error transforming HL7 message. {}",classAndStack);
}

return result;

}
return bundle;

}

@Override
public String getMessageName() {
return messageName;
}

public void setMessageName(String messageName) {
this.messageName = messageName;
}
@Override
public String getMessageName() {
return messageName;
}

@Override
public List<FHIRResourceTemplate> getResources() {
return new ArrayList<>(resources);
}
public void setMessageName(String messageName) {
this.messageName = messageName;
}

@Override
public List<FHIRResourceTemplate> getResources() {
return new ArrayList<>(resources);
}


}
25 changes: 22 additions & 3 deletions src/test/java/io/github/linuxforhealth/FHIRConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import org.apache.commons.io.IOUtils;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.r4.model.Bundle.BundleType;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Coding;
import org.hl7.fhir.r4.model.Identifier;
Expand All @@ -29,6 +26,9 @@
import org.hl7.fhir.r4.model.Quantity;
import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.model.ResourceType;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.r4.model.Bundle.BundleType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
Expand All @@ -50,6 +50,25 @@ class FHIRConverterTest {
private static final ConverterOptions OPTIONS = new Builder().withValidateResource().withPrettyPrint().build();
private static final Logger LOGGER = LoggerFactory.getLogger(FHIRConverterTest.class);

@Test
void test_patient_encounter_bundle_return() throws IOException {

String hl7message = "MSH|^~\\&|REGADT|MCM|RSP1P8|MCM|200301051530|SEC|ADT^A40^ADT_A39|00000003|P|2.6\n" +
"PID|||MR1^^^XYZ||MAIDENNAME^EVE\n" +
"MRG|MR2^^^XYZ\n";

HL7ToFHIRConverter ftv = new HL7ToFHIRConverter();
Bundle b = ftv.convertToBundle(hl7message, OPTIONS);

assertThat(b.getType()).isEqualTo(BundleType.COLLECTION);

List<Resource> patientResource = b.getEntry().stream()
.filter(v -> ResourceType.Patient == v.getResource().getResourceType())
.map(BundleEntryComponent::getResource).collect(Collectors.toList());
assertThat(patientResource).hasSize(2);

}

@Test
void test_patient_encounter() throws IOException {

Expand Down

0 comments on commit 4339970

Please sign in to comment.