h2. Bindy

*Available as of Camel 2.0*

The idea that the developers have followed to design this component was to allow the binding of non structured data (or to be more precise non-XML data) 
to Java Bean using annotations. Using Bindy, you can bind data like :
- CSV record,
- Fixedlength record,
- or any other non-structured data

to one or many POJOS and to convert the data according to the type of the java property. POJOS can be linked together. Moreover, for data type like Date, Double, Float, Integer, Short, Long and BigDecimal, you can provide the pattern to apply during the formatting of the property. 

For the BigDecimal number, you can also define the precision and the decimal or grouping separators
\\
||Type||Format Type||Pattern example||Link||
|Date|DateFormat|"dd-MM-yyyy"|[http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html]|
|Decimal*|Decimalformat|"##.###.###"|[http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html]|

Decimal* = Double, Integer, Float, Short, Long

{note:title=Be careful}This first release only support CSV record.{note} 

To work with camel-bindy, you must first define your model in a package (e.g. com.acme.model) and for each model class (e.g. Order, Client, Instrument, ...) associate the required annotations (described hereafter) with Class or property name.
\\
{table:class=confluenceTable}
{tr}
{th:class=confluenceTh}Annotation name{th}
{th:class=confluenceTh}Level{th}
{th:class=confluenceTh|colspan=3}Parameter{th}
{tr}
{tr}
{th:class=confluenceTh}{th}
{th:class=confluenceTh}{th}
{th:class=confluenceTh|}Name{th}
{th:class=confluenceTh}Type{th}
{th:class=confluenceTh}Info{th}
{tr}
{tr}
{td:class=confluenceTd}*CsvRecord*{td}
{td:class=confluenceTd|align=center}Class{td}
{td:class=confluenceTd|align=center}separator{td}
{td:class=confluenceTd|align=center}string{td}
{td:class=confluenceTd|align=center}mandatory - can be ',' or ';' or 'anything'{td}
{tr}
{tr}
{td:class=confluenceTd}{td}
{td:class=confluenceTd|align=center}{td}
{td:class=confluenceTd|align=center}skipFirstLine{td}
{td:class=confluenceTd|align=center}boolean{td}
{td:class=confluenceTd|align=center}optional - default value = false - allow to skip the first line of the CSV file{td}
{tr}
{tr:style=font-size=10px}
{td:class=confluenceTd|colspan=5}This annotation is associated to the root class of the model and must be declared one time.

case 1 : separator = ','
If the record represents orders, then this annotation is added to the Order class like this :</p>

@CsvRecord( separator = "," )
public Class Order

case 2 : separator = ';'

@CsvRecord( separator = ";" )
public Class Order

case 3 : separator & skipfirstline

@CsvRecord(separator = ",", skipFirstLine = true)
public Class Order

{td}
{tr}
{tr}
{td:class=confluenceTd|colspan=5|height=0}&nbsp;{td}
{tr}
{tr}
{td:class=confluenceTd}*Link*{td}
{td:class=confluenceTd|align=center}Class, Property{td}
{td:class=confluenceTd|align=center}linkType{td}
{td:class=confluenceTd|align=center}LinkType{td}
{td:class=confluenceTd|align=center}optional - by default the value is LinkType.oneToOne{td}
{tr}
{tr}
{td:class=confluenceTd|colspan=5}Only one-to-one relation is allowed.

e.g : If the model Class Client is linked to the Order class, then use annotation Link in the Order class like this: 

@Link
private Client client{td}
{tr}
{tr}
{td:class=confluenceTd|colspan=5}&nbsp;{td}
{tr}
{tr}
{td:class=confluenceTd}*DataField*{td}
{td:class=confluenceTd|align=center}Property{td}
{td:class=confluenceTd|align=center}int{td}
{td:class=confluenceTd|align=center}pos{td}
{td:class=confluenceTd|align=center}mandatory - digit number{td}
{tr}
{tr}
{td:class=confluenceTd}{td}
{td:class=confluenceTd|align=center}{td}
{td:class=confluenceTd|align=center}pattern{td}
{td:class=confluenceTd|align=center}string{td}
{td:class=confluenceTd|align=center}optional - default value = "" - will be used to format Decimal, Date, ...{td}
{tr}
{tr}
{td:class=confluenceTd}{td}
{td:class=confluenceTd|align=center}{td}


{td:class=confluenceTd|align=center}length{td}
{td:class=confluenceTd|align=center}int{td}


{td:class=confluenceTd|align=center}optional - digit number - represents the length of the field for fixed length format{td}
{tr}
{tr}
{td:class=confluenceTd}{td}
{td:class=confluenceTd|align=center}{td}

{td:class=confluenceTd|align=center}precision{td}
{td:class=confluenceTd|align=center}int{td}
{td:class=confluenceTd|align=center}optional - digit number - represents the precision to be used when the Decimal number will be formatted/parsed{td}
{tr}
{tr}
{td:class=confluenceTd|colspan=5}
case 1 : position 

@DataField(pos = 0)
private int orderNr;

@DataField(pos = 1)
private String ref;

case 2 : pattern

@DataField(pos = 8, pattern = "dd-MM-yyyy")
private Date orderDate;

case 3 : precision

@DataField(pos = 6, precision = 2)
private BigDecimal amount;
{td}
{tr}
{tr}
{td:class=confluenceTd|colspan=5}&nbsp;{td}
{tr}
{table}

h3. Using the Java DSL

The next step consists in instantiaing the DataFormat _bindy_ class associated with this record type and providing Java package name(s) as parameter.

For example the following uses the class CsvBindyFormat (who correspond to the class associated with the CSV record type) which is configured with "com.acme.model"
package name to initialize the model objects configured in this package.

{code}
DataFormat bindy = new CsvBindyDataFormat("com.acme.model");

from("file://inbox").
  unmarshal(bindy).
  to("bean:handleOrder");
{code}

The Camel route will pick-up files in the inbox directory, unmarshall CSV records in a collection of model objects and send the collection
to the bean referenced by 'handleOrder'.

The collection is a list of Map. Each Map of the list contains the objects of the model. Each object can be retrieve using its class name.

{code}
    int count = 0;
			
    List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
    Map<String, Object> model = new HashMap<String, Object>();
        	
    models = (List<Map<String, Object>>) exchange.getIn().getBody();
    
    Iterator<Map<String, Object>> it = models.iterator();
		
    while(it.hasNext()){
		
          model = it.next();
			  
	  for(String key : model.keySet()) {
	     Object obj = model.get(key); 
	     LOG.info("Count : " + count + ", " + obj.toString());
	  }
			  
	 count++;
    }
			
    LOG.info("Nber of CSV records received by the csv bean : " + count);
{code}


To generate CSV records from a collection of model objects, you create the following route :

from("")
   marshal(bindy)
   to("file://outbox") 

You can if you prefer use a named reference to a data format which can then be defined in your [Registry] such as via your [Spring] XML file. e.g. 

{code}
from("file://inbox").
  unmarshal("myBindyDataFormat").
  to("bean:handleOrder");
{code}




h3. Using Spring XML
TODO:

h3. Dependencies

To use Bindy in your camel routes you need to add the a dependency on *camel-bindy* which implements this data format. 

If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see [the download page for the latest versions|Download]).

{code}
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-bindy</artifactId>
  <version>2.0.0</version>
</dependency>
{code}