*&---------------------------------------------------------------------*
*& Report  ZDB_UPLOAD                                                  *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& This program 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.                                      *
*&                                                                     *
*& This program 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 this program. If not, see <http://www.gnu.org/licenses/>.*
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&  Author:     Ruediger von Creytz     ruediger.creytz@globalbit.net  *
*&  Copyright:  globalBIT, LLC          http://www.globalbit.net       *
*&  Version:    20210302                                               *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  zdb_upload.

*&---------------------------------------------------------------------*
*&  Include           ZDB_UPLOAD_VARIABLES                             *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& 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       *
*&                                                                     *
*&---------------------------------------------------------------------*


TYPE-POOLS:
abap.

TYPES:
it_string TYPE STANDARD TABLE OF string.

DATA:
g_clear TYPE flag,
g_file TYPE string,
g_tabname TYPE tabname.

TABLES:
dd02l,
tdevc.
*&---------------------------------------------------------------------*
*&  Include           ZDB_UPLOAD_CONVERT_DATA                          *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& 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       *
*&                                                                     *
*&---------------------------------------------------------------------*


*-----------------------------------------------------------------------
* form: csv2data
*-----------------------------------------------------------------------
FORM csv2data USING
                value(i_tabname) TYPE tabname
                it_csv TYPE it_string.
  DATA:
  lt_dd03l TYPE STANDARD TABLE OF dd03l,
  ls_dd02l TYPE dd02l,
  l_fieldname TYPE fieldname,
  l_position TYPE tabfdpos,
  lt_value TYPE it_string,
  l_value TYPE string,
  l_csv TYPE string,
  l_len TYPE i,
  l_cnt TYPE i,
  l_length TYPE string,
  l_quot TYPE flag,
  ls_fs TYPE REF TO data.

  FIELD-SYMBOLS:
  <ls_csv> TYPE string,
  <ls_dd03l> TYPE dd03l,
  <ls_fs> TYPE ANY,
  <l_fs> TYPE ANY,
  <l_value> TYPE ANY.

  SELECT * FROM dd03l INTO TABLE lt_dd03l
      WHERE tabname = i_tabname
      AND as4local = 'A'
      ORDER BY position.

  SELECT SINGLE * FROM dd02l INTO ls_dd02l
      WHERE tabname = i_tabname
      AND as4local = 'A'.

*content
  CREATE DATA ls_fs TYPE (i_tabname).
  ASSIGN ls_fs->* TO <ls_fs>.

  LOOP AT it_csv ASSIGNING <ls_csv>.
    IF sy-tabix = 1.
      CONTINUE.
    ENDIF.

    CLEAR l_cnt.

    SPLIT <ls_csv> AT ';' INTO TABLE lt_value.
    LOOP AT lt_value ASSIGNING <l_value>.
      IF l_quot = abap_true.
        CONCATENATE l_value ';' <l_value> INTO l_value.
      ELSE.
        l_value = <l_value>.
        IF l_value <> space AND l_value+0(1) = '"'.
          l_quot = abap_true.
          l_value = l_value+1.
        ENDIF.
      ENDIF.

      IF l_quot = abap_true.
        l_len = strlen( l_value ).
        IF l_len > 0.
          SUBTRACT 1 FROM l_len.
          IF l_value+l_len(1) = '"'.
            l_value = l_value+0(l_len).
            l_quot = abap_false.
          ENDIF.
        ENDIF.
      ENDIF.

      IF l_quot = abap_false.
        ADD 1 TO l_cnt.
        READ TABLE lt_dd03l ASSIGNING <ls_dd03l> INDEX l_cnt.
        IF sy-subrc <> 0.
*          message 'Cannot import data'.
          EXIT.
        ENDIF.

        IF <ls_dd03l>-fieldname <> 'MANDT'.
          ASSIGN COMPONENT <ls_dd03l>-fieldname OF STRUCTURE <ls_fs>
            TO <l_fs>.
          <l_fs> = l_value.
        ENDIF.
      ENDIF.
    ENDLOOP.

    MODIFY (i_tabname) FROM <ls_fs>.
    CLEAR <ls_fs>.

  ENDLOOP.

ENDFORM.                    "select_csv2data
*&---------------------------------------------------------------------*
*&  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
*&---------------------------------------------------------------------*
*&  Include           ZDB_UPLOAD_DO                                    *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& 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       *
*&                                                                     *
*&---------------------------------------------------------------------*


*-----------------------------------------------------------------------
* form: upload
*-----------------------------------------------------------------------
FORM upload CHANGING
              ct_string TYPE it_string.

  DATA:
  l_infoline(80),
  l_uri TYPE string.

  REFRESH ct_string.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename = g_file
    TABLES
      data_tab = ct_string
    EXCEPTIONS
      OTHERS   = 1.

ENDFORM.                    "upload
