Thursday, July 22, 2010

WebDynpro: Upload and Download file, part II (Download)

For download functionality we have the same process as for Upload. Download WD is created as separate WD component and is referred in the main WD application.

image

The download logic is as laid out in the diagram below. Event from download component is sent to the main WD component. Which then uses the assistance class to convert the structure into file layout. Then it calls the interface method in the download WD to send the file via the browser to the user for it to be saved.

image WD app component using Download WD component.

Download WD Component

This component will download any file that is selected into the internal table of the assistance class. The following has been created in this component.

image Layout output (V_DOWNLOAD)

image Layout design (V_DOWNLOAD)

image Components

image Context (V_DOWNLOAD & Component Controller)

There is one difference from the upload WD here, the context has been set to be as interface. The calling WD will the context with file information when it initialises the download WD.

image Events

image 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.
  1. METHOD wddoinit .
  2. wd_this->app_name = get_app_name( ).
  3. get_file_struc( ).
  4. get_date_format( ).

App name is retrieved here.
  1. METHOD get_app_name .
  2. DATA lo_api_componentcontroller TYPE REF TO if_wd_component.
  3. lo_api_componentcontroller = wd_this->wd_get_api( ).
  4. DATA lo_app TYPE REF TO if_wd_application.
  5. DATA lo_app_info TYPE REF TO if_wd_rr_application.
  6. lo_app = lo_api_componentcontroller->get_application( ).
  7. lo_app_info = lo_app->get_application_info( ).
  8. r_app_name = lo_app_info->get_name( ).

Retrieve the structure for the particular app and create dynamic structure in the assistance class.
  1. METHOD get_file_struc .
  2. DATA l_struc_name TYPE string.
  3. * Look for file structure in the lookup table
  4. SELECT SINGLE LKP_VALUE INTO l_struc_name
  5. FROM ZBC_LKP
  6. WHERE LKP_KEY = wd_this->app_name.
  7. IF sy-subrc IS NOT INITIAL.
  8. * get message manager
  9. DATA lo_api_controller TYPE REF TO if_wd_controller.
  10. DATA lo_message_manager TYPE REF TO if_wd_message_manager.
  11. lo_api_controller ?= wd_this->wd_get_api( ).
  12. lo_message_manager = lo_api_controller->get_message_manager( ).
  13. * report message
  14. lo_message_manager->report_error_message(
  15. message_text = 'No file structure defined for this app' ).
  16. * Send the assitance class the structure name
  17. TRY .
  18. wd_assist->set_structure_name( i_structure_name = l_struc_name ).
  19. CATCH zcx_library.
  20. * report message
  21. lo_message_manager->report_error_message(
  22. message_text = 'File format not supported'
  23. ).

If date format is specified then use it.
  1. METHOD get_date_format .
  2. DATA l_date_format TYPE string.
  3. * Look for date format in the lookup table
  4. SELECT SINGLE lkp_value INTO l_date_format
  5. FROM zbc_lkp
  6. WHERE lkp_key = wd_this->app_name.
  7. IF sy-subrc IS INITIAL.
  8. * Send the assitance class date format
  9. wd_assist->set_external_date_format( i_date_format = l_date_format ).

Now we come to user interactions. Assuming there is data that user wants to download, user will click the download button. Fire event for the App WD for data to be parsed and mapped into a file structure as per the application structure. Once the file structure is created then call the download method to push the file out. The file here is assumed to be tab delimited so that’s what the separator has been specified to.

image Action attached to the download button

  1. method ONACTIONDOWNLOAD_FILE .
  2. wd_comp_controller->start_download( ).
The component controller method start_download is called hence.
  1. METHOD START_DOWNLOAD.
  2. DATA lo_nd_file TYPE REF TO if_wd_context_node.
  3. DATA lo_el_file TYPE REF TO if_wd_context_element.
  4. DATA ls_file TYPE wd_this->element_file.
  5. * navigate from <CONTEXT> to <FILE> via lead selection
  6. lo_nd_file = wd_context->get_child_node( name = wd_this->wdctx_file ).
  7. * get element via lead selection
  8. lo_el_file = lo_nd_file->get_element( ).
  9. * get all declared attributes
  10. lo_el_file->get_static_attributes(
  11. IMPORTING
  12. static_attributes = ls_file ).
  13. * Calling WD should have provided file information
  14. IF ls_file IS INITIAL.
  15. throw_message( EXPORTING i_message = 'No file information provided'
  16. i_type = if_wd_message_manager=>co_type_error ).
  17. wd_this->file_name = ls_file-filename.
  18. wd_this->mime_type = ls_file-mimetype.
  19. * Fire event
  20. wd_this->fire_download_clicked_evt( ).
By this time the calling WD will receive the event and convert the data in to the file structure that it needs to send to. After this it will call the method DOWNLOAD_FILE in the download WD.
  1. METHOD download_file .
  2. DATA lt_data TYPE TABLE OF string.
  3. DATA l_data_string TYPE string.
  4. DATA l_data_xstring TYPE xstring.
  5. wd_assist->get_mapped_file( IMPORTING e_t_mapped_file = lt_data ).
  6. CONCATENATE LINES OF lt_data INTO l_data_string SEPARATED BY cl_abap_char_utilities=>newline.
  7. CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
  8. EXPORTING
  9. text = l_data_string
  10. IMPORTING
  11. buffer = l_data_xstring.
  12. cl_wd_runtime_services=>attach_file_to_response( i_filename = wd_this->file_name
  13. i_content = l_data_xstring
  14. i_mime_type = wd_this->mime_type ).

This covers the download WD component, in part III we will look at the usage of these 2 WD components.

No comments: