*&---------------------------------------------------------------------*
*&  Include           ZDB_DOWNLOAD_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: data2csv
*-----------------------------------------------------------------------
FORM data2csv USING
                value(i_tabname)
              CHANGING
                ct_csv TYPE it_string.
  DATA:
  lt_dd03l TYPE STANDARD TABLE OF dd03l,
  ls_dd02l TYPE dd02l,
  l_fieldname TYPE fieldname,
  l_position TYPE tabfdpos,
  l_value TYPE string,
  l_csv TYPE string,
  l_len TYPE i,
  l_length TYPE string,
  ls_fs TYPE REF TO data.

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

  REFRESH ct_csv.

  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'.

  LOOP AT lt_dd03l ASSIGNING <ls_dd03l>.
    IF <ls_dd03l>-fieldname+0(1) = '.'.
      CONTINUE.
    ENDIF.
    IF NOT l_csv IS INITIAL.
      CONCATENATE l_csv ';' INTO l_csv.
    ENDIF.
    l_len = <ls_dd03l>-intlen.
    l_length = l_len.
    CONDENSE l_length.
    CONCATENATE l_csv '"' <ls_dd03l>-fieldname
      '(' <ls_dd03l>-inttype l_length ')"'
      INTO l_csv.
  ENDLOOP.
  APPEND l_csv TO ct_csv.

*content
  CREATE DATA ls_fs TYPE (i_tabname).
  ASSIGN ls_fs->* TO <ls_fs>.
  SELECT * FROM (i_tabname) INTO <ls_fs>.
    CLEAR l_csv.
    LOOP AT lt_dd03l ASSIGNING <ls_dd03l>.
      CLEAR l_value.
      IF <ls_dd03l>-fieldname+0(1) = '.'.
        CONTINUE.
      ENDIF.

      IF NOT <ls_dd03l>-datatype = 'RAWSTRING'.
        ASSIGN COMPONENT <ls_dd03l>-fieldname OF STRUCTURE <ls_fs>
            TO <l_value>.
        l_value = <l_value>.
      ENDIF.

      IF NOT l_csv IS INITIAL.
        CONCATENATE l_csv ';' INTO l_csv.
      ENDIF.
      CONCATENATE l_csv '"' l_value '"' INTO l_csv.
    ENDLOOP.
    APPEND l_csv TO ct_csv.
    IF sy-dbcnt = g_add_db_max_lines.
      EXIT.
    ENDIF.
  ENDSELECT.

ENDFORM.                    "select_data2csv


*-----------------------------------------------------------------------
* form: data2xml
*-----------------------------------------------------------------------
FORM data2xml USING
                value(i_tabname)
              CHANGING
                ct_xml TYPE it_string.
  DATA:
  lt_dd03l TYPE STANDARD TABLE OF dd03l,
  ls_dd03t TYPE dd03t,
  ls_dd02l TYPE dd02l,
  ls_dd02t TYPE dd02t,
  l_fieldname(30) TYPE c,
  l_position TYPE tabfdpos,
  l_value TYPE string,
  l_xml TYPE string,
  ls_fs TYPE REF TO data.

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

  REFRESH ct_xml.

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

  APPEND '<?xml version="1.0" encoding="UTF-8" ?>' TO ct_xml.
  APPEND '<db>' TO ct_xml.
  APPEND '<definition><table>' TO ct_xml.
  SELECT SINGLE * FROM dd02l INTO ls_dd02l
    WHERE tabname = i_tabname
    AND as4local = 'A'.
  SELECT fieldname position FROM dd03l INTO (l_fieldname, l_position)
      WHERE tabname = 'DD02L'
      ORDER BY position.
    ASSIGN COMPONENT l_fieldname OF STRUCTURE ls_dd02l
        TO <l_value>.
    IF NOT <l_value> IS INITIAL.
      l_value = <l_value>.
      PERFORM encode_html CHANGING l_value.
      TRANSLATE l_fieldname TO LOWER CASE.
      CONCATENATE
        '<' l_fieldname ' value="' l_value '"/>'
        INTO l_xml.
      APPEND l_xml TO ct_xml.
    ENDIF.
  ENDSELECT.

  SELECT * FROM dd02t INTO ls_dd02t
      WHERE tabname = i_tabname
      AND as4local = 'A'.
    CONCATENATE
      '<text language="' ls_dd02t-ddlanguage '">'
      ls_dd02t-ddtext
      '</text>'
      INTO l_xml.
    APPEND l_xml TO ct_xml.
  ENDSELECT.

  APPEND '</table>' TO ct_xml.

  LOOP AT lt_dd03l ASSIGNING <ls_dd03l>.
    IF <ls_dd03l>-fieldname+0(1) = '.'.
      CONTINUE.
    ENDIF.
    APPEND '<col>' TO ct_xml.
    SELECT fieldname position FROM dd03l INTO (l_fieldname, l_position)
        WHERE tabname = 'DD03L'
        AND fieldname <> 'TABNAME'
        ORDER BY position.
      ASSIGN COMPONENT l_fieldname OF STRUCTURE <ls_dd03l>
        TO <l_value>.
      l_value = <l_value>.
      IF NOT <l_value> IS INITIAL.
        PERFORM encode_html CHANGING l_value.
        TRANSLATE l_fieldname TO LOWER CASE.
        CONCATENATE
            '<' l_fieldname '>' l_value '</' l_fieldname '>'
            INTO l_xml.
        APPEND l_xml TO ct_xml.
      ENDIF.
    ENDSELECT.
    SELECT * FROM dd03t INTO ls_dd03t
        WHERE tabname = <ls_dd03l>-tabname
        AND fieldname = <ls_dd03l>-fieldname
        AND as4local = 'A'.
      CONCATENATE
        '<text language="' ls_dd03t-ddlanguage '">'
        ls_dd03t-ddtext
        '</text>'
        INTO l_xml.
      APPEND l_xml TO ct_xml.
    ENDSELECT.
    APPEND '</col>' TO ct_xml.
  ENDLOOP.

  APPEND '</definition>' TO ct_xml.

*content
  APPEND '<lines>' TO ct_xml.

  CREATE DATA ls_fs TYPE (i_tabname).
  ASSIGN ls_fs->* TO <ls_fs>.
  SELECT * FROM (i_tabname) INTO <ls_fs>.
    CLEAR l_xml.

    APPEND '<line>' TO ct_xml.

    LOOP AT lt_dd03l ASSIGNING <ls_dd03l>.
      IF <ls_dd03l>-fieldname+0(1) = '.'.
        CONTINUE.
      ENDIF.
      CLEAR l_value.
      IF NOT <ls_dd03l>-datatype = 'RAWSTRING'.
        ASSIGN COMPONENT <ls_dd03l>-fieldname OF STRUCTURE <ls_fs>
            TO <l_value>.
        l_value = <l_value>.
        PERFORM encode_html CHANGING l_value.
      ENDIF.

      IF NOT <l_value> IS INITIAL.
        IF <ls_dd03l>-fieldname(1) = '.'.
          <ls_dd03l>-fieldname(1) = '_'.
        ENDIF.
        CONCATENATE
            '<cell name="' <ls_dd03l>-fieldname '">'
            l_value
            '</cell>' INTO l_xml.
        APPEND l_xml TO ct_xml.
      ENDIF.
    ENDLOOP.
    APPEND '</line>' TO ct_xml.
    IF sy-dbcnt = g_add_db_max_lines.
      EXIT.
    ENDIF.
  ENDSELECT.
  APPEND '</lines></db>' TO ct_xml.

ENDFORM.                    "select_data2xml
