You may want to automate the filter for your already retrieved data from the OData service in ABAP. This allows you to just send that data back, that the frontend has been requested.
Please note that you already need an internal table, filled with all the data, in the best case also unfiltered to use later the dynamic filter.
First, we need a new private method called ENTITYSET_FILTER in the same class, where also our GET_ENTITYSET method from the *DPC_EXT class is defined.
The signature of the new method ENTITYSET_FILTER.
IO_DP_FACADE IMPORTING Type Ref To /IWBEP/IF_MGW_DP_INT_FACADE
IT_FILTER_SELECT_OPTIONS IMPORTING Type /IWBEP/T_MGW_SELECT_OPTION
IV_ENTITY_NAME IMPORTING Type STRING
CT_ENTITYSET CHANGING Type STANDARD TABLE
Then the method implementation looks like the following. All the references and objects should be existing in the system.
METHOD entityset_filter.
DATA:
lx_root TYPE REF TO cx_root,
lo_data_descr TYPE REF TO cl_abap_datadescr,
lo_table_descr TYPE REF TO cl_abap_tabledescr,
lo_dp_facade TYPE REF TO /iwbep/cl_mgw_dp_facade,
lo_model TYPE REF TO /iwbep/if_mgw_odata_re_model,
ls_entity_props TYPE /iwbep/if_mgw_odata_re_prop=>ty_s_mgw_odata_property,
lt_entity_props TYPE /iwbep/if_mgw_odata_re_prop=>ty_t_mgw_odata_properties,
ls_filter_sel TYPE /iwbep/s_mgw_select_option,
lv_entity_name TYPE /iwbep/med_external_name,
lv_tabix TYPE i,
lv_type TYPE string,
lv_val TYPE c LENGTH 7.
FIELD-SYMBOLS:
<fs_val> TYPE data,
<fs_data> TYPE data.
" Pre-check.
IF lines( it_filter_select_options ) EQ 0.
RETURN.
ENDIF.
" 'Type-cast' datatype.
lv_entity_name = iv_entity_name.
" Get type of table.
TRY.
lo_model = io_dp_facade->get_model( ).
" Get Entity Properties.
lt_entity_props = lo_model->get_entity_type( lv_entity_name )->get_properties( ).
" Traverse filters.
LOOP AT it_filter_select_options INTO ls_filter_sel.
" Map Model Property to ABAP field name.
READ TABLE lt_entity_props INTO ls_entity_props
WITH KEY name = ls_filter_sel-property.
IF sy-subrc = 0.
" Evaluate (single) Property filter on EntitySet.
LOOP AT ct_entityset ASSIGNING <fs_data>.
lv_tabix = sy-tabix.
" Get Property value.
ASSIGN COMPONENT ls_entity_props-technical_name OF STRUCTURE <fs_data> TO <fs_val>.
IF sy-subrc = 0 AND <fs_val> IS ASSIGNED.
" Evaluate the filter (not adhering to filter => delete).
lv_val = |{ <fs_val> ALPHA = OUT }|.
IF lv_val NOT IN ls_filter_sel-select_options.
" Delete from table, when not adhering to filter.
DELETE ct_entityset INDEX lv_tabix.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.
CATCH /iwbep/cx_mgw_med_exception INTO lx_root.
lv_entity_name = ''.
ENDTRY.
ENDMETHOD.
In your GET_ENTITYSET method, you are now calling the new method by simply check if the io_dp_facade paramater has been supplied.
" ....
IF io_dp_facade IS SUPPLIED.
entityset_filter(
EXPORTING
it_filter_select_options = it_filter_select_options
iv_entity_name = iv_entity_name
io_dp_facade = io_dp_facade
CHANGING
ct_entityset = lt_result
).
ENDIF.
er_entityset = copy_data_to_ref( lt_result ).
You now have implemented a dynamic filter, which depends on the existing OData field names and uses the it_filter_select_options to correctly apply the filter to the lt_result.
Leave a Reply