Node Functions as UDF in SAP PI
In this post, I have tried to write the node functions which will help in understanding the node functions, as well as how User Defined Function works. I have written the standard node functions code as UDF .
USE ONE AS MANY:
A field that only occurs once is replicated as often as another field in second queue occurs and the context is adjusted as it is in the third queue in result queue.
Here C1 is repeated twice and C2 also twice and context changes adjusted as it is in third queue.
The code is given below:
int countarr1=0;int countarr2=0;int countarr3 = 0;
int i,j,k,start;
i=j=k=0;
for(i = 0;i
{
if( arr1[i].equals(ResultList.CC))
countarr1++;
}// countarr1 counts value in first queue
for(j = 0;j
{
if( arr2[j].equals(ResultList.CC))
countarr2++;
}}// countarr2 counts value in second queue
for(k = 0;k
{
if( arr3[k].equals(ResultList.CC))
countarr3++;
}// countarr3 counts value in second queue
start = 0;
ArrayList
/*checking number of context in first and second queue are same or not
And wheteher the number of values in second and third queue are same or not*/
if(countarr1==countarr2 && (arr2.length-countarr2)==(arr3.length-countarr3) )
{
for(i = 0;i
{
// taking the value from first queue,
String temp = arr1[i];
for(j=start;j
{
//repeating the value based on second queue
if(arr2[j].equals(ResultList.CC))
break;
else
arrresult.add(temp);
}
start = j+1;
}
j = 0;
// here we are adjusting the result queue based on third queue,
for(k = 0;k
{
if(arr3[k].equals(ResultList.CC))
result2.addValue(ResultList.CC);
else
{
result2.addValue(arrresult.get(j));
j++;
}
}
}
REMOVE CONTEXT:
We use removeContext() to remove all the context for an element. This deletes all top hierarchy levels, so that all elements of the target queue are assigned to a root element of the source queue.
The code is given below:
int i=0;
for(i = 0;i
{
if( !arr1[i].equals(ResultList.CC))// checking if context change is there or not
result2.addValue(arr1[i]);
}
SPLIT BY EACH VALUE:
SplitByValue() inserts a context change in the source value queue. You can insert a context change in the queue after each value, after each value change, or after each tag without a value.
The code is given below:
int i=0;
for(i = 0;i
{
if(arr1[i].equals(ResultList.CC))
break;
result2.addValue(arr1[i]);
result2.addValue(ResultList.CC);
}
FORMAT BY EXAMPLE:
We use this function if you need to adjust two queues of equal length with context change from 1st queue. The mapping runtime takes the values from the first queue, and the context change from the second queue. If the two inbound queues do not have the same number of values, the mapping runtime will generate an exception. Here the first and second queue has same number of values.
The code is given below:
int countarr1=0;int countarr2=0;
int i,j,k,start;
i=j=k=0;
for(i = 0;i
{
if( arr1[i].equals(ResultList.CC))
countarr1++; // countarr1 counts value in first queue
}
for(j = 0;j
{
if( arr2[j].equals(ResultList.CC))
countarr2++; // countarr2 counts value in second queue
}
}
j=0;
if((arr1.length-countarr1)==(arr2.length-countarr2) ) //Checking whether number of values are same or not
{
for(i = 0;i
{
if(!arr1[i].equals(ResultList.CC))
{
while(arr2[j].equals(ResultList.CC))
j++;
result2.addValue(arr2[j]);
j++;
}
else
result2.addValue(ResultList.CC);
}
}
COLLAPSE CONTEXT:
collapseContexts()copies the first value from all context to one context.
The code is given below:
for(int i = 0;i
{
if(i==0)
{
result2.addValue(arr1[0]);
result2.addValue(ResultList.CC);
}
if(arr1[i].equals(ResultList.CC))
{
result2.addValue(arr1[++i]);
result2.addValue(ResultList.CC);
}
}
SORT:
It sorts all value in a queue
The code is given below:
ArrayList a1 = new ArrayList(Arrays.asList(var1));
- Collections.sort(a1);
for(int i=0;i
- result.addValue(a1.get(i));
SORTBYKEY:
Sorts value in second queue based on first queue
Here Key is Float
Here Key is string
Here Key is Integer
The code is given below,
boolean flagint,flagstring,flagfloat;
flagint=flagfloat=flagstring= true;
String temp = key[0];
try{
// checking it is a character or not
if(temp.matches(“^.*[a-zA-Z].*$”))
{
flagint = false;
flagfloat = false;
}
else{
throw new Exception() ;
}
}catch(Exception e){
flagstring = false;
}
if(flagstring==false){
try{
Integer.parseInt(temp);
}catch(NumberFormatException e) {
flagint = false;
}
try{
if(!temp.matches(“^([+-]?(\d+\.)?\d+)$”))// checking whether it is a floating point number or not
throw new NumberFormatException();
}catch(NumberFormatException e)
{
flagfloat = false;
}
}
if(flagint==true){
HashMap
for(int i=0;i
{
int t = Integer.parseInt(key[i]);
hash.put(t,value[i]);
}
Set
ArrayList
- list.addAll(set);
- Collections.sort(list);
for (Integer key1 : list)
- result.addValue(hash.get(key1));
}
else
if(flagfloat==true){
HashMap
for(int j=0;j
{
float t = Float.parseFloat(key[j]);
hash1.put(t,value[j]);
}
Set
ArrayList
- list.addAll(set);
- Collections.sort(list);
for (Float key1 : list)
- result.addValue(hash1.get(key1));
}
else
if(flagstring==true){
HashMap
for(int k=0;k
{
hash2.put(key[k],value[k]);
}
Set
ArrayList
- list.addAll(set);
- Collections.sort(list);// sorting the value based on key
for (String key1 : list)
- result.addValue(hash2.get(key1));// adding the sorted value based on key in result
}
;
New NetWeaver Information at SAP.com
Very Helpfull