Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
gaganhl
Explorer

Introduction:

This blog is dedicated to exploring the intricacies of data comparison in SAP CPI implementation scenarios. One of the key challenges in data/message flow optimisation is effectively comparing input payloads. In this blog series, we will focus on comparing two XML payloads, regardless of their structural similarities or differences, with a keen eye on detecting and highlighting data changes.

Scenario :

In SAP CPI implementation scenarios, comparing input payloads is a crucial factor in data/message flow optimisation. In this blog, we delve into the comparison of two XML payloads. Whether the payloads possess different structures or share a similar structure, the focus remains on detecting data changes between the old and updated versions. Our goal is to highlight the updated data segments, ensuring a streamlined
output.

Solution :

Leveraging Groovy Script in the Flow for Data Comparison.

IFlow Design:

Screenshot 2024-03-22 at 3.58.27 PM.png

  • Content Modifier : We will provide a dataset of employees in the content modifier named as "Payload input1". Below is the sample data provided.

Screenshot 2024-03-24 at 10.58.43 PM.png

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<EMPLOYEE>
			<EMPLOYEE_ID>1001</EMPLOYEE_ID>
			<FIRST_NAME>Buggs</FIRST_NAME>
			<LAST_NAME>Bunny</LAST_NAME>
			<EMAIL>BuggsBunny@abc.com</EMAIL>
			<PHONE_NUMBER>212-867-5309</PHONE_NUMBER>
			<HIRE_DATE>12 DEC 1985</HIRE_DATE>
			<JOB_ID>MD12741</JOB_ID>
			<SALARY>4000.00</SALARY>
			<COMMISSION_PCT>0.03</COMMISSION_PCT>
			<DESIGNATION>Developer</DESIGNATION>
			<DEPARTMENT_ID>5341</DEPARTMENT_ID>
	</EMPLOYEE>
	<EMPLOYEE>
			<EMPLOYEE_ID>1002</EMPLOYEE_ID>
			<FIRST_NAME>Robert</FIRST_NAME>
			<LAST_NAME>Jay</LAST_NAME>
			<EMAIL>RobertJay@abc.com</EMAIL>
			<PHONE_NUMBER>212-867-2345</PHONE_NUMBER>
			<HIRE_DATE>02 JAN 1983</HIRE_DATE>
			<JOB_ID>MD12742</JOB_ID>
			<SALARY>7000.00</SALARY>
			<COMMISSION_PCT>0.13</COMMISSION_PCT>
			<DESIGNATION>Consultant</DESIGNATION>
			<DEPARTMENT_ID>5342</DEPARTMENT_ID>
	</EMPLOYEE>
	<EMPLOYEE>
			<EMPLOYEE_ID>1003</EMPLOYEE_ID>
			<FIRST_NAME>Tom</FIRST_NAME>
			<LAST_NAME>Frank</LAST_NAME>
			<EMAIL>TomFrank@abc.com</EMAIL>
			<PHONE_NUMBER>212-867-2342</PHONE_NUMBER>
			<HIRE_DATE>01 OCT 1980</HIRE_DATE>
			<JOB_ID>MD12743</JOB_ID>
			<SALARY>10000.00</SALARY>
			<COMMISSION_PCT>0.23</COMMISSION_PCT>
			<DESIGNATION>Manager</DESIGNATION>
			<DEPARTMENT_ID>5343</DEPARTMENT_ID>
	</EMPLOYEE>
</root>​
  • Content Modifier : We will declare a property named "input1" to store payload1.

Screenshot 2024-03-24 at 11.02.13 PM.png

  • Content Modifier : Another dataset of employees will be provided in the content modifier named "Payload input2". However, in this example, the XML data structure is different, and the data has been updated. Below is the sample data provided.

Screenshot 2024-03-24 at 11.11.23 PM.png

The salary data of the employee with Employee Id 1003 has been updated from <SALARY>10000.00</SALARY> to <SALARY>14000.00</SALARY>.

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<EMPLOYEE>
		<EMPLOYEE_ID>1002</EMPLOYEE_ID>
		<FIRST_NAME>Robert</FIRST_NAME>
		<LAST_NAME>Jay</LAST_NAME>
		<HIRE_DATE>02 JAN 1983</HIRE_DATE>
		<JOB_ID>MD12742</JOB_ID>
		<COMMISSION_PCT>0.13</COMMISSION_PCT>
		<DESIGNATION>Consultant</DESIGNATION>
		<DEPARTMENT_ID>5342</DEPARTMENT_ID>
		<EMAIL>RobertJay@abc.com</EMAIL>
		<PHONE_NUMBER>212-867-2345</PHONE_NUMBER>
		<SALARY>7000.00</SALARY>
	</EMPLOYEE>
	<EMPLOYEE>
		<EMPLOYEE_ID>1001</EMPLOYEE_ID>
		<FIRST_NAME>Buggs</FIRST_NAME>
		<LAST_NAME>Bunny</LAST_NAME>
		<HIRE_DATE>12 DEC 1985</HIRE_DATE>
		<JOB_ID>MD12741</JOB_ID>
		<COMMISSION_PCT>0.03</COMMISSION_PCT>
		<DESIGNATION>Developer</DESIGNATION>
		<DEPARTMENT_ID>5341</DEPARTMENT_ID>
		<EMAIL>BuggsBunny@abc.com</EMAIL>
		<PHONE_NUMBER>212-867-5309</PHONE_NUMBER>
		<SALARY>4000.00</SALARY>
	</EMPLOYEE>
	<EMPLOYEE>
		<EMPLOYEE_ID>1003</EMPLOYEE_ID>
		<FIRST_NAME>Tom</FIRST_NAME>
		<LAST_NAME>Frank</LAST_NAME>
		<HIRE_DATE>01 OCT 1980</HIRE_DATE>
		<JOB_ID>MD12743</JOB_ID>
		<COMMISSION_PCT>0.23</COMMISSION_PCT>
		<DESIGNATION>General Manager</DESIGNATION>
		<DEPARTMENT_ID>5343</DEPARTMENT_ID>
		<EMAIL>TomFrank@abc.com</EMAIL>
		<PHONE_NUMBER>212-867-2342</PHONE_NUMBER>
		<SALARY>14000.00</SALARY>
	</EMPLOYEE>
</root>
  • Content Modifier : We will declare a property named "input2" to store payload2. 

Screenshot 2024-03-24 at 11.16.13 PM.png

  • Groovy Script : The Groovy script below compares the data of both payload1 and payload2 based on the Employee Id, which is the unique data identifier. It then returns only the dataset that has been updated or modified compared to the previous data.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.util.XmlSlurper;
import groovy.xml.MarkupBuilder;


def Message processData(Message message) {

def input1 = message.getProperty("input1")
def input2 = message.getProperty("input2")

def xml1 = new XmlSlurper().parseText(input1)
def xml2 = new XmlSlurper().parseText(input2)

def changedData = []

xml1.EMPLOYEE.each { row1 ->
    def matchingRow = xml2.EMPLOYEE.find { row2 ->
        row1.EMPLOYEE_ID.text() == row2.EMPLOYEE_ID.text()
    }

    if (matchingRow) {
        def diff = false

        row1.children().each { element ->
            def correspondingElement = matchingRow."${element.name()}"
            if (element.text() != correspondingElement.text()) {
                diff = true
            }
        }

        if (diff) {
            changedData << matchingRow
        }
    }
}

def resultXml = new StringWriter()
def xmlBuilder = new MarkupBuilder(resultXml)

xmlBuilder.root {
    changedData.each { changedRow ->
        EMPLOYEE {
            changedRow.children().each { element ->
                "${element.name()}"(changedRow."${element.name()}".text())
            }
        }
    }
}

    message.setBody(resultXml.toString())
    return message;
}

Result :

Screenshot 2024-03-24 at 11.45.38 PM.png

In conclusion, the output of using Groovy script in the flow for comparing data contains the data of the employee whose dataset has been updated. This approach not only streamlines data comparison processes but also ensures that only relevant and updated information is highlighted, contributing to efficient data/message flow management.

 

Thanks and Regards,
Gagan H L

Labels in this area