SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Wrapper code for creating Dynamic Internal table using RTTS



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sat Sep 13, 2008 5:49 pm    Post subject: Wrapper code for creating Dynamic Internal table using RTTS Reply with quote

Author Sourav Bhaduri

Code:
REPORT ztest_sourav8.

* Step 1: Declare a field symbol,which will be assigned the dynamically created table

FIELD-SYMBOLS:
<f_tab> TYPE STANDARD TABLE.  " table type

* Step 2: Copy Paste the class defination and implementation
*----------------------------------------------------------------------*
*       CLASS lcl_cl DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_cl DEFINITION CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
          create_elements
           IMPORTING
             fname TYPE string      " Field Name
             ftype TYPE char10      " Field Type
             nonddic TYPE char1     " Flag for non DDIC fields
             flen  TYPE i     " Field Length(Only for elementary types)
             fdec  TYPE i       " Decimal (Only for elementary types)
           EXCEPTIONS error,
       create_dynamic_table
           EXCEPTIONS error.
  PROTECTED SECTION.
  PRIVATE SECTION.
    TYPE-POOLS abap.
    TYPES:
      BEGIN OF x_table_elements,
      fname TYPE string,      " Field Name
      ftype TYPE char10,      " Field Type
      nonddic TYPE char1,     " Flag for non DDIC fields
  flen  TYPE i,           " Field Length(Only for elementary data types)
      fdec  TYPE i,           " Decimal (Only for elementary data types)
      END OF x_table_elements.
    CLASS-DATA: wa_table_elements TYPE x_table_elements,
    i_table_elements TYPE STANDARD TABLE OF x_table_elements
       INITIAL SIZE 0.
ENDCLASS.                    "lcl_cl DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_cl IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_cl IMPLEMENTATION.
  METHOD create_elements.
    IF nonddic = 'X' AND
      ( ftype NA 'CPIXNTF' OR
       ftype = 'STRING' OR
       ftype = 'XSTRING' ).
      RAISE error.
    ENDIF.
    wa_table_elements-fname    = fname.
    wa_table_elements-ftype    = ftype.
    wa_table_elements-nonddic  = nonddic.
    wa_table_elements-flen     = flen.
    wa_table_elements-fdec     = fdec.
    APPEND wa_table_elements TO i_table_elements.
  ENDMETHOD.                    "create_elements
  METHOD create_dynamic_table.
    DATA:
    l_dynamic_type  TYPE REF TO cl_abap_structdescr,
    l_dynamic_table TYPE REF TO cl_abap_tabledescr,
    l_handle        TYPE REF TO data,
    l_i_table_components  TYPE abap_component_tab,
    l_wa_table_components TYPE abap_componentdescr,
    l_type          TYPE REF TO cl_abap_typedescr,
    l_type_kind     TYPE abap_typekind,
    l_length        TYPE i,
    l_decimals      TYPE i,
    l_typekind      TYPE char10.
    FIELD-SYMBOLS: <l_f1> TYPE x_table_elements.
    LOOP AT i_table_elements ASSIGNING <l_f1>.
      CLEAR l_wa_table_components.
      l_wa_table_components-name = <l_f1>-fname.
      IF <l_f1>-nonddic = space.
        l_type = cl_abap_typedescr=>describe_by_name( <l_f1>-ftype ).
        l_typekind = l_type_kind = l_type->type_kind.
        l_length        = l_type->length.
        l_decimals      = l_type->decimals.
      ELSE.
        l_typekind     = <l_f1>-ftype.
        l_length        = <l_f1>-flen.
        l_decimals      = <l_f1>-fdec.
      ENDIF.
      CASE l_typekind.
        WHEN 'C'.
          TRY.
              CALL METHOD cl_abap_elemdescr=>get_c
                EXPORTING
                  p_length = l_length
                RECEIVING
                  p_result = l_wa_table_components-type.
            CATCH cx_parameter_invalid_range .
              RAISE error.
          ENDTRY.
        WHEN 'P'.
          TRY.
              CALL METHOD cl_abap_elemdescr=>get_p
                EXPORTING
                  p_length   = l_length
                  p_decimals = l_decimals
                RECEIVING
                  p_result   = l_wa_table_components-type.
            CATCH cx_parameter_invalid_range .
              RAISE error.
          ENDTRY.
        WHEN 'I'.
          CALL METHOD cl_abap_elemdescr=>get_i
            RECEIVING
              p_result = l_wa_table_components-type.
        WHEN 'STRING' OR 'g'.
          CALL METHOD cl_abap_elemdescr=>get_string
            RECEIVING
              p_result = l_wa_table_components-type.
        WHEN 'XSTRING' OR 'y'.
          CALL METHOD cl_abap_elemdescr=>get_xstring
            RECEIVING
              p_result = l_wa_table_components-type.
        WHEN 'X'.
          TRY.
              CALL METHOD cl_abap_elemdescr=>get_x
                EXPORTING
                  p_length = l_length
                RECEIVING
                  p_result = l_wa_table_components-type.
            CATCH cx_parameter_invalid_range .
              RAISE error.
          ENDTRY.
        WHEN 'N'.
          TRY.
              CALL METHOD cl_abap_elemdescr=>get_n
                EXPORTING
                  p_length = l_length
                RECEIVING
                  p_result = l_wa_table_components-type.
            CATCH cx_parameter_invalid_range .
              RAISE error.
          ENDTRY.
        WHEN 'T'.
          CALL METHOD cl_abap_elemdescr=>get_t
            RECEIVING
              p_result = l_wa_table_components-type.
        WHEN 'F'.
          CALL METHOD cl_abap_elemdescr=>get_f
            RECEIVING
              p_result = l_wa_table_components-type.
      ENDCASE.
      APPEND l_wa_table_components TO l_i_table_components.
    ENDLOOP.
    IF l_i_table_components[] IS NOT INITIAL.
      TRY.
          l_dynamic_type =
             cl_abap_structdescr=>create( l_i_table_components ).
        CATCH cx_sy_struct_creation.
          RAISE error.
      ENDTRY.
      TRY.
          l_dynamic_table =
             cl_abap_tabledescr=>create( l_dynamic_type ).
        CATCH cx_sy_table_creation.
          RAISE error.
      ENDTRY.
      TRY.
          CREATE DATA l_handle TYPE HANDLE l_dynamic_table.
        CATCH cx_sy_create_data_error.
          RAISE error.
      ENDTRY.
      ASSIGN l_handle->* TO <f_tab>.
      IF sy-subrc <> 0.
        RAISE error.
      ENDIF.
    ELSE.
      RAISE error.
    ENDIF.
  ENDMETHOD.                    "create_dynamic_table
ENDCLASS.                    "lcl_cl IMPLEMENTATION

START-OF-SELECTION.
* Step 3: Call the methods CREATE_ELEMENTS for each field in the required table and pass the parameter values a required
  lcl_cl=>create_elements(
    EXPORTING
      fname   = 'EMPID' " Field Name
      ftype   = 'N'     " Field Type
      nonddic = 'X'     " Flag for non DDIC fields
      flen    = '12'    " Field Length(Only for elementary data types)
      fdec    = ''      " Decimal (Only for elementary data types)
    EXCEPTIONS
      error   = 1 ).
  lcl_cl=>create_elements(
    EXPORTING
      fname   = 'USERID'   " Field Name
      ftype   = 'SYUNAME'  " Field Type
      nonddic = ''    " Flag for non DDIC fields
      flen    = ''    " Field Length(Only for elementary data types)
      fdec    = ''    " Decimal (Only for elementary data types)
    EXCEPTIONS
      error   = 1 ).
  lcl_cl=>create_elements(
    EXPORTING
      fname   = 'WEIGHT'   " Field Name
      ftype   = 'P'        " Field Type
      nonddic = 'X'    " Flag for non DDIC fields
      flen    = '6'    " Field Length(Only for elementary data types)
      fdec    = '2'    " Decimal (Only for elementary data types)
    EXCEPTIONS
      error   = 1 ).

  IF sy-subrc = 0.
* Step 4: Call the method CREATE_DYNAMIC_TABLE of the class LCL_CL to create the table
    lcl_cl=>create_dynamic_table(
      EXCEPTIONS
        error     = 1 ).
  ENDIF.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.