*&---------------------------------------------------------------------*
*& Report  ZZ_TEST_TDATA_IMPORT
*&
*&---------------------------------------------------------------------*
*&
*& Lokales Programm zum Import der Demodaten für
*& Buch "BW Berechtigungen" von Peter John und Peter Kiener
*& Kein Support!
*&
*&---------------------------------------------------------------------*

REPORT  zz_test_tdata_import.

DATA:
l_r_tdata   TYPE REF TO if_rstt_tdata,
l_r_tunit   TYPE REF TO if_rstt_tdata,
l_r_test    TYPE REF TO cl_rstt_tdata,
l_s_tunit   TYPE rstt_s_tdata_key,
l_t_subtp   TYPE rstt_t_tdatatp,
l_s_subtp   TYPE rstt_s_tdatatp,
l_t_ow      TYPE rstt_t_tdatatp,
l_r_msg     TYPE REF TO cl_rstt_trace_log,
l_tobjnm    TYPE rstt_tobjnm,
l_sx_store  TYPE rstt_sx_tdata_store,
l_langu     TYPE sylangu,
l_sub       TYPE rstt_tdatatp,
l_init      TYPE rstt_flag,
l_end       TYPE rstt_flag,
l_msg       TYPE rstt_string,
l_value     TYPE rstt_char20,
l_val       TYPE rstt_char10,
l_numc(3)   TYPE n,
l_t_res     TYPE rstt_t_text,
l_s_res     TYPE rstt_s_text,
l_s_process TYPE rstt_s_tdata_proc,
l_sx_1      TYPE rstt_sx_tdata_load,
l_sx_2      TYPE rstt_sx_tdata_sel,
l_t_langu   TYPE cl_rstt_tdata=>nt_t_langu.
FIELD-SYMBOLS:
<l_s_res>   TYPE rstt_s_text,
<l_s_lan>   TYPE ANY,
<l_langu>   TYPE ANY.


START-OF-SELECTION.

  cl_rstt_tdata_iobj=>ignore_time_chars_hier( 'X' ).

  CALL METHOD cl_rstt_tdata=>set_transfer_direction
    EXPORTING
      i_source = cl_rstt_tdata=>n_c_transtp-client
      i_target = cl_rstt_tdata=>n_c_transtp-object.

  l_t_subtp = cl_rstt_tdata=>get_all_tdatatp( ).

  cl_rstt_tdata=>modify_file_setting(
    EXPORTING
      i_filetp_mode = cl_rstt_tdata=>n_c_filetp_mode-tdatatp ).

  l_s_process-overwrite = 'X'.
  l_s_process-loadmetadata = 'X'.

  CLEAR l_end.
  CALL METHOD cl_rstt_tdata=>create
    EXPORTING
      i_tdatatp = cl_rstt_tdata=>n_c_tdatatp-cube
      i_objnm   = 'ZFC_C04'
      i_t_subtp = l_t_subtp
    IMPORTING
      e_r_tdata = l_r_tdata.

  DO.
    IF l_end = abap_true.
      EXIT.
    ENDIF.

    l_sx_1-requsplit = 'X'.
    l_sx_1-requstate = 'X'.

    CALL METHOD l_r_tdata->load
      EXPORTING
        i_source              = cl_rstt_tdata=>n_c_transtp-client
        i_sx_loadparam        = l_sx_1
        i_sx_selparam         = l_sx_2
        i_init_load           = l_init
        i_s_process           = l_s_process
*       i_next                = RS_C_TRUE
      IMPORTING
        e_end_of_data         = l_end
      CHANGING
        c_r_tdata             = l_r_tunit
        c_r_msg               = l_r_msg
      EXCEPTIONS
        tdata_not_found       = 1
        invalid_input         = 2
        no_data_loaded        = 3
        target_system_invalid = 4
        invalid_object_state  = 5
        folder_not_valid      = 6
        rfc_no_connection     = 7
        OTHERS                = 8.
    CASE sy-subrc.
      WHEN 0.
      WHEN 1.
        MESSAGE i003(rstt0) WITH 'Testobjekt nicht gefunden!'.
        EXIT.
      WHEN 2.
        MESSAGE i003(rstt0) WITH 'Ungültige Eingabe!'.
        EXIT.
      WHEN 3.
        MESSAGE i003(rstt0) WITH 'Keine Daten geladen. Weitermachen!'.
        CONTINUE.
      WHEN 4.
        MESSAGE i003(rstt0) WITH 'Quellsystemangabe ungültig!'.
        EXIT.
      WHEN 5.
        MESSAGE i003(rstt0) WITH 'Objektzustand unzulässig. Weitermachen!'.
        CONTINUE.
      WHEN 6.
        MESSAGE i003(rstt0) WITH 'Falsche Ordnerangabe !!'.
        EXIT.
      WHEN 7.
        MESSAGE i003(rstt0) WITH 'RFC-Verbindung nicht korrekt oder kaputt!!'.
        EXIT.
      WHEN OTHERS.
        MESSAGE i003(rstt0) WITH 'Unbekannter Fehler. '.
        EXIT.

    ENDCASE.


    IF l_r_tunit IS BOUND.
      l_sx_store-storemode = '1'.

      CALL METHOD l_r_tunit->store
        EXPORTING
          i_target             = cl_rstt_tdata=>n_c_transtp-object
          i_s_process          = l_s_process
          i_sx_storeparam      = l_sx_store
        EXCEPTIONS
          no_authorization     = 1
          activation_error     = 2
          db_modify_error      = 3
          tdata_not_found      = 4
          invalid_object_state = 5
          invalid_input        = 6
          OTHERS               = 7.
      IF sy-subrc <> 0.
        l_msg = sy-subrc.
        CONDENSE l_msg.
        CONCATENATE 'ERROR:' l_msg INTO l_msg SEPARATED BY space.
        WRITE / l_msg.
        MESSAGE i003(rstt0) WITH l_msg.
        EXIT.
      ENDIF.

      CALL METHOD l_r_tunit->get_tunit_data
        IMPORTING
          e_s_tunit = l_s_tunit.

      CONCATENATE l_s_tunit-tdatatp l_s_tunit-objnm
        INTO l_msg SEPARATED BY '---'.
      WRITE / l_msg.
      CLEAR l_init.

*       collect type stats
      l_value = l_s_tunit-tdatatp.
      l_val   = l_s_tunit-tdatatp.

      READ TABLE l_t_res
        ASSIGNING <l_s_res>
        WITH KEY value = l_value.

      IF sy-subrc = 0.
        ADD 1 TO <l_s_res>-posino.
      ELSE.
        l_s_res-posino = 1.
        l_s_res-text   = cl_rstt_text_services=>get_text_of_domain(
                              i_doma_nm = 'RSTT_TDATATP'
                              i_text_id = l_val ).
        l_s_res-value  = l_value.
        APPEND l_s_res TO l_t_res.
      ENDIF.

    ELSE.
      IF l_end = rs_c_false.
        CONCATENATE 'l_r_tunit Not bound!' l_end
          INTO l_msg SEPARATED BY '---'.
      ELSE.
        l_msg = 'End of data transfer reached. Find logs in SLG1 ->RSTT/ARCHIVE.'.
        WRITE / ''.
      ENDIF.
      WRITE / l_msg.
    ENDIF.

  ENDDO.


  WRITE / '*******************************************************************'.
  WRITE / 'Summary (amount of items):'.

  LOOP AT l_t_res INTO l_s_res.
    l_numc = l_s_res-posino.
    CONCATENATE '(' l_s_res-text '):' INTO l_msg.
    CONCATENATE 'Type' l_s_res-value l_msg l_numc
      INTO l_msg SEPARATED BY space.
    WRITE / l_msg.
  ENDLOOP.


* finish processing *********************************************************************
  cl_rstt_tdata=>execute_final_process( cl_rstt_tdata=>n_c_transtp-object ).

  cl_rstt_tdata=>store_messages( ).