
| Complex Record Mapping using Java
By Carl Ozkaynak | |
IData Looping and Logic
Continuing with our example, let's use the value of OrganizationType and the value of a OrderItem element to set the value of a third element, also in the OrderItem Record List.
First, use the ValuesEmulator class' get() method to retrieve an IData array representing the Record List.
IData[] orderItemsList = (IData[])ValuesEmulator.get(orderInformationList, "OrderItem");
Next, using a standard Java for-loop, get the value of orderItemsList key "Stock". Remember that a cursor is required to navigate the IData object.
for (int i=0; i<orderItemsList.length; i++) {
IDataCursor idcItem = orderItemsList[i].getCursor();
idcItem.first("Stock");
String stock = (String)idcItem.getValue();
Lastly, to finish our example, if-else logic is applied and the value of "CUSIP" is set.
if (stock.equals("IBM") && organizationType.equals("XYZ")) {
// Set the value "CUSIP", if the transformation rule is true.
idcItem.first("CUSIP");
idcItem.setValue("12345");
}
Don't forget to destroy the cursor!
Navigating an IData object in Java
Knowing the basic methods, we can look at the example in its entirety.
/**
Use the get() method from the ValuesEmulator class to retrieve the Order record from the pipeline. Note: The pipeline is actually an IData object, and the Order record is an IData object. The IData type is the fundamental data abstraction used by webMethods Integration Server and its tools. IData is a container for key/value pairs.
*/
IData orderList = (IData)ValuesEmulator.get(pipeline, "Order");
IData newOrderList = (IData)ValuesEmulator.get(orderList, "NewOrder");
IData bulkOrderList = (IData)ValuesEmulator.get(newOrderList, "BulkOrder");
IData orderInformationList = (IData)ValuesEmulator.get(bulkOrderList, "OrderInformation");
/**
The nested OrderInformation IData object contains information about the OrderItems. Here the getCursor() method is called to retrieve an IdataCursor for this IData object. An IdataCursor contains the basic methods needed to traverse an IData object and to get and set elements within it. The cursor starts out in an ‘unpositioned’ state that is not resting on an element of the IData object.
*/
IDataCursor idc = orderInformationList.getCursor();
/**
Move the cursor to the first key/value pair that has a specified key of "CustomerName"
*/
idc.first("CustomerName");
/**
Retrieve the corresponding value of "CustomerName" using the getValue() method.
*/
String custName = (String)idc.getValue();
/**
Move the cursor to the first key/value pair that has a specified key of "OrganizationType"
*/
idc.first("OrganizationType");
/**
Retrieve the corresponding value of "OrganizationType" using the getValue() method.
*/
String organizationType = (String)idc.getValue();
/**
Always explicitly discard the IdataCursor when finished with it. The Java garbage collector will do this if you neglect to do so, but explicitly destroying the cursor will provide more efficient performance.
*/
idc.destroy();
/**
Use the get() method from the ValuesEmulator class, retrieve the OrderItem record list. This contains all the specific stock orders.
*/
IData[] orderItemsList = (IData[])ValuesEmulator.get(orderInformationList, "OrderItem");
/**
Loop through each stock order. Retrieve the Stock, Price and side values. Based on the stock and the organization type set the corresponding CUSIP, or SEDOL values accordingly.
*/
for (int i=0; i<orderItemsList.length; i++) {
IDataCursor idcItem = orderItemsList[i].getCursor();
idcItem.first("Stock");
String stock = (String)idcItem.getValue();
idcItem.first("Price");
String price = (String)idcItem.getValue();
idcItem.first("Side");
String side = (String)idcItem.getValue();
if (stock.equals("IBM") && organizationType.equals("XYZ")) {
// Set the value "CUSIP", if the transformation rule is true.
idcItem.first("CUSIP");
idcItem.setValue("12345");
}
if (stock.equals("NRWR") && organizationType.equals("ABC")) {
// Set the value "SEDOL", if the transformation rule is true.
idcItem.first("SEDOL");
idcItem.setValue("67891");
}
if (side.equals("B") && organizationType.equals("CAN")) {
// Set the value "SEDOL", if the transformation rule is true.
idcItem.first("side");
idcItem.setValue("Buy");
}
}
// clean up
idcItem.destroy();
Conclusion
Using Java, webMethods provides an elegant framework for mapping and transforming Records. For faster performance, managing complex logic, or stronger error handling, the webMethods API provides a comprehensive set of methods for creating, navigating, and manipulating IData objects.
For more information about the webMethods API, open Developer and select "Help > webMethods API".
<<Prev 1 [2]
Go Deeper on the Subject: The wMUsers Discussion Forums
Carl Ozkaynak is a Senior Application Consultant with NerveWire Inc and has significant experience and expertise in multiple technologies. Carl specializes in system integration and has significant experience in Internet/Intranet, middleware, client/server, and distributed technologies. Carl has designed, delivered and lead Internet and Intranet projects for a variety of industries, including financial services, high-tech manufacturing, and telecommunications.
Carl can be reached via email at