MapExampleTwoTablesToFlatStructure
Use
In this example there are two records in the source structure:
-
A Person record whereby the ID and Name of a person are transferred in the outbound message.
-
A Telephone record whereby one telephone number is transferred in the outbound message for one PersonID .
The name and telephone number of a person are saved using separate records and can be assigned to each other by comparing the ID of the Person record with the PersonID in the Telephone record.
The target structure is flat and saves the ID , Name , and TelephoneNumber in a Person record. The difficulty with this sort of message mapping is that you need to know the name and telephone number for the ID at the time that you create an ID field in the target structure.
Solution
-
Firstly, there have to be as many person records created in the target structure as there are Telephone records. The source field Telephone is therefore assigned to the the target field Person .
-
You assign the source field PersonID to the ID target field. Since the telephone number is assigned to this ID in the same context as PersonID (source field Number ), you can copy it using a simple assignment: TelephoneNumber = Number.
-
It is more difficult to find out the value of the target field Name for an ID and a telephone number. For this you would have to know which name matches which ID before executing the message mappings.
The Enhanced User-Defined Function getNameById notes this information and returns the name from the Person record for a PersonID from the Telephone record:
The function works in the following way:
-
The functions expects an ID and Name in the Person context so that all IDs and telephone numbers can be copied within this context.
-
The basic idea is that pairs that belong together ( ID , Name ) are stored in a Container Object so that they can be read when they are called later. For this, the function accesses a Names map. If this map does not yet exist, the function creates one and saves all pairs ( ID , Name ) in a HashMap . This map is transferred to the container object so that it is available when the function is called again.
-
Finally the function returns the name of the input argument PersonID ( a[0] ) using the ResultList Object .
Enhanced user-defined function getNameById
public void getNameById(String[] a, String[] b, String[] c,ResultList result, Container container){
Map map = (Map) container.getParameter("names");
if (map == null){
map = new HashMap();
for (int i = 0; i<b.length; i++){
// if the payload is valid to xsd, b and c have equal length
map.put(b[i], c[i]);
}
container.setParameter("names", map); }
result.addValue((String)map.get(a[0]));
}