2013-02-14 3 views
0

SuperCSV와 Dozer를 사용하여이 작업을 수행 할 수 있는지, 아니면지도 파싱으로 되돌아 가야하는지 확인하려고합니다. Map의 멤버 필드가있는 POJO가 있습니다. 고맙게도 CSV 구문 분석 중에 MyInterface의 특정 하위 클래스를 빌드해야하는지 알 수 있으며 MyEnum의 값도 정적이됩니다. 하지만이 모든 것을 열 매핑에 어떻게 설정합니까? 감사!열거 형 및 하위 클래스가있는 SuperCSV Dozer 딥 매핑 맵?

현재 내 셀 프로세서는이 구조를 가지고 있으며 CsvMapReader를 사용하고 있습니다.

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

ContactMethodPHONEEMAIL으로 열거는 다음과 Contact

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; 
} 

많은 다른 무관이 필드 구조를 갖는다. ContactMethodDataMyPhoneContactMethodData의 수퍼 클래스가 구현 된 인터페이스입니다. 코드에 대한

+0

몇 가지 예제 코드를 게시 할 수 있습니까? 저는 슈퍼 CSV 개발팀입니다 - 제가 도울 수는 있지만, 문제가 무엇인지 이해하는 경우에만! :) –

답변

0

감사합니다 - 지금은 이해하기 훨씬 쉽게 :)

다음과 같은 빈 매핑을 사용하여 MyPhoneContactMethodData 인스턴스로 CSV의 각 행을 읽을 수 있어야합니다. 이 내용을 읽으시기 전에 configureBeanMapping()으로 전화하십시오 (Super CSV website 참조).

그런 다음 (코드의 마지막 3 행에서 수행으로) 수동으로 키와 ContactMethod.PHONE와 함께 사용하여 Contact를 작성하고 contactMethodData지도에 MyPhoneContactMethodData을 추가해야합니다.

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

감사합니다. 그건 의미가 있습니다. 어쨌든 Contact의 다른 필드가 얼마나 복잡한 지에 따라 CsvMapReader가 결국에는 끝나기도합니다. 연락처에있는 다른 필드가 얼마나 복잡한 지 여기에 나와 있지 않습니다. –