Created
July 21, 2014 04:48
-
-
Save js1972/1be089098f5eb4ff1288 to your computer and use it in GitHub Desktop.
Sample OData service for a basic User model. Shows how to provide filtering and sorting and also a Function Import.
This uses the ODC (OData Channel) method for implementing an OData service which is the recommended approach by SAP.
This sample shows two method redefinition's to get the entity by key value and to get an entity set by searching.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class zcl_zusers_dpc_ext definition | |
public | |
inheriting from zcl_zusers_dpc | |
create public . | |
public section. | |
methods /iwbep/if_mgw_appl_srv_runtime~execute_action redefinition. | |
protected section. | |
methods userset_get_entity redefinition. | |
methods userset_get_entityset redefinition. | |
private section. | |
endclass. | |
class zcl_zusers_dpc_ext implementation. | |
method /iwbep/if_mgw_appl_srv_runtime~execute_action. | |
data user_entity type zcl_zusers_mpc=>ts_user. | |
data address type bapiaddr3. | |
data logondata type bapilogond. | |
data return_tab type bapiret2_tab. | |
if iv_action_name = 'GetUserID'. | |
call function 'BAPI_USER_GET_DETAIL' | |
exporting | |
username = sy-uname | |
cache_results = ' ' | |
importing | |
address = address | |
logondata = logondata | |
tables | |
return = return_tab. | |
user_entity-userid = sy-uname. | |
user_entity-firstname = address-firstname. | |
user_entity-lastname = address-lastname. | |
data tz type tznzone. | |
convert date logondata-gltgb into time stamp user_entity-validto time zone tz. | |
copy_data_to_ref( | |
exporting is_data = user_entity | |
changing cr_data = er_data ). | |
endif. | |
endmethod. | |
method userset_get_entity. | |
try. | |
data(key) = it_key_tab[ name = 'UserID' ]-value. | |
catch cx_sy_itab_line_not_found. | |
endtry. | |
if key is initial. | |
data(msg) = value scx_t100key( msgid = 'SY' msgno = '002' attr1 = 'UserID not found' ). | |
raise exception type /iwbep/cx_mgw_busi_exception | |
exporting | |
textid = msg. | |
endif. | |
data(sap_user_id) = value xubname( ). | |
sap_user_id = key. | |
data address type bapiaddr3. | |
data logondata type bapilogond. | |
data return_tab type bapiret2_tab. | |
call function 'BAPI_USER_GET_DETAIL' | |
exporting | |
username = sap_user_id | |
cache_results = ' ' | |
importing | |
address = address | |
logondata = logondata | |
tables | |
return = return_tab. | |
er_entity-userid = sap_user_id. | |
er_entity-firstname = address-firstname. | |
er_entity-lastname = address-lastname. | |
try. | |
cl_abap_tstmp=>systemtstmp_syst2utc( | |
exporting | |
syst_date = logondata-gltgb | |
syst_time = value t( ) | |
importing | |
utc_tstmp = er_entity-validto | |
). | |
catch cx_parameter_invalid_range. | |
clear er_entity-validto. | |
endtry. | |
endmethod. | |
method userset_get_entityset. | |
data return_tab type bapiret2_tab. | |
data userlist type standard table of bapiusname with empty key. | |
data selection_range type standard table of bapiussrge. | |
try. | |
data(select_options_user) = it_filter_select_options[ property = 'UserID' ]-select_options. | |
loop at select_options_user assigning field-symbol(<filter>). | |
append initial line to selection_range assigning field-symbol(<f>). | |
<f>-sign = <filter>-sign. | |
<f>-option = <filter>-option. | |
<f>-low = <filter>-low. | |
<f>-high = <filter>-high. | |
<f>-parameter = 'USERNAME'. | |
endloop. | |
catch cx_sy_itab_line_not_found. | |
clear select_options_user. | |
endtry. | |
try. | |
data(select_options_firstname) = it_filter_select_options[ property = 'Firstname' ]-select_options. | |
loop at select_options_firstname assigning field-symbol(<filter_f>). | |
append initial line to selection_range assigning field-symbol(<f_f>). | |
<f_f>-sign = <filter_f>-sign. | |
<f_f>-option = <filter_f>-option. | |
<f_f>-low = <filter_f>-low. | |
<f_f>-high = <filter_f>-high. | |
<f_f>-parameter = 'ADDRESS'. | |
<f_f>-field = 'FIRSTNAME'. | |
endloop. | |
catch cx_sy_itab_line_not_found. | |
clear select_options_firstname. | |
endtry. | |
try. | |
data(select_options_lastname) = it_filter_select_options[ property = 'Lastname' ]-select_options. | |
loop at select_options_lastname assigning field-symbol(<filter_l>). | |
append initial line to selection_range assigning field-symbol(<f_l>). | |
<f_l>-sign = <filter_l>-sign. | |
<f_l>-option = <filter_l>-option. | |
<f_l>-low = <filter_l>-low. | |
<f_l>-high = <filter_l>-high. | |
<f_l>-parameter = 'ADDRESS'. | |
<f_l>-field = 'LASTNAME'. | |
endloop. | |
catch cx_sy_itab_line_not_found. | |
clear select_options_lastname. | |
endtry. | |
"Paging | |
data(top) = is_paging-top. | |
data(skip) = is_paging-skip. | |
if skip > 0. | |
top = top + skip. | |
endif. | |
call function 'BAPI_USER_GETLIST' | |
exporting | |
max_rows = top | |
with_username = 'X' | |
tables | |
selection_range = selection_range | |
userlist = userlist | |
return = return_tab. | |
"Paging | |
if skip > 0. | |
delete userlist to skip. | |
endif. | |
data entity like line of et_entityset. | |
data validity_end_date type d. | |
loop at userlist assigning field-symbol(<u>). | |
clear entity. | |
entity-userid = <u>-username. | |
entity-firstname = <u>-firstname. | |
entity-lastname = <u>-lastname. | |
select single gltgb into validity_end_date | |
from usr02 | |
where bname = <u>-username. | |
try. | |
cl_abap_tstmp=>systemtstmp_syst2utc( | |
exporting | |
syst_date = validity_end_date | |
syst_time = value t( ) | |
importing | |
utc_tstmp = entity-validto | |
). | |
catch cx_parameter_invalid_range. | |
clear entity-validto. | |
endtry. | |
append entity to et_entityset. | |
endloop. | |
"Order By | |
try. | |
data(orderby) = it_order[ 1 ]. | |
if orderby-order eq 'desc'. | |
case orderby-property. | |
when 'UserID'. | |
sort et_entityset by userid descending. | |
when 'Firstname'. | |
sort et_entityset by firstname descending. | |
when 'Lastname'. | |
sort et_entityset by lastname descending. | |
endcase. | |
elseif orderby-order eq 'asc'. | |
case orderby-property. | |
when 'UserID'. | |
sort et_entityset by userid ascending. | |
when 'Firstname'. | |
sort et_entityset by firstname ascending. | |
when 'Lastname'. | |
sort et_entityset by lastname ascending. | |
endcase. | |
endif. | |
catch cx_sy_itab_line_not_found. | |
endtry. | |
"Inlinecount | |
if io_tech_request_context->has_inlinecount( ) = abap_true. | |
describe table et_entityset lines es_response_context-inlinecount. | |
else. | |
clear es_response_context-inlinecount. | |
endif. | |
endmethod. | |
endclass. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment