How to set repetitions in HL7 messages using HAPI Terser

In the previous post How to use Hapi Terser with HL7 I published an example about how to retrieve field repetitions from an HL7 message. In this post I will show hot to set repetitions within an HL7 message in 3 different ways:

  • Manually
  • Using for statement
  • Setting Segment Repetitions

 MANUALLY

In this example we will set two different addresses in the PID (Patient Identification) segment.
[java]
@Test
public void testSetManualRepetitions(){
try {
String m = “MSH|^~\\&|hl7Integration|hl7Integration|||||ADT^A01|||2.3|\r” +
“EVN|A01|20130617154644\r” +
“PID|1|465 306 5961||407623|Wood^Patrick^^^MR||19700101|1||||||||||\r” +
“PV1|1||Location||||||||||||||||261938_6_201306171546|||||||||||||||||||||||||20130617134644|||||”;

//Create the Terser
PipeParser pipeParser = new PipeParser();
Message message = pipeParser.parse(m);
Terser terser = new Terser(message);

//Add first Address
terser.set(“/.PID-11(0)-1”, “13 Oxford Road”);
terser.set(“/.PID-11(0)-3”, “Oxford”);
//Add second Address
terser.set(“/.PID-11(1)-1”, “16 London Road”);
terser.set(“/.PID-11(1)-3”, “London”);

System.out.println(message.encode());

} catch (HL7Exception e) {
e.printStackTrace();
}

}
[/java]

And here is the result:

MSH|^~\&|hl7Integration|hl7Integration|||||ADT^A01|||2.3
EVN|A01|20130617154644
PID|1|465 306 5961||407623|Wood^Patrick^^^MR||19700101|1|||13 Oxford Road^^Oxford~16 London Road^^London
PV1|1||Location||||||||||||||||261938_6_201306171546|||||||||||||||||||||||||20130617134644

SET REPETITIONS USING FOR STATEMENT

In this example, I will be using the for statement, but you can use other loop statements as while or do..while.
As in the previous case, I will add addresses to the PID segment, but in this case we will add 5 addresses using the for statement.

[java]

@Test
public void testSetLoopRepetitions(){
try {
String m = “MSH|^~\\&|hl7Integration|hl7Integration|||||ADT^A01|||2.3|\r” +
“EVN|A01|20130617154644\r” +
“PID|1|465 306 5961||407623|Wood^Patrick^^^MR||19700101|1||||||||||\r” +
“PV1|1||Location||||||||||||||||261938_6_201306171546|||||||||||||||||||||||||20130617134644|||||||||”;

//Create the Terser
PipeParser pipeParser = new PipeParser();
Message message = pipeParser.parse(m);
Terser terser = new Terser(message);

//Add 5 addresses
int maxRepetitions = 5;
String street;
String city;
for (int i= 0; i < maxRepetitions; i++ ){
street = “Street” + Integer.toString(i);
terser.set(“PID-11(“+i+”)-1″, street);
city = “City” + Integer.toString(i);
terser.set(“PID-11(“+i+”)-3″, city);
}

System.out.println(message.encode());

} catch (HL7Exception e) {
e.printStackTrace();
}
}
[/java]
You can see now that the message output has 5 new addresses:

MSH|^~\&|hl7Integration|hl7Integration|||||ADT^A01|||2.3
EVN|A01|20130617154644
PID|1|465 306 5961||407623|Wood^Patrick^^^MR||19700101|1|||Street0^^City0~Street1^^City1~Street2^^City2~Street3^^City3~Street4^^City4
PV1|1||Location||||||||||||||||261938_6_201306171546|||||||||||||||||||||||||20130617134644

SETTING SEGMENT REPETITIONS


Previously, we have set repetitions inside a segment, but what happen when we want to repeat segments?
In this example, I will add 2 NK1 (next of kin) segments. Please note that the only difference between this example and the previous ones is the place where we put the brackets, in the terser expression. In this case, we put the repetition next to the segment name, for instance:
terser.set(“/.NK1(1)-1-1″, “1″);

[java]
@Test
public void testSetSegmentRepetitions(){
try {

String m = “MSH|^~\\&|hl7Integration|hl7Integration|||||ADT^A01|||2.3|\r” +
“EVN|A01|20130617154644\r” +
“PID|1|465 306 5961||407623|Wood^Patrick^^^MR||19700101|1||||||||||\r” +
“PV1|1||Location||||||||||||||||261938_6_201306171546|||||||||||||||||||||||||20130617134644|||||||||”;

//Create the Terser
PipeParser pipeParser = new PipeParser();
Message message = pipeParser.parse(m);
Terser terser = new Terser(message);

//Add first next of Kin
//NK1|1|Jones^Joe|Father||||||
terser.set(“/.NK1(1)-1-1”, “1”);
terser.set(“/.NK1(1)-2-1”, “Jones”);
terser.set(“/.NK1(1)-2-2”, “Joe”);
terser.set(“/.NK1(1)-3-1”, “Father”);

//Add Second next of kin
//NK1|2|Hall^Anna|Mother
terser.set(“/.NK1(2)-1-1”, “2”);
terser.set(“/.NK1(2)-2-1”, “Hall”);
terser.set(“/.NK1(2)-2-2”, “Anna”);
terser.set(“/.NK1(2)-3-1”, “Mother”);

System.out.println(message.encode());

} catch (HL7Exception e) {
e.printStackTrace();
}
}
[/java]

As you can see there are two new NK1 segments in the HL7 message:

MSH|^~\&|hl7Integration|hl7Integration|||||ADT^A01|||2.3
EVN|A01|20130617154644
PID|1|465 306 5961||407623|Wood^Patrick^^^MR||19700101|1
NK1|1|Jones^Joe|Father
NK1|2|Hall^Anna|Mother
PV1|1||Location||||||||||||||||261938_6_201306171546|||||||||||||||||||||||||20130617134644

You can find the whole project in github.

You may also like...

2 Responses

  1. Zakariia says:

    Ignacio,
    I’ve found both of your tutorials about using the HAPI terser very useful. Thank you for taking the time to write them up for everyone’s benefit!
    One question that popped to my head as soon as I read your second tutorial above is why you are adding a dot “.” to the location expression when you added the two NK1 segments:

    terser.set(“/.NK1(1)-1-1″, “1″);

    Reading the HAPI Javadoc, I found this explanation:

    Here, a . indicates that the group should be searched for (using a SegmentFinder) starting at the current location in the message

    So my question is: did you use the “.” to force the Terser to write the “Mother” NK1 after the “Father” NK1?

    It would be great if you can shed some more light on the concept of the “Finder” and how it can be used.

    Thanks

  1. March 16, 2014

    […] Next → […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.