Friday, 30 June 2017

Gate Way Service Creation Using The CDS Views With Parameters

How to create the Gate Way service using CDS Views with Parameters and Without Parameters:

Pre-requisite is to learn basics about the Core Data Services , Basic understanding about SADL Frame work.

Step 1: Create the CDS view in Eclipse or Hana Studio.

Here I am creating two CDS views in Hana Studio

Creating CDS View for the House Bank Details

DDL SQL View: ZVTX_HBANK

DDL Source : ZTX_HBANK

@AbapCatalog.sqlViewName: 'zvtx_hbank'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'house bank data'
define view ztx_hbank
    as select from t012
    left outer join t001 on t001.bukrs = t012.bukrs
    left outer join t012d on t012d.bukrs = t012.bukrs
                         and t012d.hbkid = t012.hbkid                        
{
key t012.bukrs as CompanyCode,
key t012.hbkid as HouseBankKey,
butxt as CompanyName,
banks as BankCountry,
bankl as BankInternalId,
t012d.dtbid as AlternativeBankId,
t012d.dtelz as BankNoReceiver,
dtkid as CustomerNumber,
dtfin as CompanyNumber,
edipn as PartnerNumber,
edisn as TypeOfSignature,
dtglz as BankNumber,
dtgbk as AccountNumber,
dtgko as ControlKey,
dtgis as IsoCurrencyCode,
dtaws as InstructionKey,
dtvta as LeadDays,
telf1 as Telephone,
stcd1 as TaxNumber,
name1 as NameOfContactPerson
}

On Activation of CDS view got created in the BACK END. Please find the below screen shot

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Step 2: Creating the another CDS view with bank master details

Creating the CDS view ZTX_BANK in association with the ZTX_HBANK with Parameter.

DDL SQL View: ZVTX_BANK

DDL Source : ZTX_BANK

@AbapCatalog.sqlViewName: 'zvtx_bank'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'bank master'
define view ztx_bank 
    with parameters
        p_spras: sylangu
    as select from bnka 
    association[0..*] to ztx_hbank as _hbank on $projection.BankCountry    = _hbank.BankCountry
                                            and $projection.BankInternalId = _hbank.BankInternalId
{
key banks as BankCountry,
key bankl as BankInternalId,
    erdat                          as CreateOn,
    ernam                          as CreateBy,
    bnka.banka                     as BankName,
    provz                          as Region,
    stras                          as StreetName,
    ort01                          as BankCity,
    bnka.swift                     as Swift,
    bgrup                          as BankGroup,
    xpgro                          as PostOfficeAccount,
    bnklz                          as BankNumber,
    @EndUserText.label: 'Homeland or not'
    case when banks = 'CN' then  
//     'Homeland'      
     cast( 'Homeland' as abap.char( 20 ))
    else cast ('Foreign Country' as abap.char( 20 ))
    end as Country,
    _hbank.CompanyCode,
    _hbank.CompanyName,  
    _hbank    

On activation View got generated in the back end please find the attached screen shot:

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Step 3: Two CDS views created, now we are going to create the GATE WAY SERVICE using the                     CDS VIEWS.

In SAP GUI , Open the T-code SEGW

Create the project

Click on create button below POP-UP will open:

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Step 4:  In Data Model right click on Entity type and Click on Create button, below POPUP will open give the entity name and create the Entity Set and click on enter.

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Step 5: Give the ABAP Structure name as ZVTX_BANK as shown below which is created on                        activation of CDS View

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Step 6:  Right Click on Entity and Import Properties as shown below

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Select the parameters which are required and click on next

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Select the key and click on finish

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Follow the same steps for the Hbank Entity as well it looks like as below shown fig:

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Creation of Properties for the Bank Master and house bank data is done


Step 7: Creating Association BankToHbank i.e optional if you need any associations between two                    entities we can create as shown below:

Right Click on the association node and Click on Create below screen appears:

click on next

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Click on Next

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Click on Finish

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Step 8: In Service implementation:

Right Click on Service Implementation node & click on Map to Data Source

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

We have different types of business Entities as shown below:

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

In our case we are using the CDS view as a business entity:

ZTX_BANK is the CDS View Name

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Step 9: Click on continue below mapping screen will appear

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Click on generate Mapping or drag and drop the properties , below screen appears after mapping

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Follow same steps for the Hbank Entity as well below screen appears after Mapping

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Step 10: Save the Project and click on the generate run-time objects

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Create of the model and service definitions , Click on okay and save the objects

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Model Provider class and the Data provider classes generated success fully

Step 11:  Register the service i.e. Service registration in the Sap Gateway Hub System

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Click on Register button, Select the system alias

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Select the SYSTEM and add the service in the package and click on continue, Service gets added

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Step 12: Test the service

Click on the SAP Gateway Client, below screen will appear

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Select GET radio button and click on Execute button

HTTP Response with two entity sets get Displayed as shown below i.e BANKSet and HbankSet

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

For the bank set we used the CDS view with parameter i.e. Language, So add the                          parameter open ZCL_ZTX_BANK_DEMO1_DPC_EXT (Data Provider Extension class),              Redefine the method.

Note: If you don’t  need any parameter kindly remove the parameter in the CDS view                            and activate it.

IF_SADL_GW_QUERY_CONTROL~SET_QUERY_OPTIONS

Add the below code for adding the parameters to the query.

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

DATA: lt_parameters TYPE if_sadl_public_types=>tt_entity_parameters,
          ls_parameter  LIKE LINE OF lt_parameters,
          lt_para       LIKE ls_parameter-parameters,
          ls_para       LIKE LINE OF lt_para.

    DATA(lo_auth_provider) = cl_sadl_cond_prov_factory_pub=>create_for_authorization(  ).
    DATA(lv_target_set_name) = me->mr_request_details->*-target_entity_set.
    IF me->mr_request_details->technical_request-navigation_path IS INITIAL.
      CLEAR lv_target_set_name.
    ENDIF.

    CASE iv_entity_set.
      WHEN 'BANKSet'.
        lo_auth_provider->add_authorization_for_object(
          EXPORTING
            iv_authorization_object    = 'F_BNKA_MAO'
            it_activities              = VALUE #( ( auth_field = 'ACTVT' value = '03' ) )
            it_field_mapping           = VALUE #( ( auth_field = 'BBANKS' view_field = 'BANKCOUNTRY' ) ) ).
        ls_para-name = 'P_SPRAS'.
        ls_para-value = sy-langu.
        APPEND ls_para TO  lt_para.

        ls_parameter-entity_alias = iv_entity_set.
        ls_parameter-parameters = lt_para.
        APPEND ls_parameter TO lt_parameters.
        io_query_options->set_entity_parameters( lt_parameters ).
      WHEN OTHERS.
    ENDCASE.

    io_query_options->set_authority_provider( io_authority_provider = lo_auth_provider ).

If we are not adding the above code, then it will throw the below error i.e. Missing Parameter:

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

If you add the above code, we will get the http response with the data as shown below

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

Same way select the Hbank Entity set i.e Hbank cds view without Parameter so no need to redefine the set query options

SAP ABAP tutorials and Materials, SAP ABAP CDS, SAP ABAP Certifications

No comments:

Post a Comment