Migrate Java Mapping from SAP PO to SAP CPI/Integration Suite

Description:

As we are migrating the interfaces from SAP PI/PO to SAP Integration Suite the challenge we are facing is to migrate the java mappings from SAP PO.

There are many customers who have written complex java codes in SAP PI/PO for which they do not have any documentation neither knowledge to re-design them.

Best way to migrate the java mappings is to re-design them in SAP Integration Suite with best practices.

Sometimes there is a scenario where java logic is very complex, and we do not have the clarity of logic. In this case you can migrate the java mapping as is in groovy with less coding efforts then this blog will help you.

I have not included the steps in this code to migrate the java mapping which are calling XSLT mapping, webservice lookup, dynamic configuration, value mapping lookup or any attachment inside the java code as it needs many manual adjustments which needs advanced java knowledge. You can follow the steps in this blog to migrate the java mapping and then modify the logic relevant for the xslt, webservice lookup, value mapping or attachment logics.

Below are the high-level steps we will follow for this migration:

We will download the java mapping .java file with linked java-class and jar files if any from SAP PO operation mapping.Then will rename these .java files as ‘.groovy’ and upload in SAP Integration Suite integration flow.Further we will do some small code adjustments in the above .java files.Further we will create the new groovy mapping wrapper in the SAP Integration Suite which will call the main java mapping logic of SAP PO which we have migrated as ‘.groovy’ in SAP Integration Suite flow.

 

Below are the steps you need to follow to migrate:

Download all the java files relevant to your java mapping from SAP PI/PO. There can be one or more files linked to each other. Also download the linked jar files if any.Check the class name in all the files and name them accordingly. For example if you see the class name as “public class Utility………..” then name the file as “Utility.groovy”. Please ensure the filename and class name are same.Now open all the files one by one and add the first line as “package src.main.resources.script;” and remove if there is any code line starts with “package” in the .java file. ‘Package’ will be the first line of on code on the file. Find the main java mapping class which have the method name as “public void transform”. This method is the starting point for the java mapping in SAP PO.

          Perform below updates in this file:

           a) Remove the code “throws StreamTransformationException” from ‘transform’ method declaration line.

           b) Remove the annotation “@Override” which will be right above the method ‘transform’.

           c) Remove the inheritance code ‘extends AbstractTransformation’.

                Save this filename/class-name as we will use this in our real groovy code to call the migrated java                    mapping from new groovy mapping wrapper we create in further steps.

            d) Remove the main method if exist in the code. For example public static void main(String[] args)                         {……………….}. Please remove the method and all the code inside it.

                 Let’s assume the filename is “MainClass.groovy” where you found this method.

                 Please do not use this name, I am just setting this example to explain the further steps clearly.

    5. If you have multiple linked java files, then follow this step else you can escape this step.

        We need to now link all the java files with each other by using the import statement. I will explain this              step by an example. If you have 3 groovy files lets name them as “mainclass.groovy”, “Utility.groovy”                and “Common.groovy”.

        In the “mainclass.groovy” add the imports as

            import src.main.resources.script.Utility; and

            import src.main.resources.script.Common;

            In the “Utility.groovy” add the imports as

            import src.main.resources.script.MainClass; and

            import src.main.resources.script.Common;

            In the “Common.groovy” add the imports as

            import src.main.resources.script.MainClass; and

            import src.main.resources.script.Utility;

            In this way you can establish the linkage in all the groovy files. This can vary according to the                            number of linked groovy files you have.

     6. In this step I have identified some of the very basic changes we have to do to convert java code to                   groovy code. Please follow these corrections:

              a) Remove the SAP PO trace logs generation code like AbstractTrace trace = null, trace =                                       this.getTrace(), trace.addInfo, trace.addWarning, throw ex, ex.printStackTrace().

                   You can use the below code to through the error in SAP Integration Suite

                    throw new RuntimeException(“This is error message”);

                    You can use below code to create custom headers for monitoring logs

                    def messageLog = messageLogFactory.getMessageLog(message);

                    messageLog.addCustomHeaderProperty(“<Log Name>”,”<Log details>”);

               b) Change the array declaration in java as “String array[] = …” to groovy as “String[] array = …..”

               c) Change all the double quotes to single quotes for static string assigned to variables.

                      For ex: String str = “${date(yyyyMMdd)}” to String str = ‘${date(yyyyMMdd)}’

               d) Use timezone as ‘UTC’ when passing any date from mapping to java code as SAP Integration                            Suite runs on UTC time but SAP PO runs on local timezone. For ex: ‘Europe/Berlin’.

                       Note: You can skip this step and you will be able to get the error during iFlow run and correct                                     one by one all the errors if such type of conversion error happens.

         7. Now our groovy files are ready to be uploaded to SAP Integration Suite integration flow. You can                        upload all the groovy files and jar files using the archive option.

          8. Now we need to create the main groovy file to be used in integration process to call the groovy files                   we created. Below is the code you need to write in the groovy file. All the data inside <> brackets                     is variable and need to be entered according to your scenario and please read comments                                 properly to add the code relevant to your scenario.     

 

 

//Groovy code
//import your main groovy class here change the “<mainclass>” with your class name.
Import src.main.resources.script.<MainClass>;
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
def Message processData(Message message) {
// Initialize input and output streams
InputStream ins = null;
OutputStream outs = new ByteArrayOutputStream();
//get the message body
def body = message.getBody(String);
ins = new ByteArrayInputStream(body.getBytes());
//Create the mainclass object here and call the transform method
<MainClass> mc = new <MainClass>();
mc.transform(ins,outs);
//get the output body
String body_out = new String(outs.toByteArray());
//set the output as groovy output;
message.setBody(body_out);
return message;
}

 

 

Note: If you have any webservice lookup or attachment used in your java mapping then you can pass it as the extra parameter in the ‘transform’ method and modify the ‘MainClass.groovy’ a bit to accept the data and merge it in logic to migrate. This will need some intermediate level of groovy/java knowledge.

 

​ Description:As we are migrating the interfaces from SAP PI/PO to SAP Integration Suite the challenge we are facing is to migrate the java mappings from SAP PO.There are many customers who have written complex java codes in SAP PI/PO for which they do not have any documentation neither knowledge to re-design them.Best way to migrate the java mappings is to re-design them in SAP Integration Suite with best practices.Sometimes there is a scenario where java logic is very complex, and we do not have the clarity of logic. In this case you can migrate the java mapping as is in groovy with less coding efforts then this blog will help you.I have not included the steps in this code to migrate the java mapping which are calling XSLT mapping, webservice lookup, dynamic configuration, value mapping lookup or any attachment inside the java code as it needs many manual adjustments which needs advanced java knowledge. You can follow the steps in this blog to migrate the java mapping and then modify the logic relevant for the xslt, webservice lookup, value mapping or attachment logics.Below are the high-level steps we will follow for this migration:We will download the java mapping .java file with linked java-class and jar files if any from SAP PO operation mapping.Then will rename these .java files as ‘.groovy’ and upload in SAP Integration Suite integration flow.Further we will do some small code adjustments in the above .java files.Further we will create the new groovy mapping wrapper in the SAP Integration Suite which will call the main java mapping logic of SAP PO which we have migrated as ‘.groovy’ in SAP Integration Suite flow. Below are the steps you need to follow to migrate:Download all the java files relevant to your java mapping from SAP PI/PO. There can be one or more files linked to each other. Also download the linked jar files if any.Check the class name in all the files and name them accordingly. For example if you see the class name as “public class Utility………..” then name the file as “Utility.groovy”. Please ensure the filename and class name are same.Now open all the files one by one and add the first line as “package src.main.resources.script;” and remove if there is any code line starts with “package” in the .java file. ‘Package’ will be the first line of on code on the file. Find the main java mapping class which have the method name as “public void transform”. This method is the starting point for the java mapping in SAP PO.          Perform below updates in this file:           a) Remove the code “throws StreamTransformationException” from ‘transform’ method declaration line.           b) Remove the annotation “@Override” which will be right above the method ‘transform’.           c) Remove the inheritance code ‘extends AbstractTransformation’.                Save this filename/class-name as we will use this in our real groovy code to call the migrated java                    mapping from new groovy mapping wrapper we create in further steps.            d) Remove the main method if exist in the code. For example public static void main(String[] args)                         {……………….}. Please remove the method and all the code inside it.                 Let’s assume the filename is “MainClass.groovy” where you found this method.                 Please do not use this name, I am just setting this example to explain the further steps clearly.    5. If you have multiple linked java files, then follow this step else you can escape this step.        We need to now link all the java files with each other by using the import statement. I will explain this              step by an example. If you have 3 groovy files lets name them as “mainclass.groovy”, “Utility.groovy”                and “Common.groovy”.        In the “mainclass.groovy” add the imports as            import src.main.resources.script.Utility; and            import src.main.resources.script.Common;            In the “Utility.groovy” add the imports as            import src.main.resources.script.MainClass; and            import src.main.resources.script.Common;            In the “Common.groovy” add the imports as            import src.main.resources.script.MainClass; and            import src.main.resources.script.Utility;            In this way you can establish the linkage in all the groovy files. This can vary according to the                            number of linked groovy files you have.     6. In this step I have identified some of the very basic changes we have to do to convert java code to                   groovy code. Please follow these corrections:              a) Remove the SAP PO trace logs generation code like AbstractTrace trace = null, trace =                                       this.getTrace(), trace.addInfo, trace.addWarning, throw ex, ex.printStackTrace().                   You can use the below code to through the error in SAP Integration Suite                    throw new RuntimeException(“This is error message”);                    You can use below code to create custom headers for monitoring logs                    def messageLog = messageLogFactory.getMessageLog(message);                    messageLog.addCustomHeaderProperty(“<Log Name>”,”<Log details>”);               b) Change the array declaration in java as “String array[] = …” to groovy as “String[] array = …..”               c) Change all the double quotes to single quotes for static string assigned to variables.                      For ex: String str = “${date(yyyyMMdd)}” to String str = ‘${date(yyyyMMdd)}’               d) Use timezone as ‘UTC’ when passing any date from mapping to java code as SAP Integration                            Suite runs on UTC time but SAP PO runs on local timezone. For ex: ‘Europe/Berlin’.                       Note: You can skip this step and you will be able to get the error during iFlow run and correct                                     one by one all the errors if such type of conversion error happens.         7. Now our groovy files are ready to be uploaded to SAP Integration Suite integration flow. You can                        upload all the groovy files and jar files using the archive option.          8. Now we need to create the main groovy file to be used in integration process to call the groovy files                   we created. Below is the code you need to write in the groovy file. All the data inside <> brackets                     is variable and need to be entered according to your scenario and please read comments                                 properly to add the code relevant to your scenario.       //Groovy code
//import your main groovy class here change the “<mainclass>” with your class name.
Import src.main.resources.script.<MainClass>;
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
def Message processData(Message message) {
// Initialize input and output streams
InputStream ins = null;
OutputStream outs = new ByteArrayOutputStream();
//get the message body
def body = message.getBody(String);
ins = new ByteArrayInputStream(body.getBytes());
//Create the mainclass object here and call the transform method
<MainClass> mc = new <MainClass>();
mc.transform(ins,outs);
//get the output body
String body_out = new String(outs.toByteArray());
//set the output as groovy output;
message.setBody(body_out);
return message;
}  Note: If you have any webservice lookup or attachment used in your java mapping then you can pass it as the extra parameter in the ‘transform’ method and modify the ‘MainClass.groovy’ a bit to accept the data and merge it in logic to migrate. This will need some intermediate level of groovy/java knowledge.   Read More Technology Blogs by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author