ABAP Keyword Documentation → ABAP - Dictionary → ABAP CDS in ABAP Dictionary → ABAP CDS - Annotations → ABAP CDS - Specifying Annotations → ABAP CDS Metadata Extensions
CDS Metadata Extensions with CDS Variants
The example demonstrates the evaluation of CDS annotations that are defined in CDS metadata extensions.
Other versions:
7.31 | 7.40 | 7.54
Source Code
DATA(out) = cl_demo_output=>new(
)->next_section( 'No Meta Data Extension' ).
cl_dd_ddl_annotation_service=>get_annos(
EXPORTING
entityname = 'DEMO_CDS_MDE'
metadata_extension = abap_false
IMPORTING
element_annos = DATA(element_annos) ).
DELETE element_annos WHERE elementname <> 'ELEMENT'.
out->write( element_annos
)->next_section( 'With Meta Data Extension'
)->begin_section( 'No Variant' ).
cl_dd_ddl_annotation_service=>get_annos(
EXPORTING
entityname = 'DEMO_CDS_MDE'
metadata_extension = abap_true
IMPORTING
element_annos = element_annos ).
DELETE element_annos WHERE elementname <> 'ELEMENT'.
out->write( element_annos
)->next_section( 'DemoVariant1' ).
cl_dd_ddl_annotation_service=>get_annos(
EXPORTING
entityname = 'DEMO_CDS_MDE'
variant = 'DEMOVARIANT1'
metadata_extension = abap_true
IMPORTING
element_annos = element_annos ).
DELETE element_annos WHERE elementname <> 'ELEMENT'.
out->write( element_annos
)->next_section( 'DemoVariant2' ).
cl_dd_ddl_annotation_service=>get_annos(
EXPORTING
entityname = 'DEMO_CDS_MDE'
variant = 'DEMOVARIANT2'
metadata_extension = abap_true
IMPORTING
element_annos = element_annos ).
DELETE element_annos WHERE elementname <> 'ELEMENT'.
out->write( element_annos
)->next_section( 'Wrong Variant' ).
cl_dd_ddl_annotation_service=>get_annos(
EXPORTING
entityname = 'DEMO_CDS_MDE'
variant = '�wrxlbrxlkrk'
metadata_extension = abap_true
IMPORTING
element_annos = element_annos ).
DELETE element_annos WHERE elementname <> 'ELEMENT'.
out->write( element_annos
)->display( ).
Description
The example program uses the method GET_ANNOS of class CL_DD_DDL_ANNOTATION_SERVICE to access the following CDS view:
@AccessControl.authorizationCheck: #NOT_REQUIRED
@Metadata.allowExtensions: true
define view Demo_Cds_MDE
as select from
demo_expressions
{
@UI.dataPoint.title: 'View, title'
@UI.dataPoint.description: 'View, description'
@UI.dataPoint.longDescription: 'View, longdescription'
'X' as element
}
where
id = 'X'
The annotations of the view are extended or overridden by the following CDS metadata extensions:
- DEMO_CDS_MDE_INDSTR_NO_VARIANT
annotate view Demo_Cds_MDE with
{
@UI.dataPoint.title: 'MDE INDUSTRY, no variant, title'
@UI.dataPoint.description: 'MDE INDUSTRY, no variant, description'
element;
}
- DEMO_CDS_MDE_INDSTR_VARIANT_1
annotate view Demo_Cds_MDE with
variant
DemoVariant1
{
@UI.dataPoint.longDescription: 'MDE INDUSTRY, DemoVariant1, longDescription'
element;
}
- DEMO_CDS_MDE_INDSTR_VARIANT_2
annotate view Demo_Cds_MDE with
variant
DemoVariant2
{
@UI.dataPoint.title: 'MDE INDUSTRY, DemoVariant2, title'
@UI.dataPoint.description: 'MDE INDUSTRY, DemoVariant2, description'
element;
}
- DEMO_CDS_MDE_PARTNR_NO_VARIANT
annotate view Demo_Cds_MDE with
{
@UI.dataPoint.title: 'MDE PARTNER, no variant, title'
element;
}
- DEMO_CDS_MDE_PARTNR_VARIANT_1
annotate view Demo_Cds_MDE with
variant
DemoVariant1
{
@UI.dataPoint.title: 'MDE PARTNER, DemoVariant1, title'
element;
}
- DEMO_CDS_MDE_PARTNR_VARIANT_2
annotate view Demo_Cds_MDE with
variant
DemoVariant2
{
@UI.dataPoint.title: 'MDE PARTNER, DemoVariant2, title'
@UI.dataPoint.description: 'MDE PARTNER, DemoVariant2, description'
element;
}
The evaluation is done with different parameters for the method GET_ANNOS:
- If the value of
abap_false
is transferred to the parameter METADATA_EXTENSION, the default evaluation of the metadata extensions is switched off for the view and the annotations that are specified in the source code are returned.
- If the value of
abap_true
is transferred to the parameter METADATA_EXTENSION (default behavior), all existing metadata extensions for the view are evaluated as follows.
- If the name of a CDS variant is not transferred, only annotations from metadata extensions that are not assigned to a variant are used. DEMO_CDS_MDE_PARTNR_NO_VARIANT is evaluated first and the annotation @UI.dataPoint.title is found there. This is ignored in the next metadata extension in the hierarchy, DEMO_CDS_MDE_INDSTR_NO_VARIANT, from which only @UI.dataPoint.description is used. Finally, UI.dataPoint.longDescription is used from the view itself, which has no entry in a metadata extension.
- If the name of the CDS variant DemoVariant1 is transferred, annotations from the metadata extensions that are assigned to this variant are used first, and then annotations from metadata extensions that are not assigned to a variant. DEMO_CDS_MDE_PARTNR_VARIANT_1 is evaluated first and the annotation @UI.dataPoint.title is found there. In the next metadata extension in the hierarchy, DEMO_CDS_MDE_INDSTR_VARIANT1, the annotation @UI.dataPoint.longDescription is found. No annotation is found in DEMO_CDS_MDE_PARTNR_NO_VARIANT. In DEMO_CDS_MDE_INDSTR_NO_VARIANT, the annotation @UI.dataPoint.description (not used until now) is found and used. The annotations of the source code of the view are all overridden.
- If the name of the CDS variant DemoVariant2 is passed, DEMO_CDS_MDE_PARTNR_VARIANT_2 is evaluated first and the annotations UI.dataPoint.title and @UI.dataPoint.description are found there. In the next metadata extension in the hierarchy, DEMO_CDS_MDE_INDSTR_VARIANT2, there are no annotations that have not already been used. The same applies to DEMO_CDS_MDE_PARTNR_NO_VARIANT and DEMO_CDS_MDE_INDSTR_NO_VARIANT, which are evaluated next. Therefore, the annotation UI.dataPoint.longDescription from the view itself is used.
- If the name of an invalid CDS variant is passed, an empty internal table is returned.
The column SOURCEDDLX in the output internal tables contains the metadata extension in which an annotation is specified.