Skip to content

Commit

Permalink
Fixes hapifhir#45 : Fix Field repetition in Location object when requ…
Browse files Browse the repository at this point in the history
…ired
  • Loading branch information
Olivier Mourez committed Mar 11, 2019
1 parent 41ac02b commit 935bb33
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
8 changes: 6 additions & 2 deletions hapi-base/src/main/java/ca/uhn/hl7v2/model/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ public boolean accept(MessageVisitor visitor, Location currentLocation) throws H
if (visitor.start(this, currentLocation)) {
for (int i=0; i < reps.length; i++) {
Type t = reps[i];
if (!t.accept(visitor, currentLocation))
Location nextLocation = currentLocation;
if (reps.length > 1) {
nextLocation = provideLocation(currentLocation, currentLocation.getField(), i);
}
if (!t.accept(visitor, nextLocation))
break;
}
}
return visitor.end(this, currentLocation);
}

public Location provideLocation(Location parentLocation, int index, int repetition) {
return new Location(parentLocation).withField(index);
return new Location(parentLocation).withField(index).withFieldRepetition(repetition);
}

public boolean isEmpty() throws HL7Exception {
Expand Down
34 changes: 34 additions & 0 deletions hapi-test/src/test/java/ca/uhn/hl7v2/model/MessageVisitorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static ca.uhn.hl7v2.model.MessageVisitors.visit;
import static ca.uhn.hl7v2.model.MessageVisitors.visitPopulatedElements;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -41,6 +42,39 @@ public void testVisitOmdO03() throws HL7Exception, IOException {
assertEquals("Unexpected value", terser.get(l.toString()), tmv.nonEmptyPrimitives.get(l));
}
}

@Test
public void testVisitOmdO03WithRep() throws HL7Exception, IOException {
String msg = loadFile("/ca/uhn/hl7v2/parser/omd_o03_rep.txt", '\r');
PipeParser pipeParser = new PipeParser();
Message message = pipeParser.parse(msg);

// Visit all non-empty elements using the TestMessageVisitor
TestMessageVisitor tmv = visit(message, visitPopulatedElements(new TestMessageVisitor())).getDelegate();

assertEquals("Visitor start/end method calls not equal", 0, tmv.level);
assertEquals("Wrong number of non-empty primitive values", 47, tmv.nonEmptyPrimitives.size());

// PID|1||CIPNUM^^^CAEX^CIP^^^^EX&&ES-EX~CIPTWO||DUMAS^VICTOR HUGO|ZAPATA|19740325|1||||||||||||||||||||||||A~B

// Test that all non-empty primitive locations can be used as input to Terser.get(String)
// and all returned values correspond with the visited values.
Terser terser = new Terser(message);

assertEquals("CIPNUM", terser.get("/PATIENT/PID-3(0)-1"));
assertEquals("CIPTWO", terser.get("/PATIENT/PID-3(1)-1"));
assertEquals("CIPNUM", terser.get("/PATIENT/PID-3-1"));
assertEquals("A", terser.get("/PATIENT/PID-32(0)"));
assertEquals("B", terser.get("/PATIENT/PID-32(1)"));
assertEquals("A", terser.get("/PATIENT/PID-32"));
assertEquals("DUMAS", terser.get("/PATIENT/PID-5(0)-1"));
assertNull(terser.get("/PATIENT/PID-6(1)-1"));

for (Location l : tmv.nonEmptyPrimitives.keySet()) {
assertEquals("Unexpected value for " + l.toString(), terser.get(l.toString()), tmv.nonEmptyPrimitives.get(l));
}
}

/**
@Test
public void testEncodeOmdO03() throws HL7Exception, IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|^~\&|DIETOOLS|1^DOMINION|JARA||20101027181706||OMD^O03^OMD_O03|20101027181706|P|2.5|||ER|ALPID|1||CIPNUM^^^CAEX^CIP^^^^EX&&ES-EX~CIPTWO||DUMAS^VICTOR HUGO|ZAPATA|19740325|1||||||||||||||||||||||||A~BPV1|1|I|^^51C302-1^^^^^^^0005&51UHP31&UH TERCERA 1 HSPA&TIPOUOENF||||||||||||||||100002739^005^^^^^^^^0005&APARATO DIGESTIVO&5DIG&TIPOUOSERV|ORC|XO||||||||20101117|1^^HERNAMETQ1|1||CE||||20101117ODS|D||PAN4^PEDIATRICA 1|ODS|P||EVENTO^LACT|ODS|P||CARACTERISTICA^SIN SAL|ODS|P||CARACTERISTICA^DIABETICO|
Expand Down

0 comments on commit 935bb33

Please sign in to comment.