Skip to Content
Technical Articles
Author's profile photo Raviteja Satuluri

Learning Groovy Script for Cloud Integration – Part 1

Introduction

Here I am just explaining the Importance of Groovy script in the Real time scenarios when we are dealing with Cloud Integration.

Groovy scripting is an integral and important feature of SAP Cloud Platform Integration (CPI). The goals of this repository are: Providing templates when you are implementing a new script. Easily finding Groovy functions related to the CPI topic at hand. Minimizing search engine time for common tasks.

Business Requirement:

The Target system is expecting Contact and Detail Records in to one Single Record for their Internal data validations. Hence, we are using Groovy script for Merging two Records into a single Record.

This requirement is even we can achieve using standard options Join & Gather but to create an Interest in beginners I have chosen this option to understand few basic concepts of Groovy.

 

Inputs:

Record 1: 

<Record>

    <Detail>
        <Key>1</Key>
        <Place>Ocean</Place>
        <City>Urban</City>
    </Detail>
    <Detail>
        <Key>2</Key>
        <Place>Road</Place>
        <City>Rural</City>
    </Detail>
    <Detail>
        <Key>3</Key>
        <Place>Plane</Place>
        <City>Semiurban</City>
    </Detail>
</Record>

 

Record 2: 

<Record>
    <Contact>
        <Key>1</Key>
        <Name>Jack</Name>
    </Contact>
    <Contact>
        <Key>2</Key>
        <Name>Ethan</Name>
    </Contact>
    <Contact>
        <Key>3</Key>
        <Name>Ron</Name>
    </Contact>
</Record>
Groovy Script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import org.xml.sax.InputSource;
def Message processData(Message message)
{
    def body = message.getBody(java.io.Reader)
    def mainDoc = new XmlParser().parse(body)
    def map = message.getProperties();
    String value = map.get(“Message_2”);
    def externalXmlDoc = new XmlParser().parse(new InputSource(new StringReader(value)))
    // Create “Extension1” element for clarity
    def nodeBuilder = new NodeBuilder();
    def node1 = nodeBuilder.Extension1{}
    // Merge
    node1.append(externalXmlDoc)
    mainDoc.append(node1)
    // Write document to body
    def sw = new StringWriter()
    def xmlNodePrinter = new XmlNodePrinter(new PrintWriter(sw))
    xmlNodePrinter.with {
        preserveWhitespace = true
    }
    xmlNodePrinter.print(mainDoc)
    String result = sw.toString()
    message.setBody(result)
    return message;
}
Test Execution:
a) Give your 1st Record as an Input
b) Add second Record as an Exchange Property
c) Execute the Code
Output:
<Record>
  <Detail>
    <Key>1</Key>
    <Place>Ocean</Place>
    <City>Urban</City>
  </Detail>
  <Detail>
    <Key>2</Key>
    <Place>Road</Place>
    <City>Rural</City>
  </Detail>
  <Detail>
    <Key>3</Key>
    <Place>Plane</Place>
    <City>Semiurban</City>
  </Detail>
  <Extension1>
    <Record>
      <Contact>
        <Key>1</Key>
        <Name>Jack</Name>
      </Contact>
      <Contact>
        <Key>2</Key>
        <Name>Ethan</Name>
      </Contact>
      <Contact>
        <Key>3</Key>
        <Name>Ron</Name>
      </Contact>
    </Record>
  </Extension1>
</Record>

Conclusion –

When you’re Implementing in IFLOW, you have to capture the payload of second record as an Exchange Property. Hope this document will help to beginners to understand CPI concept of Groovy Script.

Happy Learning 🙂

 

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Benjamin Nehring
      Benjamin Nehring

      Hi Raviteja Satuluri,

      good to read another blog about Groovy. I have the following suggestion to shorten the script:

      import com.sap.gateway.ip.core.customdev.util.Message
      import groovy.xml.*
      
      Message processData(Message message) {
          def body = message.getBody(Reader)
          String value = message.getProperty('Message_2')
          def mainDoc = new XmlParser().parse(body)
          def externalDoc = new XmlParser().parseText(value)
          mainDoc.appendNode('Extension1').append(externalDoc)
          message.setBody(XmlUtil.serialize(mainDoc))
          message.setProperty('Message_2', null)
          return message
      }

      If message property Message_2 already is a String you don't need to convert it to a Reader. You can just use the parseText method of the XmlParser. And if message property Message_2 is not used after this script step it makes sense to free up that memory (i.e. by setting it to null).

      BR, Benjamin

      Author's profile photo Jay Adnure
      Jay Adnure

      Thanks for sharing the steps helped me.