I am new to Bean-IO and I was trying to configure a validation logic for occurrence of group when a particular record type is available in the file. For example if there are three records in flat file as shown below.
560866
670972
57086659
I am trying to setup the following logic
- Both 56 and 67 lines together form a multi line record
- 56&67 records can come independently of record 57,but 57 record cannot come without 56&67.
I was successful in creating the first validation using minOccurs attribute in @record annotation, but was not able to do the same for 56&67 using a group.
Please find the sample code setup below.
HeaderRecord class holds the 56&67 record details
@Group
public class HeaderRecord {
@Record(minOccurs = 1)
public TX56 tx56;
@Record(minOccurs = 1)
public TX67 tx67;
}
RecordObject is used to hold the headers and line items
public class RecordObject {
@Group(collection = List.class, minOccurs = 1)
List<HeaderRecord> headerRecords;
@Record(collection = List.class)
List<TX57> tx57s;
}
@Record(maxLength = 10, name = "TX56")
public class TX56 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "56", trim = true)
protected int id;
@Field(ordinal = 1, at = 2, length = 4, trim = true)
protected int number;
}
@Record(maxLength = 31, name = "TX67")
public class TX67 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "67", trim = true)
protected int id;
@Field(ordinal = 1, at = 2, length = 4, trim = true)
protected int number;
}
@Record(maxLength = 71, name = "TX57")
public class TX57 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "57", trim = true)
protected int id;
@Field(ordinal = 1, at = 2, length = 4, trim = true)
protected int number;
}
with the above configuration when I try to parse the file with records given below, it throws UnexpectedRecordException.
560866
670972
57086659
Stack trace:
2018-07-17 15:22:07,778[http-nio-8080-exec-2]ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]-Servlet.service()for servlet[dispatcherServlet]in context with path[]threw exception[Request processing failed;nested exception is org.beanio.UnexpectedRecordException:End of stream reached,expected record'tx56']with root cause org.beanio.UnexpectedRecordException:End of stream reached,expected record'tx56' at org.beanio.internal.parser.UnmarshallingContext.newUnsatisfiedRecordException(UnmarshallingContext.java:367)~[beanio-2.1.0.jar:2.1.0] at org.beanio.internal.parser.Group.unmarshal(Group.java:127)~[beanio-.1.0.jar:2.1.0] at org.beanio.internal.parser.DelegatingParser.unmarshal(DelegatingParser.java:39)~[beanio-2.1.0.jar:2.1.0] at org.beanio.internal.parser.RecordCollection.unmarshal(RecordCollection.java:42)~[beanio-2.1.0.jar:2.1.0] at org.beanio.internal.parser.Group.unmarshal(Group.java:118)~[beanio-2.1.0.jar:2.1.0] at org.beanio.internal.parser.BeanReaderImpl.internalRead(BeanReaderImpl.java:106)~[beanio-2.1.0.jar:2.1.0] at org.beanio.internal.parser.BeanReaderImpl.read(BeanReaderImpl.java:67)~[beanio-2.1.0.jar:2.1.0] at dk.coop.integration.fileconversion.service.sampleapplication.createFixedLengthFile(sampleapplication.java:32)~[classes/:?]
Note: with the above configuration, following scenarios works
- 56&67comes independently
560866
670972 - 57cannot come independently
57086659:this flat file fails with a proper exception - 56&67should always come as a single record.
this also works fine.
Additional Details: Sample Flatfile
560866
670972
560866
670972
560866
670972
57086659
57086659
57086659
57086659
52022
560866
670972
57086659
As seen above, in the flat file there is a possibility that multiple header records and TX57 record can come as a single entity. Also there can be other type of records that can come in between, in which case I have to treat second occurrence of TX56,67 and 57 as a different item.
In the above example first 10 records will form a single recordObject, then the second occurrence of these records will form a second record object. Sorry for not sharing earlier, but there is another wrapper class which holds a list of recordObject.
I am giving the working maven project Github URL below. https://github.com/Jayadeep2308/FlatFileParser