Skip to Content

Web Dynpro Selection Screen Variants Part 2: Assistance Class

The series Web Dynpro Selection Screen Variants continues from Part 1 with creation of the assistance class.

Part 2: Assistance Class

Create new ABAP class ZCL_WD_SELECT_OPTIONS_VARIANTS with the following attributes and inheritance.

Attributes

Create public attribute constant E_KIND with the following values to identify selection types.

constants:
  BEGIN OF e_kind,
      select_options TYPE rsscr_kind VALUE 'S',
      parameter      TYPE rsscr_kind VALUE 'P',
    END OF e_kind.

Methods

Create four public methods.

GET_VARIANT_INFO

METHOD get_variant_info.

  SELECT SINGLE *
         FROM zwdvari
         INTO CORRESPONDING FIELDS OF info
         WHERE component   = component
         AND   application = application
         AND   variant     = variant.

ENDMETHOD.

VARIANT_DELETE

method VARIANT_DELETE.

  DATA lv_subrc TYPE sysubrc.

  DELETE FROM zwdvari
         WHERE component   = component
         AND   application = application
         AND   variant     = variant.
  ADD sy-subrc TO lv_subrc.

  SELECT COUNT(*)
         FROM zwdvaricon
         WHERE component   = component
         AND   application = application
         AND   variant     = variant.

  IF sy-dbcnt IS NOT INITIAL.
    DELETE FROM zwdvaricon
           WHERE component   = component
           AND   application = application
           AND   variant     = variant.
    ADD sy-subrc TO lv_subrc.
  ENDIF.

  CASE lv_subrc.
    WHEN 0.
      COMMIT WORK.
      return-subrc = lv_subrc.
      return-msgid = 'DB'.
      return-msgty = 'S'.
      return-msgno = 618. " Variant & deleted
      return-msgv1 = variant.

    WHEN OTHERS.
      ROLLBACK WORK.
      return-subrc = lv_subrc.
      return-msgid = 'DB'.
      return-msgty = 'E'.
      return-msgno = 257. " Variant & was not deleted
      return-msgv1 = variant.

  ENDCASE. " lv_subrc

endmethod.

VARIANT_GET

method VARIANT_GET.

  DATA lt_zwdvaricon TYPE SORTED TABLE OF zwdvaricon WITH UNIQUE DEFAULT KEY.
  DATA lo_selopt TYPE REF TO cl_wdr_select_options.
  DATA lt_selection_fields TYPE if_wd_select_options=>tt_selection_screen_item.
  DATA lt_parameter_fields TYPE if_wd_select_options=>tt_selection_screen_item.

  DATA lo_ranges TYPE REF TO data.
  DATA lo_range TYPE REF TO data.
  DATA lo_data TYPE REF TO data.
  DATA lo_value TYPE REF TO data.
  DATA lo_tabledescr TYPE REF TO cl_abap_tabledescr.
  DATA lo_datadescr TYPE REF TO cl_abap_datadescr.

  FIELD-SYMBOLS <selopt> TYPE REF TO if_wd_select_options.
  FIELD-SYMBOLS <field> TYPE if_wd_select_options=>t_selection_screen_item.
  FIELD-SYMBOLS <zwdvaricon> TYPE zwdvaricon.

  FIELD-SYMBOLS <range_table> TYPE STANDARD TABLE.
  FIELD-SYMBOLS <range> TYPE any.
  FIELD-SYMBOLS <value> TYPE any.
  FIELD-SYMBOLS <sign> TYPE any.
  FIELD-SYMBOLS <option> TYPE any.
  FIELD-SYMBOLS <low> TYPE any.
  FIELD-SYMBOLS <high> TYPE any.

* -- Read variant contents

  SELECT *
         FROM zwdvaricon
         INTO TABLE lt_zwdvaricon
         WHERE component   = component
         AND   application = application
         AND   variant     = variant.

* -- Write saved variant data to the select-options references

  LOOP AT select_options_ref ASSIGNING <selopt>.

    lo_selopt ?= <selopt>.
    lo_selopt->if_wd_select_options~reset_all_selection_fields( ).
    lo_selopt->if_wd_select_options~reset_all_parameter_fields( ).

    lo_selopt->if_wd_select_options~get_selection_fields( IMPORTING et_fields = lt_selection_fields ).

    LOOP AT lt_selection_fields ASSIGNING <field>.

      lo_ranges = lo_selopt->if_wd_select_options~get_range_table_of_sel_field( <field>-m_id ).
      lo_tabledescr ?= cl_abap_typedescr=>describe_by_data_ref( lo_ranges ).
      lo_datadescr = lo_tabledescr->get_table_line_type( ).
      CREATE DATA lo_range TYPE HANDLE lo_datadescr.

      ASSIGN lo_ranges->* TO <range_table>.
      ASSIGN lo_range->* TO <range>.

      LOOP AT lt_zwdvaricon ASSIGNING <zwdvaricon>
           WHERE field = <field>-m_id
           AND   kind = me->e_kind-select_options.

        ASSIGN COMPONENT 'SIGN' OF STRUCTURE <range> TO <sign>.
        ASSIGN COMPONENT 'OPTION' OF STRUCTURE <range> TO <option>.
        ASSIGN COMPONENT 'LOW' OF STRUCTURE <range> TO <low>.
        ASSIGN COMPONENT 'HIGH' OF STRUCTURE <range> TO <high>.

        <sign> = <zwdvaricon>-sign.
        <option> = <zwdvaricon>-opti.
        <low> = <zwdvaricon>-low.
        <high> = <zwdvaricon>-high.
        APPEND <range> TO <range_table>.
      ENDLOOP. " <zwdvaricon>

      lo_selopt->if_wd_select_options~set_range_table_of_sel_field(
        i_id           = <field>-m_id
        it_range_table = lo_ranges
      ).

    ENDLOOP. " <field>

    lo_selopt->if_wd_select_options~get_parameter_fields( IMPORTING et_fields = lt_parameter_fields ).

    LOOP AT lt_parameter_fields ASSIGNING <field>.

      READ TABLE lt_zwdvaricon ASSIGNING <zwdvaricon>
           WITH KEY field = <field>-m_id
                    kind = me->e_kind-parameter.

      CHECK sy-subrc IS INITIAL.

      lo_datadescr ?= cl_abap_typedescr=>describe_by_data( <zwdvaricon>-low ).
      CREATE DATA lo_value TYPE HANDLE lo_datadescr.
      ASSIGN lo_value->* TO <value>.
      <value> = <zwdvaricon>-low.

      lo_selopt->if_wd_select_options~set_value_of_parameter_field(
        i_id    = <field>-m_id
        i_value = lo_value
      ).

    ENDLOOP. " <field>

    CLEAR: lt_selection_fields,
           lt_parameter_fields.

  ENDLOOP. " <selopt>

endmethod.

VARIANT_SAVE

method VARIANT_SAVE.

  DATA ls_zwdvari TYPE zwdvari.
  DATA lv_srtf2 TYPE srtf2.
  DATA lt_selection_fields TYPE if_wd_select_options=>tt_selection_screen_item.
  DATA lt_parameter_fields TYPE if_wd_select_options=>tt_selection_screen_item.
  DATA lo_selopt TYPE REF TO cl_wdr_select_options.
  DATA ls_contents TYPE zwdvaricon.
  DATA lt_contents TYPE SORTED TABLE OF zwdvaricon WITH UNIQUE DEFAULT KEY.
  DATA lv_subrc TYPE sysubrc.

  FIELD-SYMBOLS <selopt> TYPE REF TO if_wd_select_options.
  FIELD-SYMBOLS <field> TYPE if_wd_select_options=>t_selection_screen_item.

  FIELD-SYMBOLS <range_table> TYPE ANY TABLE.
  FIELD-SYMBOLS <range> TYPE any.
  FIELD-SYMBOLS <parameter> TYPE any.
  FIELD-SYMBOLS <sign> TYPE any.
  FIELD-SYMBOLS <option> TYPE any.
  FIELD-SYMBOLS <low> TYPE any.
  FIELD-SYMBOLS <high> TYPE any.

* -- Variant header info

  ls_zwdvari = me->get_variant_info(
    component   = component
    application = application
    variant     = variant
  ).

  IF ls_zwdvari IS INITIAL. " create new variant
    ls_zwdvari-component = component.
    ls_zwdvari-application = application.
    ls_zwdvari-variant = variant.
    ls_zwdvari-vtext = short_text.
    ls_zwdvari-version = 1.
    ls_zwdvari-ernam = sy-uname.
    ls_zwdvari-erdat = sy-datum.

  ELSE. " change existing variant
    ls_zwdvari-vtext = short_text.
    ls_zwdvari-aenam = sy-uname.
    ls_zwdvari-aedat = sy-datum.
    ADD 1 TO ls_zwdvari-version.
  ENDIF.

* -- Collect select-options' items and their values

  LOOP AT select_options_ref ASSIGNING <selopt>.

    lo_selopt ?= <selopt>.
    lo_selopt->if_wd_select_options~get_selection_fields( IMPORTING et_fields = lt_selection_fields ).

    LOOP AT lt_selection_fields ASSIGNING <field>
         WHERE mt_range_table IS NOT INITIAL.

      AT NEW m_id.
        CLEAR lv_srtf2.
      ENDAT. " NEW m_id

      ASSIGN <field>-mt_range_table->* TO <range_table>.

      LOOP AT <range_table> ASSIGNING <range>.

        MOVE-CORRESPONDING ls_zwdvari TO ls_contents.
        ls_contents-field = <field>-m_id.
        ls_contents-kind = me->e_kind-select_options.
        ls_contents-srtf2 = lv_srtf2.

        ASSIGN COMPONENT 'SIGN' OF STRUCTURE <range> TO <sign>.
        ls_contents-sign = <sign>.

        ASSIGN COMPONENT 'OPTION' OF STRUCTURE <range> TO <option>.
        ls_contents-opti = <option>.

        ASSIGN COMPONENT 'LOW' OF STRUCTURE <range> TO <low>.
        ls_contents-low = condense( <low> ).

        ASSIGN COMPONENT 'HIGH' OF STRUCTURE <range> TO <high>.
        ls_contents-high = condense( <high> ).

        INSERT ls_contents INTO TABLE lt_contents.
        CLEAR: ls_contents.
        ADD 1 TO lv_srtf2.

      ENDLOOP. " <range>
    ENDLOOP. " <field>

    lo_selopt->if_wd_select_options~get_parameter_fields( IMPORTING et_fields = lt_parameter_fields ).

    LOOP AT lt_parameter_fields ASSIGNING <field>
         WHERE m_value IS NOT INITIAL.

      MOVE-CORRESPONDING ls_zwdvari TO ls_contents.
      ls_contents-field = <field>-m_id.
      ls_contents-kind = me->e_kind-parameter.

      ASSIGN <field>-m_value->* TO <parameter>.
      ls_contents-sign = 'I'.
      ls_contents-opti = 'EQ'.
      ls_contents-low = condense( <parameter> ).

      INSERT ls_contents INTO TABLE lt_contents.
      CLEAR: ls_contents.

    ENDLOOP. " <field>

    CLEAR: lt_selection_fields,
           lt_parameter_fields.

  ENDLOOP. " <selopt>

* -- Save to database

  IF ls_zwdvari-version > 1.
    DELETE FROM zwdvari
           WHERE component   = component
           AND   application = application
           AND   variant     = variant.
    ADD sy-subrc TO lv_subrc.

    SELECT COUNT(*)
           FROM zwdvaricon
           WHERE component   = component
           AND   application = application
           AND   variant     = variant.

    IF sy-dbcnt IS NOT INITIAL.
      DELETE FROM zwdvaricon
             WHERE component   = component
             AND   application = application
             AND   variant     = variant.
      ADD sy-subrc TO lv_subrc.
    ENDIF.
  ENDIF. " version

  INSERT zwdvari FROM ls_zwdvari.
  ADD sy-subrc TO lv_subrc.

  INSERT zwdvaricon FROM TABLE lt_contents.
  ADD sy-subrc TO lv_subrc.

  CASE lv_subrc.
    WHEN 0.
      COMMIT WORK.
      return-subrc = lv_subrc.
      return-msgid = 'DB'.
      return-msgty = 'S'.
      return-msgno = 617. " Variant & saved
      return-msgv1 = variant.

    WHEN OTHERS.
      ROLLBACK WORK.
      IF ls_zwdvari-version > 1.
        return-subrc = lv_subrc.
        return-msgid = 'DB'.
        return-msgty = 'E'.
        return-msgno = 255. " Variant & was not changed
        return-msgv1 = variant.
      ELSE.
        return-subrc = lv_subrc.
        return-msgid = 'DB'.
        return-msgty = 'E'.
        return-msgno = 254. " Variant & was not created
        return-msgv1 = variant.
      ENDIF. " version

  ENDCASE. " lv_subrc

endmethod.

Up Next

In Part 3, the series continues with creation of the Web Dynpro Component and its component controller.

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