*&---------------------------------------------------------------------*
*&  Include           ZDB_UPLOAD_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.
* File
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) t_file.
PARAMETERS p_file LIKE rlgrap-filename MEMORY ID mfile.
SELECTION-SCREEN END OF LINE.
* Database
SELECTION-SCREEN ULINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) t_db.
PARAMETERS p_db LIKE dd02l-tabname MEMORY ID mfile.
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_clear.
PARAMETERS p_clear AS CHECKBOX DEFAULT abap_true.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK b2.


*-----------------------------------------------------------------------
* Display a file picker window
*-----------------------------------------------------------------------
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  DATA:
  s_obj_file TYPE REF TO cl_gui_frontend_services,
  st_picked_file TYPE filetable,
  ss_picked_file TYPE file_table,
  s_initial_file TYPE string,
  s_rc TYPE i.

  IF sy-batch IS INITIAL.
    CREATE OBJECT s_obj_file.

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

    CALL METHOD s_obj_file->file_open_dialog
      CHANGING
        file_table = st_picked_file
        rc         = s_rc
      EXCEPTIONS
        OTHERS     = 1.

    IF sy-subrc = 0.
      READ TABLE st_picked_file INTO ss_picked_file INDEX 1.
      p_file = ss_picked_file-filename.
    ELSE.
      WRITE: / text-001.
    ENDIF.
  ENDIF.


*-----------------------------------------------------------------------
*  Initialization
*-----------------------------------------------------------------------
INITIALIZATION.
* screen texts
  t_choice = 'Choice'.
  t_clear  = 'Clear old DB content'.
  t_db     = 'Database'.
  t_file   = 'File'.
  t_opts   = 'Options'.


*-----------------------------------------------------------------------
* start-of-selection
*-----------------------------------------------------------------------
START-OF-SELECTION.
  CLEAR:
  g_file,
  g_tabname.

  IF p_db IS INITIAL.
    WRITE: / text-005.
  ELSEIF p_file IS INITIAL.
    WRITE: / text-006.
  ELSE.
*DB-Table
    g_tabname = p_db.
    SELECT SINGLE tabname FROM dd02l INTO g_tabname
        WHERE tabname = g_tabname
        AND as4local = 'A'
        AND tabclass = 'TRANSP'.
    IF sy-subrc <> 0.
      CLEAR g_tabname.
    ENDIF.
*Other Parameters
    g_file = p_file.
    g_clear = p_clear.
  ENDIF.

END-OF-SELECTION.

  IF NOT g_tabname IS INITIAL.
    PERFORM start_upload.

    WRITE: 'Database table has been uploaded successfully.'.
  ENDIF.


*-----------------------------------------------------------------------
* form: start_upload
*-----------------------------------------------------------------------
FORM start_upload.
  DATA:
  l_it_string TYPE it_string.

  IF g_clear = abap_true.
    DELETE FROM (g_tabname).
  ENDIF.

  PERFORM upload CHANGING l_it_string.

  PERFORM csv2data USING g_tabname l_it_string.

ENDFORM.                    "start_upload
