3

CL_DEMO_OUTPUT invigorated

 2 years ago
source link: https://blogs.sap.com/2021/12/08/cl_demo_output-invigorated/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
December 8, 2021 3 minute read

CL_DEMO_OUTPUT invigorated

Some time ago, we introduced CL_DEMO_OUTPUT for simple examples and demonstrations.

CL_DEMO_OUTPUT, Part 1 of 2 – Usage | SAP Blogs

The functionality up to now was already nice and useful, but something was missing.

Writing a simple piece of code such as

  DATA:
    BEGIN OF super,
      col1 TYPE i VALUE 1,
      BEGIN OF sub,
        col2 TYPE i VALUE 2,
        col3 TYPE i VALUE 3,
      END OF sub,
    END OF super.

  cl_demo_output=>display( super ).

results in

Not_supported.png

We are happy to announce that from ABAP release 7.56,SP01 on, this output will look as follows:

Supported.png

In fact, you can output  (nearly?) everything now:

CLASS demo_class DEFINITION.
  PUBLIC SECTION.
    TYPES:
      BEGIN OF spfli_line,
        carrid   TYPE spfli-carrid,
        connid   TYPE spfli-connid,
        cityfrom TYPE spfli-cityfrom,
        cityto   TYPE spfli-cityto,
      END OF spfli_line,
      spfli_tab TYPE HASHED TABLE OF spfli_line
                     WITH UNIQUE KEY carrid connid,
      BEGIN OF struct,
        carrname TYPE scarr-carrname,
        spfli    TYPE REF TO spfli_tab,
      END OF struct.
    DATA result TYPE SORTED TABLE OF struct WITH UNIQUE KEY carrname.
    METHODS constructor.
ENDCLASS.

CLASS demo_class IMPLEMENTATION.
  METHOD constructor.
    SELECT s~carrname, p~carrid, p~connid, p~cityfrom, p~cityto
           FROM scarr AS s
             INNER JOIN spfli AS p
               ON s~carrid = p~carrid
           ORDER BY s~carrname
           INTO TABLE @DATA(itab).
    LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>) GROUP BY <fs>-carrname.
      INSERT VALUE #( carrname = <fs>-carrname ) INTO TABLE result
             ASSIGNING FIELD-SYMBOL(<line>).
      <line>-spfli = NEW #(
        FOR <wa> IN GROUP <fs> ( CORRESPONDING #( <wa> ) ) ).
    ENDLOOP.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

  DATA(oref) = NEW demo_class( ).

  cl_demo_output=>display( oref ).

gives

complex.png

Note, that simply the name of the reference variable oref is passed to the display method.

This has been achieved by introducing some recursions in CL_DEMO_OUTPUT_STREAM. That means that the intermediate XML coming from CL_DEMO_OUTPUT_STREAM supports nested stuff now and that the conversion capabilities of CL_DEMO_OUTPUT_HTML were enhanced accordingly.

And just for fun, we can also write self-explaining demos now:

DATA(o) = cl_demo_output=>new(
)->write_doc( cl_demo_output_helper=>get_infobox_html(
     `This output is produced by program ` && sy-repid )
)->next_section( 'ABAP CDS View Entity'
)->write_doc( cl_demo_output_helper=>get_ddls_source_code_html(
     source = 'DEMO_CDS_SPFLI_ENTITY' )
)->next_section( `ABAP SQL SELECT`
)->write_doc(
   `The following code snippet demonstrates a ` &&
   `<code>SELECT</code> statement accessing `   &&
   `the CDS view entity without `               &&
   `<code>ORDER BY</code> clause:`
)->begin_code( `select1` ).

SELECT *
       FROM demo_cds_spfli_entity
       INTO TABLE @DATA(itab). "#EC CI_NOWHERE

o->end_code( `select1`
)->write_doc( `The result in <code>itab</code> is:`
)->write( itab
)->write_doc( `Adding`
)->write_doc(
   cl_demo_output_helper=>get_abap_source_code_html(
     code = `ORDER BY cityfrom, cityto`  )
)->write_doc( `as follows`
)->begin_code( `select2` ).

SELECT *
       FROM demo_cds_spfli_entity
       ORDER BY cityfrom, cityto
       INTO TABLE @itab. "#EC CI_NOWHERE

o->end_code( `select2`
)->write_doc( `gives:`
)->write( itab
)->display( ).

gives

self_exp.png

As before, you can retrieve the HTML file with method GET instead of DISPLAY in order to become independent from SAP GUI.

Caveats

  • You have to wait for 7.56, SP01
  • The new complex outputs are not fully supported by the text mode (but who wants to reprogram a browser in CL_DEMO_OUTPUT_TEXT?)
  • ADT console supports only text but not HTML mode
  • It is for demonstration purposes only …

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK