5

Learning Groovy Script for Cloud Integration – Part 1

 1 year ago
source link: https://blogs.sap.com/2022/11/02/learning-groovy-script-for-cloud-integration-part-1/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Learning Groovy Script for Cloud Integration – Part 1

2 8 2,681

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
Capture3.jpg
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 🙂

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK