0

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.

Sasisekar
  • 1
  • 1

1 Answers1

0

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.xml file and make the last 2 fields (country and phone) 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 the FullAddress objects into the Address and FullAddress objects 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 map for your segment. But it would still require some post processing to determine if it is a Simple or Full address. See the Repeating Segments documentation, especially Sec 4.5.2.1 Inline Maps

nicoschl
  • 2,346
  • 1
  • 17
  • 17
  • Thanks for suggesstion. In my use case Adress and FullAddress properties will not be in same order or length of the property will be different. So adding optional field will not help much. Updated the example in question with different length. – Sasisekar Jun 10 '20 at 13:51