Hello… Welcome 🙂

While developing complex WDA applications, we may have to have many context nodes defined and its very much required to get and set data from/to these context nodes.

For getting and setting data from/to a context node, we need to follow the below process

  • Define data of type node ( element/elements )
  • Get the node reference by using node names
  • Get the elements using node reference
  • Get data / set data using element reference or node reference based on cardinality i.e. if cardinality is 0…1, 1..1 ( structure ) 0..n, 1…n ( table )

If we have many nodes in our application, say around 10-15 context nodes, we have to repeat above steps for each read/write data access

So, its better idea to have a common method for getting data from context node / for setting data to context node.

HELPER CLASS:

You would be wondering why this HELPER CLASS is needed ?,

It very much helpful to have all common methods in a class and it can be called from any view/ controller method.Hence,it is named as helper class to my application.

I have created the below 2 static methods in class ZCL_WD_HELPER_CLASS

  • GET_CTX_STAT_ATTR(  ) – Get static attributes / table data of context node
  • SET_CTX_STAT_ATTR(  ) – Set static attributes / table to context node

Now, let us look at these methods signature & logic 🙂

Method: GET_CTX_STAT_ATTR(  ).

Signature:

get.PNG

Logic:

GET_CTX_STAT_ATTR

METHOD get_ctx_stat_attr.
  DATA lo_nd                TYPE REF TO if_wd_context_node.
  DATA lo_nd_struct_descr   TYPE REF TO cl_abap_structdescr.
  DATA lo_nd_info           TYPE REF TO if_wd_context_node_info.
  DATA lo_stat_data         TYPE REF TO data.
  DATA lv_node_name         TYPE string.
  DATA lv_type_name         TYPE string.
  DATA lv_index             TYPE i.
  DATA lo_typedescr_target  TYPE REF TO cl_abap_typedescr.

  FIELD-SYMBOLS: <lt_stat_attr>     TYPE STANDARD TABLE,
                 <ls_stat_attr>     TYPE any.
  “—————————————————-
  “Purpose: Get data from context node of given controller context
  “—————————————————-
  CLEAR: ed_error_message,
          ed_stat_attr.

  “===================================

  “check if type of target is table
  lo_typedescr_target =
  cl_abap_typedescr=>describe_by_data( p_data = ed_stat_attr ).
  IF lo_typedescr_target IS NOT BOUND.
    RETURN.
  ENDIF.

  “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.

          ” Return if node is not dirty, and caller wants values only if dirty
          IF ib_only_if_dirty = abap_true AND
             lo_nd->is_changed_by_client( ) = abap_false.
            RETURN.
          ENDIF.

          “get node info
          lo_nd_info = lo_nd->get_node_info( ).
          IF lo_nd_info IS BOUND.

            “get the type descriptor of node
            lo_nd_struct_descr =
            lo_nd_info->get_static_attributes_type( ).
            “check if type name is DDtype
            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 & supplied index,
              ” create data type & get data from context

              IF lo_nd_info->is_multiple( ) EQ abap_true AND
                 id_index = 0.

                “============================================
                “return if target data type is not of kind – table

                IF  lo_typedescr_target->kind NE
                    lo_typedescr_target->kind_table.
                  ed_error_message = ‘Data mismatch against context node’.
                  RETURN.
                ENDIF.

                “create data object based cardinality
                CREATE DATA lo_stat_data TYPE TABLE OF (lv_type_name).

                ASSIGN lo_stat_data->* TO <lt_stat_attr>.

                IF <lt_stat_attr> IS ASSIGNED.
                  lo_nd->get_static_attributes_table(
                   IMPORTING table = <lt_stat_attr> ).

                  ed_stat_attr = <lt_stat_attr>.
                ENDIF.
              ELSE.

                “============================================
                “return if target data type is not of kind – Structure
                IF  lo_typedescr_target->kind NE
                    lo_typedescr_target->kind_struct.
                  ed_error_message = ‘Data mismatch against context node’.
                  RETURN.
                ENDIF.

                “Prepare lv_index, based on id_index
                IF id_index EQ 0.
                  lv_index = 1.
                ELSE.
                  lv_index = id_index.
                ENDIF.

                “create data object based cardinality
                CREATE DATA lo_stat_data TYPE (lv_type_name).

                ASSIGN lo_stat_data->* TO <ls_stat_attr>.

                IF <ls_stat_attr> IS ASSIGNED.
                  lo_nd->get_static_attributes(
                   EXPORTING index = lv_index
                   IMPORTING static_attributes = <ls_stat_attr> ).

                  ed_stat_attr = <ls_stat_attr>.
                ENDIF.
              ENDIF.
            ENDIF.
          ENDIF.
        ELSE.
          ed_error_message = ‘Node does not exist in the provided context’.
        ENDIF.
      CATCH cx_root.                                     “#EC CATCH_ALL
        ##no_handler
        ed_error_message = ‘Error occured during context node read’.
    ENDTRY.
  ELSE.
    ed_error_message = ‘Context is not supplied’.
    RETURN.
  ENDIF.
ENDMETHOD.

Okay, now let us see how to use the method GET_CTX_STAT_ATTR( ) to get data from context node

Example:

Get 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=>get_ctx_stat_attr
    EXPORTING
      io_context       = wd_context
      id_node_name     = 'MY_NODE_T'
*     id_index         = 0
*     ib_only_if_dirty = ABAP_FALSE
    IMPORTING
      ed_stat_attr     = lt_my_node_t
      ed_error_message = lv_error_message.




Get 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->element_my_node_s.
  DATA lv_error_message TYPE string.
  CALL METHOD zcl_wd_helper_class=>get_ctx_stat_attr
    EXPORTING
      io_context       = wd_context
      id_node_name     = 'MY_NODE_S'
*     id_index         = 0
*     ib_only_if_dirty = ABAP_FALSE
    IMPORTING
      ed_stat_attr     = ls_my_node_s
      ed_error_message = lv_error_message.




Continued…….

Custom getter & setter methods in Webdynpro ABAP – Part 2

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply