[ABAP] SAP Gateway File-Download über DEFINE und GET_STREAM

SE80: DDIC-Typ anlegen „ZFILESTREAM“

Feldname    Name im Gateway   ABAP-Typ     Bemerkung
------------------------------------------------------------------------------------------------------------------
FILEKEY     FileKey           beliebig     eindeutige Referenz (Key) zur späteren Ermittelung der Datei im Backend
MIME_TYPE   MimeType          CHAR100      Platzhalter für MimeType der Datei
VALUE       Value             XSTRINGVAL   Platzhalter für Binärdaten

SEGW: neuer Entitätstyp zum Lesen der Datei

  • Name: „FileStream“, DDIC-Typ „ZFILESTREAM“ importieren
Name      Schlüssel   EDM-Coretyp   Bezeichner        ABAP-Feldname
-------------------------------------------------------------------
FileKey   ja          Edm.String    FileId            FILEKEY
MimeType  nein        Edm.String    Mimetyp           MIME_TYPE
Value     nein        Edm.Binary    Media Ressource   VALUE
  • Haken bei Medium setzen

SE80: ZCL_…_MPC_EXT~DEFINE redefinieren

METHOD define.
  DATA: lo_entity   TYPE REF TO /iwbep/if_mgw_odata_entity_typ.
  DATA: lo_property TYPE REF TO /iwbep/if_mgw_odata_property.

* Aufruf Basisklasse
  super->define( ).

* Entität "FileStream" holen
  lo_entity = model->get_entity_type( iv_entity_name = zcl_..._mpc=>gc_filestream ).

  IF lo_entity IS BOUND.

* Feld für MimeType (==MIME_TYPE) als ContentType setzen
    lo_property = lo_entity->get_property( iv_property_name = 'MimeType' ).
    lo_property->set_as_content_type( ).

  ENDIF.
ENDMETHOD.

SE80: /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM redefinieren

METHOD /iwbep/if_mgw_appl_srv_runtime~get_stream.
  DATA: lv_file        TYPE zcl_..._mpc=>ts_filestream.
  DATA: lv_stream      TYPE ty_s_media_resource.

* Welche Entität?
  CASE io_tech_request_context->get_entity_type_name( ).
    WHEN zcl_..._mpc=>gc_filestream.
* Schlüssel ("FILEKEY") ermitteln
      io_tech_request_context->get_converted_keys( IMPORTING es_key_values = lv_file ).

* gewünschte Binärdaten anhand des Schlüssels aus SAP lesen
      DATA: it_bin_data TYPE STANDARD TABLE OF raw255.
      
      it_bin_data = ...

* Dateinamen ermitteln        
      DATA: lv_filename TYPE skwf_filnm.
      
      lv_filename = ...

* Für die Umwandlung die Dateigröße der Binärdaten berechnen
      DATA(lv_size) = lines( it_bin_data ).
      DATA: lv_line LIKE LINE OF it_bin_data.
      DATA(lv_length) = 0.
* für Unicode-Kompatibilität IN BYTE MODE
      DESCRIBE FIELD lv_line LENGTH lv_length IN BYTE MODE.
      lv_size = lv_size * lv_length.

* Binärdaten in xstring für die Rückgabe konvertieren        
      CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
        EXPORTING
          input_length = lv_size
        IMPORTING
          buffer       = lv_stream-value
        TABLES
          binary_tab   = it_bin_data
        EXCEPTIONS
          failed       = 1
          OTHERS       = 2.

      IF sy-subrc = 0.        
* MIME-Typen der Datei ermitteln und an das Frontend übergeben
        DATA: lv_mimetype TYPE skwf_mime.
              
* aus Dateinamen den MIME-Typen ermitteln
        CALL FUNCTION 'SKWF_MIMETYPE_OF_FILE_GET'
          EXPORTING
            filename = lv_filename
          IMPORTING
            mimetype = lv_mimetype.
                
        lv_stream-mime_type = lv_mimetype.

* HTTP-Header-Infos setzen (Dateiname usw.)
        DATA(lv_lheader) = VALUE ihttpnvp( name  = 'Content-Disposition'
                                           value = |inline; filename="{ escape( val = lv_filename format = cl_abap_format=>e_url ) }";| ). " Datei im Tab inline (Plugin) öffnen
*                                           value = |outline; filename="{ escape( val = lv_filename format = cl_abap_format=>e_url ) }";| ). " Datei zum direkten Herunterladen / Öffnen anbieten

        set_header( is_header = lv_lheader ).

* alle Daten zum Frontend schicken
        me->copy_data_to_ref( EXPORTING is_data = lv_stream
                              CHANGING cr_data  = er_stream ).
      ENDIF.

    WHEN OTHERS.
* andere Entitäten standardmäßig behandeln
      super->/iwbep/if_mgw_appl_srv_runtime~get_stream(
        EXPORTING
          iv_entity_name          = iv_entity_name
          iv_entity_set_name      = iv_entity_set_name
          iv_source_name          = iv_source_name
          it_key_tab              = it_key_tab
          it_navigation_path      = it_navigation_path
          io_tech_request_context = io_tech_request_context
          IMPORTING
            er_stream             = er_stream
            es_response_context   = es_response_context ).
  ENDCASE.
ENDMETHOD.

Weiterführende Infos: SAP Fiori tricks: Get rid of $value in PDF display/downloads

[ABAP] OpenSQL: Subselect verwenden

* subqueries cannot be used when accessing pool tables or cluster tables
* the ORDER BY clause cannot be used in a subquery
* if a subquery is used, the Open SQL statement bypasses SAP buffering

SELECT scustom~id, scustom~name, scustom~city, sbook~cancelled
  INTO TABLE @DATA(it_cust)
  UP TO 100 ROWS
  FROM sbook
  INNER JOIN scustom ON scustom~id = sbook~customid
  WHERE customid NOT IN ( SELECT customid FROM sbook WHERE cancelled EQ 'X' ).

cl_demo_output=>display( it_cust ).

[ABAP] URL-Encoding / URL-Decoding

DATA(lv_url) = |https://www.seite.de/?par1=123&par2="Test 123"|.
* URL-Encoding
DATA(lv_escaped) = escape( val = lv_url format = cl_abap_format=>e_url ).
DATA(lv_escaped2) = cl_http_utility=>escape_url( lv_url ).
* URL-Decoding
DATA(lv_unescaped) = cl_http_utility=>unescape_url( lv_escaped2 ).

WRITE: / lv_escaped.
WRITE: / lv_escaped2.
WRITE: / lv_unescaped.

[ABAP] ABAP Database Connectivity (ADBC) – CASE-insensitive Suche auf der Datenbank

* ab Version 7.51 auch im OpenSQL verfügbar (LOWER, UPPER)
* https://help.sap.com/doc/abapdocu_751_index_htm/7.51/de-DE/abenopen_sql_functions.htm
* https://archive.sap.com/discussions/thread/3470652

DATA: o_salv TYPE REF TO cl_salv_table.        " Anzeigeobjekt SALV Grid
DATA: it_cols TYPE adbc_column_tab.            " Selektierte Spalten
DATA: it_makt TYPE STANDARD TABLE OF makt.     " Ausgabetabelle

START-OF-SELECTION.

* zu verarbeitende Spalten
  it_cols = VALUE #( ( 'MATNR' )
                     ( 'SPRAS' )
                     ( 'MAKTX' )
                     ( 'MAKTG' ) ).

* Spaltennnamen durch Komma trennen
  DATA(cols) = to_lower( concat_lines_of( table = it_cols sep = ',' ) ).

* Spaltennamen und Parameter in SQL einfügen, alle Bezeichner suchen, die mit 'sch*' beginnen
  DATA(lv_query) = |SELECT { cols } FROM makt WHERE spras = 'D' AND upper(maktx) LIKE 'SCH%'|.

  TRY.
* SQL-Connection öffnen
      DATA(o_sql_connection) = NEW cl_sql_connection( ).

      IF abap_true <> o_sql_connection->is_closed( ).
* SQL-Statement erzeugen
        DATA(o_sql) = NEW cl_sql_statement( con_ref = o_sql_connection ).

* Query ausführen
        DATA(o_result) = o_sql->execute_query( lv_query ).

* Ergebnismenge soll in interne Tabelle it_makt und nur die Spalten, welche in it_cols stehen
        o_result->set_param_table( itab_ref             = REF #( it_makt )
                                   corresponding_fields = it_cols ).

* Ergebnismenge in interne Tabelle lesen
* cnt enthält die Anzahl der gelesenen Datensätze
        DATA(cnt) = o_result->next_package( ).
* Ergebnismenge schließen
        o_result->close( ).
* SQL-Connection schließen
        o_sql_connection->close( ).

        IF cnt > 0.
          cl_salv_table=>factory( IMPORTING
                                    r_salv_table = o_salv
                                  CHANGING
                                    t_table = it_makt ).

          o_salv->get_display_settings( )->set_list_header( |DBMS: { o_sql_connection->get_dbms( ) }| ).
          o_salv->get_functions( )->set_all( abap_true ).
          o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
          o_salv->display( ).
        ENDIF.
      ENDIF.
    CATCH cx_root INTO DATA(e_text).
      WRITE: / e_text->get_text( ).
  ENDTRY.

[ABAP] Case-sensitives SORT von internen Tabellen

TYPES: BEGIN OF ty_name,
         name    TYPE string,
         name_lc TYPE string,
       END OF ty_name.

TYPES: ty_it_name TYPE STANDARD TABLE OF ty_name WITH DEFAULT KEY.

* Tabelle mit Strings, die sortiert werden soll
DATA(it_names) = VALUE ty_it_name( ( name = 'Albert' )
                                   ( name = 'Emma' )
                                   ( name = 'Zorro' )
                                   ( name = 'Donald' )
                                   ( name = 'Lion' )
                                   ( name = 'answer' )
                                   ( name = 'type' )
                                   ( name = 'second' ) ).

* zweite Spalte mit lower-case Strings füllen
LOOP AT it_names ASSIGNING FIELD-SYMBOL(<n>).
  <n>-name_lc = to_lower( <n>-name ).
ENDLOOP.

* erstmal regulär sortieren
SORT: it_names BY name.

* zweite Tabelle anlegen (kopieren)
DATA(it_names_lc) = it_names.

* Spalte lower-case sortieren
SORT: it_names_lc BY name_lc.

* Ausgabe der Ergebnisse
cl_demo_output=>write_data( value = it_names    name = 'case-sensitives SORT' ).
cl_demo_output=>write_data( value = it_names_lc name = 'SORT nach lower-case' ).
cl_demo_output=>display( ).

[ABAP] Texttabelle zu einer Tabelle suchen

DATA: lv_tabname TYPE dd08v-tabname.
DATA: lv_for_key TYPE dd08v-fieldname.

* Prüft über die Tabelle DD08L zu Verfügbarkeit einer Texttabelle
* Siehe auch SE11 -> Tabelle anzeigen -> Menü -> Springen -> Texttabelle
CALL FUNCTION 'DDUT_TEXTTABLE_GET'
  EXPORTING
    tabname    = 'TPAR'
  IMPORTING
    texttable  = lv_tabname
    checkfield = lv_for_key.

WRITE: / 'Texttabelle:', lv_tabname.
WRITE: / 'Fremdschlüssel:', lv_for_key.

[ABAP] Datenelement und zugehörige Domäne auslesen

* Name Datenelement
PARAMETERS: p_elem TYPE rollname DEFAULT 'TXT50'.
* Sprachenschlüssel
PARAMETERS: p_lang TYPE ddlanguage DEFAULT 'D'.

SELECT * INTO TABLE @DATA(it_dd04l) FROM dd04l
  WHERE rollname  = @p_elem.

IF sy-subrc = 0.
* Bezeichner zum Datenelent holen
  SELECT * INTO TABLE @DATA(it_dd04t) FROM dd04t
    WHERE rollname   = @p_elem
      AND ddlanguage = @p_lang.

* aktive Domäne holen
  DATA(lv_domname) = it_dd04l[ as4local = 'A' ]-domname.
  SELECT * INTO TABLE @DATA(it_dd01l) FROM dd01l
    WHERE domname = @lv_domname.

* Bezeichner zur Domäne holen
  SELECT * INTO TABLE @DATA(it_dd01t) FROM dd01t
    WHERE domname    = @lv_domname
      AND ddlanguage = @p_lang.

  IF sy-subrc = 0.
    cl_demo_output=>write_data( value = it_dd04l name = |Datenelement: { p_elem }| ).
    cl_demo_output=>write_data( value = it_dd04t name = |Datenelement: { p_elem }| ).
    cl_demo_output=>write_data( value = it_dd01l name = |Domäne: { lv_domname }| ).
    cl_demo_output=>write_data( value = it_dd01t name = |Domäne: { lv_domname }| ).
    cl_demo_output=>display(  ).
  ENDIF.
ENDIF.