...
Initial Work on Customer 2 Input (CSV over HTTP)
...
To get a start on Customer 2, we'll create a POJO to convert the Customer 2 sample CSV data into the JAXB POJOs representing the canonical XML format, write a small Camel route to test it, and build that into a unit test. If we get through this, we can be pretty sure that the CSV conversion and JAXB handling is valid and can be run safely in Camel.
Create a CSV-handling POJO
To begin with, CSV is a known data format in Camel. Camel can convert a CSV file to a List (representing rows in the CSV) of Lists (representing cells in the row) of Strings (the data for each cell). That means our POJO can just assume the data coming in is of type List<List<String>>
, and we can declare a method with that as the argument.
Looking at the JAXB code in target/generated-sources/jaxb
, it looks like an Invoice
object represents the whole document, with a nested list of LineItemType objects for the line items. Therefore our POJO method will return an Invoice
(a document in the canonical XML format).
So to implement the CSV-to-JAXB POJO, we need to do something like this:
- Create a new class under
src/main/java
, perhaps calledCSVConverterBean
. - Add a method, with one argument of type
List<List<String>>
and the return typeInvoice
- In the method, the logic should look roughly like this:
- Create a new
Invoice
, using the method on the generatedObjectFactory
class - Loop through all the rows in the incoming CSV (the outer
List
) - Skip the first row, which contains headers (column names)
- For the other rows:
- Create a new
LineItemType
(using theObjectFactory
again) - Pick out all the cell values (the Strings in the inner
List
) and put them into the correct fields of theLineItemType
- Not all of the values will actually go into the line item in this example
- You may hardcode the column ordering based on the sample data file, or else try to read it dynamically from the headers in the first line
- Note that you'll need to use a JAXB
DatatypeFactory
to create theXMLGregorianCalendar
values that JAXB uses for thedate
fields in the XML – which probably means using aSimpleDateFormat
to parse the date and setting that date on aGregorianCalendar
- Add the line item to the invoice
- Create a new
- Populate the partner ID, date of receipt, and order total on the
Invoice
- Throw any exceptions out of the method, so Camel knows something went wrong
- Return the finished
Invoice
- Create a new
Create a unit test
Initial Work on Customer 3 Input (Excel over e-mail)
...