Skip to Content
Author's profile photo Ramakrishnappa Gangappa

Custom getter & setter methods in Webdynpro ABAP – Part 2

Hello welcome again 🙂

Hope, you have gone through the below link for custom getter method GET_CTX_STAT_ATTR( )

Custom getter & setter methods in Webdynpro ABAP – Part 1

Now, let us look into to the method SET_CTX_STAT_ATTR( ).

Method: SET_CTX_STAT_ATTR( )

Signature:

set.PNG

Logic:

Header 1

METHOD set_ctx_stat_attr.
  DATA lo_nd                TYPE REF TO if_wd_context_node.
  DATA lo_nd_info          TYPE REF TO if_wd_context_node_info.
  DATA lv_type_name        TYPE string.
  DATA lv_node_name        TYPE string.
  DATA lo_nd_struct_descr  TYPE REF TO cl_abap_structdescr.

  “—————————————————-
  “Purpose: set data to context node of given controller context
  “—————————————————-
  CLEAR: ed_error_message.

  “===================================
  “check if context is supplied
  IF io_context IS  BOUND.
    lv_node_name = id_node_name.
    TRANSLATE lv_node_name TO UPPER CASE.
    TRY.
        “read node
        lo_nd = io_context->get_child_node( name = lv_node_name ).
        IF lo_nd IS BOUND.
          ” check if dirty flag needs to be set
          IF id_set_dirty EQ abap_true.
            lo_nd->set_changed_by_client( abap_true ).
          ENDIF.
          “get node info
          lo_nd_info = lo_nd->get_node_info( ).
          IF lo_nd_info IS BOUND.
            lo_nd_struct_descr =
            lo_nd_info->get_static_attributes_type( ).
            IF lo_nd_struct_descr IS BOUND AND
              lo_nd_struct_descr->is_ddic_type( ) EQ abap_true.
              lv_type_name = lo_nd_struct_descr->get_relative_name( ).

              “based on node cardinality, bind data
              IF lo_nd_info->is_multiple( ) EQ abap_true.
              ctx_bind_table(
                  EXPORTING io_node = lo_nd
                            id_node_name = lv_node_name
                            id_stat_attr = id_stat_attr
                            id_nd_type_name = lv_type_name
                  IMPORTING ed_error_message = ed_error_message ).

              ELSE.
                ctx_bind_structure(
                  EXPORTING io_node = lo_nd
                            id_node_name = lv_node_name
                            id_stat_attr = id_stat_attr
                            id_nd_type_name = lv_type_name
                  IMPORTING ed_error_message = ed_error_message ).

              ENDIF.
              FREE lo_nd_info.
            ENDIF.
            FREE lo_nd.
          ELSE.
            ed_error_message = text-001.
          ENDIF.
        ENDIF.
      CATCH cx_root.                                    “#EC CATCH_ALL
        ##no_handler
        ed_error_message = text-004.
    ENDTRY.
  ELSE.
    ed_error_message = text-003.”context is not supplied,
    RETURN.
  ENDIF.
ENDMETHOD.

Now let us create 2 private methods  as below

  • CTX_BIND_TABLE( ) – Bind table to context node – For Internal use only
  • CTX_BIND_STRUCTURE( ) – Bind structure to context node – For Internal use only

Method: CTX_BIND_TABLE( )

Signature:

bind_table.PNG

Logic:

CTX_BIND_TABLE

METHOD CTX_BIND_TABLE.
  DATA lo_str_descr      TYPE REF TO cl_abap_structdescr.
  DATA lo_tbl_descr      TYPE REF TO cl_abap_tabledescr.
*  DATA lo_stat_data      TYPE REF TO data.
  DATA lv_nd_type_name    TYPE string.
  DATA lv_id_type_name    TYPE string.
  DATA lv_temp_type_name  TYPE string.

  FIELD-SYMBOLS: <lt_stat_attr>    TYPE STANDARD TABLE.

  CLEAR: ed_error_message,
        lv_nd_type_name,
        lv_id_type_name,
        lv_temp_type_name.

  IF io_node IS BOUND.
    lv_nd_type_name = id_nd_type_name.
    TRANSLATE lv_nd_type_name TO UPPER CASE.
    “get the type name of data provided
    lo_tbl_descr ?=
      cl_abap_tabledescr=>describe_by_data( id_stat_attr ).
    IF lo_tbl_descr IS BOUND.
      “check if the data type provided and
      “node type are same
      lo_str_descr ?= lo_tbl_descr->get_table_line_type( ).
      IF lo_str_descr IS BOUND.
        lv_id_type_name = lo_str_descr->get_relative_name( ).
      ENDIF.

      “check if the type name is used from context node
      IF lv_id_type_name NE lv_nd_type_name.
        CONCATENATE ‘ELEMENT’ id_node_name
        INTO lv_temp_type_name SEPARATED BY ‘_’.
        TRANSLATE lv_temp_type_name TO UPPER CASE.

        IF lv_id_type_name EQ lv_temp_type_name.
          lv_id_type_name = lv_nd_type_name.
        ENDIF.
      ENDIF.

      IF lv_id_type_name EQ lv_nd_type_name.
        ASSIGN id_stat_attr TO <lt_stat_attr>.
        IF <lt_stat_attr> IS ASSIGNED.
          io_node->bind_table(
          EXPORTING new_items = <lt_stat_attr> ).
        ENDIF.
      ELSE.
        ed_error_message = ‘Data mismatch against context node’.
      ENDIF.
      FREE lo_tbl_descr.
    ENDIF.
  ENDIF.
ENDMETHOD.

Method: CTX_BIND_STRUCTURE( )

Signature:

bind_str.PNG

Logic:

CTX_BIND_STRUCTURE

METHOD CTX_BIND_STRUCTURE.

  DATA lo_str_descr      TYPE REF TO cl_abap_structdescr.
*  DATA lo_stat_data      TYPE REF TO data.
  DATA lv_id_stat_attr_type_name    TYPE string.
  DATA lv_temp_type_name  TYPE string.
  DATA lv_nd_type_name    TYPE string.
  FIELD-SYMBOLS: <ls_stat_attr>    TYPE any.

  CLEAR: ed_error_message,
        lv_nd_type_name,
        lv_id_stat_attr_type_name,
        lv_temp_type_name.

  IF io_node IS BOUND.
    lv_nd_type_name = id_nd_type_name.
    TRANSLATE lv_nd_type_name TO UPPER CASE.
    “get the type name of data provided
    lo_str_descr ?=
    cl_abap_structdescr=>describe_by_data( id_stat_attr ).
    “check if the data type provided and node type are same
    IF lo_str_descr IS BOUND.
      lv_id_stat_attr_type_name = lo_str_descr->get_relative_name( ).
      “check if the type name is used from context node
      IF lv_id_stat_attr_type_name NE lv_nd_type_name.
        CONCATENATE ‘ELEMENT’ id_node_name
        INTO lv_temp_type_name SEPARATED BY ‘_’.
        TRANSLATE lv_temp_type_name TO UPPER CASE.
        IF lv_id_stat_attr_type_name EQ lv_temp_type_name.
          lv_id_stat_attr_type_name = lv_nd_type_name.
        ENDIF.
      ENDIF.
      IF lv_id_stat_attr_type_name EQ lv_nd_type_name.
        ASSIGN id_stat_attr TO <ls_stat_attr>.
        IF <ls_stat_attr> IS ASSIGNED.
          io_node->set_static_attributes(
          EXPORTING index = 1
                    static_attributes = <ls_stat_attr> ).
        ENDIF.
      ELSE.
        ed_error_message = ‘Data mismatch against context node’.
      ENDIF.
      FREE lo_str_descr.
    ENDIF.
  ENDIF.
ENDMETHOD.

Now, let us see how to bind data to context node by using method SET_CTX_STAT_ATTR( ).

Example:

Set data : Let us say we have a context node MY_NODE_T in component controller context with cardinality 0….n /1..n


DATA lt_my_node_t     TYPE wd_this->elements_my_node_t.
  DATA lv_error_message TYPE string.
 
  CALL METHOD zcl_wd_helper_class=>set_ctx_stat_attr
    EXPORTING
      io_context       = wd_context    " WD Context ref
      id_node_name     = 'MY_NODE_T'    " Ctx Node Name
      id_stat_attr     = lt_my_node_t
*     id_set_dirty     = ABAP_FALSE
    IMPORTING
      ed_error_message = lv_error_message.

Set data : Let us say we have a context node MY_NODE_S in component controller context with cardinality 0…1 /1..1


  DATA ls_my_node_s     TYPE wd_this->elements_my_node_s.
  DATA lv_error_message TYPE string.
 
  CALL METHOD zcl_wd_helper_class=>set_ctx_stat_attr
    EXPORTING
      io_context       = wd_context    " WD Context ref
      id_node_name     = 'MY_NODE_s'    " Ctx Node Name
      id_stat_attr     = ls_my_node_s
*     id_set_dirty     = ABAP_FALSE
    IMPORTING
      ed_error_message = lv_error_message.

Hope this helps for those who are looking for a common logic in place to get and set data from/to context node in WDA applications 🙂 🙂 🙂

I appreciate your comments/feedback 🙂

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Ramakrishnappa,

      Its as informative as first part of series. Keep up good work. Thanks for this informative step by step guide.

      Regards,

      Saurabh

      Author's profile photo Ramakrishnappa Gangappa
      Ramakrishnappa Gangappa
      Blog Post Author

      Hi Saurabh,

      Thank you for going through the document and I appreciate your compliments 🙂

      Happy that you are finding this document as useful.

      I would be looking forward to you as an active member and keep going 😉

      Regards,

      Rama