0

I'm brand new on using BeanIO 2.1 and I'm again facing an issue.

I'm trying to decode a fixed-length file, which has some records spread on several lines, where the 'rid' is only marked on the first of these lines

Example:

:10:BLABLABLABLA
:11:/TRTP/SEPA OVERBOEKING/IBAN
HR W HERMAN
503270327C30,49NTRFSCT20111212
:12:BLABLABLABLA

As you can see, the record ':11:' is spread over 3 lines. I'd like to grab these lines as a List of String, where the rid ':11:' would just be ignored.

This is the mapping.xml file:

<record name="ownerInformation" order="2" class="com.batch.records.OwnerInformation" minOccurs="1" maxOccurs="6" collection="list">
    <field name="tag" type="string" length="4" rid="true" literal=":11:" ignore="true" />
    <field name="description" type="string" length="unbounded" maxLength="65" />
</record>

As a result, the exception UnexpectedRecordException:

org.beanio.UnexpectedRecordException: End of stream reached, expected record 'ownerInformation'

Thanks again for helping me

Fabiz
  • 43
  • 2
  • 8

2 Answers2

1

It is possible to Map Bean Objects that Span Multiple Records.

You will have to create record IDs for each of the lines, for example: I'm using commans to delimit fields.

11,/TRTP/SEPA OVERBOEKING/IBAN
12,HR W HERMAN
13,503270327C30
13,49NTRFSCT20111212

Something like this:

<group name=ownerInfo class="com.batch.records.OwnerInformation" minOccurs="1" maxOccurs="6">
  <record name="typeInfo" class="com.batch.records.Type" order="1" minOccurs="1" maxOccurs="1" >
    <field name="recordType" rid="true" literal="11" ignore="true" />
    <field name="iban" />
  </record>
  <record name="customer" class="com.batch.records.Customer" order="2" minOccurs="1" maxOccurs="1" >
    <field name="recordType" rid="true" literal="12" ignore="true" />
    <field name="name" />
  </record>
  <record name="items" class="com.batch.records.Item" collection="list" order="3" minOccurs="1" maxOccurs="unbounded" >
    <field name="recordType" rid="true" literal="13" ignore="true" />
    <field name="id" />
  </record>
</group>

This will map to an OwnerInformation like this:

package com.batch.records;

/* Getters and Setter are omitted for brevity */

public class OwnerInformation {
  Type type;
  Customer customer;
  List<Item> items;
}

public class Type {
  String iban;
}

public class Customer {
  String name;
}

public class Item {
  String id;
}
Niel de Wet
  • 7,806
  • 9
  • 63
  • 100
0

One method would be to map out the lines you don't need and thus it includes what you want to derive from the data. Below is a mapping that can process your different records types as-is. This gives you the correct line items for which you're looking.

Note that you have an 'rid' for each line, and for your data lines you basically use a regex to say "anything that doesn't start with a colon". In your 'while read()' code you can trip your logic with:

if (reader.getRecordName().equals("record11"))

or go further with the mapping and add the groups.

Here's the mapping.xml to distinguish your lines as you need:

<beanio xmlns="http://www.beanio.org/2012/03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

<stream name="fabizFile" format="fixedlength">
    <record name="record10">
        <field name="recordType" rid="true" literal=":10:" />
        <field name="sometext" length="12" />
    </record>
    <record name="record11">
        <field name="recordType" rid="true" literal=":11:" />
        <field name="sometext" length="unbounded" maxLength="40" />
    </record>
    <record name="record12">
        <field name="recordType" rid="true" literal=":12:" />
        <field name="sometext" length="unbounded" maxLength="40" />
    </record>
    <record name="goodstuff" class="FabizModel">
        <field name="recordText" rid="true" regex="^(?!:).+" length="unbounded" maxLength="50" />
    </record>
</stream>
</beanio>
J8lug
  • 21
  • 4
  • Hi and thanks for your reply. In the end I went for having a group where the first record is indeed the one with the literal :11: and the following lines records not bounded to any prefix. That way I could retrieve a single object with all the info in one place. – Fabiz Oct 21 '16 at 12:26