Skip to content

Commit

Permalink
Fixed a couple of issues in HL7Message and refactored to extract segm…
Browse files Browse the repository at this point in the history
…ent into a HL7Segment record

Co-authored-by: luis-pabon-tf <luis-pabon-tf@users.noreply.github.com>
  • Loading branch information
basiliskus and luis-pabon-tf committed Dec 16, 2024
1 parent 031b66c commit b4380a6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,36 @@
*/
public class HL7Message implements HealthData<HL7Message> {

private final Map<String, List<String>> segments;
private final List<HL7Segment> segments;
private final Map<String, Character> encodingCharacters;

public HL7Message(
Map<String, List<String>> segments, Map<String, Character> encodingCharacters) {
HL7Message(List<HL7Segment> segments, Map<String, Character> encodingCharacters) {
this.segments = segments;
this.encodingCharacters = encodingCharacters;
}

public List<HL7Segment> getSegments(String name) {
return segments.stream().filter(segment -> segment.name().equals(name)).toList();
}

public HL7Segment getSegment(String name, int index) {
return getSegments(name).get(index);
}

public HL7Segment getSegment(String name) {
return getSegments(name).get(0);
}

public List<String> getSegmentFields(String name, int index) {
return getSegment(name, index).fields();
}

public List<String> getSegmentFields(String name) {
return segments.get(name);
return getSegment(name, 0).fields();
}

public int getSegmentCount(String name) {
var matches = getSegmentFields(name);
if (matches != null) {
return matches.size();
}
return 0;
return getSegments(name).size();
}

public String getValue(String hl7Path) {
Expand Down Expand Up @@ -83,21 +94,19 @@ public String getIdentifier() {
public String toString() {
return String.join(
HL7Parser.DEFAULT_SEGMENT_DELIMITER,
segments.entrySet().stream().map(this::formatSegment).toList());
segments.stream().map(this::segmentToString).toList());
}

private String formatSegment(Map.Entry<String, List<String>> entry) {
String name = entry.getKey();
List<String> fields = entry.getValue();
private String segmentToString(HL7Segment segment) {
String fieldSeparator =
String.valueOf(getEncodingCharacter(HL7Parser.FIELD_DELIMITER_NAME));

return name
+ (name.equals(HL7Parser.MSH_SEGMENT_NAME) ? fields.get(0) : fieldSeparator)
+ String.join(
fieldSeparator,
name.equals(HL7Parser.MSH_SEGMENT_NAME)
? fields.subList(1, fields.size())
: fields);
if (segment.name().equals(HL7Parser.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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -32,7 +31,7 @@ public class HL7Parser {
protected static final Pattern HL7_FIELD_NAME_PATTERN = Pattern.compile("(\\w+)(?:-(\\S+))?");

public static HL7Message parse(String content) {
Map<String, List<String>> segments = new HashMap<>();
List<HL7Segment> segments = new ArrayList<>();
String encodingCharactersField = null;
String[] lines = content.split(NEWLINE_REGEX);
for (String line : lines) {
Expand All @@ -45,7 +44,7 @@ public static HL7Message parse(String content) {
encodingCharactersField = fields[1];
segmentFields.add(0, String.valueOf(DEFAULT_FIELD_DELIMITER));
}
segments.put(segmentName, segmentFields);
segments.add(new HL7Segment(segmentName, segmentFields));
}

return new HL7Message(segments, getEncodingCharacters(encodingCharactersField));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gov.hhs.cdc.trustedintermediary.rse2e.hl7;

import java.util.List;

public record HL7Segment(String name, List<String> fields) {

public int getIndex() {
return Integer.parseInt(fields.get(0));

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,19 @@ import spock.lang.Specification
class HL7ExpressionEvaluatorTest extends Specification {

def evaluator = HL7ExpressionEvaluator.getInstance()
def encodingChars = HL7Parser.getEncodingCharacters("|^~\\&")

HL7Message mshMessage
String mshSegmentText
HL7Message hl7Message
String messageContent

def setup() {
TestApplicationContext.reset()
TestApplicationContext.init()
TestApplicationContext.register(HL7ExpressionEvaluator, evaluator)

mshSegmentText = "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"

def segments = [
MSH: [
"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\n"
],
PID: [
"PID|1||11102779^^^CR^MR||SMITH^BB SARAH^^^^^L\n"
]
]

mshMessage = new HL7Message(segments as Map<String, List<String>>, encodingChars)
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.parse(messageContent)

TestApplicationContext.injectRegisteredImplementations()
}
Expand Down Expand Up @@ -191,7 +182,7 @@ class HL7ExpressionEvaluatorTest extends Specification {
def operator = "="

when:
def result = evaluator.evaluateCollectionCount(mshMessage, segmentName, rightOperand, operator)
def result = evaluator.evaluateCollectionCount(hl7Message, segmentName, rightOperand, operator)

then:
result
Expand All @@ -204,7 +195,7 @@ class HL7ExpressionEvaluatorTest extends Specification {
def operator = "="

when:
def result = evaluator.evaluateCollectionCount(mshMessage, segmentName, rightOperand, operator)
def result = evaluator.evaluateCollectionCount(hl7Message, segmentName, rightOperand, operator)

then:
!result
Expand All @@ -217,7 +208,7 @@ class HL7ExpressionEvaluatorTest extends Specification {
def operator = "="

when:
evaluator.evaluateCollectionCount(mshMessage, segmentName, rightOperand, operator)
evaluator.evaluateCollectionCount(hl7Message, segmentName, rightOperand, operator)

then:
def e = thrown(IllegalArgumentException)
Expand All @@ -231,7 +222,7 @@ class HL7ExpressionEvaluatorTest extends Specification {
def operator = "="

when:
def result = evaluator.evaluateCollectionCount(mshMessage, segmentName, rightOperand, operator)
def result = evaluator.evaluateCollectionCount(hl7Message, segmentName, rightOperand, operator)

then:
!result
Expand All @@ -257,7 +248,7 @@ class HL7ExpressionEvaluatorTest extends Specification {
def msh3 = "Sender Application^sender.test.com^DNS"

when:
def result = evaluator.getLiteralOrFieldValue(mshMessage, inputMessage, operand)
def result = evaluator.getLiteralOrFieldValue(hl7Message, inputMessage, operand)

then:
result == msh3
Expand All @@ -270,7 +261,7 @@ class HL7ExpressionEvaluatorTest extends Specification {
def inputMessage = Mock(HL7Message)

when:
def result = evaluator.getFieldValue(mshMessage, inputMessage, fieldName)
def result = evaluator.getFieldValue(hl7Message, inputMessage, fieldName)

then:
result == msh3
Expand All @@ -282,7 +273,7 @@ class HL7ExpressionEvaluatorTest extends Specification {
def inputMessage = Mock(HL7Message)

when:
evaluator.getFieldValue(mshMessage, inputMessage, fieldName)
evaluator.getFieldValue(hl7Message, inputMessage, fieldName)

then:
def e = thrown(IllegalArgumentException)
Expand All @@ -295,7 +286,7 @@ class HL7ExpressionEvaluatorTest extends Specification {
def inputMessage = Mock(HL7Message)

when:
evaluator.getFieldValue(mshMessage, inputMessage, fieldName)
evaluator.getFieldValue(hl7Message, inputMessage, fieldName)

then:
def e = thrown(IllegalArgumentException)
Expand Down

0 comments on commit b4380a6

Please sign in to comment.