The HashtableFormat definition is similar to that of the record format, and the way in which the format process searches for the data elements in the context is exactly the same: what is different is the result of formatting the data. In the case of using the record format, the result is the concatenation of the data element values to build a string. In the case of using the HashtableFormat, the result is a Java(TM) object of type Hashtable, with the key being the data element name and the value being the data element value that has to be inserted into or retrieved from a column of the database. The mapping between the data element name and the column name is done in the service definition.
The following is an example of the HashtableFormat in the formats definition file:
<fmtDef id="journalFormat"> <hashtable> <fObject dataName ="data_name_1"/> <fObject dataName = "data_name_2"/> ........... <fObject dataName = "data_name_n"/> </hashtable> </fmtDef>
The fObject tag identifies another toolkit format which, when it calls its format() method with a data element as argument, returns the data element value as it is stored in the context. This format can be thought of as similar to the constant format, but the result is not a Java String but any type of Java Object. The resulting Java Object type must match the expected type of the column table that this value needs to be inserted into or retrieved from .
This HashtableFormat definition does not include specific data element formatting. The data value is retrieved as it is from the context and used, with no additional formatting, to build the database record. If additional formatting is required for some data, the fObject tag may be changed by any tag that identifies the format to be used for that specific data element. In the following example, the data element named 'data_name_1' is expected to be a Date that will be converted into a String after formatting:
<fmtDef id="journalFormat"/> <hashtable> <fDate dataName = "data_name_1"/> <fObject dataName = "data_name_2"/> ........... <fObject dataName = "data_name_n"> </hashtable> </fmtDef>
The dataName values in a HashtableFormat contain data element names, and therefore you can either specify the full path for accessing data elements in the hierarchy context or use the * modifier.
By using the previous format definition, the relationship between a data element and a column in the database is static; a column in the database will only be able to hold the value of a specific data element within the context, since the mapping between columns and data elements set in the JDBC Service definition does not allow assignment of different data element names to the same column name. But in some situations, the application may need to insert data that is located in different places within the context hierarchy into the same database table. For instance, when working with the journal, different operations may need to add a record into the journaling table, although they may be working with a different data structure. This can be solved by using the following example object format inside the HashtableFormat definition:
<fmtDef id=journalFormat> <hashtable> <fObject key= key_1 dataName = "data_name_1" /> <fObject key=key_2 dataName = "data_name_2" /> ........... <fObject key=key_n dataName = "data_name_n" /> </hashtable> </fmtDef>
The following column to data element mapping pattern in the JDBC Service definition is used with the above example format:
<column id="COLUMN1" dataName="key_1" /> <column id="COLUMN2" dataName="key_2" /> ................. <column id="COLUMNN" dataName="key_n" />
In this example, the resulting Hashtable will have the key name (as specified in the Object format definition) as the key and the value of the data element (set in the dataName attribute of the Object format) as the associated value. Different formatters may then be used with the same service definition in order to work with data from different places within the context. The followings are the sample code to use a HashtableFormat through the CHA Formatter Service:
com.ibm.btt.formatter.client.FormatElement tableFormat = new com.ibm.btt.client.FormatElement(); dataTable1.setName("journalFormat"); Hashtable dataTable1 = new Hashtable(); dataTable1 = (Hashtable) tableFormat.formatHashtable(tableContext); tableFormat.unformatHashtable(dataTable1,tableContext);
If a toolkit application presentation layer entity or client entity needs to access the HashtableFormat, use the following code to access the formatter directly:
com.ibm.dse.base.HashtableFormat tableFormat= (com.ibm.dse.base.HashtableFormat)FormatElement.readObject("tableFormatName"); Hashtable dataTable1 = new Hashtable(); dataTable1 = (Hashtable) tableFormat.format(tableContext); tableFormat.unformat(dataTable1,tableContext);