
This example is similar to the MapExampleTwoTablesToFlatStructure example: The starting structure consists of two records that each contain the name and telephone number of a person. An additional difficulty is that the target strcutre is not flat but all telephone numbers of a person are to be stored within a Person record.
Solution
The number of Person records in the target structure must be the same as the number of Person records in the source structure. The assignment /ns:Map Example NestedTablesMessage/Person = /ns:Map Example TwoTablesMessage/Persons/Person is therefore sufficient.
The values for ID and Name in the target structure can be copied by a simple assignment from the source structure.
You need to have an ID available for every ID in order to be able to copy the list of telephone numbers. Proceed as described in the example MapExampleTwoTablesToFlatStructure with an Enhanced User-Defined Function getPhonesById :
The function works in the following way:
The functions expects PersonID and a number in telephone-context so that all IDs and telephone numbers can be copied within this context.
The basic idea is to store for every ID a list of telephone numbers that belong to the ID in a Container Object so that you can call and read them later on. For this, the function accesses a phones map. If this map does not exist, the function creates a new HashMap . The function then creates a list (the first time) for each personID ( b[i] ) or gets an existing list for PersonID ( b[i] ) and adds the relevant telephone number ( c[i] ). The map with the telephone number lists is copied to the container object phones so that it is available for other calls of the same function.
Finally, the function returns the telephone list for the input argument (a[0]) using ResultList Object .
Enhanced user-defined function getPhonesById
public void getPhonesById(String[] a, String[] b, String[] c, ResultList result, Container container){
Map map = (Map) container.getParameter("phones");
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
List list = (List) map.get(b[i]);
if (list == null){
list = new ArrayList();
map.put(b[i], list);
}
list.add(c[i]);
}
container.setParameter("phones", map);
}
List list = (List)map.get(a[0]);
for (int i = 0; i< list.size(); i++) result.addValue((String) list.get(i));
}