Amit Vyas
Amit Vyas

Reputation: 790

SuperCSV and Dozer

I'm working on building a piece of software in which input is csv file data which can have 90k plus rows of data in single file.

Just to inform file will not have HEADER row at all it will start with data itself.

Through googling I came to super csv and dozer mapper extension. I am not able to find an example of where while reading of single csv line can fill Pojo with many collection of other pojos.

My Customer Pojo looks like below snapshot...

class CustomerPojo {

    private Id<CustomerId> customerId;

    private String lookupCode;

    // enum type  
    private QuoteType quoteType;

    private String bCovCa;
    private Money bCovRa;


    private String recommCovCa;
    private Money recommCoveRa;

    private Date insertedDate;

    private String pdfName;

    private List<VehicleInfo> vehicleInfoPojoList;

    private List<Address> addressList;

    // All setter-getter
}

Can some give guidance, I'm too reading through the site but it has simple examples where as day to day software have multiple associations and custom data types.

Thanks in advance.

Upvotes: 0

Views: 1031

Answers (1)

James Bassett
James Bassett

Reputation: 9868

No header is fine - just be aware that the Super CSV configuration will be static (i.e. hard-coded) as you'll have to explicitly define the structure of the CSV. Just note that you won't need the call to beanReader.getHeader() as the first line of your file is data, not a header.

If each customer maps to 1 row in the CSV file, then you should be able to configure Super CSV the same way as in the examples on the website.

For example, if your CSV looked like this:

1,1 First St,London,1 First Rd,New York,111AAA,111BBB
2,2 Second St,Paris,2 Second Rd,Munich,222AAA,222BBB

Your bean mapping (based on your POJO) might look something like:

new String[]{"customerId", "addressList[0].street", "addressList[0].city",   
    "addressList[1].street",  "addressList[1].city", 
    "vehicleInfoPojoList[0].rego", "vehicleInfoPojoList[1].rego"}

Dozer will instantiate the necessary VehicleInfo and Address classes, as well as the Lists. If there was no 2nd vehicle rego, then Dozer shouldn't populate the second element in the vehicle list.

You'll notice that because each customer maps to a single row, then you can used indexed mapping because you will always have a fixed number of addresses/vehicles etc.


If each customer spans multiple rows, then it's not as easy.

In that case I'd probably recommend reading each row/Customer, and if you find the same Customer (i.e. customerId) then copy the new addresses/vehicles etc to the existing Customer.

Alternatively, you can play around with writing your own cell processor (as done here), or perhaps seeing if the iterate-method functionality of Dozer might help.

Upvotes: 1

Related Questions