[
https://issues.apache.org/activemq/browse/CAMEL-398?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Charles Moulliard updated CAMEL-398:
------------------------------------
Attachment: Annotation.rar
Hi,
I'm very proud to propose you the first version of Camel bindy component. The
attachment contains a maven project.
1) Introduction
"bindy" Why, the idea behind this framework is to bind data to Java Bean using
annotations. The framework in this first release allows a user to bind a CSV
record to more than one POJOs and to convert the data according to the type of
the java property (for more info, look to the folder
org.apache.camel.bindy.model & model2 of folder src/test/java).
The annotation used to detect data field is :
@DataField : only the pos attribute is mandatory, patterns can be provided
date, byte, float, double, integer, long, number and short format,
The objects created can be link together in an one to one relation
@Link
public Client client;
The CSV separator is defined through the @record annotation :
@Record(separator =",")
2) Example
A) Model with 2 classes linked (Order has a reference to client)
@Record(separator =",")
public class Order {
@DataField(pos = 0)
public int orderNr;
@Link
public Client client;
@DataField(pos = 4)
public String ISIN_Code;
@DataField(name = "Name", pos = 5)
public String Instrument_Name;
@DataField(pos = 6)
public String Quantity;
@DataField(pos = 7)
public String Currency;
@DataField(pos = 8, pattern="dd-MM-yyyy")
public Date OrderDate;
public class Client {
@DataField(pos = 1)
public String clientNr;
@DataField(pos = 2)
public String FirstName;
@DataField(pos = 3)
public String LastName;
B) CSV record
10,A1,Julia,Roberts,BE123456789,Belgium Ventage 10/12,150,USD,14-01-2009
where we have two model classes Order.class and Client.class linked (Order has
a reference to client)
C) Objects generated
{org.apache.camel.bindy.model.Order=Model : org.apache.camel.bindy.model.Order
: 10, 150, BE123456789, Belgium Ventage 10/12, USD, Model :
org.apache.camel.bindy.model.Client : A1, Julia, Roberts,Wed Jan 14 16:30:30
CET 2009, org.apache.camel.bindy.model.Client=Model :
org.apache.camel.bindy.model.Client : A1, Julia, Roberts}
Remarks :
- Two camel junit tests have been added to test the marshalling and
unmarshalling
- In the next step, it will be possible to bind also fixed length content
(including header and footer).
Any remarks, comments are welcome
Charles
> Map the content of a CSV file to a POJO using @annotation and .convertBodyTo()
> ------------------------------------------------------------------------------
>
> Key: CAMEL-398
> URL: https://issues.apache.org/activemq/browse/CAMEL-398
> Project: Apache Camel
> Issue Type: New Feature
> Components: camel-core
> Affects Versions: 1.4.0
> Reporter: Charles Moulliard
> Fix For: 2.0.0
>
> Attachments: Annotation.rar
>
>
> Hi,
> It should be nice if in a next relase of Camel, it will be possible to map
> the content of a CSV file to a POJO using @annotation.
> For the moment, I use an ArrayList + iterator (see code hereafter) to achieve
> the extraction of the content but I'm sure that we can simplify this code
> using @Annotation
> and the following action (.convertBodyTo(Order<List>) by example.
> Current situation
> Camel route
> from("file:///c:/temp/test?noop=true")
> .unmarshal().csv()
> .to("bean:converter?methodName=TransformMessage"); --> should be replaced by
> something like .convertBodyTo(Order<List>)
> Converter class
> public void TransformMessage(Exchange in) {
> process(in.getIn().getBody(List.class));
> }
> @SuppressWarnings("unchecked")
> private void process(List messages) {
>
> // Iterate through the list of messages
> for (Iterator<ArrayList> it = messages.iterator();
> it.hasNext();) {
> // Split the content of the message into field
> message = it.next();
> field = (String[]) message.toArray();
> order = new Order();
> order.setId(Integer.valueOf(field[0]).intValue());
> order.setBank(field[1]);
> order.setAmountFrom(Double.parseDouble(field[2]));
> order.setAmountTo(Double.parseDouble(field[3]));
> order.setOrderInstruction(field[4].trim());
> this.orderService.createOrder(order);
> }
> }
> Regards,
> Charles
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.