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.
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.
Leveraging Groovy Script in the Flow for Data Comparison.
<?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>
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>
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;
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
8 | |
7 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |