Friday 18 February 2022

ABAP Core Data Services: New syntax for extending CDS entities

EXTEND VIEW was yesterday. Since ABAP release 7.78, two new statements are available to extend CDS entities: EXTEND VIEW ENTITY and EXTEND ABSTRACT ENTITY. Read this blog post to learn about the new syntax for extending CDS entities.

Overview of CDS entity extensions

ABAP Core Data Services, SAP ABAP Central, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP, SAP ABAP Tutorial and Materials

EXTEND VIEW ENTITY


The statement EXTEND VIEW ENTITY is a replacement of EXTEND VIEW, designed for the extension of CDS view entities and CDS projection views.

Differences Between View Extensions and View Entity Extensions

◉ A view entity extension does not have a DDIC append view.

◉ CDS view extends with EXTEND VIEW require you to define at least one view field. This requirement has been removed. CDS view entity extensions also work without any new field. You can, for example, define only a new association.

◉ For a view entity extension, no name is specified after WITH. A view entity extension has only one name, which is the name of the repository object. This name is assigned in the wizard for creating a new repository object.

◉ In view entity extensions, no header annotations are allowed. That means that no annotations are allowed in front of the statement EXTEND VIEW ENTITY.

◉ View entity extensions were developed for CDS view entities and they have the same advantages, for example, much better activation performance and new features, such as typed literals and improved currency and quantity handling.

An existing CDS view can have one or more CDS view entity extensions. A CDS entity extension can not be further extended.

Both kinds of view extensions, EXTEND VIEW and EXTEND VIEW ENTITY, can be used for all three kinds of CDS views (CDS view entities, CDS projection views, CDS DDIC-based views). This is supported for migration and compatibility reasons, but it is not recommended.

How to extend a CDS view entity


Prerequisites

The header annotation @AbapCatalog.viewEnhancementCategory[ ] specifies how a CDS view entity can be extended. The possible values are:

◉ #PROJECTION_LIST: Extensions of the SELECT list and additional CDS associations are allowed
◉ #GROUP_BY: can only be specified together with #PROJECTION_LIST. Allows extensions to views that have aggregate expressions in the SELECT list.
◉ #UNION: Can only be specified together with #PROJECTION_LIST. Allows extensions to views that use a clause with a set operator (UNION, EXCEPT, INTERSECT).
◉ #NONE: Extensions are not allowed.

What can be extended?

◉ New fields can be added.
◉ New associations can be defined and exposed.
◉ All kinds of elements that are allowed in the SELECT list of the respective entity (CDS view entity or CDS projection view) can be added in the extension. That means that a CDS view entity extension to a CDS view entity can define elements such as path expressions, typed literals, expressions, and functions. A CDS view entity extension to a CDS projection view can define elements such as virtual elements, localized elements, and redefined associations.

Restrictions

◉ New input parameters are not possible.
◉ A regular view entity cannot be transformed into a root view entity.
◉ An appended field cannot be defined as key field.
◉ No new header annotations can be defined.

Example

The following CDS view entity allows extensions of the SELECT list.

@AccessControl.authorizationCheck: #NOT_REQUIRED
@AbapCatalog.viewEnhancementCategory: [#PROJECTION_LIST]
@EndUserText.label: 'Further information about the CDS entity'
define view entity DEMO_CDS_ORIGINAL_VIEW_VE 
  as select from
           spfli
      join scarr on
        scarr.carrid = spfli.carrid
    {
      key scarr.carrname     as carrier,
      key spfli.carrid       as carrierId,
      key spfli.connid       as flight,
          spfli.cityfrom     as departure,
          spfli.cityto       as destination
    }; 

The following CDS view entity extension defines and exposes a new association:

extend view entity DEMO_CDS_ORIGINAL_VIEW_VE with
association [1..*] to sflight as _sflight 
  on $projection.carrierId = _sflight.carrid
{
  _sflight
}

EXTEND ABSTRACT ENTITY


CDS abstract entity extensions were designed specifically to extend CDS abstract entities. Here’s a short overview of the rules:

◉ Prerequisite: As a prerequisite for the enhancement of the CDS abstract entity with the statement EXTEND ABSTRACT ENTITY, the base entity must allow extensions. Extensions are allowed per default or can be explicitly allowed with the annotation @AbapCatalog.extensibility.extensible with the value true.
◉ Elements of the extension: New elements and new associations can be defined in the extension.
◉ Restrictions: New input parameters are not possible. An abstract entity cannot be turned into a root entity by an extensions. Defining new header annotations is not supported.

Example

The following CDS abstract entity allows extensions:

@EndUserText.label: 'CDS abstract entity with extension'
@AbapCatalog.extensibility.extensible: true

define abstract entity DEMO_CDS_ORIGINAL_ABSTRACT
  with parameters
    param1 : abap.int4
{
  col1 : abap.char( 5 );
  col2 : abap.int4;
  col3 : abap.int4;
}

A new field and a new association are added by the following CDS abstract entity extension:

extend abstract entity DEMO_CDS_ORIGINAL_ABSTRACT with
{
  col4   : abap.char( 1 );
  _assoc : association to demo_cds_abstract_entity  
             on $projection.col1 = _assoc.col1;
}

The following program evaluates the structure of the enhanced CDS abstract entity using RTTI methods:

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA(out) = cl_demo_output=>new( ).

    DATA(components) =
      CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_name(
                                  'DEMO_CDS_ORIGINAL_ABSTRACT' )
                               )->components.
    out->write( components ).

    cl_dd_ddl_annotation_service=>get_direct_annos_4_entity(
      EXPORTING
        entityname =    'DEMO_CDS_ORIGINAL_ABSTRACT'
      IMPORTING
        annos      =    DATA(annos) ).
    out->write( annos ).
*
    DATA(struct) = VALUE demo_cds_original_abstract(
       col1 = 'hallo'
       col2 = 333
       col3 = 2
       col4 = 'A' ).
    out->write( struct ).

    out->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

The result set contains the column added via the extension:

ABAP Core Data Services, SAP ABAP Central, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP, SAP ABAP Tutorial and Materials

Outlook and further information


Next in the  development pipeline is the statement EXTEND CUSTOM ENTITY to extend CDS custom entities. If you are interested, keep yourself up-to-date by regularly checking our release news.

If you want to extend a CDS data model from a restricted ABAP language version, such as ABAP for Cloud Development or ABAP for Key Users, special rules are in place. Extensions are allowed only to such CDS entities that are published as released APIs under the C0 stability contract for the respective language version. The extensions must comply with rules for API consumers, in order to avoid problems, interruptions, and wrong behavior after an upgrade.

Source: sap.com

No comments:

Post a Comment