Using a bean property converter for conversion between a String and a Java object

You can use bean property converter to perform conversion between a string and a specific Java object. When you register a converter, this type is treated as a simple data type by BeanCollection. When you access the value of a simple data type object, it is initialized as BeanDataField. Within WebSphere® Multichannel Bank Transformation Toolkit, there are different simple data converters such as Integer, Byte, Boolean, Character, and so on.

General steps

Concurring bean property converter

Add the following codes to the settings section in the btt.xml file:
<kColl id="beanPropertyConverters">
	<field id="java.util.Date"
					value="com.ibm.btt.typeconverter.converter.DateConverter" />
	<field id="java.util.Locale"
					value="com.ibm.btt.typeconverter.converter.LocaleConverter" />
	<!-- primitive array -->
	<field id="[B"
					value="com.ibm.btt.typeconverter.converter.ByteArrayConverter" />
	<field id="[I"
					value="com.ibm.btt.typeconverter.converter.IntArrayConverter" />
	<field id="java.util.ArrayList"
					value="com.ibm.btt.typeconverter.converter.ArrayListConverter" />
	<field id="java.util.HashMap"
					value="com.ibm.btt.typeconverter.converter.HashMapConverter" />
	<field id="com.ibm.btt.typeconverter.bean.Customer"
					value="com.ibm.btt.typeconverter.converter.CustomerConverter" />

	</kColl>

Defining POJO element with complex attributes

To declare bean collection element, use the following code:
package com.ibm.btt.typeconverter.bean;

import java.io.Serializable;
import java.util.*;

public class Account implements Serializable {

	// define attributes of account
	private Date date;
	private Locale locale1;
	private Customer customer;
	private int id;
	
	public void setDate(Date date) {
		this.date = date;
	}
	public Date getDate() {
		return date;
	}
	public void setLocale1(Locale locale1) {
		this.locale1 = locale1;
	}
	public Locale getLocale1() {
		return locale1;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return id;
	}
}

Develop converter between string and complex Java object

To declare customizing converter, you must implement an interface com.ibm.btt.element.simple.PropertyConverter, and define the convert logic in convertToValue method. Here is a sample:
package com.ibm.btt.typeconverter.converter;

import java.util.Date;
import com.ibm.btt.element.simple.PropertyConverter;
import com.ibm.btt.typeconverter.bean.Account;
import com.ibm.btt.typeconverter.bean.Customer;

public class AccountConverter implements PropertyConverter {

	@Override
	public Object convertToValue(String text) {

		String[] properties = text.split(",");
		Account account = new Account();
		Customer customer = new Customer();
		try {
			customer.setName(properties[0]);
			customer.setPhone(properties[2]);
			account.setCustomer(customer);
			account.setId(Integer.parseInt(properties[1]));
			account.setDate(new Date());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return account;
	}
}

Converting string value to specific Java object

First, initialize the converter:
InitManager.reset("jar:///definition/btt.xml");
Then convert string value to specific Java object:
Context beanCtx = (Context)ContextFactory.createContext("complexCtx");
beanCtx.setValueAt("account.id", "12345");
beanCtx.setValueAt("account", "steven,28,1234567890");