Skip to Content

Define and Expose a CDS-Based Travel Data Model

Define CDS-based data model and create projection view.
You will learn
  • How to create CDS based data model
  • How to create projection view
  • How to create service definition
  • How to create service binding
mervey45Merve TemelMarch 17, 2022
Created by
mervey45
September 6, 2019
Contributors
mervey45
mervey45

Prerequisites

In this tutorial, wherever XXX appears, use a number (e.g. 000).

If you also want to deploy your SAP Fiori application, please finish this tutorial first and then continue with
Develop a Fiori App Using the ABAP RESTful Application Programming Model (Managed Scenario) starting with following tutorial Create Behavior Definition for Managed Scenario.

If you want to deploy your SAP Fiori application, please continue with
Develop a Fiori App Using the ABAP RESTful Application Programming Model (Managed Scenario) starting with following tutorial Create Behavior Definition for Managed Scenario.

After completing this mission, you might be interested in the next in the series: Level Up with SAP BTP, ABAP Environment

  • Step 1
    1. Right-click on your package ZTRAVEL_APP_XXX, select New > Other ABAP Repository Object.

      Define CDS based travel data model
    2. Search for data definition, select it and click Next >.

      Define CDS based travel data model
    3. Create a data definition:

      • Name: ZI_TRAVEL_M_XXX
      • Description: Data model for travel

      Click Next >.

      Define CDS based travel data model
    4. Click Finish to use your transport request.

      Define CDS based travel data model
    5. Add associations, fields, UI semantics and the root node to your data definition. Therefore replace your code with following:

      ABAP
      Copy
      @EndUserText.label: 'Travel data XXX'
      @AccessControl.authorizationCheck: #CHECK
      define root view entity ZI_TRAVEL_M_XXX
      
        as select from ztravel_xxx as Travel
      
        /* Associations */
        association [0..1] to /DMO/I_Agency   as _Agency   on $projection.agency_id = _Agency.AgencyID
        association [0..1] to /DMO/I_Customer as _Customer on $projection.customer_id = _Customer.CustomerID
        association [0..1] to I_Currency      as _Currency on $projection.currency_code = _Currency.Currency
      
      {
      
        key mykey,
            travel_id,
            agency_id,
            customer_id,
            begin_date,
            end_date,
            @Semantics.amount.currencyCode: 'currency_code'
            booking_fee,
            @Semantics.amount.currencyCode: 'currency_code'
            total_price,
            currency_code,
            overall_status,
            description,
      
            /*-- Admin data --*/
            @Semantics.user.createdBy: true
            created_by,
            @Semantics.systemDateTime.createdAt: true
            created_at,
            @Semantics.user.lastChangedBy: true
            last_changed_by,
            @Semantics.systemDateTime.lastChangedAt: true
            last_changed_at,
      
            /* Public associations */
            _Agency,
            _Customer,
            _Currency
      }
      
    6. Save and activate.

      save and activate

      Your CDS view for travel booking is defined now. You can use and manipulate data that is persisted in your database.

  • Step 2
    1. Right-click on your package ZTRAVEL_APP_XXX, select New > Other ABAP Repository Object.

      Create projection view for travel
    2. Search for data definition, select it and click Next >.

      Create projection view for travel
    3. Create a data definition:

      • Name: ZC_TRAVEL_M_XXX
      • Description: Projection view for travel

      Click Next >.

      Create projection view for travel
    4. Click Finish to use your transport request.

      Create projection view for travel
    5. Replace your code with following:

      ABAP
      Copy
      @EndUserText.label: 'Travel projection view - Processor'
      @AccessControl.authorizationCheck: #NOT_REQUIRED
      
      @UI: {
       headerInfo: { typeName: 'Travel', typeNamePlural: 'Travels', title: { type: #STANDARD, value: 'TravelID' } } }
      
      @Search.searchable: true
      
      define root view entity ZC_TRAVEL_M_XXX
        as projection on ZI_TRAVEL_M_XXX
      {
            @UI.facet: [ { id:              'Travel',
                           purpose:         #STANDARD,
                           type:            #IDENTIFICATION_REFERENCE,
                           label:           'Travel',
                           position:        10 } ]
      
            @UI.hidden: true
        key mykey              as TravelUUID,
      
      
            @UI: {
                lineItem:       [ { position: 10, importance: #HIGH } ],
                identification: [ { position: 10, label: 'Travel ID [1,...,99999999]' } ] }
            @Search.defaultSearchElement: true
            travel_id          as TravelID,
      
            @UI: {
                lineItem:       [ { position: 20, importance: #HIGH } ],
                identification: [ { position: 20 } ],
                selectionField: [ { position: 20 } ] }
            @Consumption.valueHelpDefinition: [{ entity : {name: '/DMO/I_Agency', element: 'AgencyID'  } }]
      
            @ObjectModel.text.element: ['AgencyName'] ----meaning?
            @Search.defaultSearchElement: true
            agency_id          as AgencyID,
            _Agency.Name       as AgencyName,
      
            @UI: {
                lineItem:       [ { position: 30, importance: #HIGH } ],
                identification: [ { position: 30 } ],
                selectionField: [ { position: 30 } ] }
            @Consumption.valueHelpDefinition: [{ entity : {name: '/DMO/I_Customer', element: 'CustomerID'  } }]
      
            @ObjectModel.text.element: ['CustomerName']
            @Search.defaultSearchElement: true
            customer_id        as CustomerID,
      
            @UI.hidden: true
            _Customer.LastName as CustomerName,
      
            @UI: {
                lineItem:       [ { position: 40, importance: #MEDIUM } ],
                identification: [ { position: 40 } ] }
            begin_date         as BeginDate,
      
            @UI: {
                lineItem:       [ { position: 41, importance: #MEDIUM } ],
                identification: [ { position: 41 } ] }
            end_date           as EndDate,
      
            @UI: {
                lineItem:       [ { position: 50, importance: #MEDIUM } ],
                identification: [ { position: 50, label: 'Total Price' } ] }
            @Semantics.amount.currencyCode: 'CurrencyCode'
            total_price        as TotalPrice,
      
            @Consumption.valueHelpDefinition: [{entity: {name: 'I_Currency', element: 'Currency' }}]
            currency_code      as CurrencyCode,
      
            @UI: {
            lineItem:       [ { position: 60, importance: #HIGH },
                              { type: #FOR_ACTION, dataAction: 'acceptTravel', label: 'Accept Travel' } ],
            identification: [ { position: 60, label: 'Status [O(Open)|A(Accepted)|X(Canceled)]' } ]  }
            overall_status     as TravelStatus,
      
            @UI.identification: [ { position: 70, label: 'Remarks' } ]
            description        as Description,
      
            @UI.hidden: true
            last_changed_at    as LastChangedAt
      
      }
      
      
    6. Save and activate.

      save and activate

      The projection is created and includes UI annotations. The projection is the subset of the fields of the travel data model, which are relevant for the travel booking application.

  • Step 3
    1. Right-click on your data definition ZC_TRAVEL_M_XXX and select New Service Definition.

      Create service definition
    2. Create a new service definition:

      • Name: ZUI_C_TRAVEL_M_XXX
      • Description: Service definition for travel

      Click Next >.

      Create service definition
    3. Click Finish to use your transport request.

      Create service definition
    4. Replace your code with following:

      ABAP
      Copy
      @EndUserText.label: 'Service Definition for ZC_Travel_M_XXX'
      define service ZUI_C_TRAVEL_M_XXX
      {
        expose ZC_TRAVEL_M_XXX as TravelProcessor;
        expose /DMO/I_Customer as Passenger;
        expose /DMO/I_Agency as TravelAgency;
        expose /DMO/I_Airport as Airport;
        expose I_Currency as Currency;
        expose I_Country as Country;
      }   
      
    5. Save and activate.

      save and activate

      With the service definition you are able to define which data is exposed as a business service in your travel booking application.

  • Step 4
    1. Right-click on your service definition ZUI_C_TRAVEL_M_XXX and select New Service Binding.

      Create service binding
    2. Create a new service binding:

      • Name: ZUI_C_TRAVEL_M_XXX
      • Description: Service binding for travel
      • Binding Type: ODATA V2 - UI

      Click Next >.

      Create service binding
    3. Click Finish to use your transport request.

      Create service binding
    4. Activate your service binding and then publish it.

      Create service binding

      Now your service binding is created and you are able to see your service with its entities and associations.

      The service binding allows you to bind the service definition to an ODATA protocol. Therefore you are able to see the travel booking application on the UI.

  • Step 5

    Which ABAP repository object offers you the option to open your SAP Fiori Elements App Preview?

Back to top