ABAP restful programming model (RAP) – Unmanaged scenario

8728

Hello everyone!!
I am writing this blog to give an example to how to handle the unmanaged behavior implementation in ABAP restful programming model (RAP).

The ABAP RESTful programming model defines the architecture for efficient end-to-end development of intrinsically SAP HANA-optimized OData services (such as Fiori apps) in SAP Cloud Platform ABAP Environment. It supports the development of all types of Fiori applications as well as A2X services. It is based on technologies and frameworks such as Core Data Services (CDS) for defining semantically rich data models and a service model infrastructure for creating OData services with bindings to an OData protocol and ABAP-based application services for custom logic and SAPUI5-based user interfaces.

I have taken two tables for this example as shown below

Here the tables ZORDHDR is the header table and ZORDITM is the item table.

  • Below is the root cds view for which we can create the behavior definition and services.

In the above cds view, we can observe that I have used composition to item view which gives the

  • Implementation for behavior definition

For the above created behavior definition, below screen shot will show you the behavior implementation

Here the behavior implantation is nothing but a class where we can implement the our business logic. At the very bottom of this behavior implementation we can see the local types tab, where we can implement our logic.

Click on local types, in that we can implement the logic. Here I have implemented the creating, updating, deletion of data from table. We can observe how the implementation takes place in these local classes.

CLASS lhc_ZBO_A_ORDER DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    METHODS create FOR MODIFY
      IMPORTING entities FOR CREATE zbo_a_order.
    METHODS delete FOR MODIFY
      IMPORTING keys FOR DELETE zbo_a_order.
    METHODS update FOR MODIFY
      IMPORTING entities FOR UPDATE zbo_a_order.
    METHODS read FOR READ
      IMPORTING keys FOR READ zbo_a_order RESULT result.
    METHODS cba_ITEM FOR MODIFY
      IMPORTING entities_cba FOR CREATE zbo_a_order\_item.
    METHODS rba_ITEM FOR READ
      IMPORTING keys_rba FOR READ zbo_a_order\_item FULL result_requested RESULT result LINK association_links.
ENDCLASS.
CLASS lhc_ZBO_A_ORDER IMPLEMENTATION.
  METHOD create.
    DATA: lt_orders TYPE TABLE OF zordhdr.
    lt_orders = VALUE #(
        FOR ls_order IN entities
        (
            ordid = ls_order-ordid
            descr = ls_order-descr
            ord_date = ls_order-ord_date
            mbl_no = ls_order-mbl_no
        )
    ).
    INSERT zordhdr FROM TABLE lt_orders.
  ENDMETHOD.
  METHOD delete.
    DATA: lt_orders TYPE RANGE OF char10.
    lt_orders = VALUE #(
        FOR ls_order IN keys
        (
            sign  = ‘I’
            option = ‘EQ’
            low = ls_order-ordid
        )
    ).
    DELETE FROM zordhdr WHERE ordid IN lt_orders.
  ENDMETHOD.
  METHOD update.
    DATA: lt_orders TYPE TABLE OF zordhdr.
    lt_orders = VALUE #(
        FOR ls_order IN entities
        (
            ordid = ls_order-ordid
            descr = ls_order-descr
            ord_date = ls_order-ord_date
            mbl_no = ls_order-mbl_no
        )
    ).
    UPDATE zordhdr FROM TABLE lt_orders.
  ENDMETHOD.
  METHOD read.
  ENDMETHOD.
  METHOD cba_ITEM.
    DATA: lt_items TYPE TABLE OF zorditm.
    lt_items = VALUE #(
       FOR ls_entity IN entities_cba
       FOR ls_item IN ls_entity-%target
       (
          ordid = ls_item-ordid
          orditm = ls_item-orditm
          item_descr = ls_item-item_descr
          item_cost = ls_item-item_cost
       )
    ).
    INSERT zorditm FROM TABLE lt_items.
  ENDMETHOD.
  METHOD rba_ITEM.
  ENDMETHOD.
ENDCLASS.
CLASS lhc_ZBO_ITEM DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    METHODS create FOR MODIFY
      IMPORTING entities FOR CREATE zbo_item.
    METHODS delete FOR MODIFY
      IMPORTING keys FOR DELETE zbo_item.
    METHODS update FOR MODIFY
      IMPORTING entities FOR UPDATE zbo_item.
    METHODS read FOR READ
      IMPORTING keys FOR READ zbo_item RESULT result.
ENDCLASS.
CLASS lhc_ZBO_ITEM IMPLEMENTATION.
  METHOD create.
  ENDMETHOD.
  METHOD delete.
  ENDMETHOD.
  METHOD update.
  ENDMETHOD.
  METHOD read.
  ENDMETHOD.
ENDCLASS.
CLASS lsc_ZBO_A_ORDER DEFINITION INHERITING FROM cl_abap_behavior_saver.
  PROTECTED SECTION.
    METHODS check_before_save REDEFINITION.
    METHODS finalize          REDEFINITION.
    METHODS save              REDEFINITION.
ENDCLASS.
CLASS lsc_ZBO_A_ORDER IMPLEMENTATION.
  METHOD check_before_save.
  ENDMETHOD.
  METHOD finalize.
  ENDMETHOD.
  METHOD save.
  ENDMETHOD.
ENDCLASS.

In the above code snippet, you may observe that separate create, update, delete methods to update the data to table of an entity.

We can also handle these crud operations via one method called ‘modify’. But SAP suggests an architecture which says like we need to hold the data in a global tables(buffer tables) in modify method.

Then implement the crud operation in save method of saver class(lsc_ZBO_A_ORDER). These methods will automatically called means we don’t need to call the methods manually. And not only these there, are some other methods like check_before_save, adjust_nmber which we can use basing on our requirement.

  • Service definition

Once we completed with our implementation part, we can create service definition as shown below.

  • Service binding

For above created service definition, below is the service binding and activation of service

Click on activate to activate the service.

If you liked this post, please hit the share buttons.

Thank you very much for your time!!

8,728 Comments

  1. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  2. Right here is the right site for anybody who wants to understand this topic. You know a whole lot its almost tough to argue with you (not that I really would want toÖHaHa). You definitely put a brand new spin on a topic that has been written about for a long time. Wonderful stuff, just excellent!
    ישראל אקס קלאב