Saturday, 15 March 2014

SuperCSV Dozer deep-mapping map with enum and subclasses? -



SuperCSV Dozer deep-mapping map with enum and subclasses? -

i'm trying see if possible supercsv , dozer, or if should revert map parsing. have pojo has fellow member field of map. thankfully, during csv parsing, know specific subclass of myinterface should built, , value of myenum static. how set in column mappings? thanks!

currently, cell processors have structure, , using csvmapreader.

private static final cellprocessor[] cell_processors = new cellprocessor[] { new notnull(new trim(new strregex("^\\d{10,}$"))), // phone1 new optional(new trim(new strregex("^\\d{10,}$"))), // phone2 new optional(new trim(new strregex("^\\d{10,}$"))), // phone3 new optional(new trim()), // callvar1 new optional(new trim()), // callvar2 new optional(new trim()), // callvar3 new optional(new trim()), // callvar4 new optional(new trim()), // callvar5 new optional(new trim()), // callvar6 new optional(new trim()), // callvar7 new optional(new trim()), // callvar8 new optional(new trim()), // callvar9 new optional(new trim()), // callvar10 }; private contact maprowtocontact(map<string, object> row) { contact contact = new contact(); myphonecontactmethoddata methoddata = new myphonecontactmethoddata(); list<phone> phones = new arraylist<>(); phone phone = new phone(); phone.setphonenumber((string)row.get("phone1")); phones.add(phone); phone = new phone(); phone.setphonenumber((string)row.get("phone2")); if (phone.getphonenumber() != null) { phones.add(phone); } phone = new phone(); phone.setphonenumber((string)row.get("phone3")); if (phone.getphonenumber() != null) { phones.add(phone); } methoddata.setphones(phones); list<string> callvars = new arraylist<>(); callvars.add((string)row.get("callvar1")); callvars.add((string)row.get("callvar2")); callvars.add((string)row.get("callvar3")); callvars.add((string)row.get("callvar4")); callvars.add((string)row.get("callvar5")); callvars.add((string)row.get("callvar6")); callvars.add((string)row.get("callvar7")); callvars.add((string)row.get("callvar8")); callvars.add((string)row.get("callvar9")); callvars.add((string)row.get("callvar10")); methoddata.setenterprisecallvardata(callvars); map<contactmethod, contactmethoddata> methoddatamap = new hashmap<>(); methoddatamap.put(contactmethod.phone, methoddata); contact.setcontactmethoddata(methoddatamap); homecoming contact; }

a contact has structure, many other unrelated fields:

public class contact { private integer id; private map<contactmethod, contactmethoddata> contactmethoddata; }

contactmethod enum, values phone , email. contactmethoddata interface, of superclass of myphonecontactmethoddata implements.

thanks code - much easier understand :)

you should able read each row of csv myphonecontactmethoddata instance using next bean mapping. create sure phone call configurebeanmapping() before reading (as shown on super csv website).

you have manually create contact , add together myphonecontactmethoddata contactmethoddata map, using contactmethod.phone key (as done in lastly 3 lines of code).

final string[] beanmapping = new string[]{ "phones[0].phonenumber", "phones[1].phonenumber", "phones[2].phonenumber", "enterprisecallvardata[0]", "enterprisecallvardata[1]", "enterprisecallvardata[2]", "enterprisecallvardata[3]", "enterprisecallvardata[4]", "enterprisecallvardata[5]", "enterprisecallvardata[6]", "enterprisecallvardata[7]", "enterprisecallvardata[8]", "enterprisecallvardata[9]" }; beanreader.configurebeanmapping(myphonecontactmethoddata.class, beanmapping); myphonecontactmethoddata methoddata; while( (methoddata = beanreader.read(myphonecontactmethoddata.class, cell_processors)) != null ) { // add together contact }

map enums subclass supercsv

No comments:

Post a Comment