質問

I have following test programs using http://camel.apache.org/bindy.html Problem is code is able to marshal pojo structure into flat CSV file, but it is not able to unmarshal the file data back to pojo.

apache-camel-bindy: 2.13.0

Order Java File:

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

}

Item Java File:

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 File:

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

Test Program for marshal and unmarshal :

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

Any clues why unmarshal does not work on same file generated by marshal operation?

役に立ちましたか?

解決 2

Concluding that Bindy does not support multi-level pojo structure. I am going forward with BEANIO http://www.beanio.org/. This does support multi-level, but needs mapping file definition instead of annotations on Pojos

他のヒント

It's hard to know how many InventoryItem there in the cvs file, as cvs file doesn't provide the any information for that.

Did you try to put the DataFiled Annotation with OneToMany?

I resolved a similar problem like this:

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

And my Layout class I have:

public class Layout {
  @DataField(pos=2)
  private String id;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top