As with normal SAPGUI applications there are instances when there is need to write applications that interact with users with a file for sending and receiving data. You might want to upload a list of new Sales Orders or download a list of customers. Like the SAPGUI client there are UI elements here that provide functionality of file services. The approach taken here is to provide a generic file service that can be plugged into any WebDynpro component. Also work done earlier for upload and download in vanilla ABAP is reused.
WD app component using upload WD component
The WD components have been created sharing one assistance class. The WD components will be event driven. When the user clicks to upload/download the file, an event will be raised so that the WD App using the service can be alerted. The calling application can then use the assistance class to receive/send data.
Since the file has to be mapped to a structure, concept of a lookup table is used to decide what structure each application uses. At the initialisation of component a dynamic structure is created in the assistance class.
Assistance class
Assistance class will help the components to create the dynamic structure for each App. This will also map the file into the structure and vice versa. If the input file has a date then app can specify the date format of the input file to convert from. Here are the methods of the assistance class.
CONVERT_DATE2SAP | Instance Method | Private | Convert the external date format to internal SAP date format |
SET_EXTERNAL_DATE_FORMAT | Instance Method | Public | Set the external date format to be used in file conversion |
GET_MAPPED_FILE | Instance Method | Public | Get the file content mapped in the structure |
SET_STRUCTURE_NAME | Instance Method | Public | Set the structure for which data needs to be mapped |
CONVERT_STRUC2FILE | Instance Method | Public | Converts structure into file |
CONVERT_FILE2STRUC | Instance Method | Public | Converts file into structure |
METHOD convert_date2sap. * Get the type of the field l_o_type = cl_abap_typedescr=>describe_by_data( p_data = i_sap_field ). * For date type convert from DD.MM.YYYY to YYYYMMDD i_ext_field IS NOT INITIAL AND date_format IS NOT INITIAL. TRY. CLEAR e_sap_field. ELSE. CALL METHOD cl_abap_datfm=>conv_date_ext_to_int EXPORTING im_datext = i_ext_field im_datfmdes = date_format IMPORTING ex_datint = e_sap_field. CATCH cx_abap_datfm_no_date cx_abap_datfm_invalid_date cx_abap_datfm_format_unknown cx_abap_datfm_ambiguous . e_sap_field = i_ext_field. ELSE. e_sap_field = i_ext_field.
METHOD SET_EXTERNAL_DATE_FORMAT. date_format = i_date_format.
METHOD get_mapped_file. e_t_mapped_file[] = <lt_file_table>.
METHOD set_structure_name. CALL METHOD cl_abap_structdescr=>describe_by_name EXPORTING p_name = i_structure_name RECEIVING p_descr_ref = lo_type EXCEPTIONS type_not_found = 1 OTHERS = 2. RAISE EXCEPTION TYPE zcx_library EXPORTING textid = zcx_library=>incorrect_structure. o_struc_type ?= lo_type. CREATE DATA file_row TYPE HANDLE o_struc_type. TRY. CALL METHOD cl_abap_tabledescr=>create EXPORTING p_line_type = o_struc_type RECEIVING p_result = lo_tabletype. CATCH cx_sy_table_creation . RAISE EXCEPTION TYPE zcx_library EXPORTING textid = zcx_library=>incorrect_structure. CREATE DATA file_tab TYPE HANDLE lo_tabletype. * Structure mapped successfully
METHOD convert_struc2file. DATA: l_struc TYPE REF TO data, l_o_datadescr TYPE REF TO cl_abap_datadescr, l_o_tabledescr TYPE REF TO cl_abap_tabledescr, l_t_fields TYPE string_table, l_s_fields TYPE string. <l_filecontent> TYPE ANY TABLE, <l_filerow> TYPE ANY, <l_field> TYPE ANY, <l_row> TYPE ANY, <l_table_content> TYPE table. * Check structure mapping has happened first. RAISE EXCEPTION TYPE zcx_library EXPORTING textid = zcx_library=>no_structure_mapped. ********************************************************************** CALL METHOD o_struc_type->get_ddic_field_list EXPORTING p_including_substructres = abap_true RECEIVING p_field_list = lt_dfies. * Add header. LOOP AT lt_dfies INTO lst_dfies. l_column_name = lst_dfies-scrtext_s. AT FIRST. SEPARATED BY i_seperator. AT LAST. * add the content. LOOP AT i_t_data INTO <l_row>. ASSIGN COMPONENT sy-index OF STRUCTURE <l_row> TO <l_field>. EXIT. SEPARATED BY i_seperator. **********************************************************************
METHOD convert_file2struc. DATA: l_struc TYPE REF TO data, l_o_datadescr TYPE REF TO cl_abap_datadescr, l_o_tabledescr TYPE REF TO cl_abap_tabledescr, l_t_fields TYPE string_table, l_s_fields TYPE string. <l_filecontent> TYPE ANY TABLE, <l_filerow> TYPE ANY, <l_field> TYPE ANY, <l_row> TYPE ANY, <l_table_content> TYPE table. * Check structure mapping has happened first. RAISE EXCEPTION TYPE zcx_library EXPORTING textid = zcx_library=>no_structure_mapped. * Take out the header l_start = 2. ELSE. l_start = 1. LOOP AT i_t_file_data ASSIGNING <l_filerow> FROM l_start. <l_row> = <l_filecontent>. ELSE. *---split based on the separator LOOP AT l_t_fields INTO l_s_fields. ASSIGN COMPONENT sy-tabix OF STRUCTURE <l_row> TO <l_field>. * If external date format is mentioned then * convert file content into SAP format convert_date2sap( EXPORTING i_sap_field = <l_field> i_ext_field = l_s_fields IMPORTING e_sap_field = <l_field> ). ELSE. <l_field> = l_s_fields.
Upload WD Component
This component will upload any file that is selected into the internal table of the assistance class. The following has been created in this component.Context (V_UPLOAD & Component Controller)
Methods in the main controller
At the component start up get the app name it is embedded in and create structure for mapping the file.App name is retrieved here.
METHOD wddoinit . wd_this->app_name = get_app_name( ). get_file_struc( ). get_date_format( ).
Retrieve the structure for the particular app and create dynamic structure in the assistance class.
METHOD get_app_name . lo_api_componentcontroller = wd_this->wd_get_api( ). lo_app = lo_api_componentcontroller->get_application( ). lo_app_info = lo_app->get_application_info( ). r_app_name = lo_app_info->get_name( ).
If date format is specified then use it.
METHOD get_file_struc . * Look for file structure in the lookup table FROM ZBC_LKP WHERE LKP_KEY = wd_this->app_name. * get message manager lo_api_controller ?= wd_this->wd_get_api( ). lo_message_manager = lo_api_controller->get_message_manager( ). * report message lo_message_manager->report_error_message( message_text = 'No file structure defined for this app' ). ELSE. * Send the assitance class the structure name TRY . wd_assist->set_structure_name( i_structure_name = l_struc_name ). CATCH zcx_library. * report message lo_message_manager->report_error_message( message_text = 'File format not supported' ).
Now we come to user interactions. User will select the file and click the upload button. The file will be parsed and mapped into internal table as per the application structure. Once the file is parsed into the internal table fire event for the App WD to receive the internal table. The file here is assumed to be tab delimited so that’s what the separator has been specified to.
METHOD get_date_format . * Look for date format in the lookup table FROM zbc_lkp WHERE lkp_key = wd_this->app_name. * Send the assitance class date format wd_assist->set_external_date_format( i_date_format = l_date_format ).
Action attached to the upload button
The component controller method upload_file is called hence.
METHOD onactionupload . wd_comp_controller->upload_file( ).
This covers the upload WD component, in part II we will look at download WD component.
METHOD upload_file . * get message manager lo_api_controller ?= wd_this->wd_get_api( ). lo_message_manager = lo_api_controller->get_message_manager( ). * ASSIGN mydate TO <target_fld>. * navigate from <CONTEXT> to <FILE> via lead selection lo_nd_file = wd_context->get_child_node( name = wd_this->wdctx_file ). * get element via lead selection lo_el_file = lo_nd_file->get_element( ). * get File content lo_el_file->get_attribute( EXPORTING name = `FILE` IMPORTING value = item_file ). * get Header flag lo_el_file->get_attribute( EXPORTING name = `FLAG_HEADER` IMPORTING value = lv_flag_header ). TRY. convt = cl_abap_conv_in_ce=>create( encoding = 'UTF-8' CATCH cx_sy_conversion_codepage cx_sy_codepage_converter_init cx_parameter_invalid_type cx_parameter_invalid_range. * report message lo_message_manager->report_error_message( message_text = 'File format not supported'). "Column headers * Send the file data TRY . wd_assist->convert_file2struc( i_t_file_data = t_table[] i_seperator = cl_abap_char_utilities=>horizontal_tab i_flag_header = lv_flag_header ). CATCH zcx_library. * report message lo_message_manager->report_error_message( message_text = 'Error processing file' ). wd_this->fire_upload_clicked_evt( ).
No comments:
Post a Comment