groovy

Remove XML node based on the difference in days comparison


The requirement is to compare the dates, if the difference is more than the specified days we need to discard the particular XML node for further processing. I have written groovy scripts for the same to use the same in the SAP CPI Integration tool. Not sure why it is not working. Per the script, I expect the first node to be removed as 57 is not greater than 60. Second node 423 is greater than 60, so I expect this node in the output. I am not getting any errors, I am getting the same input in the output

Any help or suggestions would be highly appreciated.

Parameters:

interfaceLastRunTime = 2024-06-01T00:00:00
effDateDiff = 60
ManualRun= Yes
golive_date = 
Manual_Delta_LSRD = 2024-06-01T00:00:00
end_dt = 2024-07-31T00:00:00

Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.util.XmlSlurper;
import groovy.xml.MarkupBuilder;
import groovy.xml.XmlUtil;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import groovy.time.TimeCategory;
import java.util.GregorianCalendar;

def Message processData(Message message) {

    def body = message.getBody(java.io.Reader);
    def payload = new XmlSlurper().parse(body);

    map = message.getProperties();
    def lsrd = map.get("interfaceLastRunTime");
    int effDateDiff = map.get("effDateDiff").toInteger();
    def manual_run = map.get("ManualRun");
    golive_date = map.get("golive_date");
    Manual_Delta_LSRD = map.get("Manual_Delta_LSRD");
    end_dt = map.get("end_dt");
    
    if(lsrd == null || lsrd == "")
    {
       if(Manual_Delta_LSRD == null || Manual_Delta_LSRD == "")
        {
            lsrd = golive_date;
        }
        lsrd = Manual_Delta_LSRD;
    }
    lsrd = lsrd.substring(0, 19);
    SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
    SimpleDateFormat sdtf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 

    payload.Row.each {r ->
       def pid = r.PID.text();
       def jobinfo_stdt = r.jobinfo_stdt.text()
       
       if(jobinfo_stdt != null && jobinfo_stdt != "")
       {
           Date new_jobinfo_stdt = sdf.parse(jobinfo_stdt);
           Date new_LSRD = sdtf.parse(lsrd); 
           
            int dateDiffInDays = new_LSRD - new_jobinfo_stdt;
            dateDiffInDays=dateDiffInDays.toInteger()+1
            println dateDiffInDays
            println effDateDiff
           if(dateDiffInDays >= effDateDiff )
           {
               r.replaceNode{}
            //message.setBody(dateDiffInDays.toString())
            }
       }
    }       
    message.setBody(XmlUtil.serialize(payload));
    return message;
}

XML Input:

<?xml version="1.0" encoding="UTF-8"?>
<Records>
    <Row>
        <ACTION>CHANGE</ACTION>
        <PID>M123456</PID>
        <Last_Name/>
        <Last_Name_previous/>
        <First_Name/>
        <First_Name_previous/>
        <salutation/>
        <Gender/>
        <jobinfo_stdt>06042024</jobinfo_stdt>
        <jobinfo_stdt_previous/>
        <event>5</event>
        <event_previous/>
        <emplStatus>T</emplStatus>
        <FullPartTimeInd>true</FullPartTimeInd>
        <Permanent_Temporary>R</Permanent_Temporary>
        <recurring>
            <recurring_stdt>07052024</recurring_stdt>
            <recurring_value>1234</recurring_value>
            <recurring_value_previous/>
            <recurring_code>1111</recurring_code>
        </recurring>
        <recurring>
            <recurring_stdt>06042024</recurring_stdt>
            <recurring_value>42123</recurring_value>
            <recurring_value_previous/>
            <recurring_code>2222</recurring_code>
        </recurring>
    </Row>
    <Row>
        <ACTION>CHANGE</ACTION>
        <PID>M654321</PID>
        <Last_Name/>
        <Last_Name_previous/>
        <First_Name/>
        <First_Name_previous/>
        <salutation/>
        <Gender/>
        <jobinfo_stdt>07042023</jobinfo_stdt>
        <jobinfo_stdt_previous/>
        <event>5</event>
        <event_previous/>
        <emplStatus>T</emplStatus>
        <FullPartTimeInd>true</FullPartTimeInd>
        <Permanent_Temporary>R</Permanent_Temporary>
        <recurring>
            <recurring_stdt>07052024</recurring_stdt>
            <recurring_value>1234</recurring_value>
            <recurring_value_previous/>
            <recurring_code>1111</recurring_code>
        </recurring>
        <recurring>
            <recurring_stdt>06042024</recurring_stdt>
            <recurring_value>42123</recurring_value>
            <recurring_value_previous/>
            <recurring_code>2222</recurring_code>
        </recurring>
    </Row>
</Records>

Regards, Pavan


Solution

  • I have fixed the issue by using the XML Parser instead of XML slurper and bit modifications to the script:

    solution:


    import com.sap.gateway.ip.core.customdev.util.Message;
    import java.util.HashMap;
    import groovy.util.XmlParser;
    import groovy.xml.MarkupBuilder;
    import groovy.xml.XmlUtil;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import groovy.time.TimeCategory;
    import java.util.GregorianCalendar;
    
    def Message processData(Message message) {
    
        def body = message.getBody(java.io.Reader);
        def payload = new XmlParser().parse(body);
    
        map = message.getProperties();
        def lsrd = map.get("interfaceLastRunTime");
        int effDateDiff = map.get("effDateDiff").toInteger();
        def manual_run = map.get("ManualRun");
        golive_date = map.get("golive_date");
        Manual_Delta_LSRD = map.get("Manual_Delta_LSRD");
        end_dt = map.get("end_dt");
        
        if(lsrd == null || lsrd == "")
        {
           if(Manual_Delta_LSRD == null || Manual_Delta_LSRD == "")
            {
                lsrd = golive_date;
            }
            lsrd = Manual_Delta_LSRD;
        }
        lsrd = lsrd.substring(0, 19);
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
        SimpleDateFormat sdtf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
    
        def output = ""
        payload.Row.findAll {
            def pid = it.PID.text();
            def jobinfo_stdt = it.jobinfo_stdt.text()
            int dateDiffInDays
            if(jobinfo_stdt != null && jobinfo_stdt != "")
            {
               Date new_jobinfo_stdt = sdf.parse(jobinfo_stdt);
               Date new_LSRD = sdtf.parse(lsrd); 
               
                dateDiffInDays = new_LSRD - new_jobinfo_stdt;
                dateDiffInDays=dateDiffInDays.toInteger()+1
            }
            dateDiffInDays >= effDateDiff 
        } .each { payload.remove(it) }
        message.setBody(XmlUtil.serialize(payload));
        return message;
    }