*&---------------------------------------------------------------------*
*& Report  ZSOFTCOPY_MAKE                                              *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& 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  zsoftcopy_make.


INCLUDE zsoftcopy_types.

DATA:
gt_src TYPE it_abapsource.

CONSTANTS:
gc_separator TYPE c VALUE ';',
gc_include_csv_name TYPE progname VALUE 'ZSOFTCOPY_CSV',
gc_include_csv_title TYPE repti VALUE 'SoftCopy - CSV',
gc_include_txt_name TYPE progname VALUE 'ZSOFTCOPY_TEXT',
gc_include_txt_title TYPE repti VALUE 'SoftCopy - TEXT'.


PERFORM make.


INCLUDE zutil_convert_text.


*-----------------------------------------------------------------------
* make
*-----------------------------------------------------------------------
FORM make.

  DATA:
  l_subrc_csv TYPE string,
  l_subrc_txt TYPE string,
  l_infoline(80) TYPE c.

  PERFORM add_report_begin
    USING gc_include_csv_name
    CHANGING gt_src.

  PERFORM csv_table_structure.
  PERFORM csv_tabstrip.
  PERFORM csv_text_elements
    CHANGING
      l_subrc_txt.
  PERFORM csv_varid_broken.

  PERFORM add_report_end
    CHANGING gt_src.

  PERFORM save
    USING
      gc_include_csv_name
      gc_include_txt_title
      gt_src
    CHANGING
      l_subrc_csv.

  IF l_subrc_csv = 0 AND l_subrc_txt = 0.
    CONCATENATE
      'created ZSOFTCOPY_CSV'
      ' and ZSOFTCOPY_TEXT successfully'
      INTO l_infoline.
  ELSE.
    CONCATENATE
      'created ZSOFTCOPY_CSV with subrc=' l_subrc_csv
      ' and ZSOFTCOPY_TXT with subrc=' l_subrc_txt
      INTO l_infoline.
  ENDIF.

  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
    EXPORTING
      text = l_infoline.
  WAIT UP TO 5 SECONDS.

ENDFORM.                    "make


*-----------------------------------------------------------------------
* add_csv_line
*-----------------------------------------------------------------------
FORM add_csv_line
  USING
    value(i_str)
  CHANGING
    c_counter TYPE i.

  DATA:
  ls_src TYPE abapsource,
  l_length TYPE i,
  l_str TYPE string,
  l_strlen TYPE i,
  l_cnt TYPE i VALUE 0.

  CONSTANTS:
  lc_length_content TYPE i VALUE 68.

  IF c_counter = 100.
    PERFORM add_form_middle.
    c_counter = 0.
  ENDIF.

  l_strlen = strlen( i_str ).
  IF l_strlen > lc_length_content.
    CONCATENATE '''' i_str+0(lc_length_content) ''''
        INTO ls_src-line.
    APPEND ls_src TO gt_src.
    l_length = strlen( i_str ) - lc_length_content.
    l_str = i_str+lc_length_content(l_length).
    PERFORM add_csv_line
      USING l_str
      CHANGING l_cnt.
  ELSE.
    CONCATENATE '''' i_str '|''' INTO ls_src-line.
    APPEND ls_src TO gt_src.
  ENDIF.
  c_counter = c_counter + 1.

ENDFORM.                    "add_csv_line


*-----------------------------------------------------------------------
* add_form_begin
*-----------------------------------------------------------------------
FORM add_form_begin
  USING
    value(i_name).

  DATA:
  ls_src TYPE abapsource.

  CONCATENATE 'form' i_name
      INTO ls_src-line SEPARATED BY space.
  APPEND ls_src TO gt_src.

  ls_src-line = 'changing ct_csv type it_string.'.
  APPEND ls_src TO gt_src.

  ls_src-line = 'refresh ct_csv.'.
  APPEND ls_src TO gt_src.

  ls_src-line = 'data ls_csv type string.'.
  APPEND ls_src TO gt_src.

  ls_src-line = 'concatenate'.
  APPEND ls_src TO gt_src.

ENDFORM.                    "add_form_begin


*-----------------------------------------------------------------------
* add_form_end
*-----------------------------------------------------------------------
FORM add_form_end
  USING
    value(i_name).

  DATA:
  ls_src TYPE abapsource.

  ls_src-line = 'space into ls_csv.'.
  APPEND ls_src TO gt_src.

  ls_src-line = 'append ls_csv to ct_csv.'.
  APPEND ls_src TO gt_src.

  CONCATENATE 'endform.  "' i_name INTO ls_src-line.
  APPEND ls_src TO gt_src.

ENDFORM.                    "add_form_end


*-----------------------------------------------------------------------
* add_form_middle
*-----------------------------------------------------------------------
FORM add_form_middle.

  DATA:
  ls_src TYPE abapsource.

  ls_src-line = 'space into ls_csv.'.
  APPEND ls_src TO gt_src.

  ls_src-line = 'append ls_csv to ct_csv.'.
  APPEND ls_src TO gt_src.

  ls_src-line = 'concatenate'.
  APPEND ls_src TO gt_src.

ENDFORM.                    "add_form_end


*-----------------------------------------------------------------------
* add_report_begin
*-----------------------------------------------------------------------
FORM add_report_begin
  USING
    value(i_name) TYPE progname
  CHANGING
    ct_src TYPE it_abapsource.

  DATA:
  ls_src TYPE abapsource.

  REFRESH ct_src.

  CONCATENATE
  '*&-----------------------------'
  '----------------------------------------*'
  INTO ls_src-line.
  APPEND ls_src TO ct_src.

  CLEAR ls_src.
  ls_src-line+0(11) = '*&  Include'.
  ls_src-line+21(30) = i_name.
  ls_src-line+71(1) = '*'.
  APPEND ls_src TO ct_src.

  CONCATENATE
  '*&-----------------------------'
  '----------------------------------------*'
  INTO ls_src-line.
  APPEND ls_src TO ct_src.

  CONCATENATE
  '*&!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
  '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*'
  INTO ls_src-line.
  APPEND ls_src TO ct_src.

  CONCATENATE
  '*&!!!!!!!!!!! generated by ZSOFT'
  'COPY_MAKE !!!!! do not edit !!!!!!!!!!!*'
  INTO ls_src-line.
  APPEND ls_src TO ct_src.

  CONCATENATE
  '*&!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
  '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*'
  INTO ls_src-line.
  APPEND ls_src TO ct_src.

  CONCATENATE
  '*&------------------------------'
  '---------------------------------------*'
  INTO ls_src-line.
  APPEND ls_src TO ct_src.

ENDFORM.                    "add_report_begin


*-----------------------------------------------------------------------
* add_report_end
*-----------------------------------------------------------------------
FORM add_report_end
  CHANGING
    ct_src TYPE it_abapsource.
* nothing to do
ENDFORM.                    "add_report_end


*-----------------------------------------------------------------------
* csv_table_structure
*-----------------------------------------------------------------------
FORM csv_table_structure.

  DATA:
  lt_table TYPE STANDARD TABLE OF zsoftcopy_table,
  l_line TYPE string,
  l_cnt TYPE i VALUE 0.

  FIELD-SYMBOLS:
  <ls_table> TYPE zsoftcopy_table.

  CONSTANTS:
  lc_name TYPE fieldname VALUE 'csv_table_structure'.


  PERFORM add_form_begin USING lc_name.

  SELECT * FROM zsoftcopy_table INTO TABLE lt_table
      ORDER BY id column_nr.

  LOOP AT lt_table ASSIGNING <ls_table>.
    CONCATENATE
        <ls_table>-id              gc_separator
        <ls_table>-column_nr       gc_separator
        <ls_table>-fieldname       gc_separator
        <ls_table>-title_elem      gc_separator
        <ls_table>-link_column     gc_separator
        <ls_table>-column_def      gc_separator
        <ls_table>-colored_column  gc_separator
        <ls_table>-comparator      gc_separator
        <ls_table>-comp_value
        INTO l_line.
    PERFORM add_csv_line
      USING l_line
      CHANGING l_cnt.
  ENDLOOP.

  PERFORM add_form_end USING lc_name.

ENDFORM.                    "csv_table_structure


*-----------------------------------------------------------------------
* csv_tabstrip
*-----------------------------------------------------------------------
FORM csv_tabstrip.

  DATA:
  lt_strip TYPE STANDARD TABLE OF zsoftcopy_strip,
  l_line TYPE string,
  l_cnt TYPE i VALUE 0.

  FIELD-SYMBOLS:
  <ls_strip> TYPE zsoftcopy_strip.

  CONSTANTS:
  lc_name TYPE fieldname VALUE 'csv_tabstrip'.


  PERFORM add_form_begin USING lc_name.

  SELECT * FROM zsoftcopy_strip INTO TABLE lt_strip
      ORDER BY form tab_nr.

  LOOP AT lt_strip ASSIGNING <ls_strip>.
    CONCATENATE
        <ls_strip>-form         gc_separator
        <ls_strip>-id           gc_separator
        <ls_strip>-parent_id    gc_separator
        <ls_strip>-tab_nr       gc_separator
        <ls_strip>-firstrow     gc_separator
        <ls_strip>-active_order gc_separator
        <ls_strip>-text_elem    gc_separator
        <ls_strip>-comparator   gc_separator
        <ls_strip>-comp_value
        INTO l_line.
    PERFORM add_csv_line
      USING l_line
      CHANGING l_cnt.
  ENDLOOP.

  PERFORM add_form_end USING lc_name.

ENDFORM.                    "csv_table_structure


*-----------------------------------------------------------------------
* csv_text_elements
*-----------------------------------------------------------------------
FORM csv_text_elements
  CHANGING
    c_subrc TYPE string.

  DATA:
  lt_txt TYPE STANDARD TABLE OF zsoftcopy_text,
  lt_txtt TYPE STANDARD TABLE OF zsoftcopy_textt,
  l_line TYPE string,
  l_text TYPE string,
  ls_src TYPE abapsource,
  lt_src_txt TYPE it_abapsource,
  l_cnt TYPE i VALUE 0.

  FIELD-SYMBOLS:
  <ls_txt> TYPE zsoftcopy_text,
  <ls_txtt> TYPE zsoftcopy_textt.

  CONSTANTS:
  lc_name_1 TYPE fieldname VALUE 'csv_text_elements_1',
  lc_name_2 TYPE fieldname VALUE 'csv_text_elements_2'.


*txt
  l_cnt = 0.
  PERFORM add_form_begin USING lc_name_1.

  SELECT * FROM zsoftcopy_text INTO TABLE lt_txt
      ORDER BY fieldname.

  LOOP AT lt_txt ASSIGNING <ls_txt>.
    CONCATENATE
        <ls_txt>-fieldname      gc_separator
        <ls_txt>-text_prefix    gc_separator
        <ls_txt>-dtel_rollname  gc_separator
        <ls_txt>-dtel_field     gc_separator
        <ls_txt>-domname        gc_separator
        <ls_txt>-domvalue       gc_separator
        <ls_txt>-icon_id        gc_separator
        <ls_txt>-icon_field     gc_separator
        <ls_txt>-conc_v1        gc_separator
        <ls_txt>-conc_copula    gc_separator
        <ls_txt>-conc_v2        gc_separator
        <ls_txt>-conc_sep_space gc_separator
        <ls_txt>-msg_id         gc_separator
        <ls_txt>-msg_nr         gc_separator
        <ls_txt>-msg_v1         gc_separator
        <ls_txt>-msg_v2         gc_separator
        <ls_txt>-msg_v3         gc_separator
        <ls_txt>-msg_v4         gc_separator
        INTO l_line.
    PERFORM add_csv_line
      USING l_line
      CHANGING l_cnt.
  ENDLOOP.

  PERFORM add_form_end USING lc_name_1.

*txtt
  l_cnt = 0.
  PERFORM add_form_begin USING lc_name_2.

  SELECT * FROM zsoftcopy_textt INTO TABLE lt_txtt
      ORDER BY fieldname.

  LOOP AT lt_txtt ASSIGNING <ls_txtt>.
    l_text = <ls_txtt>-text.
    PERFORM encode_html CHANGING l_text.
    CONCATENATE
        <ls_txtt>-fieldname gc_separator
        <ls_txtt>-spras     gc_separator
        l_text
        INTO l_line.
    PERFORM add_csv_line
      USING l_line
      CHANGING l_cnt.
  ENDLOOP.

  PERFORM add_form_end USING lc_name_2.

*params
  PERFORM add_report_begin
    USING gc_include_txt_name
    CHANGING lt_src_txt.

* constants
  CONCATENATE 'constants gc_version type sydatum value '''
      sy-datum '''.'
      INTO ls_src-line.
  APPEND ls_src TO lt_src_txt.

* texts
  LOOP AT lt_txt ASSIGNING <ls_txt>.
    AT FIRST.
      ls_src-line = 'data:'.
      APPEND ls_src TO lt_src_txt.
    ENDAT.
    CONCATENATE <ls_txt>-fieldname 'type string'
        INTO ls_src-line SEPARATED BY space.
    AT LAST.
      CONCATENATE ls_src-line '.' INTO ls_src-line.
      APPEND ls_src TO lt_src_txt.
      EXIT.
    ENDAT.
    CONCATENATE ls_src-line ',' INTO ls_src-line.
    APPEND ls_src TO lt_src_txt.
  ENDLOOP.

  PERFORM add_report_end
    CHANGING lt_src_txt.

  PERFORM save
    USING
      gc_include_txt_name
      gc_include_txt_title
      lt_src_txt
    CHANGING
      c_subrc.

ENDFORM.                    "csv_text_elements


*-----------------------------------------------------------------------
* csv_varid_broken
*-----------------------------------------------------------------------
FORM csv_varid_broken.

  DATA:
  lt_varid TYPE STANDARD TABLE OF zsoftcopy_varid,
  l_line TYPE string,
  l_cnt TYPE i VALUE 0.

  FIELD-SYMBOLS:
  <ls_varid> TYPE zsoftcopy_varid.

  CONSTANTS:
  l_c_name TYPE fieldname VALUE 'csv_varid_broken'.


  PERFORM add_form_begin USING l_c_name.

  SELECT * FROM zsoftcopy_varid INTO TABLE lt_varid.

  LOOP AT lt_varid ASSIGNING <ls_varid>.
    CONCATENATE
        <ls_varid>-report       gc_separator
        <ls_varid>-vari
        INTO l_line.
    PERFORM add_csv_line
      USING l_line
      CHANGING l_cnt.
  ENDLOOP.

  PERFORM add_form_end USING l_c_name.

ENDFORM.                    "csv_varid_broken


*-----------------------------------------------------------------------
* save
*-----------------------------------------------------------------------
FORM save
  USING
    value(i_progname) TYPE progname
    value(i_title) TYPE repti
    i_it_abapsource TYPE it_abapsource
  CHANGING
    c_subrc TYPE string.

  CALL FUNCTION 'BC_PR_INSERT'
    EXPORTING
      program_name     = i_progname
      program_type     = 'I'
      title_string     = i_title
    TABLES
      SOURCE           = i_it_abapsource
    EXCEPTIONS
      already_exists   = 1
      cancelled        = 2
      name_not_allowed = 3
      permission_error = 4
      OTHERS           = 5.

  IF sy-subrc = 1.
    CALL FUNCTION 'BC_PR_UPDATE'
      EXPORTING
        program_name       = i_progname
      TABLES
        SOURCE             = i_it_abapsource
      EXCEPTIONS
        not_exists         = 6
        cancelled          = 7
        name_not_allowed   = 8
        permission_error   = 9
        object_is_inactive = 10
        wrong_user         = 11
        OTHERS             = 12.
  ENDIF.

  c_subrc = sy-subrc.

ENDFORM.                    "save
