Skip to Content
Author's profile photo Andreas Gautsch

TechEd Session DEV224 – BRFplus example with calling from ABAP

The following document is an addition to the TECHED Session DEV224, where the calling from BRFplus from ABAP is shown.

In the first part the modeling of a simple function in BRFplus is shown. This function assigns the value of a String variable to a number variable. As this is not always possible an additional parameter contains the logic if the conversion could be done.

More technically:

If the variable TechEd_String contains a valid number then the variables TechEd_Number is set to that value and the variable TechEd_Boolean gets the value true. Otherwise TechEd_Number remains unchanged and TechEd_Boolean gets the value false.

BRFplus sample

Sample Application

Sample Function

Sample Ruleset

Sample Rule

ABAP Code to call BRFplus function

In this section two classes for calling BRF objects are shown.
The first one shows how to call a function. In our case the above shown function: “TECHED_FUNCTION_STRING2NUMBER”.
The second class can be used to extract metadata for objects of the BRF Application repository.

Static class to call function

class ZCL_TECHED_BRFPLUS definition
  public
  final
  create public .

public section.

CLASS-METHODS: callFunction importing pTestString type string
                            exporting pTestNumber type p
                            returning value(pTestBoolean) type boolean.

protected section.
private section.
ENDCLASS.



CLASS ZCL_TECHED_BRFPLUS IMPLEMENTATION.

method callFunction.
  CONSTANTS: lv_function_id TYPE if_fdt_types=>id 
                            VALUE 'ECC8630284E41ED6A5DD4A3C478800D7'.
  DATA:      lv_timestamp TYPE timestamp,
             lt_name_value TYPE abap_parmbind_tab,
             ls_name_value TYPE abap_parmbind,
             lr_data TYPE REF TO data,
             lx_fdt TYPE REF TO cx_fdt,
             la_test_string TYPE if_fdt_types=>element_text,
             la_test_number TYPE if_fdt_types=>element_number,
             la_test_boolean TYPE if_fdt_types=>element_boolean.

  FIELD-SYMBOLS <la_any> TYPE any.


  GET TIME STAMP FIELD lv_timestamp.
  ls_name_value-name = 'TECHED_STRING'.
  la_TEST_STRING = pTestString.
  GET REFERENCE OF la_TEST_STRING INTO lr_data.
  ls_name_value-value = lr_data.
  INSERT ls_name_value INTO TABLE lt_name_value.
  ls_name_value-name = 'TECHED_NUMBER'.
  la_TEST_NUMBER = pTestNumber.
  GET REFERENCE OF la_TEST_NUMBER INTO lr_data.
  ls_name_value-value = lr_data.
  INSERT ls_name_value INTO TABLE lt_name_value.
  ls_name_value-name = 'TECHED_BOOLEAN'.
  la_TEST_BOOLEAN = pTestBoolean.
  GET REFERENCE OF la_TEST_BOOLEAN INTO lr_data.
  ls_name_value-value = lr_data.
  INSERT ls_name_value INTO TABLE lt_name_value.
  cl_fdt_function_process=>get_data_object_reference( 
                    EXPORTING iv_function_id      = lv_function_id
                              iv_data_object      = '_V_RESULT'
                              iv_timestamp        = lv_timestamp
                              iv_trace_generation = abap_false
                    IMPORTING er_data             = lr_data ).

  ASSIGN lr_data->* TO <la_any>.
  TRY.
      cl_fdt_function_process=>process( 
                   EXPORTING iv_function_id = lv_function_id
                             iv_timestamp   = lv_timestamp
                   IMPORTING ea_result      = <la_any>
                   CHANGING  ct_name_value  = lt_name_value ).
      CATCH cx_fdt into lx_fdt.
  ENDTRY.
  cl_fdt_function_process=>get_context_value( 
                EXPORTING  iv_function_id = lv_function_id
                           iv_timestamp   = lv_timestamp:
                           iv_data_object = 'ECC8630284E41ED6A5DD51C8C03260D7' "TECHED_STRING
                IMPORTING ev_data             = la_test_string ),
                          iv_data_object = 'ECC8630284E41ED6A5DD554F4E84E0D7' "TECHED_NUMBER
                IMPORTING ev_data             = la_test_number ),
                          iv_data_object = 'ECC8630284E41ED6A5DD58696945C0D7' "TECHED_BOOLEAN
                IMPORTING ev_data             = la_test_boolean ).

pTestBoolean = la_test_boolean.
pTestNumber = la_test_number.

endmethod.

ENDCLASS.

Testclass for the static function call

class ZCL_TEST_BRFPLUS_UNIT definition FOR TESTING.
"#AU Risk_Level Harmless
  PUBLIC SECTION.
  private section.
  METHODS: testCallfunction FOR TESTING.

ENDCLASS.

CLASS ZCL_TEST_BRFPLUS_UNIT IMPLEMENTATION.

   method testCallfunction.
    DATA: result type boolean,
          testnumber type p decimals 0.


    result = ZCL_TECHED_BRFPLUS=>callfunction( 
                       exporting pTestString = '123'  
                       importing pTestNumber = testnumber ).
    cl_aunit_assert=>assert_equals( EXP = abap_true  ACT = result  MSG = '' ).
    cl_aunit_assert=>assert_equals( EXP = 123  ACT = testnumber  MSG = '' ).

    result = ZCL_TECHED_BRFPLUS=>callfunction( 
                       exporting pTeststring = 'ABC' 
                      importing pTestNumber = testnumber ).
    cl_aunit_assert=>assert_equals( EXP = abap_false  ACT = result  MSG = '' ).

   endmethod.



ENDCLASS.

Static class to get Metadata of the BRFplus application repository

class ZCL_TECHED_BRFPLUS_METADATA definition
  public
  final
  create public .

public section.

CLASS-METHODS: getFunctionDefinition 
                     importing pFunctionId type if_fdt_types=>id
                     returning  value(pFunctionName) type string,
               getFunctionContextParams 
                     importing pFunctionId type if_fdt_types=>id
                     returning  value(pContextParams) type ZT_TECHED_CONTEXT_PARAMS,
               getFunctionId 
                     importing pFunctionname type IF_FDT_TYPES=>NAME
                     returning value(pFunctionId) type IF_FDT_TYPES=>ID.

protected section.
private section.
ENDCLASS.



CLASS ZCL_TECHED_BRFPLUS_METADATA IMPLEMENTATION.

method getFunctionDefinition.

  CL_FDT_FACTORY=>get_instance_generic( 
             EXPORTING iv_id    = pFunctionId
             IMPORTING eo_instance   = DATA(lo_instance) ).

  pFunctionName = lo_instance->get_name( ).

endmethod.

method getFunctionContextParams.
  DATA: fdtFunction   TYPE REF TO if_fdt_function,
        fdtDataObject   TYPE REF TO if_fdt_data_object,
        fdtElement       TYPE REF TO if_fdt_element,
        contextParam type ZSTRUCT_TECHED_CONTEXT_PARAM.


  CL_FDT_FACTORY=>get_instance_generic( 
               EXPORTING iv_id    = pFunctionId
               IMPORTING eo_instance   = DATA(lo_instance) ).

  fdtFunction ?= lo_instance.
  DATA(fdtObjectIds) = fdtFunction->get_context_data_objects( ).

  loop at fdtObjectIds assigning FIELD-SYMBOL(<fdtObjectId>).

      CL_FDT_FACTORY=>get_instance_generic( 
                    EXPORTING iv_id         = <fdtObjectId>
                    IMPORTING eo_instance   = lo_instance ).

      contextParam-name = lo_instance->get_name( ).
      fdtDataObject ?= lo_instance.
      fdtElement ?= fdtDataObject.
      DATA(lv_element_type) = fdtElement->get_element_type( ).

      CASE lv_element_type.
         WHEN if_fdt_constants=>gc_element_type_amount.
          contextParam-type = 'AMOUNT'.
         WHEN if_fdt_constants=>gc_element_type_boolean.
          contextParam-type = 'BOOLEAN'.
         WHEN if_fdt_constants=>gc_element_type_text.
          contextParam-type = 'TEXT'.
         "...
         WHEN OTHERS.
          contextParam-type = '<UNDEFINED>'.
      ENDCASE.
      append contextParam to pContextParams.

  endloop.

endmethod.

method GetFunctionId.

  DATA(factory) = CL_FDT_FACTORY=>get_instance( 'ECC8630284E41ED6A5DD30C447FA80D7' ).
  DATA(query) = factory->get_query( 
                     iv_object_type = if_fdt_constants=>gc_object_type_function ).

  query->get_ids( exporting iv_name = pFunctionname
                     importing ets_object_id = DATA(idsOfMatchingFuntions) ) .

  read table idsOfMatchingFuntions INDEX 1 into pFunctionId.

endmethod.

ENDCLASS.

Testclass for the BRF Metadata class

class ZCL_TEST_BRFPLUS_METADATA_UNIT definition FOR TESTING.
"#AU Risk_Level Harmless
  PUBLIC SECTION.
  private section.
  CONSTANTS: techedFunctionId   type if_fdt_types=>id 
                                VALUE 'ECC8630284E41ED6A5DD4A3C478800D7',
             techedFunctionName type IF_FDT_TYPES=>NAME 
                                VALUE 'TECHED_FUNCTION_STRING2NUMBER'.

  METHODS: testGetFunctionDefinition FOR TESTING,
           testGetFunctionContextParams FOR TESTING,
           testGetFunctionId FOR TESTING.

ENDCLASS.

CLASS ZCL_TEST_BRFPLUS_METADATA_UNIT IMPLEMENTATION.

 method testGetFunctionDefinition.

   DATA(functionname) = ZCL_TECHED_BRFPLUS_METADATA=>getfunctiondefinition( 
                                     pFunctionId = techedFunctionId ).
   CL_AUNIT_ASSERT=>assert_equals( EXP = techedFunctionName  ACT = functionName ).
 endmethod.

 method testGetFunctionContextParams.

   DATA(contextParams)  = ZCL_TECHED_BRFPLUS_METADATA=>getFunctionContextParams( 
                                       pFunctionId = techedFunctionId ).
   CL_AUNIT_ASSERT=>assert_equals( EXP = 3  ACT = lines( contextParams ) ).

   read table contextParams with key name = 'TECHED_STRING' into DATA(contextParam).
   CL_AUNIT_ASSERT=>assert_equals( EXP = 'TEXT' ACT = contextParam-type  ).

   clear contextParam.
   read table contextParams with key name = 'TECHED_NOT_EXISTING' into contextParam.
   CL_AUNIT_ASSERT=>assert_initial( contextParam ).

 endmethod.

 method testGetFunctionId.

    DATA(functionIdResult) = ZCL_TECHED_BRFPLUS_METADATA=>getFunctionId( 
                                        techedFunctionName  ).
    CL_AUNIT_ASSERT=>assert_equals( EXP = techedFunctionId ACT = functionIdResult ).

 endmethod.

ENDCLASS.

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.