Friday, 12 March 2021

Random UUID generator in SAP CPI

 Groovy script:


import com.sap.it.api.mapping.*;

def String customFunc(String arg1){

def randomID = UUID.randomUUID().toString().replace("-", "").toUpperCase();

return randomID

}


Monday, 8 March 2021

Basics: SOAP vs HTTP vs REST

SOAP :Simple Object Access Protocol vs REST:


SOAP - XML-based messaging protocol for exchanging information among computers.

SOAP needs more bandwidth for its usage whereas REST doesn't need much bandwidth

SOAP only works with XML formats whereas REST work with plain text, XML, HTML and JSON.

SOAP is a protocol whereas REST is an architectural pattern.

SOAP cannot make use of REST whereas REST can make use of SOAP.

Ease of coding – Coding REST Services and subsequent implementation is far easier than SOAP.

Protocol means - set of rules that determine how data is transmitted between different devices.


SOAP vs HTTP:
S.No.    SOAP    HTTP
1.SOAP stands for Simple Object Access Protocol.HTTP stands for Hypertext Transfer Protocol.
2.It is XML based used for sending and receiving messages.It is used to transfer information over the internet.
3.It supports web socket or WS-Addressing, WS-Security, SwA.It do not supports web socket or WS-Addressing, WS-Security, SwA.
4.HTTP is over TCP and IP.SOAP is over HTTP.
5.It support runtime checking against WSDL.It do not support runtime checking against WSDL.
6.It support Automatic processing of Message Transmission Optimization Mechanism (MTOM).It also support MTOM, but it must use the MIME message domain and design flow should be done to handle the attachments explicitly.
7.This protocol’s design is Data centric.This protocol’s design is Document centric.
8.It is a light weight data interchange protocol.It is not light weight data transfer protocol as SOAP.





Sunday, 7 March 2021

CPI search using ID's

 

We would need to implement this feature in CPI to search on any specific Key ID’s like CustomerID, TicketID. So we will utilize this below.

 

Challenges:

As of now finding the specific data in CPI using any ID search is not implemented, as we search using timing and it is very difficult for us to find it if there are several message on every secs.

 

Solution:

 

Interface implementation:

 





Friday, 5 February 2021

Header , Item, Trailer flatfile/fixed length file conversion in groovy script in SAP CPI

 There is no option in the CVS to XML convertor in CPI for converting the flat file. We can customize using the groovy script to achieve this.

Input:

NHEDSAPPS220C
16 20190523045
16 20190523046
16 20190523047
16 20190523048
TRAR000712

Groovy Script:


import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {

//Body

def body = message.getBody(java.lang.String) as String;

def lines = body.split("\n");


// variable initialization

def lineArr;

def itemtypPre;

def irlflag="E";

def irflag="E";

def hflag="E";

def Fieldentry = "";

def itemtyp;

def i=0;


//XML and root element formation

Fieldentry += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

Fieldentry += "<Root>";

for(i=0;i<lines.length;i++)

{

if(i>0)

{

//Item Type holder split from File i.e NH, 16, TR

itemtypPre = lines[i-1].substring(0,1);

}

//Item Type holder split from File i.e NH, 16, TR

itemtyp= lines[i].substring(0,2);


if(itemtyp.equals("NH"))

{

    Fieldentry += "<Header>";
    Fieldentry += "<ItemType>"+lines[i].substring(0,3)+"</ItemType>";
    Fieldentry += "<ISPID>"+lines[i].substring(4,5)+"</ISPID>";
    Fieldentry += "<Rate>"+lines[i].substring(6,7)+"</Rate>"
    Fieldentry += "</Header>";
}


if(itemtyp.equals("16")){


        if(irlflag.equals("O")) 

      {

        Fieldentry += "</Items>";

        irlflag="E";

      }
    Fieldentry += "<Items>";

    irlflag="O";

    // Fieldentry += "<ItemType>";
    Fieldentry += "<Type>"+lines[i].substring(0,2)+"</Type>"
    Fieldentry += "<FullClaimNumber>"+lines[i].substring(3,5)+"</FullClaimNumber>"
    Fieldentry += "<ClaimNumber>"+lines[i].substring(4,12)+"</ClaimNumber>"
    Fieldentry += "<AuthorizationCode>"+lines[i].substring(13,15)+"</AuthorizationCode>"
    // Fieldentry += "</ItemType>";
}


if(itemtyp.equals("TR")){

Fieldentry += "</Items><Trailer>";
Fieldentry += "<Type>"+lines[i].substring(0,4)+"</Type>"
Fieldentry += "<NumExtendedWarranty>"+lines[i].substring(5,8)+"</NumExtendedWarranty>"
Fieldentry += "<ElectroluxCustomerId>"+lines[i].substring(9,10)+"</ElectroluxCustomerId>"
Fieldentry += "</Trailer>";

} }

Fieldentry += "</Root>";

message.setBody(Fieldentry);

return message;

}


Output:

<?xml version="1.0" encoding="UTF-8"?>

<Root>

    <Header>

        <ItemType>NHE</ItemType>

        <ISPID>S</ISPID>

        <Rate>P</Rate>

    </Header>

    <Items>

        <Type>16</Type>

        <FullClaimNumber> 2</FullClaimNumber>

        <ClaimNumber>20190523</ClaimNumber>

        <AuthorizationCode>45</AuthorizationCode>

    </Items>

    <Items>

        <Type>16</Type>

        <FullClaimNumber> 2</FullClaimNumber>

        <ClaimNumber>20190523</ClaimNumber>

        <AuthorizationCode>46</AuthorizationCode>

    </Items>

    <Items>

        <Type>16</Type>

        <FullClaimNumber> 2</FullClaimNumber>

        <ClaimNumber>20190523</ClaimNumber>

        <AuthorizationCode>47</AuthorizationCode>

    </Items>

    <Items>

        <Type>16</Type>

        <FullClaimNumber> 2</FullClaimNumber>

        <ClaimNumber>20190523</ClaimNumber>

        <AuthorizationCode>48</AuthorizationCode>

    </Items>

    <Trailer>

        <Type>TRAR</Type>

        <NumExtendedWarranty>007</NumExtendedWarranty>

        <ElectroluxCustomerId>2</ElectroluxCustomerId>

    </Trailer>

</Root>





Monday, 25 January 2021

Split Hour, Month separately from the date

Split Hour, Month separately from the date 

Groovy Script:

Date date = new Date();
println(date)// given date
Calendar calendar = Calendar.getInstance(); // creates a new calendar instance
def date1=calendar.setTime(date); // assigns calendar to given date
def time=calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
println("time="+time)
def Time=calendar.get(Calendar.HOUR); // gets hour in 12h format
println("Time="+Time)
def month=calendar.get(Calendar.MONTH);
println("month="+month)
String a=time;
println("a="+a)
if(a=="6")
{
println("yes")
}
else
{
println("No")
}

Output:

12 ms

Mon Jan 25 15:43:58 UTC 2021

time=15

Time=3

month=0

a=15

No


Hours splitting from the current date

Groovy script to pick only the Hour from the current date.


Groovy script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.sap.it.api.mapping.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

def Message processData(Message message) {

def header = message.getHeaders() as String;
def body = message.getBody(java.lang.String) as String;

//String timeStamp = new SimpleDateFormat("HH:mm:ss.SSS").format(new Date());
Calendar cal=Calendar.getInstance();//it return same time as new Date()
def hour = cal.get(Calendar.HOUR_OF_DAY)   

message.setProperty("Hour",hour);

return message;
}

------------------------------------------------------------------

Note:

def hour = cal.get(Calendar.HOUR_OF_DAY)   #24 hour format

def hour = cal.get(Calendar.HOUR) # 12 hours format  



Wednesday, 6 January 2021

Split the value at @ and lowercase in groovyscript in sap cpi

 

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*;
import groovy.util.logging.*;
import org.codehaus.*;
import groovy.xml.*;

def Message processData(Message message) {
    
def body = message.getBody(java.lang.String) as String;

map = message.getProperties();
def email = "blake1566@BLAKEGUER.COM"
def (value1, value2) = email.tokenize( '@' )
def chemail =value2.toLowerCase();
def fullemail = value1 + "@" + chemail;
message.setProperty("Header",fullemail);
return message;
}