Step 1: - Open Eclipse and create a new project.
Step 2: - Select Java Project on the window and click on Next.
Step 3: - Give project name and select correct Java version.
Step 4: - Click on Finish and you will be able to see your project on left hand side of Eclipse.
Step 5: - Create a new package under your Project. If you expand your project, you will be able to see something called as ‘src’. You need to right click on that folder and select New Package.
Step 6: - Create a new Class under the new Package create in previous step. You need to right click on the newly created package and select new class.
Step 8: - Import this Jar file in your CPI IFlow. Go to Resources -> Add -> Archive
<root>
<row>
<FirstName>Jon</FirstName>
<MiddleName>Vander</MiddleName>
<LastName>Williamson</LastName>
<AnnualSalary>12000</AnnualSalary>
</row>
</root>
We will be using mapping step to format the xml.
import com.sap.it.api.mapping.*;
import test.*;
def String FullName(String FirstName, String MiddleName, String LastName){
CpiTest c1 = new CpiTest();
String fullname = c1.getFullName(FirstName,MiddleName,LastName);
return fullname;
}
def String MonthlySalary(int AnnualSalary){
CpiTest c1 = new CpiTest();
def SalaryperMonth = c1.getSalaryperMonth(AnnualSalary);
return SalaryperMonth;
}
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import test.*;
def Message processData(Message message) {
def body = message.getBody(java.lang.String);
def xmlBody = new XmlSlurper().parseText(body);
String FinalBody = "FullName,Salary\n";
xmlBody.row.each { it ->
String FN = it.FirstName.text();
String MN = it.MiddleName.text();
String LN = it.LastName.text();
String Sal = it.AnnualSalary.text();
int Salary = Sal.toInteger();
CpiTest c1 = new CpiTest();
def fullname = c1.getFullName(FN,MN,LN);
def SalaryperMonth = c1.getSalaryperMonth(Salary);
FinalBody = FinalBody + fullname + "," +SalaryperMonth + "\n";
}
message.setBody(FinalBody);
return message;
}
By using this Script, we get a CSV file with Headers as ‘FullName’ and ‘Salary’. We also convert the incoming XML data by using Jar File functions to populate the respective headers. Please refer to the below screenshot which shows the final CSV file data.
If you have a requirement where you need to process the data as an Array, then you can add a function in your Jar file which will accept input data as an Array. I have added a function which will accept AnnualSalary field data of all Employees as an Array and will add then all and send the total amount as an output. Please refer to the below screenshot.
def void TotalSalary(int[] Salary, Output output){
CpiTest c1 = new CpiTest();
def Ans = c1.CalTotalSalary(Arrays.asList(Salary));
output.addValue(Ans);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Subject | Kudos |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
User | Count |
---|---|
8 | |
5 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |