Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
docs:3500:0500:0530 [2023/04/05 07:50] – removed - external edit (Unknown date) 127.0.0.1docs:3500:0500:0530 [2023/04/06 04:45] (current) sean
Line 1: Line 1:
 +{{tag>Programmers_Reference Example IFunctionLibrary_Interface }}
 +[<10>]
 +==== Function library example ====
 +
 +See the code snippet below for an example of a function library. For illustration purposes this code snippet contains only a handful of functions supported by the standard Date Function Library.
 +
 +<code java>public class DateFunctionLibrary implements IFunctionLibrary
 +{
 +    supported date functions
 +    public static final StringDAY_OF_WEEK_FUNCTION="DAY_OF_WEEK";
 +    public static final StringCURRENT_YEAR_FUNCTION="CURRENT_YEAR";
 +    public DateFunctionLibrary()
 +    {
 +    }
 +    /** 
 +     * @see IFunctionLibrary#getName()
 +     **/
 +    @Override
 +    public StringgetName()
 +    {
 +        return “DateFunctionLibrary”;
 +    }
 +    /** 
 +     *@see IFunctionLibrary#hasFunction(String,int)
 +     */
 +    @Override
 +    public boolean hasFunction(StringfunctionName, int paramNmb)
 +    {
 +        if (paramNmb==0)
 +        {
 +            return functionName.equalsIgnoreCase (CURRENT_YEAR_FUNCTION);
 +        }
 +        else if (paramNmb==1)
 +        {
 +            return functionName.equalsIgnoreCase (DAY_OF_WEEK_FUNCTION);
 +            return false ;
 +        }
 +        /** 
 +         * @see IFunctionLibrary#getAvailableFunctions()
 +         **/
 +        @Override
 +        public FunctionDescription[]getAvailableFunctions()
 +        {
 +            FunctionDescription[]results= new FunctionDescription[2];
 +            results[0]= new FunctionDescription(CURRENT_YEAR_FUNCTION,
 +                "Parameters: none\nReturn: current year as 4 digit Number");
 +            results[1]= new FunctionDescription(DAY_OF_WEEK_FUNCTION, "Parameters: attribute of Date type or expression producing Date"+"\nReturn:  day of week of the provided date as Plain Text ('Monday', 'Tuesday' etc)");
 +            return results;
 +        }
 +        /**
 +         ** @see IFunctionLibrary#calculate(String,Object[],INodeHelper)
 +         **/
 +        public Objectcalculate(StringfunctionName,Object[]params, INodeHelperhelper)
 +        {
 +            if (params== null ||params.length==0)
 +            {
 +                if (functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION))
 +                {
 +                    Calendarcal=Calendar.getInstance();
 +                    cal.setTime(new Date ());
 +                    return new Integer(cal.get(Calendar.YEAR));
 +                }
 +            }
 +            else if (params!= null &&params.length==1)
 +            {
 +                // a single parameter must be of DateHolder type
 +                if (functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION))
 +                {
 +                    if (!(params[0] instanceof DateHolder))
 +                        return null ;
 +                    Datedate=TypeUtils.toDate((DateHolder)params[0]);
 +                    Calendarcal=Calendar.getInstance();
 +                    cal.setTime(date);
 +                    SimpleDateFormatsdf= new SimpleDateFormat("E");
 +                    return sdf.format(date);
 +                }
 +            }
 +            return null ;
 +        }
 +        /** 
 +         ** @see IFunctionLibrary#toSQL(String,Collection,ISQLBuilderHelper)
 +         **/
 +        @Override
 +        public StringtoSQL(StringfunctionName,Collectionparameters, ISQLBuilderHelpersqlHelper)
 +        throw Exception
 +        {
 +            IDatabaseClientInterfacedbi =sqlHelper.getDatabaseInterface();
 +            if (parameters== null ||parameters.size()==0)
 +            {
 +                if (dbi instanceof MySQLClientInterface)
 +                {
 +                    if (functionName.equalsIgnoreCase (CURRENT_YEAR_FUNCTION))
 +                    {
 +                        return "YEAR(CURDATE())";
 +                    }
 +                }
 +                else if (dbi instanceof MSSQLServerClientInterface)
 +                {
 +                    if (functionName.equalsIgnoreCase (CURRENT_YEAR_FUNCTION))
 +                    {
 +                        return "DATEPART (yy, GETDATE())";
 +                    }
 +                }
 +            }
 +            else if (parameters!= null &&parameters.size()==1)
 +            {
 +                Iteratoriter=parameters.iterator();
 +                IArithmeticNodearNode=(IArithmeticNode)iter.next();
 +                // representation of the argument
 +                StringdateArg=arNode.toSQL(sqlHelper);
 +                if (dateArg== null ||dateArg.length()==0)
 +                return ""; // the expression won't be calculated
 +            if (functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION))
 +            {
 +                if (sqlFlavour==CommonConstants.SQL_FLAVOUR_TSQL)
 +                    return "DATENAME(dw,"+dateArg+")";
 +                else if (sqlFlavour==CommonConstants.SQL_FLAVOUR_MYSQL)
 +                    return "DAYNAME("+dateArg+")";
 +            }
 +        }
 +        throw new UnsupportedSQLElementException(functionName);
 +    }
 +    /** @see IFunctionLibrary#getTypeClass(String,Collection)*/
 +    @Override
 +    public ClassgetTypeClass(StringfunctionName,Collectionparams)
 +    {
 +        return Integer. class ;
 +    }
 +    /** @see IFunctionLibrary#getRequiredFactPatterns(String,Collection)*/
 +    @Override
 +    public HashSetgetRequiredFactPatterns(StringfunctionName,Collectionparams)
 +    {
 +        return null;
 +    }
 +}</code>
 +