Quantcast
Channel: SCN : All Content - SAPUI5 Developer Center
Viewing all articles
Browse latest Browse all 6178

From Odata Service to SAPUI5 - Part 1

$
0
0

It took me a while to get from the initial steps of creation of Odata Services to the creation of SAPUI5 web app. I thought it would be a good idea to put it together in one blog so that others can make use of it. Here are some of the steps a followed:

 

Using Classes:

To create odata services, we need to follow the MVC approach. We need to create two classes. A model class and a data class. Before we create the class we create the structure of our data in se11. This example is based on the SFLIGHT table. Create the following structure. 

 

1.png

 

Now go to se80 and create model class: Z_CL_FL_MODEL_DEV20 and activate


1.png


Go to the properties tab and click on superclass. Add: /IWBEP/CL_MGW_ABS_MODEL as super class. Save. Do not activate yet


1.png


Go to methods>>inherited methods>>define. Then right click and click on redefine:

1.png

 

In this class you will be creating a new data object with name FLIGHT and will define the properties of the FLIGHT object giving ABAP dictionary names and OData friendly names. Cut copy paste this code. Some will be keys. Eventually Bind these values to the structure you initially created. 'ZSFLIGHT_ST'


****************************************************************************************************************************************************

" ABAP OData Data Object
DATA: lo_data_object
TYPE REF TO /IWBEP/IF_MGW_ODATA_ENTITY_TYP,
lo_property
TYPE REF TO /IWBEP/IF_MGW_ODATA_PROPERTY.

"Create a new data object with name Flight/WF
lo_data_object = model->create_entity_type(
'FLIGHT' ).

"Define the properties of the FLIGHT object
"IV_ABAP_FIELDNAME is the ABAP dictionary name
"IV_PROPRETY_NAME is the OData friendly name


lo_property = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'MANDT'
IV_PROPERTY_NAME =
'client' ).
"Set as a key
lo_property->set_is_key( abap_true ).

lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'CARRID'
IV_PROPERTY_NAME =
'Code' ).
"Set as a key
lo_property->set_is_key( abap_true ).

lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'CONNID'
IV_PROPERTY_NAME =
'connectionNo' ).
"Set as a key
lo_property->set_is_key( abap_true ).


lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'FLDATE'
IV_PROPERTY_NAME =
'flightDate' ).
"Set as a key
lo_property->set_is_key( abap_true ).


lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'PRICE'
IV_PROPERTY_NAME =
'price' ).
lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'CURRENCY'
IV_PROPERTY_NAME =
'currency' ).
lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'PLANETYPE'
IV_PROPERTY_NAME =
'AircraftType' ).
lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'SEATSMAX'
IV_PROPERTY_NAME =
'maxCapacity' ).
lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'SEATSOCC'
IV_PROPERTY_NAME =
'occupiedSeats' ).
lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'PAYMENTSUM'
IV_PROPERTY_NAME =
'totalOfBookings' ).
lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'SEATSMAX_B'
IV_PROPERTY_NAME =
'MaxCapacityBus' ).
lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'SEATSOCC_B'
IV_PROPERTY_NAME =
'occupiedSeatsBusiness' ).
lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'SEATSMAX_F'
IV_PROPERTY_NAME =
'maximumInFirst' ).
lo_property  = lo_data_object->create_property( IV_ABAP_FIELDNAME =
'SEATSOCC_F'
IV_PROPERTY_NAME =
'occupiedSeatsFirst' ).


"Bind your structure that represents the above properties
lo_data_object->bind_structure(
'ZSFLIGHT_ST' ).


********************************************************************************

Now you create a new data class in se80 : Z_CL_FL_DATA_DEV20  Save and activate.

 

1.png




Go to the properties tab and click on superclass. Add: /IWBEP/CL_MGW_ABS_DATA as super class. Save. Do not activate yet.

We will be redefining three methods in here.

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITY (This method will READ data)

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITYSET (This method will Filter down to a subset of the data. Also a READ operation_

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~UPDATE_ENTITY (This is an update method. Not necessary. Depends on your requirements)

 

First redefine: /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITY


1.png


Delete the comments and add this code:

*****************************************************************

method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITY.
DATA: lv_string 
TYPE string,
ls_key_tab
TYPE /IWBEP/S_MGW_NAME_VALUE_PAIR,
lv_and(4),
lv_value
TYPE string,
*         wa_WF type ZTRIPAPPROVAL_ST.
wa_FL
type ZSFLIGHT_ST.
* data: lv_Pernr type PERNR_D.

CONSTANTS: con_filler(1) 
VALUE ''''.

* Loop over the key values passed in
LOOP AT it_key_tab INTO ls_key_tab.

* surround values with ''
CONCATENATE con_filler ls_key_tab-value con_filler INTO lv_value.

* Change OData key field names to the FLIGHT table properties name
TRANSLATE ls_key_tab-name TO UPPER CASE.

IF ( ls_key_tab-name = 'CLIENT' ).
ls_key_tab-name =
'MANDT'.
ELSEIF ( ls_key_tab-name = 'CODE' ).
ls_key_tab-name =
'CARRID'.
ELSEIF ( ls_key_tab-name = 'CONNECTIONO' ).
ls_key_tab-name =
'CONNID'.
ELSEIF ( ls_key_tab-name = 'FLIGHTDATE' ).
ls_key_tab-name =
'FLDATE'.
ENDIF.




* build where the string
CONCATENATE lv_string
lv_and
ls_key_tab-name
'='
lv_value
INTO lv_string
SEPARATED BY space.
* now and is needed.
lv_and =
'and'.
ENDLOOP.


* Get the data from tables
*  SELECT SINGLE banks bankl banka provz stras ort01 adrnr FROM BNKA
*         INTO CORRESPONDING FIELDS OF wa_bnka
*         WHERE (lv_string).
***********************************************************************
select single *
from SFLIGHT
into CORRESPONDING FIELDS OF wa_FL
where (lv_string).

***********************************************************************


IF sy-subrc EQ 0.
* fill the OData structure
copy_data_to_ref(
EXPORTING
is_data = wa_FL
CHANGING
cr_data = er_entity
).
ELSE.
" throw exception
ENDIF.
endmethod.
 

***************************************************************

Here we are simply looping over our key values and getting the data from the tables.

Now redefine /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITYSET


1.png


And delete the comments. And add this code:

************************************************************************

method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITYSET.

DATA:   lt_FL                   
TYPE TABLE OF ZSFLIGHT_ST,
wa_FL                   
TYPE ZSFLIGHT_ST,
ls_filter_select_options  
TYPE /iwbep/s_mgw_select_option,
ls_mgw_select_options     
TYPE /iwbep/s_mgw_select_option,
ls_mgw_range_options      
TYPE /iwbep/s_cod_select_option,
lt_range_FL            
TYPE RANGE OF S_CARR_ID,
ls_range_FL              
LIKE LINE OF lt_range_FL,
lv_Pernr
type PERNR_D.

"Loop at the filter options internal table that is passed to this method
LOOP AT it_filter_select_options INTO ls_filter_select_options.

"Loop over the selection options internal table - each filter option can have
"more than one selection option
LOOP AT ls_filter_select_options-select_options INTO ls_mgw_range_options.

"Change to uppercase so we don't have to worry about case issues
TRANSLATE ls_filter_select_options-property TO UPPER CASE.

"Check which property is being filtered on, in our case we will
"only worry about the trip_COUTNRY field, but often multiple fields
"can be filtered on
CASE ls_filter_select_options-property.
WHEN 'CARRID'.

"Setup the range table.
MOVE-CORRESPONDING ls_mgw_range_options TO ls_range_FL.
APPEND ls_range_FL TO lt_range_FL.
ENDCASE.
ENDLOOP.
ENDLOOP.



**********get Flight data**************************************************
select *
from SFLIGHT
into table lt_FL
where CARRID IN lt_range_FL.



*************************************************************************

"Use the helper method to copy the internal table data into the
"structure of the OData Service
copy_data_to_ref(
EXPORTING is_data = lt_FL
CHANGING cr_data = er_entityset ).
endmethod.

 

************************************************************************

The above code populates the internal table based on the filter and uses a helper method to copy to the structure of the Odata service.

Now you can redefine /IWBEP/IF_MGW_APPL_SRV_RUNTIME~UPDATE_ENTITY method:


1.png

 

Delete the comments and add this code:

**********************************************************

method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~UPDATE_ENTITY.
DATA: ls_FL
TYPE SFLIGHT.
DATA: ex_ref
TYPE REF TO cx_root.
*  DATA: message_text TYPE string.



* Get the updated fl data
io_data_provider->read_entry_data(
IMPORTING es_data = ls_fl ).

modify SFLIGHT from ls_fl.
Commit work.
if sy-subrc = 0.


copy_data_to_ref(
EXPORTING
is_data = ls_fl
CHANGING
cr_data = er_entity
).
else.
*
*
*    try.
*
*RAISE EXCEPTION TYPE zcx_error
*
*Exporting
*
*message = ' '.
*    endtry.

endif.



endmethod.

*****************************************************************

 

We can similarly redefine create and delete methods or any other methods given there.


Stay tuned for Part 2.


Viewing all articles
Browse latest Browse all 6178

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>