Mapping CSV to multiple nested beans of same type

260 views Asked by At

I am trying to map CSV data row to FlightBooking bean, which has 2 nested beans of same type FlightSegment.
CSV header is below for reference:

Booking Date,Booking Reference,Total Price,Aviation Taxes,Flight Reference,Flight Date,Flight Price,Seat Reservation Price,Prepaid Meal Price,Flight Reference,Flight Date,Flight Price,Seat Reservation Price,Prepaid Meal Price

FlightBooking bean definition is below for reference:

public class FlightBooking {
    private String bookingReference;
    private String bookingDate;
    private String aviationTaxes;
    private String totalPrice;
    private FlightSegment segmentOut;
    private FlightSegment segmentReturn;
}

FlightSegment bean definition is below for reference:

public class FlightSegment {
    private String flightReference;
    private String flightDate;
    private String flightPrice;
    private String seatReservationPrice;
    private String prepaidMealPrice;
}

Columns 5 to 9 represent outwards leg of the flight and should be mapped to segmentOut.
Columns 10 to 14 represent return leg of the flight and should be mapped to segmentReturn.

Restrictions:
Bean Definitions cannot be altered.

I could have easily used OpenCSV 5.x @CsvRecurse if there were only one FlightSegment attribute in FlightBooking.

Please help.

0

There are 0 answers