import groovy.util.XmlSlurper import java.time.* import java.time.format.DateTimeFormatter import java.time.LocalDateTime String EmpJobHistory = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ActionCode> <EmpJob> <emplStatusNav> <PicklistOption> <externalCode>A</externalCode> </PicklistOption> </emplStatusNav> <lastModifiedDateTime>2023-11-01T09:18:08.000</lastModifiedDateTime> <endDate>2023-09-09T00:00:00.000</endDate> <employeeClassNav/> <userId>70002627</userId> <eventReason>HIRE-DATACONV</eventReason> <startDate>1995-03-06T00:00:00.000</startDate> </EmpJob> <EmpJob> <emplStatusNav> <PicklistOption> <externalCode>A</externalCode> </PicklistOption> </emplStatusNav> <lastModifiedDateTime>2023-11-17T12:55:54.000</lastModifiedDateTime> <endDate>2024-03-22T00:00:00.000</endDate> <employeeClassNav> <PicklistOption> <externalCode>S</externalCode> </PicklistOption> </employeeClassNav> <userId>70002627</userId> <eventReason>C2/01</eventReason> <startDate>2023-09-10T00:00:00.000</startDate> </EmpJob> <EmpJob> <emplStatusNav> <PicklistOption> <externalCode>T</externalCode> </PicklistOption> </emplStatusNav> <lastModifiedDateTime>2024-03-21T17:19:46.000</lastModifiedDateTime> <endDate>9999-12-31T00:00:00.000</endDate> <employeeClassNav> <PicklistOption> <externalCode>S</externalCode> </PicklistOption> </employeeClassNav> <userId>70002627</userId> <eventReason>EN/81</eventReason> <startDate>2024-03-23T00:00:00.000</startDate> </EmpJob> </ActionCode>"//"<ActionCode> <EmpJob> <emplStatusNav> <PicklistOption> <externalCode>A</externalCode> </PicklistOption> </emplStatusNav> <lastModifiedDateTime>2023-11-02T06:28:45.000</lastModifiedDateTime> <endDate>2023-02-12T00:00:00.000</endDate> <employeeClassNav> <PicklistOption> <externalCode>S</externalCode> </PicklistOption> </employeeClassNav> <userId>70012459</userId> <eventReason>EJ/30</eventReason> <startDate>2022-02-14T00:00:00.000</startDate> </EmpJob> <EmpJob> <emplStatusNav> <PicklistOption> <externalCode>A</externalCode> </PicklistOption> </emplStatusNav> <lastModifiedDateTime>2024-04-01T09:18:14.000</lastModifiedDateTime> <endDate>2022-02-13T00:00:00.000</endDate> <employeeClassNav/> <userId>70012459</userId> <eventReason>HIRE-DATACONV</eventReason> <startDate>2007-10-29T00:00:00.000</startDate> </EmpJob> <EmpJob> <emplStatusNav> <PicklistOption> <externalCode>P</externalCode> </PicklistOption> </emplStatusNav> <lastModifiedDateTime>2024-04-12T12:14:59.000</lastModifiedDateTime> <endDate>9999-12-31T00:00:00.000</endDate> <employeeClassNav> <PicklistOption> <externalCode>S</externalCode> </PicklistOption> </employeeClassNav> <userId>70012459</userId> <eventReason>C2/01</eventReason> <startDate>2023-09-10T00:00:00.000</startDate> </EmpJob> <EmpJob> <emplStatusNav> <PicklistOption> <externalCode>P</externalCode> </PicklistOption> </emplStatusNav> <lastModifiedDateTime>2024-04-12T12:14:59.000</lastModifiedDateTime> <endDate>2023-09-09T00:00:00.000</endDate> <employeeClassNav> <PicklistOption> <externalCode>S</externalCode> </PicklistOption> </employeeClassNav> <userId>70012459</userId> <eventReason>EE/16</eventReason> <startDate>2023-06-30T00:00:00.000</startDate> </EmpJob> <EmpJob> <emplStatusNav> <PicklistOption> <externalCode>A</externalCode> </PicklistOption> </emplStatusNav> <lastModifiedDateTime>2023-11-02T06:28:45.000</lastModifiedDateTime> <endDate>2023-06-29T00:00:00.000</endDate> <employeeClassNav> <PicklistOption> <externalCode>S</externalCode> </PicklistOption> </employeeClassNav> <userId>70012459</userId> <eventReason>EJ/30</eventReason> <startDate>2023-02-13T00:00:00.000</startDate> </EmpJob> </ActionCode>" String cust_ActionHistory ="<?xml version=\"1.0\" encoding=\"UTF-8\"?><cust_DemgSync> <cust_OFBChDate/> <cust_ActionHistory> <cust_ActionHistory> <cust_startDate>9/10/2023</cust_startDate> <cust_SeqNo/> <cust_ActionCode>C2</cust_ActionCode> <cust_StatusCode>A</cust_StatusCode> <cust_AltRec>X</cust_AltRec> <cust_ChDate>1/10/2024</cust_ChDate> <cust_ActionReason>01</cust_ActionReason> </cust_ActionHistory> </cust_ActionHistory> <cust_EmpNQPERNR/> </cust_DemgSync>" //"<cust_DemgSync> <cust_ActionHistory/></cust_DemgSync> "// <cust_ActionHistory> <cust_startDate>9/10/2023</cust_startDate> <cust_SeqNo/> <cust_ActionCode>C2</cust_ActionCode> <cust_StatusCode>A</cust_StatusCode> <cust_AltRec>X</cust_AltRec> <cust_ChDate>12/22/2023</cust_ChDate> <cust_ActionReason>01</cust_ActionReason> </cust_ActionHistory> <cust_ActionHistory> <cust_startDate>03/09/2024</cust_startDate> <cust_SeqNo/> <cust_ActionCode>EE</cust_ActionCode> <cust_StatusCode>A</cust_StatusCode> <cust_AltRec/> <cust_ChDate>03/09/2024</cust_ChDate> <cust_ActionReason>13</cust_ActionReason> </cust_ActionHistory> <cust_ActionHistory> <cust_startDate>03/15/2024</cust_startDate> <cust_SeqNo/> <cust_ActionCode>EG</cust_ActionCode> <cust_StatusCode>A</cust_StatusCode> <cust_AltRec>X</cust_AltRec> <cust_ChDate>03/09/2024</cust_ChDate> <cust_ActionReason>26</cust_ActionReason> </cust_ActionHistory> </cust_ActionHistory> <cust_EmpNQPERNR/> </cust_DemgSync>" def ActionType = "ActionType" def ActionReason = "ActionReason" def lastSync = "lastSync" // def lastInterfaceRun=context.getProperty(lastSync) // ActionType = context.getProperty(ActionType) // ActionReason = context.getProperty(ActionReason) def lastInterfaceRun="2024-02-25T00:00:00.000" ActionType = "C2,C3,C5,C6,C7,CD,CG,CH,CI,CJ,CK,CK,EA,EA,EA,ED,ED,EE,EE,EE,EE,EE,EE,EE,EF,EF,EF,EF,EF,EF,EF,EF,EF,EG,EG,EH,EH,EH,EI,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EN,EQ,EQ,ER,ER,EW,EW,FE,FF,FG,FG,FG,FG,FG,FG,FG,FG,FG,FG,FG,FG,FG,FG,FG,FG,FG,FG" ActionReason = "01,01,01,01,01,01,01,01,CI,CJ,G1,G3,01,03,04,10,11,12,13,14,15,16,17,18,13,19,20,21,22,23,24,25,T6,26,27,28,A9,S6,29,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,A6,A7,A8,C1,S0,S1,U0,W1,W2,W3,W4,X7,X8,X9,Y1,02,U9,Z9,Z0,B3,B4,B5,B6,C2,Q3,S5,S7,V0,V1,V5,V6,V7,V8,V9,W0,Y0,Z8" def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS") lastInterfaceRun = LocalDateTime.parse(lastInterfaceRun, formatter) // lastInterfaceRun=lastInterfaceRun.toLocalDate() // lastInterfaceRun=Date.parse("MM-dd-yyyy", lastInterfaceRun.toString()) def actionTypes = ActionType.split(',') def actionReasons = ActionReason.split(',') def result = [] for (int i = 0; i < Math.min(actionTypes.size(), actionReasons.size()); i++) { result.add("${actionTypes[i]}/${actionReasons[i]}") // Combining the actionType and actionReason to match the corresponding eventReason } def EmplStatus2 = "" def EmplStatus3 = "" def actionCode1 = "" def actionCode2 = "" def actionCode3 = "" def actionReason1 = "" def actionReason2 = "" def actionReason3 = "" def targetEffectiveDate1 = "" def targetEffectiveDate2 = "" def targetEffectiveDate3 = "" if (EmpJobHistory.isEmpty()) { if(cust_ActionHistory.toString()!=""){ def cust_DemoSyncParsedXML = new XmlSlurper().parseText(cust_ActionHistory) def actionHistories = cust_DemoSyncParsedXML.'**'.findAll { it.name() == 'cust_ActionHistory' } if(actionHistories){ boolean isEmpty = actionHistories.every { it.children().isEmpty() } if (!isEmpty) { def sortStartDate= cust_DemoSyncParsedXML.cust_ActionHistory.cust_ActionHistory.sort{Date.parse("MM/dd/yyyy", it.cust_startDate.text())}.reverse() //Sorting by startdate in descending if(sortStartDate&&!sortStartDate[0].cust_AltRec.toString().isEmpty() && sortStartDate[0].cust_AltRec=='X'){ // consider the first array which has cust_AltRec=='X' and send the fields related AC, ARC, ED actionCode1=sortStartDate.cust_ActionCode[0] actionReason1=sortStartDate.cust_ActionReason[0] targetEffectiveDate1=sortStartDate.cust_startDate[0] } if(sortStartDate.size()>1){ //Here check if there are more than once array actionCode2=sortStartDate.cust_ActionCode[1] actionReason2=sortStartDate.cust_ActionReason[1] targetEffectiveDate2=sortStartDate.cust_startDate[1] EmplStatus2=sortStartDate.cust_StatusCode[1] } if(sortStartDate.size()>2){ actionCode3=sortStartDate.cust_ActionCode[2] actionReason3=sortStartDate.cust_ActionReason[2] targetEffectiveDate3=sortStartDate.cust_startDate[2] EmplStatus3=sortStartDate.cust_StatusCode[2] } } } } } //if EmpJobHistoryis present, below code else if(!EmpJobHistory.isEmpty()){ def root = new XmlSlurper().parseText(EmpJobHistory) def sortedEnddate=root.EmpJob.sort{ Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSS", it.endDate.text()) }.reverse() //Sorting the empJobHistory in descending order def index=0 //if Emjob is present , check for 1st set of tagetfields ,if present store the index for(int i=0;i<sortedEnddate.size();i++){ if( result.contains(sortedEnddate[i].eventReason)){ actionCode1=sortedEnddate[i].eventReason.toString().substring(0,2) actionReason1=sortedEnddate[i].eventReason.toString().substring(3) targetEffectiveDate1=sortedEnddate[i].startDate index=i break } } if(actionCode1.isEmpty()){ // if the first set of action code is not found in the empJobHistory then check the Cust_ActionHistory if(!cust_ActionHistory.isEmpty()){ def cust_DemoSyncParsedXML = new XmlSlurper().parseText( cust_ActionHistory) def actionHistories = cust_DemoSyncParsedXML.'**'.findAll { it.name() == 'cust_ActionHistory' } if(actionHistories){ boolean isEmpty = actionHistories.every { it.children().isEmpty() } if (!isEmpty) { def root1 = new XmlSlurper().parseText(cust_ActionHistory) if(root1.cust_ActionHistory.toString()!=""){ def sortStartDate= root1.cust_ActionHistory.cust_ActionHistory.sort{Date.parse("MM/dd/yyyy", it.cust_startDate.text())}.reverse() //Sort cust_ActionHistory startDate in descending order if(sortStartDate&&!sortStartDate[0].cust_AltRec.toString().isEmpty() && sortStartDate[0].cust_AltRec=='X'){ // consider the first array which has cust_AltRec=='X' and send the fields related AC, ARC, ED actionCode1=sortStartDate.cust_ActionCode[0] actionReason1=sortStartDate.cust_ActionReason[0] targetEffectiveDate1=sortStartDate.cust_startDate[0] } if(sortStartDate.size()>1){ actionCode2=sortStartDate.cust_ActionCode[1] actionReason2=sortStartDate.cust_ActionReason[1] targetEffectiveDate2=sortStartDate.cust_startDate[1] EmplStatus2=sortStartDate.cust_StatusCode[1] } if(sortStartDate.size()>2){ actionCode3=sortStartDate.cust_ActionCode[2] actionReason3=sortStartDate.cust_ActionReason[2] targetEffectiveDate3=sortStartDate.cust_startDate[2] EmplStatus3=sortStartDate.cust_StatusCode[2] } } }} } }else{ for(int i=index+1;i<sortedEnddate.size();i++){ // if the first set of the actionCode is matched from the EMpJob history then iteration continues from that stored index i.e i = index+1 if(sortedEnddate[i].lastModifiedDateTime.toString()){ def lastModifiedDateTime = LocalDateTime.parse(sortedEnddate[i].lastModifiedDateTime.toString()) //lastModifiedDateTime=lastModifiedDateTime.toLocalDate() // lastModifiedDateTime=Date.parse("MM-dd-yyyy", lastModifiedDateTime.toString()) if((lastModifiedDateTime.isAfter(lastInterfaceRun) || lastModifiedDateTime.equals(lastInterfaceRun))){ // checking the lastmodified date of that records is >= lastSync if( result.contains(sortedEnddate[i].eventReason)){ actionCode2=sortedEnddate[i].eventReason.toString().substring(0,2) actionReason2=sortedEnddate[i].eventReason.toString().substring(3) EmplStatus2=sortedEnddate[i].emplStatusNav.PicklistOption.externalCode.toString() targetEffectiveDate2=sortedEnddate[i].startDate index=i break }}}} } if(actionCode2.isEmpty()){ // if the second set of action code is not found in the empJobHistory then check the Cust_ActionHistory if(!cust_ActionHistory.isEmpty()){ def cust_DemoSyncParsedXML = new XmlSlurper().parseText(cust_ActionHistory) def actionHistories = cust_DemoSyncParsedXML.'**'.findAll { it.name() == 'cust_ActionHistory' } if(actionHistories){ boolean isEmpty = actionHistories.every { it.children().isEmpty() } if (!isEmpty) { def root1 = new XmlSlurper().parseText(cust_ActionHistory) if(root1.cust_ActionHistory.toString()!=""){ def sortStartDate= root1.cust_ActionHistory.cust_ActionHistory.sort{Date.parse("MM/dd/yyyy", it.cust_startDate.text())}.reverse() //Sort cust_ActionHistory startDate in descending order if(!sortStartDate[0].cust_ChDate.toString().isEmpty()){ // def ChangeDate=Date.parse("MM/dd/yyyy", sortStartDate[0].cust_ChDate.toString()) // consider the ChangeDate of the first array in cust_actionHistory def dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy") def ChangeDate = LocalDate.parse(sortStartDate[0].cust_ChDate.toString(), dateFormatter) ChangeDate = LocalDateTime.of(ChangeDate, LocalTime.MIDNIGHT) if((ChangeDate.isAfter(lastInterfaceRun) || ChangeDate.isEqual(lastInterfaceRun))){ //then checkweather the change date is greater than or equal to lastSync if(sortStartDate&&!sortStartDate[0].cust_AltRec.toString().isEmpty() && sortStartDate[0].cust_AltRec=='X'){ // then check if that first array has the cust_AltRec=='X'then pass the fields to the second set actionCode2=sortStartDate.cust_ActionCode[0] actionReason2=sortStartDate.cust_ActionReason[0] targetEffectiveDate2=sortStartDate.cust_startDate[0] EmplStatus2=sortStartDate.cust_StatusCode[0] } if(sortStartDate.size()>1){ actionCode3=sortStartDate.cust_ActionCode[1] actionReason3=sortStartDate.cust_ActionReason[1] targetEffectiveDate3=sortStartDate.cust_startDate[1] EmplStatus3=sortStartDate.cust_StatusCode[1] } } } } }} } }else{ for(int i=index+1;i<sortedEnddate.size();i++){ // then start iterating for third set as its Override the first index we stored, now again we are considering it as i=index+1 if(sortedEnddate[i].lastModifiedDateTime.toString()){def lastModifiedDateTime = LocalDateTime.parse(sortedEnddate[i].lastModifiedDateTime.toString()) // lastModifiedDateTime=lastModifiedDateTime.toLocalDate() // lastModifiedDateTime=Date.parse("MM-dd-yyyy", lastModifiedDateTime.toString()) if((lastModifiedDateTime.isAfter(lastInterfaceRun) || lastModifiedDateTime.equals(lastInterfaceRun))){//check the lastmodified date of that record is >= lastSync the send it to 3rd set if( result.contains(sortedEnddate[i].eventReason)){ actionCode3=sortedEnddate[i].eventReason.toString().substring(0,2) actionReason3=sortedEnddate[i].eventReason.toString().substring(3) targetEffectiveDate3=sortedEnddate[i].startDate EmplStatus3=sortedEnddate[i].emplStatusNav.PicklistOption.externalCode.toString() index=i break }}}} } if(actionCode3.isEmpty()){ // if the third set of action code is not found in the empJobHistory then check the Cust_ActionHistory if(!cust_ActionHistory.isEmpty()){ def cust_DemoSyncParsedXML = new XmlSlurper().parseText(cust_ActionHistory) def actionHistories = cust_DemoSyncParsedXML.'**'.findAll { it.name() == 'cust_ActionHistory' } if(actionHistories){ boolean isEmpty = actionHistories.every { it.children().isEmpty() } if (!isEmpty) { def root2 = new XmlSlurper().parseText(cust_ActionHistory) if(root2.cust_ActionHistory.toString()!=""){ def sortStartDate= root2.cust_ActionHistory.cust_ActionHistory.sort{Date.parse("MM/dd/yyyy", it.cust_startDate.text())}.reverse() //Sort cust_ActionHistory startDate in descending order if(!sortStartDate[0].cust_ChDate.toString().isEmpty()){ // def ChangeDate=Date.parse("MM/dd/yyyy", sortStartDate[0].cust_ChDate.toString()) // consider the ChangeDate of the first array in cust_actionHistory def dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy") def ChangeDate = LocalDate.parse(sortStartDate[0].cust_ChDate.toString(), dateFormatter) ChangeDate = LocalDateTime.of(ChangeDate, LocalTime.MIDNIGHT) if((ChangeDate.isAfter(lastInterfaceRun) || ChangeDate.isEqual(lastInterfaceRun))){ //then checkweather the change date is greater than or equal to lastSync if(sortStartDate&&!sortStartDate[0].cust_AltRec.toString().isEmpty() && sortStartDate[0].cust_AltRec=='X'){ // then check if that first array has the cust_AltRec=='X'then pass the fields to the second set actionCode3=sortStartDate.cust_ActionCode[0] actionReason3=sortStartDate.cust_ActionReason[0] targetEffectiveDate3=sortStartDate.cust_startDate[0] EmplStatus3=sortStartDate.cust_StatusCode[0] } } } } } } } } } //} println "AC1: "+actionCode1 + "; ACR1:" +actionReason1 + " ;targetEffectiveDate1: " + targetEffectiveDate1 println "AC2: " +actionCode2 +"; ACR2: "+actionReason2 + "; EmplStatus2: " +EmplStatus2+ "; targetEffectiveDate2: " +targetEffectiveDate2 println "AC3: " +actionCode3 +"; ACR3: "+actionReason3+"; EmplStatus3: "+EmplStatus3+"; targetEffectiveDate3: "+targetEffectiveDate3
Write, Run & Share Groovy code online using OneCompiler's Groovy online compiler for free. It's one of the robust, feature-rich online compilers for Groovy language, running the latest Groovy version 2.6. Getting started with the OneCompiler's Groovy editor is easy and fast. The editor shows sample boilerplate code when you choose language as Groovy and start coding.
OneCompiler's Groovy online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Groovy program which takes name as input and prints hello message with your name.
def name = System.in.newReader().readLine()
println "Hello " + name
Groovy is an object-oriented programming language based on java. Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc.
Data type | Description | Range |
---|---|---|
String | To represent text literals | NA |
char | To represent single character literal | NA |
int | To represent whole numbers | -2,147,483,648 to 2,147,483,647 |
short | To represent short numbers | -32,768 to 32,767 |
long | To represent long numbers | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
double | To represent 64 bit floating point numbers | 4.94065645841246544e-324d to 1.79769313486231570e+308d |
float | To represent 32 bit floating point numbers | 1.40129846432481707e-45 to 3.40282346638528860e+38 |
byte | To represent byte value | -128 to 127 |
boolean | To represent boolean values either true or false | True or False |
You can define variables in two ways
data-type variable-name;
[or]
def variable-name;
0.upto(n) {println "$it"}
or
n.times{println "$it"}
where n is the number of loops and 0 specifies the starting index
When ever you want to perform a set of operations based on a condition or set of conditions, then If / Nested-If / If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
List allows you to store ordered collection of data values.
def mylist = [1,2,3,4,5];
List Methods | Description |
---|---|
size() | To find size of elements |
sort() | To sort the elements |
add() | To append new value at the end |
contains() | Returns true if this List contains requested value. |
get() | Returns the element of the list at the definite position |
pop() | To remove the last item from the List |
isEmpty() | Returns true if List contains no elements |
minus() | This allows you to exclude few specified elements from the elements of the original |
plus() | This allows you to add few specified elements to the elements of the original |
remove() | To remove the element present at the specific position |
reverse() | To reverse the elements of the original List and creates new list |