平面文件到Pojo w / oneToMany unmarshal不起作用

时间:2014-04-16 22:23:13

标签: java file-io apache-camel bindy

我使用http://camel.apache.org/bindy.html跟踪测试程序 问题是代码能够将pojo结构编组为平面CSV文件,但是它无法将文件数据解组回pojo。

apache-camel-bindy:2.13.0

订购Java文件:

package test.bindy.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
import org.apache.camel.dataformat.bindy.annotation.OneToMany;

@CsvRecord(separator = ",")
public class Order {

    @DataField(pos = 1)
    private String clOrdID;

    @DataField(pos = 2)
    private String handlInstId;

    @DataField(pos = 3)
    private int orderQty;

    @DataField(pos = 4)
    private String ordType;

    @DataField(pos = 5)
    private String rule80A;

    @DataField(pos = 6, pattern = "yyyyMMdd-HH:mm:ss")
    private Date sendingTime;

    @DataField(pos = 7)
    private String side;

    @DataField(pos = 8)
    private String symbol;

    @DataField(pos = 9)
    private String timeInForce;

    @DataField(pos = 10, pattern = "yyyyMMdd-HH:mm:ss")
    private Date transactTime;

    @DataField(pos = 11)
    private String message;

    @DataField(pos = 12)
    private String OnBehalfOfCompID;

    @DataField(pos = 13)
    private String securityExchange;

    @OneToMany(mappedTo = "test.bindy.model.Item")
    private List<Item> items;

    public void addItem(Item item) {
        if (null == items) {
            items = new ArrayList<Item>();
        }
        items.add(item);
    }

    public String getClOrdID() {
        return clOrdID;
    }

    public void setClOrdID(String clOrdID) {
        this.clOrdID = clOrdID;
    }

    public String getHandlInstId() {
        return handlInstId;
    }

    public void setHandlInstId(String handlInstId) {
        this.handlInstId = handlInstId;
    }

    public int getOrderQty() {
        return orderQty;
    }

    public void setOrderQty(int orderQty) {
        this.orderQty = orderQty;
    }

    public String getOrdType() {
        return ordType;
    }

    public void setOrdType(String ordType) {
        this.ordType = ordType;
    }

    public String getRule80A() {
        return rule80A;
    }

    public void setRule80A(String rule80a) {
        rule80A = rule80a;
    }

    public Date getSendingTime() {
        return sendingTime;
    }

    public void setSendingTime(Date sendingTime) {
        this.sendingTime = sendingTime;
    }

    public String getSide() {
        return side;
    }

    public void setSide(String side) {
        this.side = side;
    }

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public String getTimeInForce() {
        return timeInForce;
    }

    public void setTimeInForce(String timeInForce) {
        this.timeInForce = timeInForce;
    }

    public Date getTransactTime() {
        return transactTime;
    }

    public void setTransactTime(Date transactTime) {
        this.transactTime = transactTime;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getOnBehalfOfCompID() {
        return OnBehalfOfCompID;
    }

    public void setOnBehalfOfCompID(String onBehalfOfCompID) {
        OnBehalfOfCompID = onBehalfOfCompID;
    }

    public String getSecurityExchange() {
        return securityExchange;
    }

    public void setSecurityExchange(String securityExchange) {
        this.securityExchange = securityExchange;
    }

    @Override
    public String toString() {
        return "Order [clOrdID=" + clOrdID + ", handlInstId=" + handlInstId + ", orderQty=" + orderQty + ", ordType=" + ordType + ", rule80A=" + rule80A
                + ", sendingTime=" + sendingTime + ", side=" + side + ", symbol=" + symbol + ", timeInForce=" + timeInForce + ", transactTime=" + transactTime
                + ", message=" + message + ", OnBehalfOfCompID=" + OnBehalfOfCompID + ", securityExchange=" + securityExchange + "]";
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

}

项目Java文件:

package test.bindy.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.dataformat.bindy.annotation.DataField;
import org.apache.camel.dataformat.bindy.annotation.OneToMany;

  public class Item {

    @DataField(pos = 14)
    private String idSource;

    @DataField(pos = 15)
    private String itemCode;

    @DataField(pos = 16)
    private String categoryCode;

    @OneToMany(mappedTo = "test.bindy.model.InventoryItem")
    private List<InventoryItem> inventoryItems;

    public void addInventoryItem(InventoryItem ii) {
        if (null == inventoryItems) {
            inventoryItems = new ArrayList<InventoryItem>();
        }
        inventoryItems.add(ii);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Item other = (Item) obj;
        if (categoryCode == null) {
            if (other.categoryCode != null)
                return false;
        } else if (!categoryCode.equals(other.categoryCode))
            return false;
        if (idSource == null) {
            if (other.idSource != null)
                return false;
        } else if (!idSource.equals(other.idSource))
            return false;
        if (itemCode == null) {
            if (other.itemCode != null)
                return false;
        } else if (!itemCode.equals(other.itemCode))
            return false;
        return true;
    }

    public String getCategoryCode() {
        return categoryCode;
    }

    public String getIdSource() {
        return idSource;
    }

    public String getItemCode() {
        return itemCode;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((categoryCode == null) ? 0 : categoryCode.hashCode());
        result = prime * result + ((idSource == null) ? 0 : idSource.hashCode());
        result = prime * result + ((itemCode == null) ? 0 : itemCode.hashCode());
        return result;
    }

    public void setCategoryCode(String categoryCode) {
        this.categoryCode = categoryCode;
    }

    public void setIdSource(String idSource) {
        this.idSource = idSource;
    }

    public void setItemCode(String itemCode) {
        this.itemCode = itemCode;
    }
}

InventoryItem Java文件:

package test.bindy.model;

import org.apache.camel.dataformat.bindy.annotation.DataField;

public class InventoryItem {
    @DataField(pos = 17)
    private Long invetoryId;
    @DataField(pos = 18)
    private String location;
    @DataField(pos = 19)
    private Double qty;
    @DataField(pos = 20)
    private String message;

    public Long getInvetoryId() {
        return invetoryId;
    }

    public String getLocation() {
        return location;
    }

    public String getMessage() {
        return message;
    }

    public Double getQty() {
        return qty;
    }

    public void setInvetoryId(Long invetoryId) {
        this.invetoryId = invetoryId;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setQty(Double qty) {
        this.qty = qty;
    }
}

编组和解编的测试程序:

import test.bindy.model.InventoryItem;
import test.bindy.model.Item;
import test.bindy.model.Order;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.spi.DataFormat;

public class Test3 {
    static File file;
    static DataFormat bindy;
    static CamelContext context;
    static Exchange exchange;
    static {
        file = new File("c:/temp/bindy/test.txt");
        bindy = new BindyCsvDataFormat("test.bindy.model");
        context = new DefaultCamelContext();
        exchange = new DefaultExchange(context);
    }

    public static void main(String[] args) throws Exception {
        System.out.println("Marshalling POJOs...");
        testMarshal();
        System.out.println("Success...1");
        System.out.println("Unmarshalling POJOs...");
        testUnMarshal();
        System.out.println("Success...2");
    }

    @SuppressWarnings("unchecked")
    static void testUnMarshal() throws Exception {
        InputStream is = new FileInputStream(file);
        List<Map<String, Object>> models = (List<Map<String, Object>>) bindy.unmarshal(exchange, is);
        System.out.println("Output....");
        for (Map<String, Object> m : models) {
            for (String k : m.keySet()) {
                System.out.println(m.get(k));
            }
        }
    }

    static void testMarshal() throws Exception {
        List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
        models.add(generateData());
        OutputStream os = new FileOutputStream(file);
        bindy.marshal(exchange, models, os);
    }

    static Map<String, Object> generateData() {
        Map<String, Object> model = new HashMap<String, Object>();
        Order order = new Order();
        order.setClOrdID("892329");
        order.setOnBehalfOfCompID("Amazon");
        order.setOrderQty(199);
        order.setSymbol("GARMIN");
        order.setMessage("Immediate Delievery");
        order.setOrdType("EXPRESS");
        order.setRule80A("89893B");
        order.setTimeInForce("5");
        order.setSecurityExchange("INS");
        order.setSendingTime(new Date());
        Item item = new Item();
        item.setIdSource("1");
        item.setItemCode("FORERUNNER620");
        item.setCategoryCode("SPORTS");
        InventoryItem ii = new InventoryItem();
        ii.setInvetoryId(1000L);
        ii.setQty(1D);
        ii.setLocation("NY");
        item.addInventoryItem(ii);/*
                                    ii = new InventoryItem();
                                    ii.setInvetoryId(1001L);
                                    ii.setQty(1D);
                                    ii.setLocation("WI");
                                    ii.setMessage("Promotion");
                                    item.addInventoryItem(ii);*/
        //
        order.addItem(item);
        //
        item = new Item();
        item.setIdSource("2");
        item.setItemCode("HRM-RUN");
        item.setCategoryCode("SPORTS");
        ii = new InventoryItem();
        ii.setInvetoryId(1003L);
        ii.setQty(1D);
        ii.setLocation("FL");
        item.addInventoryItem(ii);
        order.addItem(item);
        //
        item = new Item();
        item.setIdSource("3");
        item.setItemCode("FOOTPAD");
        item.setCategoryCode("SPORTS");
        ii = new InventoryItem();
        ii.setInvetoryId(1004L);
        ii.setQty(1D);
        ii.setLocation("AZ");
        item.addInventoryItem(ii);
        order.addItem(item);
        model.put("0", order);
        return model;
    }
}

为什么unmarshal不能对编组操作生成的同一文件起作用?

3 个答案:

答案 0 :(得分:0)

很难知道cvs文件中有多少个InventoryItem,因为cvs文件没有提供任何信息。

答案 1 :(得分:0)

结论Bindy 不支持支持多级pojo结构。我和BEANIO http://www.beanio.org/一起前进。这确实支持多级,但需要映射文件定义而不是Pojos上的注释

答案 2 :(得分:0)

您是否尝试将DataFiled Annotation与OneToMany放在一起?

我解决了类似的问题:

@OneToMany
@DataField(pos=2)
private List<Layout> layouts;

我的布局课程:

public class Layout {
  @DataField(pos=2)
  private String id;
}