需求是比较日期,如果差异超过指定的天数,我们需要丢弃特定的 XML 节点以进行进一步处理。我已经为相同的内容编写了 groovy 脚本,以便在 SAP CPI 集成工具中使用相同的内容。不知道为什么它不起作用。 根据脚本,我预计第一个节点将被删除,因为 57 不大于 60。第二个节点 423 大于 60,因此我预计输出中会出现该节点。我没有收到任何错误,我在输出中得到相同的输入
任何帮助或建议将不胜感激。
参数:
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 脚本:
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 输入:
<?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>
问候, 帕万
我通过使用 XML 解析器而不是 XML slurper 解决了这个问题,并对脚本进行了一些修改:
解决方案:
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;
}