I am trying to map a fixed length messages which may have different segments depending on the value in another field?
For example:
<stream name="employeeFile" format="csv">
<record name="employee" class="example.Employee">
<field name="firstName" length="10" />
<field name="lastName" length="10" />
<field name="title" length="10" />
<field name="salary" length="6" padding="0" justify="right" />
<field name="hireDate" length="8" format="MMddyyyy" />
<field name="segmentEnum" length="10" />
<segment name="mailingAddressSimple" class="example.Address">
<field name="street" length="50" />
<field name="city" length="20" />
<field name="state" length="2" />
<field name="zip" length="5" />
</segment>
<segment name="mailingAddressFull" class="example.FullAddress">
<field name="street" length="30" />
<field name="state" length="2" />
<field name="city" length="20" />
<field name="zip" length="5" />
<field name="country" length="10" />
<field name="phone" length="10" />
</segment>
</record>
</stream>
</beanio>
In this example, I will need to map fixed length messages that has dynamic segments depending on the segmentEnum value. When segmentEnum value is "Simple", use segment "mailingAddressSimple" to map string, but when the value is "Full", map the message by "mailingAddressFull" segment. It's either Simple OR Full.
What you are asking is not possible from the best of my knowledge and understanding of the user manual.
I see that the difference between the Simple and Full mapping is only the last 2 fields that is extra for the Full mapping. My suggestion would be to only include the details for the Full mapping in your
mapping.xmlfile and make the last 2 fields (countryandphone) optional. This would still allow you to fully read the flat file and populate your objects. I would then after reading the file traverse the object graph and then perhaps split theFullAddressobjects into theAddressandFullAddressobjects based on the value of the enum.This is all highly dependent on your use case and the control you have over the code and process where this is used. If you can elaborate more on the other factors involved we can perhaps get to another solution.
EDIT: The only alternative that you could consider is to use an inline
mapfor your segment. But it would still require some post processing to determine if it is aSimpleorFulladdress. See the Repeating Segments documentation, especiallySec 4.5.2.1 Inline Maps