0

I have a scenario to map user information to Map of Objects (Map or List).

I have say user info as follows CommonHeader(CX) block followed by 0 or more ID blocks followed by 0 or more Address blocks

Below are valid formats for user records

CX|19981222|19981222|ID|DriversLicence|111111111|ID|Passport|ABC12345|AD|123 Main Street|Atlanta|GA|30316|AD|100 PeachTree RD|Atlanta|Ga|3007|
CX|19981222|19981222|ID|DriversLicence|111111111|ID|Passport|ABC12345|
CX|19981222|19981222|AD|123 Main Street|Atlanta|GA|30316|AD|100 PeachTree RD|Atlanta|Ga|3007|

Is it possible to map such scenarios using beanio ?

Whats the best solution to handle these cases ?

I am using Beanio-2.1

My beanio mapping file is as follows

<stream name="userrRecord" format="delimited">
        <parser>
            <property name="delimiter" value="|"/>
        </parser>
        <record name="urecord" class="map" minOccurs="0" maxOccurs="unbounded" >
            <segment name="CX" class="map">
                <field name="CX"/>
                <field name="DateFirstReported" type="date" format="yyyyMMdd"/>
                <field name="DateLastReported" type="date" format="yyyyMMdd"/>
           </segment>     
            <segment name="ID" class="map"  minOccurs="0" maxOccurs="unbounded" collection="list">
                <field name="ID"/>
                <field name="IDType"/>
                <field name="DocumentID"/>
            </segment>
            < segment name="AD" class="map" minOccurs="0" maxOccurs="unbounded" collection="list">
                <field name="AD"/>
                <field name="Street"/>
                <field name="City"/>
                <field name="State"/>
                <field name="Zipcode"/>                
            </segment>
        </record>
    </stream>

When I try to unmarshall a record with 2 ID segmenst and 0 AD segments running into InvalidRecord exception.

Any help is highly appreciated.

Indrani
  • 21
  • 1
  • 3

2 Answers2

0

I think this is not possible, as stated in the BeanIO Reference Guide:

Flat file formats (CSV, delimited and fixed length) may only contain one
field or segment of indeterminate length (i.e. where maxOccurs is greater
than minOccurs). The position of components that follow are assumed to be
relative to the end of the record.
Michael Brohl
  • 782
  • 5
  • 21
0

You should change each of your segments to a separate record, and wrap it in a group. Then your sample input will look like this:

CX|19981222|19981222|
ID|DriversLicence|111111111|
ID|Passport|ABC12345|
AD|123 Main Street|Atlanta|GA|30316|
AD|100 PeachTree RD|Atlanta|Ga|3007|
CX|19981222|19981222|
ID|DriversLicence|111111111|
ID|Passport|ABC12345|
CX|19981222|19981222|
AD|123 Main Street|Atlanta|GA|30316|
AD|100 PeachTree RD|Atlanta|Ga|3007|

And your mapping like this:

<stream name="userrRecord" format="delimited">
    <parser>
        <property name="delimiter" value="|"/>
    </parser>
    <group name="urecord" class="map" minOccurs="0" maxOccurs="unbounded">
        <record name="CX" class="map">
            <field name="CX"/>
            <field name="DateFirstReported" type="date" format="yyyyMMdd"/>
            <field name="DateLastReported" type="date" format="yyyyMMdd"/>
        </record>
        <record name="ID" class="map" minOccurs="0" maxOccurs="unbounded" collection="list">
            <field name="ID"/>
            <field name="IDType"/>
            <field name="DocumentID"/>
        </record>
        <record name="AD" class="map" minOccurs="0" maxOccurs="unbounded" collection="list">
            <field name="AD"/>
            <field name="Street"/>
            <field name="City"/>
            <field name="State"/>
            <field name="Zipcode"/>
        </record>
    </group>
</stream>
Niel de Wet
  • 7,806
  • 9
  • 63
  • 100