Gyan Factory

Gyan Factory
SAP Technical Project Support

Tuesday, February 16, 2016

OO ABAP – Selection Object with Direct Access

In continuation to the previous article and in quest about the Selection object, lets check out this different flavor of the selection screen object.

Introduction

Thanks to Clemens Li for sharing this utility class in his comment on the article Object Oriented Approach for Reports with multiple Datasource
Selection criteria class, which I generally refer to as the Selection Object. If you haven’t read about where it would fit in the overall design, I suggest you read ABAP Object Oriented Approach for Reports – Redesign.

Selection Screen Object with Direct Access

This utility class is based on the global memory access using the field-symbols. I know, that you know that I don’t like the global memory access (Why NOT to use Field-Symbols to access Global Memory?). But, lets just explore this option as well.
This option, contrary to other options, this approach doesn’t need you to create any attribute in the class. That would be considered a win, if you have many select options to deal with. But, it would also defeat the purpose, if there is no selection screen variables.
Also, since it is generic, you don’t need to create the class again and again into your development. You can simply reuse this class for all reports. Just instantiate the object and done. But, One more drawback is, when you get or a set the value, you would need to create temporary tables / variables in order to receive the value back.
There are 4 methods:
  • GET_: These methods are to get the value of the select-option (GET_SELECT_OPTION_TABLE) or a Parameter (GET_PARAMETER_VALUE)
  • SET_: These methods are to set the value to the select-option (SET_SELECT_OPTION_TABLE) or a Parameter (SET_PARAMETER_VALUE)
Code lines

Utility class for Selection Criteria

 
CLASS lcl_selection DEFINITION.
  PUBLIC SECTION.
    METHODS get_parameter_value
        IMPORTING
          !iv_parameter_name TYPE feld-NAME
        RETURNING
          VALUE(rv_value) TYPE STRING .
    METHODS get_select_option_table
      IMPORTING
        !iv_select_option TYPE feld-NAME
      RETURNING
        VALUE(rt_range) TYPE rsis_t_range .
    METHODS set_parameter_value
      IMPORTING
        !iv_parameter_name TYPE feld-NAME
        !iv_value TYPE STRING .
    METHODS set_select_option_table
      IMPORTING
        !iv_select_option TYPE feld-NAME
        !it_range TYPE rsis_t_range .
ENDCLASS.                    "lcl_selection DEFINITION
*
*
CLASS lcl_selection IMPLEMENTATION.
 
* ---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_ZJL_EIF_MENGEN_SONDER->GET_PARAMETER_VALUE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_PARAMETER_NAME              TYPE        FELD-NAME
* | [<-()] RV_VALUE                       TYPE        STRING
* +-------------------------------------------------------------------------------------
  METHOD get_parameter_value.
    DATA:
      lv_assign TYPE STRING.
    FIELD-SYMBOLS:
      <fs>   TYPE ANY.
    lv_assign = '(' && sy-cprog && ')' && iv_parameter_name.
    ASSIGN (lv_assign) TO <fs>.
    ASSERT sy-subrc = 0.
    rv_value = <fs>.
  ENDMETHOD.                    "GET_PARAMETER_VALUE
* ---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_ZJL_EIF_MENGEN_SONDER-&gt;GET_SELECT_OPTION_TABLE
* +-------------------------------------------------------------------------------------------------+
* | [---&gt;] IV_SELECT_OPTION               TYPE        FELD-NAME
* | [&lt;-()] RT_RANGE                       TYPE        RSIS_T_RANGE
* +-------------------------------------------------------------------------------------
  METHOD get_select_option_table.
    DATA:
      lv_assign TYPE STRING.
    FIELD-SYMBOLS:
      <tab>     TYPE TABLE,
      <line>    TYPE ANY,
      <sign>    TYPE ANY,
      <option>  TYPE ANY,
      <low>     TYPE ANY,
      <high>    TYPE ANY,
      <range>   TYPE LINE OF rsis_t_range.
    lv_assign = '(' && sy-cprog && ')' && iv_select_option && '[]'.
    ASSIGN (lv_assign) TO <tab>.
    ASSERT sy-subrc = 0.
    LOOP AT <tab> ASSIGNING <line>.
      APPEND INITIAL LINE TO rt_range ASSIGNING <range>.
      ASSIGN COMPONENT 'SIGN'   OF STRUCTURE <line>  TO <sign>.
      ASSIGN COMPONENT 'OPTION' OF STRUCTURE <line>  TO <option>.
      ASSIGN COMPONENT 'LOW'    OF STRUCTURE <line>  TO <low>.
      ASSIGN COMPONENT 'HIGH'   OF STRUCTURE <line>  TO <high>.
      <range>-SIGN   = <sign>.
      <range>-OPTION = <option>.
      <range>-LOW    = <low>.
      <range>-HIGH   = <high>.
    ENDLOOP.
  ENDMETHOD.                    "GET_SELECT_OPTION_TABLE
* ---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_ZJL_EIF_MENGEN_SONDER-&gt;SET_PARAMETER_VALUE
* +-------------------------------------------------------------------------------------------------+
* | [---&gt;] IV_PARAMETER_NAME              TYPE        FELD-NAME
* | [---&gt;] IV_VALUE                       TYPE        STRING
* +-------------------------------------------------------------------------------------
  METHOD set_parameter_value.
    DATA:
      lv_assign TYPE STRING.
    FIELD-SYMBOLS:
      <fs>     TYPE ANY.
    lv_assign = '(' && sy-cprog && ')' && iv_parameter_name.
    ASSIGN (lv_assign) TO <fs>.
    ASSERT sy-subrc = 0.
    <fs> = iv_value.
  ENDMETHOD.                    "SET_PARAMETER_VALUE
* ---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_ZJL_EIF_MENGEN_SONDER-&gt;SET_SELECT_OPTION_TABLE
* +-------------------------------------------------------------------------------------------------+
* | [---&gt;] IV_SELECT_OPTION               TYPE        FELD-NAME
* | [---&gt;] IT_RANGE                       TYPE        RSIS_T_RANGE
* +-------------------------------------------------------------------------------------
  METHOD set_select_option_table.
    DATA:
      lv_assign TYPE STRING.
    FIELD-SYMBOLS:
      <tab>     TYPE TABLE,
      <range>   TYPE LINE OF rsis_t_range,
      <line>    TYPE ANY,
      <sign>    TYPE ANY,
      <option>  TYPE ANY,
      <low>     TYPE ANY,
      <high>    TYPE ANY,
      <range2>  TYPE LINE OF rsis_t_range.
    lv_assign = '(' && sy-cprog && ')' && iv_select_option && '[]'.
    ASSIGN (lv_assign) TO <tab>.
    ASSERT sy-subrc = 0.
    CLEAR <tab>.
    LOOP AT it_range ASSIGNING <range>.
      APPEND INITIAL LINE TO <tab> ASSIGNING <line>.
      ASSIGN COMPONENT 'SIGN'   OF STRUCTURE <line>  TO <sign>.
      ASSIGN COMPONENT 'OPTION' OF STRUCTURE <line>  TO <option>.
      ASSIGN COMPONENT 'LOW'    OF STRUCTURE <line>  TO <low>.
      ASSIGN COMPONENT 'HIGH'   OF STRUCTURE <line>  TO <high>.
      <sign>    = <range>-SIGN.
      <option>  = <range>-OPTION.
      <low>     = <range>-LOW.
      <high>    = <range>-HIGH.
    ENDLOOP.
  ENDMETHOD.                    "set_select_option_table
 
ENDCLASS.                    "lcl_selection IMPLEMENTATION
 
To test it out:

Test Program

 
DATA: lo_sel TYPE REF TO lcl_selection.
 
SELECT-OPTIONS: s_erdat FOR sy-datum OBLIGATORY.
 
INITIALIZATION.
  CREATE OBJECT lo_sel.
  DATA: lt_erdat_t TYPE rsis_t_range.
  DATA: ls_erdat_t LIKE LINE OF lt_erdat_t.
  DATA: lv_date TYPE d.
  ls_erdat_t = 'IEQ'.
  lv_date = sy-datum - 1.
  ls_erdat_t-LOW = lv_date. "sy-datum - 1.
  APPEND ls_erdat_t TO lt_erdat_t.
  lo_sel->set_select_option_table(
    iv_select_option = 'S_ERDAT'
    it_range         = lt_erdat_t
  ).
 
START-OF-SELECTION.
  "
  DATA: lt_erdat TYPE rsis_t_range.
  lt_erdat = lo_sel->get_select_option_table( 'S_ERDAT' ).
  IF sy-datum IN lt_erdat.
    WRITE: 'Date in Range' .
  ELSE.
    WRITE: 'date out of the range'.
  ENDIF.
 
Let me know your thoughts.

No comments:

Post a Comment