2013-02-14 40 views
0

我試圖看看,如果這甚至可能與SuperCSV和Dozer,或者如果我應該恢復到地圖解析。我有一個POJO,它有一個Map的成員字段。值得慶幸的是,在CSV解析期間,我知道應該創建什麼特定的MyInterface子類,並且MyEnum的值也是靜態的。但是,我將如何在列映射中設置所有這些?謝謝!帶有枚舉和子類的SuperCSV Dozer深度映射圖?

目前,我的單元處理器有這種結構,我使用的是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); 

    return contact; 
} 

Contact具有這種結構,與許多其他不相關的領域:

public class Contact { 
    private Integer id; 
    private Map<ContactMethod, ContactMethodData> contactMethodData; 
} 

ContactMethod是一個枚舉,帶有值PHONEEMAILContactMethodData是一個接口,其中MyPhoneContactMethodData的超類實現了該接口。

+0

你可以發佈一些示例代碼?我是一名超級CSV開發者 - 我可以提供幫助,但前提是我明白問題所在! :) – 2013-02-14 22:24:47

回答

0

感謝您的代碼 - 更容易,現在明白:)

您應該能夠通過以下豆映射讀取CSV每行作爲一個MyPhoneContactMethodData實例。只需確保在閱讀之前撥打configureBeanMapping()(如Super CSV website所示)。

那麼您必須手動創建Contact並添加MyPhoneContactMethodDatacontactMethodData地圖,與ContactMethod.PHONE作爲重點用(如在最後3行代碼來完成)。

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 to contact 
} 
+0

謝謝。這就說得通了。無論如何,我最終可能會堅持使用CsvMapReader,這是因爲Contact中其他一些字段的複雜程度非常複雜,而這些字段在此處未顯示,因此無法將事情與問題保持隔離。 – 2013-02-21 14:49:32