[ABAP] DMS: Dateien an Dokumenteninfosätze anhängen (CVAPI_DOC_CREATE)

TYPES: BEGIN OF bin_file,
         name TYPE string,
         size TYPE i,
         data TYPE solix_tab,
       END OF bin_file.

* Dokumentart (TA DC10, Tabelle TDWA)
PARAMETERS: p_type TYPE bapi_doc_aux-doctype OBLIGATORY.
* Teildokument
PARAMETERS: p_part TYPE bapi_doc_aux-docpart OBLIGATORY DEFAULT '000'.
* Dokumentversion
PARAMETERS: p_ver TYPE bapi_doc_aux-docversion OBLIGATORY DEFAULT '00'.
* Beschreibung
PARAMETERS: p_desc TYPE bapi_doc_drat-description DEFAULT 'Test 123'.
* Ablagekategorie (TA OACT, Tabelle SDOKSTCAE, View V_SDOKSTCA)
PARAMETERS: p_stor TYPE bapi_doc_files2-storagecategory OBLIGATORY.
* Logisches Dokument/Workstation Application (TA S_ALR_87008829, Tabelle TDWP, Bild -> JPG)
PARAMETERS: p_appl TYPE bapi_doc_files2-application_id OBLIGATORY DEFAULT 'JPG'.
* Objekttyp (Material -> MARA)
PARAMETERS: p_otype TYPE bapi_doc_drad-objecttype OBLIGATORY DEFAULT 'MARA'.
* Objektkey (Material -> MATNR)
PARAMETERS: p_okey TYPE bapi_doc_drad-objectkey OBLIGATORY DEFAULT '000000001122334455'.

START-OF-SELECTION.
  TRY.
      DATA: lv_rc TYPE i.
      DATA: it_files TYPE filetable.
      DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
      cl_gui_frontend_services=>file_open_dialog( EXPORTING file_filter = |jpg (*.jpg)\|*.jpg\|{ cl_gui_frontend_services=>filetype_all }|
                                                  CHANGING  file_table  = it_files
                                                            rc          = lv_rc
                                                            user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.
* Dateien auf den Appl.-Server hochladen
          DATA: lv_file TYPE bin_file.
          cl_gui_frontend_services=>gui_upload( EXPORTING filename   = CONV #( it_files[ 1 ]-filename )
                                                          filetype   = 'BIN'
                                                IMPORTING filelength = lv_file-size
                                                CHANGING  data_tab   = lv_file-data ).

          DATA: lv_xstring TYPE xstring.

* Datei binär zu xstring wandeln
          CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
            EXPORTING
              input_length = lv_file-size
            IMPORTING
              buffer       = lv_xstring
            TABLES
              binary_tab   = lv_file-data.

* Dokumentinformationssatz
          DATA(lv_draw) = VALUE draw(
                                      dokar = p_type
                                      doktl = p_part
                                      dokvr = p_ver
                                    ).

* DVS: Steuerung für APIS
          DATA(lv_api_control) = VALUE cvapi_api_control(
                                                            no_update_task = abap_true
                                                        ).

* Kurztexte für Dokuinfosätze
          DATA: it_drat TYPE STANDARD TABLE OF dms_db_drat WITH DEFAULT KEY.
          it_drat = VALUE #( (
                                langu = sy-langu
                                dktxt = p_desc
                             )
                           ).

* Verknüpfung Dokument-Objekt
          DATA: it_drad TYPE STANDARD TABLE OF dms_db_drad WITH DEFAULT KEY.

          it_drad = VALUE #( (
                               dokob = p_otype
                               objky = p_okey
                             )
                           ).

* Originale für Dokumente (Dateianhang)
          DATA: it_bin_content TYPE STANDARD TABLE OF drao WITH DEFAULT KEY.

          DATA: lv_data_length TYPE i.
          DATA: it_content_raw TYPE STANDARD TABLE OF orblk WITH DEFAULT KEY.

* xstring --> ORBLK (Binärformat mit Zeilenlänge 2550 Bytes)
          CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
            EXPORTING
              buffer        = lv_xstring
            IMPORTING
              output_length = lv_data_length
            TABLES
              binary_tab    = it_content_raw.

* Datenaufbereitung
          LOOP AT it_content_raw ASSIGNING FIELD-SYMBOL(<raw>).

            APPEND VALUE #(
                            zaehl = sy-tabix
                            appnr = '1'
                            dokar = p_type
                            orln  = lv_data_length
*                            orbkl = 2550
                            orblk = <raw>
                          ) TO it_bin_content.
          ENDLOOP.

* DVS: Originale zu einem Dokument
          DATA: it_doc_files TYPE STANDARD TABLE OF cvapi_doc_file WITH DEFAULT KEY.

          it_doc_files = VALUE #( ( updateflag  = 'I'
                                    appnr       = '1'
                                    dappl       = p_appl
                                    filename    = it_files[ 1 ]-filename
                                    storage_cat = p_stor
                                    description = p_desc ) ).

          DATA(lv_message) = VALUE messages( ).
          DATA(lv_pfx_dokar) = VALUE draw-dokar( ).
          DATA(lv_pfx_doknr) = VALUE draw-doknr( ).
          DATA(lv_pfx_dokvr) = VALUE draw-dokvr( ).
          DATA(lv_pfx_doktl) = VALUE draw-doktl( ).

* DVS: Dokument anlegen
          CALL FUNCTION 'CVAPI_DOC_CREATE'
            EXPORTING
              ps_draw            = lv_draw
              ps_api_control     = lv_api_control
              pf_content_provide = 'TBL' " Binärdaten des Files als Tabelle übergeben
            IMPORTING
              psx_message        = lv_message
              pfx_dokar          = lv_pfx_dokar
              pfx_doknr          = lv_pfx_doknr
              pfx_dokvr          = lv_pfx_dokvr
              pfx_doktl          = lv_pfx_doktl
            TABLES
              pt_drad_x          = it_drad
              pt_drat_x          = it_drat
              pt_files_x         = it_doc_files
              pt_content         = it_bin_content.

          IF lv_message-msg_type CA 'EA'.
* Rollback bei Fehler
            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          ELSE.
* Daten verbuchen
            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait = abap_true.

            cl_demo_output=>write_data( lv_message ).
            cl_demo_output=>write_data( lv_pfx_dokar ).
            cl_demo_output=>write_data( lv_pfx_doknr ).
            cl_demo_output=>write_data( lv_pfx_dokvr ).
            cl_demo_output=>write_data( lv_pfx_doktl ).

* HTML-Code vom Demo-Output holen
            DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
            cl_abap_browser=>show_html( EXPORTING title       = 'CVAPI_DOC_CREATE'
                                                  html_string = lv_html
                                                  container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
            WRITE: space.
          ENDIF.
        ENDIF.
      ENDIF.
    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
  ENDTRY.