Skip to content

Commit

Permalink
Moved logic in HL7Parser.parseMessageFieldValue to HL7Message.getValu…
Browse files Browse the repository at this point in the history
…e to avoid circular dependency
  • Loading branch information
basiliskus committed Dec 23, 2024
1 parent c524996 commit e350ce1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

/**
* Represents a HL7 message that implements the HealthData interface and adds methods to access the
Expand Down Expand Up @@ -74,6 +75,27 @@ public HL7Segment getSegment(String name) throws HL7MessageException {

public String getValue(String path) throws HL7MessageException {
HL7Path hl7Path = HL7Parser.parsePath(path);
return HL7Parser.parseMessageFieldValue(this, hl7Path);
int[] indices = hl7Path.indices();
List<String> fields = this.getSegment(hl7Path.segmentName()).fields();
char[] delimiters = this.getEncoding().getOrderedDelimiters();

if (indices[0] > fields.size()) {
return "";
}

String value = fields.get(indices[0] - 1);
for (int i = 1; i < indices.length; i++) {
if (i >= delimiters.length) {
throw new HL7ParserException("Invalid HL7 path: too many sub-levels provided");
}
char segmentDelimiter = delimiters[i];
int index = indices[i] - 1;
String[] parts = value.split(Pattern.quote(String.valueOf(segmentDelimiter)));
if (index < 0 || index >= parts.length) {
return "";
}
value = parts[index];
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,6 @@ public static HL7Message parseMessage(String content) {
return new HL7Message(segments, HL7Encoding.fromEncodingField(encodingCharactersField));
}

public static String parseMessageFieldValue(HL7Message message, HL7Path hl7Path)
throws HL7ParserException, HL7MessageException {
if (hl7Path == null || hl7Path.indices().length == 0 || message == null) {
throw new HL7ParserException("Invalid HL7 path: message or path is null or empty");
}

int[] indices = hl7Path.indices();
List<String> fields = message.getSegment(hl7Path.segmentName()).fields();
char[] delimiters = message.getEncoding().getOrderedDelimiters();

if (indices[0] > fields.size()) {
return "";
}

String value = fields.get(indices[0] - 1);
for (int i = 1; i < indices.length; i++) {
if (i >= delimiters.length) {
throw new HL7ParserException("Invalid HL7 path: too many sub-levels provided");
}
char segmentDelimiter = delimiters[i];
int index = indices[i] - 1;
String[] parts = value.split(Pattern.quote(String.valueOf(segmentDelimiter)));
if (index < 0 || index >= parts.length) {
return "";
}
value = parts[index];
}
return value;
}

public static HL7Path parsePath(String path) {
Matcher matcher = HL7_FIELD_NAME_PATTERN.matcher(path);
if (!matcher.matches()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ OBX|2|NM|||3122||||||F|||202402221854-0500||"""
message.getValue("PID-40.4.2") == "LRI_NDBS_COMPONENT"
message.getValue("PID-40.4.3") == ""
message.getValue("NK1-33.4.1.1") == "Medicaid"
message.getValue("PID-99") == ""
}

def "getValue should throws an exception when the hl7 field index has too many sublevels"() {
Expand All @@ -106,10 +107,4 @@ OBX|2|NM|||3122||||||F|||202402221854-0500||"""
then:
thrown(HL7MessageException)
}

// TODO: move to HL7EncodingTest
def "getEscapeCharacter should return the escape character"() {
expect:
message.getEncoding().getEscapeCharacter() == '\\' as char
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,79 +49,4 @@ PID|||12345"""
then:
result.getSegments().get(0).fields().get(3) == ""
}

def "parseMessageFieldValue should handle different field levels"() {
given:
def message = HL7Parser.parseMessage("TST|value1|component1^component2|rep1~rep2^|comp1^~sub1&sub2")

when:
def hl7Path = HL7Parser.parsePath(path)
def result = HL7Parser.parseMessageFieldValue(message, hl7Path)

then:
result == expectedValue

where:
scenario | path | expectedValue
"simple field" | "TST-1" | "value1"
"component" | "TST-2.2" | "component2"
"repetition" | "TST-3.1.2" | "rep2"
"subcomponent" | "TST-4.2.2.2" | "sub2"
"invalid index" | "TST-5" | ""
}

def "parseMessageFieldValue returns an exception when hl7 path argument is empty or null"() {
when:
def hl7Path = HL7Parser.parsePath("")
HL7Parser.parseMessageFieldValue(null, hl7Path)

then:
thrown(HL7ParserException)

when:
def message = HL7Parser.parseMessage("MSH|fakeValues")
HL7Parser.parseMessageFieldValue(message, null)

then:
thrown(HL7ParserException)
}

def "parseMessageFieldValue returns an exception when message argument is null"() {
when:
HL7Parser.parseMessageFieldValue(null, null)

then:
thrown(HL7ParserException)

when:
def hl7Path = HL7Parser.parsePath("MSH-3")
HL7Parser.parseMessageFieldValue(null, hl7Path)

then:
thrown(HL7ParserException)
}

def "parseMessageFieldValue returns an exception when message argument is empty"() {
given:
def hl7Path = HL7Parser.parsePath("MSH-3")
def message = HL7Parser.parseMessage("")

when:
def out = HL7Parser.parseMessageFieldValue(message, hl7Path)

then:
thrown(HL7MessageException)
}

def "parseMessageFieldValue returns an empty string if the indices in hl7 path are pointing outside the expected range"() {
given:
def message = HL7Parser.parseMessage("MSH|fakeValues\nOBR|fakeValues")
def hl7Path = HL7Parser.parsePath("MSH-3")

when:
def out = HL7Parser.parseMessageFieldValue(message, hl7Path)

then:
out == ""
}
}

0 comments on commit e350ce1

Please sign in to comment.