diff --git a/lib/src/main/java/gdv/xport/Datenpaket.java b/lib/src/main/java/gdv/xport/Datenpaket.java index 306a07d0..e85068af 100644 --- a/lib/src/main/java/gdv/xport/Datenpaket.java +++ b/lib/src/main/java/gdv/xport/Datenpaket.java @@ -563,9 +563,17 @@ public void importFrom(final File file, final Charset encoding) throws IOExcepti public Datenpaket pack() { for (int i = 0; i < datensaetze.size(); i++) { Datensatz ds = datensaetze.get(i); - if (!ds.isComplete()) { - Optional next = findNextDatensatz(ds.getSatzTyp(), i+1); + if (ds.getTeildatensaetze().size() == 0) { + // keine Teildatensätze, evtl. bereits gemergt, übergehe den Datensatz + continue; + } + boolean nextVsnrReached = false; + for (int j = i+1; j < datensaetze.size() && !ds.isComplete() && !nextVsnrReached; j++) { + Optional next = findNextDatensatzWithinVsnr(ds.getVersicherungsscheinNummer(), ds.getSatzTyp(), j); next.ifPresent(ds::mergeWith); + if (!next.isPresent()) { + nextVsnrReached = true; + } } } removeEmptyDatensaetze(); @@ -583,10 +591,19 @@ private void removeEmptyDatensaetze() { datensaetze.addAll(cleaned); } - private Optional findNextDatensatz(SatzTyp satzTyp, int position) { + private Optional findNextDatensatzWithinVsnr(String vsNr, SatzTyp satzTyp, int position) { for (int i = position; i < datensaetze.size(); i++) { Datensatz ds = datensaetze.get(i); + if (ds.getTeildatensaetze().size() == 0) { + // keine Teildatensätze, evtl. bereits gemergt, übergehe den Datensatz + continue; + } + if (!ds.getVersicherungsscheinNummer().equals(vsNr)) { + // Versicherungsscheinnummer hat geändert, fertig + break; + } if (satzTyp.equals(ds.getSatzTyp())) { + // Potentieller Datensatz gefunden, fertig return Optional.of(ds); } } diff --git a/lib/src/test/java/gdv/xport/DatenpaketTest.java b/lib/src/test/java/gdv/xport/DatenpaketTest.java index ee4190af..0b8c1559 100644 --- a/lib/src/test/java/gdv/xport/DatenpaketTest.java +++ b/lib/src/test/java/gdv/xport/DatenpaketTest.java @@ -50,8 +50,7 @@ import java.util.Date; import java.util.List; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; /** @@ -745,9 +744,28 @@ public void testExportImport221Wagnisdaten() throws IOException { @Test public void testPack() throws IOException { Datenpaket x = checkImport("gdv/xport/satz/testcase_0220_mit_0221_versetzt.txt", Charset.forName("IBM850")); - int anzahlSaetze = x.getDatensaetze().size(); +// MatcherAssert.assertThat(x.getDatensaetze().size(), equalTo(27)); + Datenpaket unpacked = x.pack(); - MatcherAssert.assertThat(unpacked.getDatensaetze().size(), lessThan(anzahlSaetze)); + /* + After pack, expected the following: + 3x0100 for 2222222 + 1x0200 for 2222222 + 1x0210 for 2222222 + 1x0220.010.0 for 2222222 + 1x0220.010.13.1 for 2222222 + 1x0221.010.13.1 for 2222222 + 3x0220.010.13.6 for 2222222 + 3x0100 for 2222244 + 1x0200 for 2222244 + 1x0210 for 2222244 + 1x0220.010.0 for 2222244 + 1x0220.010.13.1 for 2222244 + 1x0221.010.13.1 for 2222244 + 3x0220.010.13.6 for 2222244 + Total: 22 + */ + MatcherAssert.assertThat(unpacked.getDatensaetze().size(), equalTo(22)); checkDatensatz(x, SatzTyp.of("0220.010.13.1")); checkDatensatz(x, SatzTyp.of("0221.010.13.1")); } diff --git a/lib/src/test/resources/gdv/xport/satz/testcase_0220_mit_0221_versetzt.txt b/lib/src/test/resources/gdv/xport/satz/testcase_0220_mit_0221_versetzt.txt index 18d4a064..8ad446d8 100644 --- a/lib/src/test/resources/gdv/xport/satz/testcase_0220_mit_0221_versetzt.txt +++ b/lib/src/test/resources/gdv/xport/satz/testcase_0220_mit_0221_versetzt.txt @@ -12,12 +12,34 @@ 02001234 0010 2222222011234567890 01 000000000000000000 Testpro 000000101199215011992 2 02101234 0010 2222222011234567890 EUR0 00000000000000 0 00000 00000 00000000000000000000000000000000000000000000000000000 00000000 1 02101234 0010 2222222011234567890 0000000000 000000 50 00000000 2 -02201234 0010 222222201123456789011111111 0 Tester Cornelia 010119802123Sekret?rin 100000000 11111111 000000000100000000 00000000 1 +02201234 0010 222222201123456789011111111 0 Tester Cornelia 010119802123Sekret„rin 100000000 11111111 000000000100000000 00000000 1 02201234 0010 2222222011234567890 11111111110112199201122025090820210331000000014123000010000000010000 000000014123000000000Testpro 1000000000000000000020000000000005001201122023011220090451210000000000000000000000000 000000001 02211234 0010 2222222011234567890 111111111100000020000000000000200000000000002000000000000020000000000000200000000000002000000000000020000000 1 02201234 0010 2222222011234567890 111111111100000000000000000000000 00000000000000000000 011220000000000000000000000000000001 01122023 2 02211234 0010 2222222011234567890 1111111111000000010000 2 +02201234 0010 2222222011234567890 1 000000000000000000000000000000000000000000 00000000000000 3 02201234 0010 2222222011234567890 1111111111 1 00000 9 000000 6 02201234 0010 2222222011234567890 1111111111 2 00000 9 000000 6 02201234 0010 2222222011234567890 1111111111 31 000000 00000 6 +01001234 0010 22222440112345678902Tester Cornelia D 85053 Ingolstadt Teststrasse 12 a 01011980D 01 99 21 +01001234 0010 2222244011234567890 11111111 10(012) 3456789 11 0000002 +01001234 0010 2222244011234567890 01 3 +01001234 0010 22222440112345678902Tester Cornelia D 85053 Ingolstadt Teststrasse 12 a 01011980D 03 99 21 +01001234 0010 2222244011234567890 11111111 10(012) 3456789 11 0000002 +01001234 0010 2222244011234567890 01 3 +01001234 0010 22222440112345678902Tester Cornelia D 85053 Ingolstadt Teststrasse 12 a 01011980D 09 99 21 +01001234 0010 2222244011234567890 11111111 10(012) 3456789 11 0000002 +01001234 0010 2222244011234567890 01 3 +02001234 0010 2222244011234567890201091992010920250112202111 0000000099 01122021100000 EUR000000014123 1 11111111 004000001011992 EUR 1 +02001234 0010 2222244011234567890 01 000000000000000000 Testpro 000000101199215011992 2 +02101234 0010 2222244011234567890 EUR0 00000000000000 0 00000 00000 00000000000000000000000000000000000000000000000000000 00000000 1 +02101234 0010 2222244011234567890 0000000000 000000 50 00000000 2 +02201234 0010 222224401123456789011111111 0 Tester Cornelia 010119802123Sekret„rin 100000000 11111111 000000000100000000 00000000 1 +02201234 0010 2222244011234567890 11111111110112199201122025090820210331000000014123000010000000010000 000000014123000000000Testpro 1000000000000000000020000000000005001201122023011220090451210000000000000000000000000 000000001 +02211234 0010 2222244011234567890 111111111100000020000000000000200000000000002000000000000020000000000000200000000000002000000000000020000000 1 +02201234 0010 2222244011234567890 111111111100000000000000000000000 00000000000000000000 011220000000000000000000000000000001 01122023 2 +02211234 0010 2222244011234567890 1111111111000000010000 2 +02201234 0010 2222244011234567890 1111111111 1 00000 9 000000 6 +02201234 0010 2222244011234567890 1111111111 2 00000 9 000000 6 +02201234 0010 2222244011234567890 1111111111 31 000000 00000 6 99990000000019123456789000000000000000000000000000000+00000000000000+00000000000000+00000000000000+