Skip to content

Commit

Permalink
Removed HL7Parser and moved message parsing logic to HL7Message to av…
Browse files Browse the repository at this point in the history
…oid circular dependencies
  • Loading branch information
basiliskus committed Dec 23, 2024
1 parent 466c3f4 commit 4c0a923
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Map<String, HL7Message> parseAndMapMessageByControlId(List<HL7FileStream>
String fileName = hl7FileStream.fileName();
try (InputStream inputStream = hl7FileStream.inputStream()) {
String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
HL7Message message = HL7Parser.parseMessage(content);
HL7Message message = HL7Message.parse(content);
String msh10 = message.getIdentifier();
if (msh10 == null || msh10.isEmpty()) {
throw new HL7FileMatcherException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gov.hhs.cdc.trustedintermediary.rse2e.hl7;

import gov.hhs.cdc.trustedintermediary.wrappers.HealthData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/**
Expand All @@ -10,6 +13,9 @@
*/
public class HL7Message implements HealthData<HL7Message> {

static final String MSH_SEGMENT_NAME = "MSH";
static final String NEWLINE_REGEX = "\\r?\\n|\\r";

private final List<HL7Segment> segments;
private final HL7Encoding encoding;

Expand Down Expand Up @@ -37,10 +43,32 @@ public String toString() {
return String.join(
HL7Encoding.DEFAULT_SEGMENT_DELIMITER,
getSegments().stream()
.map(segment -> HL7Parser.segmentToString(segment, this.encoding))
.map(segment -> segmentToString(segment, this.encoding))
.toList());
}

public static HL7Message parse(String content) {
List<HL7Segment> segments = new ArrayList<>();
String encodingCharactersField = null;
String[] lines = content.split(NEWLINE_REGEX);
for (String line : lines) {
if (line.trim().isEmpty()) continue;
String[] fields =
line.split(
Pattern.quote(String.valueOf(HL7Encoding.DEFAULT_FIELD_DELIMITER)), -1);
String segmentName = fields[0];
List<String> segmentFields =
new ArrayList<>(Arrays.asList(fields).subList(1, fields.length));
if (Objects.equals(segmentName, MSH_SEGMENT_NAME)) {
encodingCharactersField = fields[1];
segmentFields.add(0, String.valueOf(HL7Encoding.DEFAULT_FIELD_DELIMITER));
}
segments.add(new HL7Segment(segmentName, segmentFields));
}

return new HL7Message(segments, HL7Encoding.fromEncodingField(encodingCharactersField));
}

public List<HL7Segment> getSegments() {
return this.segments;
}
Expand Down Expand Up @@ -98,4 +126,16 @@ public String getValue(String path) throws HL7MessageException {
}
return value;
}

protected static String segmentToString(HL7Segment segment, HL7Encoding encoding) {
String fieldSeparator = String.valueOf(encoding.getFieldDelimiter());

if (segment.name().equals(MSH_SEGMENT_NAME)) {
return segment.name()
+ segment.fields().get(0)
+ String.join(
fieldSeparator, segment.fields().subList(1, segment.fields().size()));
}
return segment.name() + fieldSeparator + String.join(fieldSeparator, segment.fields());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class HL7ExpressionEvaluatorTest extends Specification {

messageContent = """MSH|^~\\&|Sender Application^sender.test.com^DNS|Sender Facility^0.0.0.0.0.0.0.0^ISO|Receiver Application^0.0.0.0.0.0.0.0^ISO|Receiver Facility^simulated-lab-id^DNS|20230101010000-0000||ORM^O01^ORM_O01|111111|T|2.5.1
PID|1||11102779^^^CR^MR||SMITH^BB SARAH^^^^^L"""
hl7Message = HL7Parser.parseMessage(messageContent)
hl7Message = HL7Message.parse(messageContent)

TestApplicationContext.injectRegisteredImplementations()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,52 @@ ORC|NW|4560411583^ORDERID||||||||||12345^^^^^^^^NPI&2.16.840.1.113883.4.6&ISO^L|
OBR|1|4560411583^ORDERID||54089-8^Newborn screening panel AHIC^LN|||202402221854-0500|||||||||12345^^^^^^^^NPI&2.16.840.1.113883.4.6&ISO^L||||||||
OBX|1|ST|57723-9^Unique bar code number of Current sample^LN||123456||||||F|||202402221854-0500
OBX|2|NM|||3122||||||F|||202402221854-0500||"""
message = HL7Parser.parseMessage(messageContent)
message = HL7Message.parse(messageContent)
}

def "parse should handle basic HL7 message"() {
given:
def content = """MSH|^~\\&|sending_app|sending_facility
PID|||12345||Doe^John||19800101|M"""

when:
def message = HL7Message.parse(content)
def segments = message.getSegments()

then:
segments.size() == 2
segments[0].name() == "MSH"
segments[0].fields().size() == 4
segments[0].fields().get(0) == "|"
segments[0].fields().get(1) == "^~\\&"
segments[1].name() == "PID"
segments[1].fields().size() == 8
segments[1].fields().get(2) == "12345"
segments[1].fields().get(4) == "Doe^John"
}

def "parse should handle empty lines in message"() {
given:
def content = """MSH|^~\\&|sending_app
PID|||12345"""

when:
def result = HL7Message.parse(content)

then:
result.getSegments().size() == 2
}

def "parse should preserve empty fields"() {
given:
def content = "MSH|^~\\&|sending_app||sending_facility"

when:
def result = HL7Message.parse(content)

then:
result.getSegments().get(0).fields().get(3) == ""
}

def "getUnderlyingData should correctly return itself"() {
Expand Down

This file was deleted.

0 comments on commit 4c0a923

Please sign in to comment.