0

これが SuperCSV と Dozer でも可能かどうか、またはマップの解析に戻るだけでよいかどうかを確認しようとしています。Map のメンバー フィールドを持つ POJO があります。ありがたいことに、CSV の解析中に、MyInterface の特定のサブクラスを構築する必要があることがわかり、MyEnum の値も静的になります。しかし、列マッピングでこれらすべてをどのように設定すればよいでしょうか? ありがとう!

現在、私のセル プロセッサにはこの構造があり、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;
}

AContactはこの構造を持ち、他の多くの無関係なフィールドがあります。

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

ContactMethodPHONE値とを持つ列挙型ですEMAILContactMethodDataはインターフェースであり、そのスーパークラスはMyPhoneContactMethodDataimplements です。

4

1 に答える 1

0

コードをありがとう-今でははるかに理解しやすい:)

MyPhoneContactMethodData次のBeanマッピングを使用すると、CSVの各行をインスタンスとして読み取ることができるはずです。読む前に必ずconfigureBeanMapping()これを使って電話してください(Super CSV Webサイトに表示されています)。

次に、をキーとして使用して、を手動で作成し、マップにContact追加する必要があります(コードの最後の3行で行ったように)。MyPhoneContactMethodDatacontactMethodDataContactMethod.PHONE

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
}
于 2013-02-20T23:02:35.373 に答える