Skip to content

ABAP Keyword Documentation →  ABAP - Dictionary →  ABAP CDS in ABAP Dictionary →  ABAP CDS - Annotations →  ABAP CDS - Evaluation of Annotations 

ABAP CDS, Evaluation of Annotations

This example demonstrates how semantics annotations of DDL source code are evaluated.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA incomplete_addresses TYPE STANDARD TABLE
                              OF demo_cds_semantics_annotation
                              WITH EMPTY KEY.

    cl_dd_ddl_annotation_service=>get_drct_annos_4_entity_elmnts(
      EXPORTING
        entityname =     'DEMO_CDS_SEMANTICS_ANNOTATION'
      IMPORTING
        annos      =     DATA(elementannos) ).

    TYPES element_anno LIKE LINE OF elementannos.
    DATA address_annos TYPE STANDARD TABLE OF element_anno-annoname
                            WITH EMPTY KEY.
    address_annos = VALUE #(
      ( 'SEMANTICS.NAME.FULLNAME' )
      ( 'SEMANTICS.ADDRESS.STREET' )
      ( 'SEMANTICS.ADDRESS.CITY' )
      ( 'SEMANTICS.ADDRESS.ZIPCODE' )
      ( 'SEMANTICS.ADDRESS.COUNTRY' ) ).

    DATA address_components TYPE TABLE OF element_anno-elementname
                                 WITH EMPTY KEY.
    address_components = VALUE #(
      FOR address_anno IN address_annos
      ( VALUE #( elementannos[ annoname = address_anno ]-elementname
                 DEFAULT '---' ) ) ).

    SELECT *
           FROM demo_cds_semantics_annotation
           INTO @DATA(address).
      LOOP AT address_components INTO DATA(component).
        ASSIGN COMPONENT component OF STRUCTURE address
                                  TO FIELD-SYMBOL(<value>).
        IF sy-subrc <> 0 OR <value> IS INITIAL.
          incomplete_addresses = VALUE #( BASE incomplete_addresses
                                         ( address ) ).
          EXIT.
        ENDIF.
      ENDLOOP.
    ENDSELECT.

    cl_demo_output=>display( incomplete_addresses ).

Description

This program uses a simple example to show how annotations can be evaluated in a framework. All rows in a CDS entity are to be found that do not contain a fully specified address. Here, a fully specified address is simply a set of elements for name, street, city, postal code, and country, none of which can be empty.

To do this, the program uses cl_dd_ddl_annotation_service=>get_drct_annos_4_entity_elmnts to read the element annotations of a CDS entity and gets the names of the elements defined (using semantics annotations) as the required components of an address in the internal table address_components. In the next step, the CDS entity is read using SELECT and a dynamic ASSIGN statement is used to check whether all required elements have a non-initial value for each read row. The output consists of any rows that do not contain a full address.

The following CDS view is used as an example for a CDS entity:

@AbapCatalog.sqlViewName: 'DEMO_CDS_SEMANNO'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_semantics_annotation
as select from
scustom
{
id,
@Semantics.name.fullName
name,
@Semantics.name.prefix
form,
@Semantics.address.street
street,
@Semantics.address.postBox
postbox,
@Semantics.address.zipCode
postcode,
@Semantics.address.city
city,
@Semantics.address.country
country,
@Semantics.address.subRegion
region,
@Semantics.contact.type
custtype,
@Semantics.language
langu,
@Semantics.eMail.address
email
}    

This view wraps the database table SCUSTOM, which contains address data. The assignment of the semantics annotations to the columns of the database table gives the columns semantics that can be evaluated. Previously, this was only provided by those names and data elements that are no longer relevant for the evaluation depicted here.