Skip to Content

USEFUL UDFs

Dear SCN Users,

Collected some UDFs which will be used in most of the scenarios.

1.UDF to get the input filename and write the output file with input filename

Capture.JPG

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);


DynamicConfigurationKey key = DynamicConfigurationKey.create(“http://sap.com/xi/XI/System/File“, “FileName”);

DynamicConfigurationKey key1 = DynamicConfigurationKey.create( “http://sap.com/xi/XI/System/File”,”FileName“);

String filename=”output_”+conf.get(key);

conf.put(key1,filename);

return filename;

2.UDF to add two timestamps

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

int date1 = var1;// first argumnet in UDF

int date2 = var2;//Second Argumnet in UDF

Date date = null;

String date1String = “” + date1;

DateFormat df = new SimpleDateFormat(“yyyyMMdd”);

try {

date = df.parse(date1String);

} catch (ParseException pe) {

pe.printStackTrace();

}

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(date);

cal.add(Calendar.DAY_OF_YEAR, date2);

Date Cal = cal.getTime();

String newstring = new SimpleDateFormat(“MM/dd/yyyy”).format(Cal);

 

return newstring;

3.Getting the last 3 digits(If there are only 2 then only last 2 and so on).

if(EmpId.length()>3)

{

EmpId=EmpId.substring(EmpId.length()-3,EmpId.length());

return EmpId;

}

else

return EmpId;

Capture.JPG

4.Making the String to particular number of digits(In this case I am making it as 10 digits adding ‘X’ to fill the spaces)

if(empName.length()<10)

{

for(int i=empName.length();i<10;i++)

{

empName=empName+”x”;

}

return empName;

}

else

return empName;

Capture.JPG

5.UDF to get the last value in the queue\Context

int i=EmpTime.length;

result.addValue(EmpTime[i-1]);

Capture.JPG

6.UDF to check the duplicate Entries in the queue(In this case if the entry is first then’ FirstEntry’ else ‘ Duplicate is populated)

if(EmpDetails.length==2)

{

if(EmpName[0]==EmpName[1])

{

result.addValue(“True”);

}

else

result.addValue(“False”);

}

Capture.JPG

7. UDF to add context change(In this case after every 2 values)

int count = 0;               

           int j =0;

               for(int i=0;i<EmpGift.length;i++)

               {
                 j++;

               
                  if(j == 2&& i <EmpGift.length-1)

                       {

                               result.addValue(EmpGift[i]);

                               result.addContextChange();                               

                               count++;

                               j =0;

                       }

               
                       else

                       {

                               result.addValue(EmpGift[i]);

                       }

                       
               }

Capture.JPG

8.UDF to Sort Elements in the queue

int var2=0;

Arrays.sort(var1);

for(int i=0;i<var1.length;i++)

{

var2 =var1[i];

result.addValue(var2);

}

Capture.JPG

9. Remove Decimal(This UDF can be used to remove special characters as well)

String Out= EmpAllowances.replace(“.”,””);

return Out;

Capture.JPG

10. Rounding off to 3 Decimal digits

java.math.BigDecimal bd  = new java.math.BigDecimal(var1).setScale(2,java.math.BigDecimal.ROUND_HALF_UP);   

String s=bd.toString();

return s;

Capture.JPG

21 Comments
You must be Logged on to comment or reply to a post.