Problem
The source structure in this example contains two records:
· A Person record that stores the Id and Name of a person in the source message
· A Telephone record that stores one telephone number for a PersonId in the source message
The name and telephone numbers for a person are therefore stored in separate records, and can be assigned to each other by comparing the Id in the Person record with the PersonId in the Telephone record.
The target structure is flat and stores Id, Name, and TelephoneNumberin a Person record. The problem with this message mapping is that you also need to know the name and telephone number for an ID when you create the Id field in the target structure.
Solution
· First of all, you need to create the exact same number of Person records in the target structure as there are Telephone records. The source field Telephone is therefore assigned to the target field Person.
· Assign the source field PersonId to the target field Id. Since the telephone number for this ID is in the same context as PersonId (source field Number), you can transfer it by means of a simple assignment: TelephoneNumber = Number.
· It is more difficult, however, to determine the value of the target field Name for a particular ID and telephone number. You need to know which name belongs to which ID before you execute the message mapping.
The advanced user-defined function getNameById records this information and returns the name from a Person record when passed the PersonId from the Telephone record.
The function proceeds as follows:
...
1. The function needs Id and Name in the Persons context so that all the IDs and names within this context can be copied.
2. The basic idea is to save all related (Id, Name) pairs in one container object so they can be read again later. To do this, the function accesses a map names. If this map does not already exist, the function creates it and saves all (Id, Name) pairs in a HashMap. This map is passed to the container object so that it is available each time the function is called.
3. Finally, the function uses the ResultList Object to return the name for the input argument PersonId (a[0]).
Advanced 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
// 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]));
}
No comments:
Post a Comment