Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class zkhr_gw_odata_dpc_ext definition
- public
- inheriting from zkhr_gw_odata_dpc
- create public .
- " this class is used to implement business logic in processing requests to a odata service
- " by implementing exact methods of inherited base class we are able to change request processing flow
- " moreover, only in that way we are able to implement some features of odata service, like search, expand, filter, etc.
- public section.
- " class constants for implementing custom query parameter
- constants old_param type string value `OLD`. " parameter name
- constants old_border type int2 value 65.
- " to get expanded entity with $expand query option we should redefine this method
- methods /iwbep/if_mgw_appl_srv_runtime~get_expanded_entity redefinition.
- " to process and implement custom actions to odata service we should redefine this method
- methods /iwbep/if_mgw_appl_srv_runtime~execute_action redefinition.
- " to create deep entity (with depended instances in one request) we should redefine this method
- methods /iwbep/if_mgw_appl_srv_runtime~create_deep_entity redefinition.
- " to process $batch query option we should redefine three methods of pre- post and process of request
- " they implement many request processing at one time
- methods /iwbep/if_mgw_appl_srv_runtime~changeset_begin redefinition.
- methods /iwbep/if_mgw_appl_srv_runtime~changeset_process redefinition.
- methods /iwbep/if_mgw_appl_srv_runtime~changeset_end redefinition.
- protected section.
- " give access to set of entity and returns many of them in GET
- methods employeeset_get_entityset redefinition.
- " process access to one exact entity by GET
- methods employeeset_get_entity redefinition.
- " process creation of the new entity by POST request
- methods employeeset_create_entity redefinition.
- " implements logic of entry updating by PUT request
- methods employeeset_update_entity redefinition.
- " process deletion of an entry by DELETE request
- methods employeeset_delete_entity redefinition.
- " another GET request only for one task
- methods taskset_get_entity redefinition.
- " another GET request for set of tasks
- methods taskset_get_entityset redefinition.
- private section.
- " flag, that indicate if errors occurred while processing $batch request
- data changeset_errors_occurred type abap_bool value abap_false.
- methods parse_custom_uri_parameters
- importing
- request type ref to /iwbep/cl_mgw_request
- exporting
- parameters type /iwbep/t_mgw_name_value_pair.
- endclass.
- class zkhr_gw_odata_dpc_ext implementation.
- method employeeset_get_entityset.
- data filters type /iwbep/t_mgw_select_option.
- data filter type /iwbep/s_mgw_select_option.
- data so_employeeid type /iwbep/s_cod_select_option.
- data so_employeeid_table type table of /iwbep/s_cod_select_option.
- data so_employeename type /iwbep/s_cod_select_option.
- data so_employeename_table type table of /iwbep/s_cod_select_option.
- data orderby_table type /iwbep/t_mgw_tech_order.
- data orderby type /iwbep/s_mgw_tech_order.
- data top type int4.
- data skip type int4.
- data employeeset type zkhr_gw_odata_mpc=>tt_employee.
- data employee type zkhr_gw_odata_mpc=>ts_employee.
- data count type i.
- data search_string type string.
- data parameters type /iwbep/t_mgw_name_value_pair.
- data request type ref to /iwbep/cl_mgw_request.
- " downcasting of request object and its technical fields
- request ?= io_tech_request_context.
- " parsing custom uri
- me->parse_custom_uri_parameters(
- exporting
- request = request
- importing
- parameters = parameters ).
- try.
- " attempt to get value of custom query parameter
- data(old_only_param) = parameters[ name = me->old_param ].
- catch cx_sy_itab_line_not_found.
- endtry.
- " getting search string from the request context
- search_string = io_tech_request_context->get_search_string( ).
- " search processing (does not work in our system but still)
- if search_string is not initial.
- insert value #( sign = 'I' option = 'CP' low = |*{ search_string }*| ) into table so_employeename_table.
- insert value #( sign = 'I' option = 'CP' low = |*{ search_string }*| ) into table so_employeeid_table.
- select * from zkhr_employee into corresponding fields of table employeeset
- where zkhr_employee~employeeid in so_employeeid_table
- or zkhr_employee~employeename in so_employeename_table.
- " processing custom parameter
- elseif old_only_param is not initial and to_lower( old_only_param-value ) = 'true'.
- select * from zkhr_employee into corresponding fields of table employeeset
- where zkhr_employee~employeeage ge me->old_border.
- " in the end try to get and use filters
- else.
- " getting info about filters from the request context
- filters = io_tech_request_context->get_filter( )->get_filter_select_options( ).
- if filters is not initial.
- " there filtering works only with two fields of the entity
- " you should add more logic to enable additional filtering
- " getting filter property of the entity
- read table filters with table key property = 'EMPLOYEEID' into filter.
- if sy-subrc eq 0.
- " if there about employee id we add all values to an internal table
- loop at filter-select_options into so_employeeid.
- insert so_employeeid into table so_employeeid_table.
- endloop.
- endif.
- read table filters with table key property = 'EMPLOYEENAME' into filter.
- if sy-subrc eq 0.
- loop at filter-select_options into so_employeename.
- insert so_employeename into table so_employeename_table.
- endloop.
- endif.
- endif.
- " selecting entries using filters, which got previously
- select * from zkhr_employee into corresponding fields of table employeeset
- where zkhr_employee~employeeid in so_employeeid_table
- and zkhr_employee~employeename in so_employeename_table.
- " ets ands, you can add more
- endif.
- " $orderby processing
- " getting info about ordering parameter
- orderby_table = io_tech_request_context->get_orderby( ).
- if orderby_table is not initial.
- " getting first ordering object
- read table orderby_table into orderby index 1.
- if sy-subrc eq 0.
- " choosing the way to order selected entries
- if orderby-order eq 'desc'.
- " choosing field of ordering
- case orderby-property.
- when 'EMPLOYEEID'.
- sort employeeset by employeeid descending.
- when 'EMPLOYEEAGE'.
- sort employeeset by employeeage descending.
- endcase.
- elseif orderby-order eq 'asc'.
- case orderby-property.
- when 'EMPLOYEEID'.
- sort employeeset by employeeid descending.
- when 'EMPLOYEEAGE'.
- sort employeeset by employeeage descending.
- endcase.
- endif.
- endif.
- endif.
- " getting value of $top query parameter
- top = io_tech_request_context->get_top( ).
- "getting value of $skip query parameter
- skip = io_tech_request_context->get_skip( ).
- " making post processing according to top and skip parameters
- if skip is not initial.
- delete employeeset to skip.
- endif.
- if top is not initial.
- delete employeeset to top + 1.
- endif.
- et_entityset = employeeset.
- endmethod.
- method employeeset_get_entity.
- data keys type /iwbep/t_mgw_tech_pairs.
- data employee_id type zkhr_gw_odata_mpc=>ts_employee-employeeid.
- data key_tab type /iwbep/s_mgw_tech_pair.
- " getting value of the required entry id
- keys = io_tech_request_context->get_keys( ).
- read table keys into key_tab with key name = 'EMPLOYEEID'.
- if sy-subrc eq 0.
- employee_id = key_tab-value.
- endif.
- " getting exact one entry
- select single * from zkhr_employee into corresponding fields of er_entity
- where employeeid = employee_id.
- if er_entity is initial.
- raise exception type /iwbep/cx_mgw_busi_exception
- exporting
- textid = /iwbep/cx_mgw_busi_exception=>business_error
- entity_type = io_tech_request_context->get_entity_type_name( )
- http_status_code = /iwbep/cx_mgw_busi_exception=>gcs_http_status_codes-not_found
- message = |Employee with id { employee_id } id does not exist.|.
- endif.
- endmethod.
- method employeeset_create_entity.
- data employee like er_entity.
- " getting fields of a brand new entity
- io_data_provider->read_entry_data( importing es_data = employee ).
- " filling admin field
- employee-mandt = sy-mandt.
- " filling returning object for response
- er_entity = employee.
- " adding new entry to the active table
- insert into zkhr_employee values employee.
- endmethod.
- method employeeset_update_entity.
- data requested_input_data like er_entity.
- " getting fields and their value to update
- io_data_provider->read_entry_data( importing es_data = requested_input_data ).
- " filling admin field. again
- requested_input_data-mandt = sy-mandt.
- " updating requested entry
- update zkhr_employee from requested_input_data.
- " if something went wrong, raise business exception
- " and remember, you should always return something in such requests
- if sy-subrc <> 0.
- raise exception type /iwbep/cx_mgw_busi_exception
- exporting
- textid = /iwbep/cx_mgw_busi_exception=>business_error
- entity_type = io_tech_request_context->get_entity_type_name( )
- http_status_code = /iwbep/cx_mgw_busi_exception=>gcs_http_status_codes-not_found
- message = |Employee with { requested_input_data-employeeid } id does not exist.|.
- endif.
- endmethod.
- method employeeset_delete_entity.
- data key_tab type /iwbep/s_mgw_tech_pair.
- data employee_id type zkhr_gw_odata_mpc=>ts_employee-employeeid.
- data keys type /iwbep/t_mgw_tech_pairs.
- " getting keys of the request
- keys = io_tech_request_context->get_keys( ).
- " getting key value of id to delete
- read table keys with key name = 'EMPLOYEEID' into key_tab.
- if sy-subrc = 0.
- employee_id = key_tab-value.
- endif.
- " deleting entry from the active table
- delete from zkhr_employee where employeeid = employee_id.
- if sy-subrc <> 0.
- raise exception type /iwbep/cx_mgw_busi_exception
- exporting
- textid = /iwbep/cx_mgw_busi_exception=>business_error
- entity_type = io_tech_request_context->get_entity_type_name( )
- http_status_code = /iwbep/cx_mgw_busi_exception=>gcs_http_status_codes-not_found
- message = |Couldn't delete employee: employee with { employee_id } id does not exist.|.
- endif.
- endmethod.
- method /iwbep/if_mgw_appl_srv_runtime~get_expanded_entity.
- data deep_employee type zkhr_gw_odata_mpc_ext=>deep_employee.
- data keys type /iwbep/s_mgw_name_value_pair.
- data employeeid type zkhr_gw_odata_mpc=>ts_employee-employeeid.
- data expanded_clause like line of et_expanded_tech_clauses.
- data compare_result type io_expand->ty_e_compare_result.
- data entityset_name type /iwbep/mgw_tech_name.
- " getting info about navigation in request to expand
- " you can use only predefined
- compare_result = io_expand->compare_to_tech_names( 'EMPLOYEETOTASKSNAV' ).
- " getting name of the entity to expand
- entityset_name = io_tech_request_context->get_entity_set_name( ).
- " now we have info about entityset and navigation, so let
- if entityset_name eq 'EmployeeSet' and ( compare_result eq io_expand->gcs_compare_result-match_subset
- or compare_result eq io_expand->gcs_compare_result-match_equals ).
- " filling key table with entities to expand
- read table it_key_tab into keys with key name = 'EmployeeId'.
- if sy-subrc = 0.
- employeeid = keys-value.
- endif.
- " selecting one entity
- select single * from zkhr_employee into corresponding fields of deep_employee
- where employeeid = employeeid.
- " selecting all depended entities
- if deep_employee is not initial.
- select * from zkhr_task into corresponding fields of table deep_employee-employeetotasksnav
- where employeeid = employeeid.
- endif.
- expanded_clause = 'EMPLOYEETOTASKSNAV'.
- append expanded_clause to et_expanded_tech_clauses.
- endif.
- " filling response objects
- me->copy_data_to_ref(
- exporting
- is_data = deep_employee
- changing
- cr_data = er_entity ).
- endmethod.
- method taskset_get_entity.
- data keys type /iwbep/t_mgw_tech_pairs.
- data taskid type zkhr_gw_odata_mpc=>ts_task-taskid.
- data key_tab type /iwbep/s_mgw_tech_pair.
- keys = io_tech_request_context->get_keys( ).
- read table keys into key_tab with key name = 'TASKID'.
- if sy-subrc eq 0.
- taskid = key_tab-value.
- endif.
- select single * from zkhr_task into corresponding fields of er_entity
- where taskid = taskid.
- if er_entity is initial.
- raise exception type /iwbep/cx_mgw_busi_exception
- exporting
- textid = /iwbep/cx_mgw_busi_exception=>business_error
- entity_type = io_tech_request_context->get_entity_type_name( )
- http_status_code = /iwbep/cx_mgw_busi_exception=>gcs_http_status_codes-not_found
- message = |Tasl with { taskid } id does not exist.|.
- endif.
- endmethod.
- method taskset_get_entityset.
- data key type /iwbep/s_mgw_tech_pair.
- data keys type /iwbep/t_mgw_tech_pairs.
- data employeeid type zkhr_gw_odata_mpc=>ts_employee-employeeid.
- data taskid type zkhr_gw_odata_mpc=>ts_task-taskid.
- keys = io_tech_request_context->get_source_keys( ).
- read table keys into key with key name = 'EMPLOYEEID'.
- if sy-subrc = 0.
- employeeid = key-value.
- endif.
- if employeeid is not initial.
- select * from zkhr_task into corresponding fields of table et_entityset
- where zkhr_task~employeeid = employeeid.
- else.
- select * from zkhr_task into corresponding fields of table et_entityset.
- endif.
- endmethod.
- method parse_custom_uri_parameters.
- " request check
- if request is not bound.
- return.
- endif.
- clear parameters.
- " getting request string
- read table request->get_request_details( )-technical_request-request_header
- into data(request_header) with key name = '~request_uri'.
- if sy-subrc = 0.
- " filling system field with index of question mark in request string
- search request_header-value for '?'.
- if sy-subrc = 0.
- " splitting substring with parameters by & clause
- data(pos) = sy-fdpos + 1.
- split request_header-value+pos at '&' into table data(uri_params).
- " looping through parameters and filling name-value table
- loop at uri_params into data(param).
- append initial line to parameters assigning field-symbol(<init_param>).
- split param at '=' into <init_param>-name <init_param>-value.
- <init_param>-name = to_upper( <init_param>-name ).
- endloop.
- endif.
- endif.
- endmethod.
- method /iwbep/if_mgw_appl_srv_runtime~execute_action.
- data parameter type /iwbep/s_mgw_name_value_pair.
- data employee type zkhr_gw_odata_mpc=>ts_employee.
- data employees type zkhr_gw_odata_mpc=>tt_employee.
- data task type zkhr_gw_odata_mpc=>ts_task.
- " processing request according to action name
- " ATTANTION! case is important
- case iv_action_name.
- when 'GetEmployeesByAge'.
- clear employees.
- " getting parameter according to imported function signature
- read table it_parameter into parameter with key name = 'Age'.
- data age type int2.
- if sy-subrc = 0.
- age = parameter-value.
- endif.
- " selecting employees with exact age
- select * from zkhr_employee into table employees
- where employeeage = age.
- " if we couldn't get anything, do response with error
- if employees is initial.
- raise exception type /iwbep/cx_mgw_busi_exception
- exporting
- textid = /iwbep/cx_mgw_busi_exception=>business_error
- message = 'Could not find employee data.'.
- endif.
- " copying selected data
- me->copy_data_to_ref(
- exporting
- is_data = employees
- changing
- cr_data = er_data ).
- when 'ChangeTaskStatus'.
- clear task.
- read table it_parameter into parameter with key name = 'TaskId'.
- data taskid type string.
- if sy-subrc = 0.
- taskid = parameter-value.
- endif.
- select single * from zkhr_task into corresponding fields of task
- where taskid = taskid.
- if task is initial.
- raise exception type /iwbep/cx_mgw_busi_exception
- exporting
- textid = /iwbep/cx_mgw_busi_exception=>business_error
- message = |Could not find task with { taskid } id.|.
- endif.
- task-taskstatus = cond #( when task-taskstatus = 'C'
- then 'O'
- else 'C' ).
- update zkhr_task from task.
- if sy-subrc <> 0.
- raise exception type /iwbep/cx_mgw_busi_exception
- exporting
- textid = /iwbep/cx_mgw_busi_exception=>business_error
- message = |Could not update task with { taskid } id.|.
- endif.
- me->copy_data_to_ref(
- exporting
- is_data = task
- changing
- cr_data = er_data ).
- endcase.
- endmethod.
- method /iwbep/if_mgw_appl_srv_runtime~create_deep_entity.
- data deep_employee type zkhr_gw_odata_mpc_ext=>deep_employee.
- data employee type zkhr_gw_odata_mpc=>ts_employee.
- data tasks type zkhr_gw_odata_mpc=>tt_task.
- " filling predefined deep entity fields with values from the request body
- io_data_provider->read_entry_data( importing
- es_data = deep_employee ).
- move-corresponding deep_employee to employee.
- employee-mandt = sy-mandt.
- " creating main entity
- insert into zkhr_employee values employee.
- " creating depended entities
- loop at deep_employee-employeetotasksnav into data(task).
- task-employeeid = employee-employeeid.
- task-mandt = sy-mandt.
- insert task into table tasks.
- endloop.
- insert zkhr_task from table tasks.
- " as always, return created entries
- me->copy_data_to_ref( exporting
- is_data = deep_employee
- changing
- cr_data = er_deep_entity ).
- endmethod.
- method /iwbep/if_mgw_appl_srv_runtime~changeset_begin.
- " build-up before $batch processing
- me->changeset_errors_occurred = abap_false.
- if lines( it_operation_info ) > 1.
- cv_defer_mode = abap_true.
- endif.
- endmethod.
- method /iwbep/if_mgw_appl_srv_runtime~changeset_end.
- " if something went wrong, do rollback in tables
- if me->changeset_errors_occurred = abap_true.
- call function 'BAPI_TRANSACTION_ROLLBACK'.
- endif.
- endmethod.
- method /iwbep/if_mgw_appl_srv_runtime~changeset_process.
- data context type ref to /iwbep/cl_mgw_request.
- data entity type ref to data.
- data key type /iwbep/t_mgw_name_value_pair.
- data response type /iwbep/if_mgw_appl_types=>ty_s_changeset_response.
- " looping through all subrequests
- loop at it_changeset_request reference into data(change_request).
- try.
- " getting info about subrequest
- context ?= change_request->request_context.
- catch cx_root into data(ex).
- raise exception type /iwbep/cx_mgw_tech_exception
- exporting
- textid = /iwbep/cx_mgw_tech_exception=>internal_error
- previous = ex.
- endtry.
- try.
- " try to execute subrequest according to its type
- case change_request->operation_type.
- when /iwbep/if_mgw_appl_types=>gcs_operation_type-create_entity.
- " create
- me->/iwbep/if_mgw_appl_srv_runtime~create_entity(
- exporting
- io_data_provider = change_request->entry_provider
- io_tech_request_context = context
- importing
- er_entity = entity ).
- when /iwbep/if_mgw_appl_types=>gcs_operation_type-update_entity.
- " update
- me->/iwbep/if_mgw_appl_srv_runtime~update_entity(
- exporting
- io_data_provider = change_request->entry_provider
- io_tech_request_context = context
- importing
- er_entity = entity ).
- when /iwbep/if_mgw_appl_types=>gcs_operation_type-patch_entity.
- " patch
- me->/iwbep/if_mgw_appl_srv_runtime~patch_entity(
- exporting
- io_data_provider = change_request->entry_provider
- io_tech_request_context = context
- importing
- er_entity = entity ).
- when /iwbep/if_mgw_appl_types=>gcs_operation_type-execute_action.
- " action execution
- me->/iwbep/if_mgw_appl_srv_runtime~execute_action(
- exporting
- io_tech_request_context = context
- iv_action_name = context->/iwbep/if_mgw_req_func_import~get_function_import_name( )
- it_parameter = context->/iwbep/if_mgw_req_func_import~get_parameters( )
- importing
- er_data = entity ).
- endcase.
- " filling results of a subrequest in the response table
- response-operation_no = change_request->operation_no.
- response-entity_data = entity.
- insert response into table ct_changeset_response.
- catch /iwbep/cx_mgw_busi_exception into data(busi_ex).
- " exceptions processing
- me->changeset_errors_occurred = abap_true.
- call function 'BAPI_TRANSACTION_ROLLBACK'.
- raise exception type /iwbep/cx_mgw_busi_exception
- exporting
- http_status_code = busi_ex->get_http_status_code( )
- textid = /iwbep/cx_mgw_busi_exception=>business_error
- previous = busi_ex
- message = busi_ex->message.
- catch /iwbep/cx_mgw_tech_exception into data(tech_ex).
- me->changeset_errors_occurred = abap_true.
- call function 'BAPI_TRANSACTION_ROLLBACK'.
- raise exception type /iwbep/cx_mgw_tech_exception
- exporting
- http_status_code = tech_ex->get_http_status_code( )
- textid = /iwbep/cx_mgw_tech_exception=>internal_error
- previous = tech_ex.
- endtry.
- endloop.
- endmethod.
- endclass.
Add Comment
Please, Sign In to add comment