Skip to content

Commit 945e694

Browse files
authored
Merge pull request #5 from imsweb/segment-pos-order-4
Segment pos order (#4)
2 parents 88bc471 + d5e6d1b commit 945e694

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/main/java/com/imsweb/x12/reader/X12Reader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,19 @@ private boolean validateLines(List<String> segments, String loopId, Separators s
562562
int[] segmentCounter = new int[format.size()];
563563
boolean lineMatchesFormat = false;
564564

565+
String previousPos = null;
565566
for (String segment : segments) {
566567
int i = 0;
567568
for (SegmentDefinition segmentConf : format) {
568569
String[] tokens = segment.split(Pattern.quote(separators.getElement().toString()));
569570
if (tokens[0].equals(segmentConf.getXid()) && codesValidated(tokens, segmentConf)) {
571+
String currentPos = segmentConf.getPos();
572+
if (previousPos != null && Integer.parseInt(previousPos) > Integer.parseInt(currentPos))
573+
_errors.add("Segment " + segmentConf.getXid() + " in loop " + loopId + " is not in the correct position.");
574+
570575
segmentCounter[i]++;
571576
lineMatchesFormat = true;
577+
previousPos = currentPos;
572578
break;
573579
}
574580
i++;

src/test/java/com/imsweb/x12/reader/X12ReaderTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,4 +531,17 @@ private void validateMultipleSTLoops(Loop loop) {
531531
Assert.assertEquals("AN INSURANCE COMPANY", loop.getLoop("ST_LOOP", 1).getLoop("2010BB", 1).getSegment("NM1").getElementValue("NM103"));
532532
}
533533

534+
@Test
535+
public void testSegmentsNotInOrder() throws Exception {
536+
URL url = this.getClass().getResource("/837_5010/x12_segments_out_of_order.txt");
537+
X12Reader reader = new X12Reader(FileType.ANSI837_5010_X222, new File(url.getFile()));
538+
539+
List<String> errors = reader.getErrors();
540+
541+
System.out.println(errors);
542+
Assert.assertEquals(2, errors.size());
543+
544+
Assert.assertTrue(errors.contains("Segment N4 in loop 2010AA is not in the correct position."));
545+
Assert.assertTrue(errors.contains("Segment N3 in loop 2010AB is not in the correct position."));
546+
}
534547
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
ISA*00* *01*SECRET *ZZ*SUBMITTERS.ID *ZZ*RECEIVERS.ID *030101*1253*U*00501*000000905*1*T*:~
2+
GS*HC*SENDER CODE*RECEIVER CODE*19991231*0802*1*X*005010X222A1~
3+
ST*837*987654*005010X222A1~
4+
BHT*0019*00*0123*19960918*0932*CH~
5+
NM1*41*2*ANGELASZEK MEDICAL*****46*999999999~
6+
PER*IC*DAVID ANGELASZEK*TE*3016809770*EX*123~
7+
NM1*40*2*HEALTH RECEIVER*****46*111222333~
8+
HL*1**20*1~
9+
NM1*85*2*MEDICAL GROUP*****XX*1234567890~
10+
N3*3901 CALVERTON BLVD~
11+
REF*EI*123456789~
12+
N4*CALVERTON*MD*20705~
13+
PER*IC*JANE JONES*TE*3022893453~
14+
PER*IC*JANE JANES*TE*3012833053*EX*201~
15+
NM1*87*2~
16+
N4*GREENBELT*MD*20770~
17+
N3*227 LASTNER LANE~
18+
HL*2*1*22*1~
19+
SBR*P**SUBSCRIBER GROUP******CI~
20+
NM1*IL*1*DOE*JOHN*T**JR*MI*123456~
21+
NM1*PR*2*HEALTH INSURANCE COMPANY*****PI*11122333~
22+
CLM*A37YH556*500***11:B:1*Y*A*Y*I*P~
23+
HI*BK*8901*BF*87200*BF:5559~
24+
LX*1~
25+
SV1*HC:99211:25*12.25*UN*1*11**1**Y~
26+
DTP*472*RD8*20050314-20050325~
27+
HL*2*3*22*1~
28+
SBR*P**SUBSCRIBER GROUP TWO******CI~
29+
NM1*IL*1*DOE*DAVID*J**JR*MI*123456~
30+
NM1*PR*2*HEALTH INSURANCE COMPANY TWO*****PI*11122333~
31+
CLM*A37YH667*500***11:B:1*Y*A*Y*I*P~
32+
HI*BK*1987*BF*4134*BF:5559~
33+
LX*2~
34+
SV1*HC:478331:25*12.25*UN*1*11**1:2:3**Y~
35+
DTP*472*RD8*20050322-20050325~
36+
SE*25*987654~
37+
GE*1*1~
38+
IEA*1*000000905~
39+
40+
41+
42+
43+
44+
45+

0 commit comments

Comments
 (0)