In some cases the data types of objects differ extremely from field types of database tables, especially when existing table structures are connected with classes. It happens, that values in the table are maintained completely differently to those in the class.
The date, for example, could be maintained in a database table as a text field, but inside Java the java.util.Date value is used.
To deal with these problems, Signsoft intelliBO introduces exchange operations. You can use these to do type conversion when reading or writing without modifying the source code of classes.
Example
The following example uses an exchange operation to store the value of a date attribute in a table as a String , according to the ISO format. The type of the attribute in the object model is a java.util.Date instead.
Every exchange operation is a class that should implement the interface ExchangeOperation from package com.signsoft.ibo.jdbc:
public class ISODateExchangeOperation implements ExchangeOperation
{
public void init(Properties p) throws ExchangeException
{
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
}
public Object toDatabase(Object o)
throws ExchangeException
{
if (o == null) return "";
if (o instanceof Date) return dateFormat.format((Date)o);
else throw new ExchangeException("Not supported.");
}
public Object fromDatabase(Object o)
throws ExchangeException
{
if (o == null) return null;
if (o instanceof String) return dateFormat.parse((String)o);
else throw new ExchangeException("Not supported.");
}
private SimpleDateFormat dateFormat;
}
An object of this class is initialized using the init() method. The parameters passed in this method are retrieved from the additional information that is declared in the JDO file. The methods toDatabase() and fromDatabase() are used for conversion. Please make sure, that the correct data type is returned here.
The following options should be defined in the JDO file:
<field name="birthday">
<extension vendor-name="ssibo" key="jdbc">
. . .
<extension vendor-name="ssibo" key="exchange-operation">
<extension vendor-name="ssibo" key="class-name" value="com.test.ISODateExchangeOperation"/>
<!--
<extension vendor-name="ssibo" key="user-key" value="user-value"/>
-->
</extension>
</extension>
</extension>
</field>
An exchange operator can be set on a per-attribute basis. You have to specify the name of the implementing class and, optionally, any number of parameters that are declared within additional extensions as key-value pairs and passed to the operator at runtime.
The exchange operator requires no modification to an existing class when applied.
Please note: Not every data type can be converted in some other data type using this operator. On all base types, their wrapper classes and using the class String , you can only change the values, never the type itself.