HashMaps are a fundamental and highly efficient data structure in Groovy, and they are incredibly useful for handling key-value pairs within SAP CPI integration flows. They provide a quick way to store and retrieve data, making your scripts more performant and easier to manage, especially when dealing with dynamic message payloads or configuration data.
A HashMap maps keys to values. Think of it like a dictionary or a phonebook where you look up a person's name (the key) to find their phone number (the value). In a Groovy script, you can use it to store and access message headers, properties, or any other data you need to manipulate.
Why Use a HashMap in SAP CPI?
- Efficient Data Access: Retrieving a value by its key is extremely fast, making it ideal for lookup operations.
- Flexible Data Storage: It can store different types of data (strings, numbers, objects) as values.
- Simplifies Code: It reduces the need for complex conditional statements (if-else, switch) by allowing you to map inputs to specific actions or values.
- Dynamic Lookups: You can build the map dynamically based on your incoming message payload, which is a common requirement in integration scenarios.
Example: Using HashMap to Map and Transform Data
Let's consider a common scenario in SAP CPI: transforming a vendor's country code into a full country name. The challenge is you have to get the country code from another call in CPI and those values you need to use in message transformation of initial payload. We can use a HashMap to store the country code-to-name mapping.
Initial Payload before the HashMap Initialize:
This in incoming message in which we need to modify with country code.
<root> <row> <City>Delhi</City> <Country>IN</Country> </row> <row> <City>Berlin</City> <Country>DE</Country> </row> </root>
Groovy Script to Prepare the HashMap:
This script initializes a HashMap and stores it as a message property.
import com.sap.gateway.ip.core.customdev.util.Message; import java.util.HashMap; def Message processData(Message message) { // Initialize a new HashMap HashMapCountryCode = new HashMap (); // Store the HashMap as a message exchange property message.setProperty("countryMapping",CountryCode); return message }
The payload extracted from another call (Lookup Payload):
This xml payload contain country code along with its name.
<root> <row> <CountryCode>IN</CountryCode> <CountryName>India</CountryName> </row> <row> <CountryCode>DE</CountryCode> <CountryName>Germany</CountryName> </row> <row> <CountryCode>US</CountryCode> <CountryName>United State</CountryName> </row> <row> <CountryCode>JP</CountryCode> <CountryName>Japan</CountryName> </row> </root>
Groovy Script to store this values in HashMap:
This stores country code as Key and country name as its values against the key.
import com.sap.gateway.ip.core.customdev.util.Message; def Message processData(Message message) { def body = message.getBody(java.lang.String); def list = new XmlSlurper().parseText(body); def map = message.getProperties(); String code = ""; String val = ""; def act = map.get("countryMapping"); list.row.each { code = it.CountryCode.text(); val = it.CountryName.text(); act.put(code,val); } message.setProperty("countryMapping", act); return message; }
Message Mapping User-Defined Function Code:
This function, used in a Message Mapping at Country field, retrieves the HashMap property from the message exchange and uses it for the lookup.
import com.sap.it.api.mapping.*; def String getFrequency(String code, MappingContext context) { def strData=""; HashMapgetdata = context.getProperty("countryMapping"); strData = getdata.get(code); if(!"".equals(strData) && (!" ".equals(strData)) && strData != null) { return strData; } else { return ""; } }
Final outcome after the messag mapping:
As you can see the message have been transformed for Country field with its full name.
<root> <row> <City>Delhi</City> <Country>India</Country> </row> <row> <City>Berlin</City> <Country>Germany</Country> </row> </root>