*&---------------------------------------------------------------------*
*&  Include           ZDB_DOWNLOAD_GUI                                 *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& This file is part of ZDB.                                           *
*&                                                                     *
*& ZDB_DOWNLOAD is free software: you can redistribute it and/or       *
*& modify it under the terms of the GNU General Public License as      *
*& published by the Free Software Foundation, either version 3 of the  *
*& License, or any later version.                                      *
*&                                                                     *
*& ZDB_DOWNLOAD is distributed in the hope that it will be useful,     *
*& but WITHOUT ANY WARRANTY; without even the implied warranty of      *
*& MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
*& GNU General Public License for more details.                        *
*&                                                                     *
*& You should have received a copy of the GNU General Public License   *
*& along with ZDOWNLOAD. If not, see <http://www.gnu.org/licenses/>.   *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&  Author:     Ruediger von Creytz     ruediger.creytz@globalbit.net  *
*&  Copyright:  globalBIT, LLC          http://www.globalbit.net       *
*&                                                                     *
*&---------------------------------------------------------------------*


*-----------------------------------------------------------------------
*  Selection screen declaration
*-----------------------------------------------------------------------
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE t_choice.
* Package
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(22) t_pack.
SELECT-OPTIONS p_pack FOR tdevc-devclass.
SELECTION-SCREEN END OF LINE.
* Database
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(22) t_db.
SELECT-OPTIONS p_db FOR dd02l-tabname.
SELECTION-SCREEN END OF LINE.
*Folder
SELECTION-SCREEN ULINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) t_path.
PARAMETERS: p_folder LIKE rlgrap-filename MEMORY ID mfolder.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK b1.

SELECTION-SCREEN: BEGIN OF BLOCK b2 WITH FRAME TITLE t_opts.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) t_format.
PARAMETERS p_csv AS CHECKBOX DEFAULT abap_true.
SELECTION-SCREEN COMMENT 29(15) t_csv.
PARAMETERS p_xml AS CHECKBOX DEFAULT abap_true.
SELECTION-SCREEN COMMENT 47(15) t_xml.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) t_max.
PARAMETERS p_max TYPE i DEFAULT 500.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK b2.


*-----------------------------------------------------------------------
* Display a directory picker window
*-----------------------------------------------------------------------
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_folder.

  DATA:
  s_obj_file TYPE REF TO cl_gui_frontend_services,
  s_picked_folder TYPE string,
  s_initial_folder TYPE string.

  IF sy-batch IS INITIAL.
    CREATE OBJECT s_obj_file.

    IF NOT p_folder IS INITIAL.
      s_initial_folder = p_folder.
    ELSE.
      CALL METHOD s_obj_file->get_temp_directory
        CHANGING
          temp_dir = s_initial_folder
        EXCEPTIONS
          OTHERS   = 1.
    ENDIF.

    CALL METHOD s_obj_file->directory_browse
      EXPORTING
        initial_folder  = s_initial_folder
      CHANGING
        selected_folder = s_picked_folder
      EXCEPTIONS
        OTHERS          = 1.

    IF sy-subrc = 0.
      p_folder = s_picked_folder.
    ELSE.
      WRITE: / text-001.
    ENDIF.
  ENDIF.


*-----------------------------------------------------------------------
*  Initialization
*-----------------------------------------------------------------------
INITIALIZATION.
* screen texts
  t_choice = 'Choice'.
  t_csv    = gc_extension_csv.
  t_db     = 'Database'.
  t_format = 'Format'.
  t_max    = 'Max. no. of lines'.
  t_opts   = 'Options'.
  t_pack   = 'Package'.
  t_path   = 'Directory'.
  t_xml    = gc_extension_xml.


*-----------------------------------------------------------------------
* start-of-selection
*-----------------------------------------------------------------------
START-OF-SELECTION.
  DATA:
  lt_tadir TYPE STANDARD TABLE OF tadir,
  l_tabname TYPE tabname.

  FIELD-SYMBOLS:
  <ls_tadir> TYPE tadir.

  CLEAR:
  g_folder,
  g_add_db_format_csv,
  g_add_db_format_xml.

  g_add_db_max_lines = 500.

  REFRESH:
  gt_tadir.

  IF p_pack IS INITIAL AND p_db IS INITIAL.
    WRITE: / text-005.
  ELSEIF p_folder IS INITIAL.
    WRITE: / text-006.
  ELSEIF p_csv IS INITIAL AND p_xml IS INITIAL.
    WRITE: / text-014.
  ELSE.
*DB-Tables
    IF NOT p_pack IS INITIAL
    AND NOT p_db IS INITIAL.
      SELECT * FROM tadir
          INTO TABLE lt_tadir
          WHERE pgmid = 'R3TR'
          AND object = 'TABL'
          AND obj_name IN p_db
          AND devclass IN p_pack.
    ELSEIF NOT p_pack IS INITIAL.
      SELECT * FROM tadir
          INTO TABLE lt_tadir
          WHERE pgmid = 'R3TR'
          AND object = 'TABL'
          AND devclass IN p_pack.
    ELSEIF NOT p_db IS INITIAL.
      SELECT * FROM tadir
          INTO TABLE lt_tadir
          WHERE pgmid = 'R3TR'
          AND object = 'TABL'
          AND obj_name IN p_db.
    ENDIF.
    LOOP AT lt_tadir ASSIGNING <ls_tadir>.
      l_tabname = <ls_tadir>-obj_name.
      SELECT SINGLE tabname FROM dd02l INTO l_tabname
          WHERE tabname = l_tabname
          AND as4local = 'A'
          AND tabclass = 'TRANSP'.
      IF sy-subrc = 0.
        APPEND <ls_tadir> TO gt_tadir.
      ENDIF.
    ENDLOOP.
*Other Parameters
    g_folder = p_folder.
    g_add_db_format_csv = p_csv.
    g_add_db_format_xml = p_xml.
    g_add_db_max_lines = p_max.
  ENDIF.

END-OF-SELECTION.

  IF NOT gt_tadir IS INITIAL.
    PERFORM start_download.

    WRITE: 'Database tables have been downloaded successfully.'.
  ENDIF.


*-----------------------------------------------------------------------
* form: start_download
*-----------------------------------------------------------------------
FORM start_download.
  DATA:
  l_tabname TYPE tabname,
  lt_string TYPE it_string.

  FIELD-SYMBOLS:
  <ls_tadir> TYPE tadir.

  LOOP AT gt_tadir ASSIGNING <ls_tadir>.
    l_tabname = <ls_tadir>-obj_name.
    IF g_add_db_format_csv = abap_true.
      PERFORM data2csv
        USING l_tabname
        CHANGING lt_string.
      PERFORM download
        USING
          <ls_tadir>-devclass
          l_tabname
          gc_extension_csv
          lt_string.
    ENDIF.
    IF g_add_db_format_xml = abap_true.
      PERFORM data2xml
        USING l_tabname
        CHANGING lt_string.
      PERFORM download
        USING
          <ls_tadir>-devclass
          l_tabname
          gc_extension_xml
          lt_string.
    ENDIF.
  ENDLOOP.

ENDFORM.                    "start_download
