Runtime components : Core components : Typed data : Tasks : Defining typed data elements
  
Defining typed data elements
To declare typed data elements at configuration or installation time, modify the XML files using the CHA Editor or any ASCII editor. These typed data elements can be dynamically updated at runtime. Define typed data elements in the data definition file. The types definition file should only be used as a repository of generic types. The typed data elements defined in the data file can reference these generic types and override some of type generic properties depending on what the data element requires.
The following are example definitions of data elements as they might appear in a typed data definition file:
Example 1
Instantiate a typed data field of type money.
Typed data definition
<type id="Money" implClass="com.ibm.btt.base.DataField">
<Descriptor id="typeDefault"
implClass="com.ibm.btt.base.types.ext.FloatDescriptor">
<Converter convTypes="default"
implClass="com.ibm.btt.base.types.ext.FloatConverter"/>
<Validator
implClass="com.ibm.btt.base.types.ext.FloatValidator"
lowerLimit="0"/>
</Descriptor >
</kColl>
Code
DataField elem1 = (DataField)
DSEType.readObject("Money");
elem1.setValue(new Float("200"));
Example 2
Instantiate a typed keyed collection of type account and set its balance to 200.
Typed data definition
<type id="AccountNumber"
implClass="com.ibm.btt.base.DataField">
<Descriptor id="typeDefault"

implClass="com.ibm.btt.base.types.ext.StringPropertyDescriptor">
<Converter convTypes="default,xml"
implClass="com.ibm.btt.base.types.ext.StringConverter"/>
<Validator
implClass="com.ibm.btt.base.types.ext.IntegerStringValidator"
minLength="8" maxLength="8"/>
</Descriptor>
</type>
<type id="Account"
implClass="com.ibm.btt.base.KeyedCollection">
<KCollDescriptor id="typeDefault" refType="Account"/>
<StringDescriptor id="accountNumber" refType="AccountNumber"
initialValue="12345678"
isMandatory="true"/>
<FloatDescriptor id="accountBalance" refType="Money"
initialValue="50"
upperLimit="100000"/>
</type>
Code
KeyedCollection kColl = (KeyedCollection)
DSEType.readObject("Account");
kColl.setValueAt("accountBalance", new Float("200"));
Note To ensure optimum performance in the type instantiation process, all default descriptors for types should include the implClass attribute in their definition. The rest of descriptors should also have the implClass attribute defined although the system can get the implementation class from the default descriptor of the type identified by the refType attribute.
Example 3
Instantiate the myBalance typed data field defined in the data definition file and set its balance to 200.
Definition in the toolkit types definition file:
<type id="Money" implClass="com.ibm.btt.base.DataField">
<Descriptor id="typeDefault"
implClass="com.ibm.btt.base.types.ext.FloatDescriptor">
<Converter convTypes="default"
implClass=com.ibm.btt.base.types.ext.FloatConverter/>
<Validator
implClass="com.ibm.btt.base.types.ext.FloatValidator"
lowerLimit="0"/>
</Descriptor >
</kColl>
Definition in the toolkit data definition file:
<data id="myBalance" refType="Money" value="1000"/>
Code
DataField elem1 = (DataField)DataElement.readObject("myBalance");
elem1.setValue(new Float("200"));
Example 4
Value conversions in typed data
Definition in the toolkit types definition file:
<type id="Currency" implClass="com.ibm.btt.base.DataField">
<Descriptor id="typeDefault" implClass="com.ibm.btt.base.types.impl.SimplePropertyDescriptor">
<Converter convTypes="default" implClass="com.ibm.btt.base.types.impl.CurrencyConverter">
<param value="EUR" id="currency"/>
<param value="bigDecimal" id="numberType"/>
</Converter>
<Validator implClass="com.ibm.btt.base.types.impl.CurrencyValidator">
<param value="bigDecimal" id="numberType"/>
</Validator>
</Descriptor>
</type>
Definition in the toolkit data definition file:
<data id="validCur" refType="Currency">
<param value="true" id="isMandatory" />
<param value="2" id="decimalPlaces" />
<param value="USD" id="currency" />
</data>
Code
1 Set value with expected java instance by type: "Currency"
DataField validCur = (DataField) DataElement.readObject("validCur");
validCur.setValidValue(new Currency("CNY",BigDecimal.TEN));
2 Set value with String instance:
DataField validCur = (DataField) DataElement.readObject("validCur");
validCur.setValidValue("345.34");
Note All above codes are correct. If the parameter value is not expected, type validation raises an exception, specifying a type mismatch (NLS key is BASE009). However, if the parameter value is a String instance, the type converter unformats the String instance to an expected Java type, and then validates the value. This is why you can set a String value to a Currency data element.
See also
Tasks