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.

  1. public class DateFunctionLibrary implements IFunctionLibrary
  2. {
  3. // supported date functions
  4. public static final String DAY_OF_WEEK_FUNCTION = "DAY_OF_WEEK";
  5. public static final String CURRENT_YEAR_FUNCTION = "CURRENT_YEAR";
  6.  
  7. public DateFunctionLibrary()
  8. {
  9. }
  10.  
  11. /**
  12.   * @see https://docs.awareim.com/ref/p_m/getname_ifunction
  13.   */
  14. @Override
  15. public String getName()
  16. {
  17. return "DateFunctionLibrary";
  18. }
  19.  
  20. /**
  21.   * @see https://docs.awareim.com/ref/p_m/hasfunction
  22.   */
  23. @Override
  24. public boolean hasFunction(String functionName, int paramNmb)
  25. {
  26. if (paramNmb == 0)
  27. {
  28. return functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION);
  29. }
  30. else if (paramNmb == 1)
  31. {
  32. return functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION);
  33. }
  34. return false;
  35. }
  36.  
  37. /**
  38.   * @see https://docs.awareim.com/ref/p_m/getavailablefunctions
  39.   */
  40. @Override
  41. public FunctionDescription[] getAvailableFunctions()
  42. {
  43. FunctionDescription[] results = new FunctionDescription[2];
  44. results[0] = new FunctionDescription(CURRENT_YEAR_FUNCTION,
  45. "Parameters: none\nReturn: current year as 4 digit Number");
  46. results[1] = new FunctionDescription(DAY_OF_WEEK_FUNCTION,
  47. "Parameters: attribute of Date type or expression producing Date" +
  48. "\nReturn: day of week of the provided date as Plain Text ('Monday', 'Tuesday' etc)");
  49. return results;
  50. }
  51.  
  52. /**
  53.   * @see https://docs.awareim.com/ref/p_m/calculate
  54.   */
  55. public Object calculate(String functionName, Object[] params, INodeHelper helper)
  56. {
  57. if (params == null || params.length == 0)
  58. {
  59. if (functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION))
  60. {
  61. Calendar cal = Calendar.getInstance();
  62. cal.setTime(new Date());
  63. return new Integer(cal.get(Calendar.YEAR));
  64. }
  65. }
  66. else if (params != null && params.length == 1)
  67. {
  68. // a single parameter must be of DateHolder type
  69. if (functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION))
  70. {
  71. if (!(params[0] instanceof DateHolder))
  72. return null;
  73. Date date = TypeUtils.toDate((DateHolder) params[0]);
  74. Calendar cal = Calendar.getInstance();
  75. cal.setTime(date);
  76. return sdf.format(date);
  77. }
  78. }
  79. return null;
  80. }
  81.  
  82. /**
  83.   * @see https://docs.awareim.com/ref/p_m/tosql
  84.   */
  85. @Override
  86. public String toSQL(String functionName, Collection parameters, ISQLBuilderHelper sqlHelper)
  87. throws Exception
  88. {
  89. IDatabaseClientInterface dbi = sqlHelper.getDatabaseInterface();
  90. if (parameters == null || parameters.size() == 0)
  91. {
  92. if (dbi instanceof MySQLClientInterface)
  93. {
  94. if (functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION))
  95. {
  96. return "YEAR(CURDATE())";
  97. }
  98. }
  99. else if (dbi instanceof MSSQLServerClientInterface)
  100. {
  101. if (functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION))
  102. {
  103. return "DATEPART (yy, GETDATE())";
  104. }
  105. }
  106. }
  107. else if (parameters != null && parameters.size() == 1)
  108. {
  109. Iterator iter = parameters.iterator();
  110. IArithmeticNode arNode = (IArithmeticNode) iter.next();
  111. // representation of the argument
  112. String dateArg = arNode.toSQL(sqlHelper);
  113. if (dateArg == null || dateArg.length() == 0)
  114. return ""; // the expression won't be calculated
  115. if (functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION))
  116. {
  117. if (sqlFlavour == CommonConstants.SQL_FLAVOUR_TSQL)
  118. return "DATENAME(dw," + dateArg + ")";
  119. else if (sqlFlavour == CommonConstants.SQL_FLAVOUR_MYSQL)
  120. return "DAYNAME(" + dateArg + ")";
  121. }
  122. }
  123. throw new UnsupportedSQLElementException(functionName);
  124. }
  125.  
  126. /**
  127.   * @see https://docs.awareim.com/ref/p_m/gettypeclass
  128.   */
  129. @Override
  130. public Class getTypeClass(String functionName, Collection params)
  131. {
  132. return Integer.class;
  133. }
  134.  
  135. /**
  136.   * @see https://docs.awareim.com/ref/p_m/getrequiredfactpatterns
  137.   */
  138. @Override
  139. public HashSet getRequiredFactPatterns(String functionName, Collection params)
  140. {
  141. return null;
  142. }
  143. }