cancel
Showing results for 
Search instead for 
Did you mean: 

UDF Help

former_member188791
Participant
0 Kudos

Hi Group,

I am using the below UDF for returning multiple values by passing two string inputs,its working fine in eclips ,but in function library

to return multiple values we need to have result list for this I have to take inputs as queue instead of single value,when I am taking

inputs as queue parse method is not taking,please suggest.

package Sap_MyFunctions;

import java.util.*;
import java.io.*;
import java.text.*;
import com.sap.aii.mappingtool.tf3.rt.Container;
import com.sap.aii.mappingtool.tf3.rt.ResultList;
import com.sap.aii.mappingtool.tf3.rt.ResultListImpl;
public class Getdates {
	public static void indate(String a,String b,ResultList result,Container container)throws Exception{
	List <Date> dates = new ArrayList<Date>();
	SimpleDateFormat formatter;
	formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String str_date = a;
	String end_date = b;				
	Date startDate = formatter.parse(str_date);
	Date endDate   = formatter.parse(end_date);
	long interval = 24*1000*60*60;
	long endTime = endDate.getTime();
	long curTime = startDate.getTime();
	while (curTime <= endTime){
	dates.add(new Date(curTime));
	curTime+=interval;
		}
		
		for(int i=0;i<dates.size();i++){
			
			Date iDate = (Date)dates.get(i);
			String ds = formatter.format(iDate);
			String ds1 = ds.substring(0, 10);	
			result.addValue(ds1);
			//result.addContextChange();
			//System.out.println("Dates are...." + ds1);	
			
		}
	
	}	
	
	public static void main(String[] arg0)throws Exception{
		String str_date1 = "2010-08-29 00:00:00";
		String end_date1 = "2010-09-01 00:00:00";
		ResultListImpl result = new ResultListImpl();
		Container container = null;
		Getdates getdates;
		indate(str_date1,end_date1,result,container);
		String output = result.toString();
		System.out.println(output);
	}
        
         
 }

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member188791
Participant
0 Kudos

its solved myself,as I was giving input in the wrong format,after correcting it works fine

markangelo_dihiansan
Active Contributor
0 Kudos

Hello,

Since using the context type of UDF automatically translates the parameters to


public static void indate(String[] a,String[] b,ResultList result,Container container)throws Exception{}

You will need to loop your code


for(int j=0; j<a.length; j++){
                     List <Date> dates = new ArrayList<Date>();
	SimpleDateFormat formatter;
	formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String str_date = a[j];
	String end_date = b[j];				
	Date startDate = formatter.parse(str_date);
	Date endDate   = formatter.parse(end_date);
	long interval = 24*1000*60*60;
	long endTime = endDate.getTime();
	long curTime = startDate.getTime();
	while (curTime <= endTime){
	dates.add(new Date(curTime));
	curTime+=interval;
		}
		
		for(int i=0;i<dates.size();i++){
			
			Date iDate = (Date)dates.get(i);
			String ds = formatter.format(iDate);
			String ds1 = ds.substring(0, 10);	
			result.addValue(ds1);
			//result.addContextChange();
			//System.out.println("Dates are...." + ds1);	
			
		}
	}
}	

Hope this helps,

Mark

former_member188791
Participant
0 Kudos

HI Mark,

previously I tried this ,but I am getting the below error:

unreported exception java.text.ParseException; must be caught or declared to be thrown 	
Date startDate = formatter.parse(str_date);    
unreported exception java.text.ParseException; must be caught or declared to be thrown 	
Date endDate   = formatter.parse(end_date);

Regards,

Rajiv ^

markangelo_dihiansan
Active Contributor
0 Kudos

Hello,

You need to enclose the


Date startDate = formatter.parse(str_date);    	
Date endDate   = formatter.parse(end_date);

in a try-catch statement


try{
Date startDate = formatter.parse(str_date);    	
Date endDate   = formatter.parse(end_date);
}
catch(java.text.ParseException e){
//Your exception here
}

Hope this helps,

Mark

baskar_gopalakrishnan2
Active Contributor
0 Kudos

In addition to Mark's suggestion you might have to use try catch block for the formatter class.

Example:

try{
  for(int j=0; j<a.length; j++){
                     List <Date> dates = new ArrayList<Date>();
	SimpleDateFormat formatter;
	formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String str_date = a[j];
	String end_date = b[j];				
	Date startDate = formatter.parse(str_date);
	Date endDate   = formatter.parse(end_date);
	long interval = 24*1000*60*60;
	long endTime = endDate.getTime();
	long curTime = startDate.getTime();
	while (curTime <= endTime){
	dates.add(new Date(curTime));
	curTime+=interval;
		}
		
		for(int i=0;i<dates.size();i++){
			
			Date iDate = (Date)dates.get(i);
			String ds = formatter.format(iDate);
			String ds1 = ds.substring(0, 10);	
			result.addValue(ds1);
			//result.addContextChange();
			//System.out.println("Dates are...." + ds1);	
			
		}
	}
}catch(Exception ex){

}
	

former_member188791
Participant
0 Kudos

Hi Mark,

Thank you,its working now.

One more query:in UDF out put if I get 4 date in between start and stop dates ,I need to get 4 output records ,how I can achieve this.

Regards,Rajiv

rodrigoalejandro_pertierr
Active Contributor
0 Kudos

hi

as result of the UDF put the standard function spliByValue(eachValue)

Rgds

RP-.

former_member188791
Participant
0 Kudos

HI Group,

Still I am getting the below error when I am calling this UDF in mapping:

Exception:[java.lang.NullPointerException: while trying to invoke the method java.util.Date.getTime() of an object loaded from local variable '<8>'] in class

anupam_ghosh2
Active Contributor
0 Kudos

Hi Rajiv,

I did some changes to the code you are using. Use advanced UDF of cache type "CONTEXT" and not "QUEUE".


public void indates(String[] a,String[] b,ResultList result,Container container){
   try   {
		java.text.SimpleDateFormat formatter;
		formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str_date = a[0];
		String end_date = b[0];
		java.util.Date startDate = formatter.parse(str_date);
		java.util.Date endDate   = formatter.parse(end_date);
		long interval = 24*1000*60*60;
		long endTime = endDate.getTime();
		long curTime = startDate.getTime();
		while(curTime <= endTime)
         		{
			String ds = formatter.format(new java.util.Date(curTime));
			String ds1 = ds.substring(0, 10);	
			curTime+=interval;
			result.addValue(ds1);
			
			
		}
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}//pls don't copy this last closing brace. This is already there in UDF by default.

No need of any import statement. I beleive you have only two inputs to the UDF thus I am considering only first two values of the arrays a and b within the UDF. In case your input is array of strings with multiple values of a and b , just modify the UDF as shown below


public void indates(String[] a,String[] b,ResultList result,Container container){
try   {
		java.text.SimpleDateFormat formatter;
		formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(int i=0;i<a.length;++i)
		{
			String str_date = a<i>;
			String end_date = b<i>;
			java.util.Date startDate = formatter.parse(str_date);
			java.util.Date endDate   = formatter.parse(end_date);
			long interval = 24*1000*60*60;
			long endTime = endDate.getTime();
			long curTime = startDate.getTime();
			while(curTime <= endTime)
         			{
				String ds = formatter.format(new java.util.Date(curTime));
				String ds1 = ds.substring(0, 10);	
				curTime+=interval;
				result.addValue(ds1);
			
			}
		}
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}//pls don't copy this last closing brace. This is already there in UDF by default.

Now just map this UDF to target field whose minimum occurence is 1 and max occurence is unbounded.

Values will get auto populated once you run this code. Also ensure that root nodes of source and target have been properly mapped.

Hope this solves your problem.

regards

Anupam

Edited by: anupamsap on Nov 9, 2011 3:52 PM