2024 Jul 20 7:35 AM
DB table: "ZVK_T_EXAMPLE"
NOTE: File should be saved as '.CSV' format or the file should be text file
METHOD /iwbep/if_mgw_appl_srv_runtime~create_stream.
DATA lo_msg_container TYPE REF TO /iwbep/if_message_container.
lo_msg_container = me->mo_context->get_message_container( ).
DATA:lt_data TYPE TABLE OF zvk_t_example,
ls_data TYPE zvk_t_example,
lt_string TYPE TABLE OF string,
lv_string TYPE string,
lo_obj TYPE REF TO cl_abap_conv_in_ce.
lo_obj = cl_abap_conv_in_ce=>create(
input = is_media_resource-value
).
lo_obj->read(
IMPORTING
data = lv_string " Data Object To Be Read
).
SPLIT lv_string AT cl_abap_char_utilities=>cr_lf INTO TABLE lt_string.
LOOP AT lt_string INTO DATA(ls_string).
SPLIT ls_string AT ',' INTO ls_data-name
ls_data-gender
ls_data-mobile.
IF ls_data IS NOT INITIAL.
"Name validations
IF ls_data-name = ' '.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '002' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF ls_data-name+0(1) CA ' '.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '003' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF strlen( ls_data-name ) <= 3.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '004' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF NOT CONV string( ls_data-name ) CO 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '005' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ENDIF.
"Gender validations
IF ls_data-gender <> 'Female' AND
ls_data-gender <> 'FEMALE' AND
ls_data-gender <> 'MALE' AND
ls_data-gender <> 'Male'.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '006' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ENDIF.
"Mobile number validations
IF ls_data-mobile = ' '.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '007' " Message Number
).
ELSEIF strlen( ls_data-mobile ) < 10.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '008' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF ls_data-mobile+0(1) = ' '.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '009' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF ls_data-mobile CA ' '.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '010' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF NOT ls_data-mobile CO '0123456789'.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '011' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF NOT ( ls_data-mobile CP '6*' OR
ls_data-mobile CP '7*' OR
ls_data-mobile CP '8*' OR
ls_data-mobile CP '9*' ).
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '012' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ENDIF.
" Generating SNRO
IF ls_data IS NOT INITIAL.
CALL FUNCTION 'NUMBER_GET_NEXT'
EXPORTING
nr_range_nr = '01'
object = 'ZVK_EXAMPL'
IMPORTING
number = ls_data-id
EXCEPTIONS
interval_not_found = 1
number_range_not_intern = 2
object_not_found = 3
quantity_is_0 = 4
quantity_is_not_1 = 5
interval_overflow = 6
buffer_overflow = 7
OTHERS = 8.
ENDIF.
APPEND ls_data TO lt_data.
ENDIF.
ENDLOOP.
"Updating data to the DB table
IF lt_data IS NOT INITIAL.
CALL FUNCTION 'ZVK_FM_INSERT_FILE' IN UPDATE TASK
EXPORTING
lt_data = lt_data
EXCEPTIONS
ex_insert = 1
OTHERS = 2.
COMMIT WORK.
IF sy-subrc <> 0.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '013' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSE.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>success " Message Type
iv_msg_id = 'ZVK_MSG_CLASS' " Message Class
iv_msg_number = '001' " Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ENDIF.
ENDIF.
ENDMETHOD.
2024 Jul 23 8:17 AM
For the conversion of a CSV line into ABAP structure, you should better make use of FM RSDS_CONVERT_CSV. It support also handling of values which include the separator character ("," in your case) by utilizing an escape character, like described in the CSV RFC.