Code: Alles auswählen.
REPORT z_abapobj_ma_rttc.
PARAMETERS p_table TYPE databrowse-tablename DEFAULT 'SCARR'.
TYPE-POOLS abap.
INCLUDE z_abapobj_ma_rttc_dyn_rep.
INCLUDE z_lcx_abapobj_ma_rttc.
INCLUDE z_lcl_abapobj_ma_rttc.
START-OF-SELECTION.
lcl_abapobj_ma_rttc=>main( ).
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Include Z_ABAPOB_MA_RTTC_DYN_REP
*&---------------------------------------------------------------------*
CLASS lcl_dynamic_report DEFINITION.
PUBLIC SECTION.
METHODS:
constructor,
submit,
add_line IMPORTING i_source_line TYPE csequence " line to add
.
PRIVATE SECTION.
DATA:
lt_source_table TYPE TABLE OF string,
le_source_line TYPE string,
le_report_name TYPE c LENGTH 23.
ENDCLASS. "lcl_dynamic_report DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_dynamic_report IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_dynamic_report IMPLEMENTATION.
METHOD constructor.
CONCATENATE 'Z_MA_' sy-uname INTO le_report_name.
TRANSLATE le_report_name TO UPPER CASE.
CONCATENATE 'REPORT ' le_report_name ' NO STANDARD PAGE HEADING.'
INTO le_source_line RESPECTING BLANKS.
me->add_line( le_source_line ).
ENDMETHOD. "constructor
METHOD submit.
DATA le_msg TYPE string.
DATA le_wrd TYPE string.
DATA le_lin TYPE i.
INSERT REPORT le_report_name FROM lt_source_table.
SYNTAX-CHECK FOR lt_source_table MESSAGE le_msg LINE le_lin WORD le_wrd PROGRAM le_wrd.
IF sy-subrc EQ 0.
SUBMIT (le_report_name) VIA SELECTION-SCREEN AND RETURN.
DELETE REPORT le_report_name.
ELSE.
FORMAT COLOR COL_NEGATIVE.
WRITE: / 'FEHLER',
/ 'MESSAGE:', le_msg,
/ 'LINE:', le_lin,
/ 'WORD:', le_wrd.
SKIP 3.
FORMAT COLOR OFF.
LOOP AT lt_source_table INTO le_source_line.
WRITE / le_source_line.
ENDLOOP.
ENDIF.
ENDMETHOD. "submit
METHOD add_line.
le_source_line = i_source_line.
TRANSLATE le_source_line TO UPPER CASE.
APPEND le_source_line TO lt_source_table.
ENDMETHOD. "add_line
ENDCLASS. "lcl_dynamic_report IMPLEMENTATION
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Include Z_LCX_ABAPOBJ_MA_RTTC
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
* CLASS lcx_invalid_table DEFINITION
*----------------------------------------------------------------------*
* Raised ín lcl_abapobj_ma_rttc=>get_table_components,
* in case a invalid table name is entered by the user.
*----------------------------------------------------------------------*
CLASS lcx_invalid_table DEFINITION INHERITING FROM cx_static_check.
ENDCLASS. "lcx_invalid_table DEFINITION
*----------------------------------------------------------------------*
* CLASS lcx_invalid_table IMPLEMENTATION
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
CLASS lcx_invalid_table IMPLEMENTATION.
ENDCLASS. "lcx_invalid_table IMPLEMENTATION
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Include Z_LCL_ABAPOBJ_MA_RTTC
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
* CLASS lcl_abapobj_ma_rttc DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_abapobj_ma_rttc DEFINITION.
PUBLIC SECTION.
CLASS-DATA:
go_struct_descr TYPE REF TO cl_abap_structdescr,
go_type_descr TYPE REF TO cl_abap_typedescr,
go_dyn_report TYPE REF TO lcl_dynamic_report,
gs_component TYPE abap_compdescr, " component description object
ge_source_line TYPE string, " to concatenate source code into.
gt_marked_comp TYPE TABLE OF string, " will contain component names selected by the user.
ge_marked_comp TYPE string " used as workarea when looping through sel. components.
.
CLASS-METHODS:
main.
PRIVATE SECTION.
CLASS-METHODS:
get_table_components RAISING lcx_invalid_table,
select_components,
generate_report,
add_data_declaration,
add_top_of_page,
add_data_selection,
add_select_options,
add_salv_display
.
ENDCLASS. "lcl_abapobj_ma_rttc DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_abapobj_ma_rttc IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_abapobj_ma_rttc IMPLEMENTATION.
METHOD main.
DATA le_exception_txt TYPE string.
TRY.
" Use Runtime Type Services to determine
" the structure of the table, the user entered.
get_table_components( ).
" Show a popup to let the user select
" the fields for his selection screen.
select_components( ).
" generate report dynamicly based on the
" selected components for the selection screen.
generate_report( ).
CATCH lcx_invalid_table.
CONCATENATE `Tabelle ` p_table ` ist unbekannt`
INTO le_exception_txt.
WRITE / le_exception_txt COLOR COL_NEGATIVE.
ENDTRY.
ENDMETHOD. "main
METHOD get_table_components.
TRY.
" Try to describe the table using it's absolute name.
cl_abap_typedescr=>describe_by_name(
EXPORTING p_name = p_table
RECEIVING p_descr_ref = go_type_descr
EXCEPTIONS type_not_found = 1 ).
IF sy-subrc NE 0.
RAISE EXCEPTION TYPE lcx_invalid_table.
ENDIF.
" Down-Cast type-description-object into struct-description-object.
go_struct_descr ?= go_type_descr.
CATCH cx_sy_move_cast_error.
" Impossible to happen.
ENDTRY.
ENDMETHOD. "get_table_components
* &---------------------------------------------------------------------*
* & Method select_components
* &---------------------------------------------------------------------*
* popup for letting the user select his select-options
* ----------------------------------------------------------------------*
METHOD select_components.
DATA:
ls_line TYPE c LENGTH 100,
lt_head LIKE TABLE OF ls_line,
lt_list LIKE TABLE OF ls_line,
lt_mark LIKE TABLE OF ls_line.
" Title
CONCATENATE 'Selektionsbild' space p_table INTO ls_line.
" Heading
APPEND 'Bitte wählen Sie die Merkmale' TO lt_head.
APPEND 'aus, nach denen Sie selektieren' TO lt_head.
APPEND 'möchten' TO lt_head.
" List items
LOOP AT go_struct_descr->components INTO gs_component WHERE name NE 'MANDT'.
APPEND gs_component-name TO lt_list.
ENDLOOP.
CALL FUNCTION 'RH_LISTPOPUP'
EXPORTING
title = ls_line
mark_mode = 1 " select with checkboxes
TABLES
page_header = lt_head
list = lt_list
marked_lines = lt_mark
EXCEPTIONS
wrong_mark_mode = 0
f15 = 0
OTHERS = 0.
" transfer only those fields that were marked by the user
" into the table gt_marked_comp, wich will be used for dynamically
" adding SELECT-OPTIONS to the new report.
LOOP AT go_struct_descr->components INTO gs_component.
READ TABLE lt_mark INTO ls_line
WITH KEY table_line = gs_component-name.
IF sy-subrc EQ 0.
APPEND ls_line TO gt_marked_comp.
ENDIF.
ENDLOOP.
ENDMETHOD. "select_components
METHOD generate_report.
"*********************************************
CREATE OBJECT go_dyn_report.
add_data_declaration( ).
add_select_options( ).
add_top_of_page( ).
add_data_selection( ).
add_salv_display( ).
go_dyn_report->submit( ).
ENDMETHOD. "generate_report
*&---------------------------------------------------------------------*
*& METHOD add_data_declaration
*&---------------------------------------------------------------------*
* add Data declarations to the report.
*----------------------------------------------------------------------*
METHOD add_data_declaration.
"*********************************************
go_dyn_report->add_line( 'DATA go_salv TYPE REF TO cl_salv_table.' ).
go_dyn_report->add_line( 'DATA gt_itab TYPE TABLE OF' ).
go_dyn_report->add_line( p_table ).
go_dyn_report->add_line( '.' ).
go_dyn_report->add_line( 'DATA gs_wa LIKE LINE OF gt_itab.' ).
ENDMETHOD. "add_data_declaration
METHOD add_top_of_page.
CONCATENATE 'sy-title = ' '''' p_table '''' '.'
INTO ge_source_line.
go_dyn_report->add_line( 'TOP-OF-PAGE.' ).
go_dyn_report->add_line( ge_source_line ).
ENDMETHOD. "add_top_of_page
*&---------------------------------------------------------------------*
*& METHOD add_data_selection
*&---------------------------------------------------------------------*
* add the SELECT statement for reading the data from
* the database to an internal table
*----------------------------------------------------------------------*
METHOD add_data_selection.
"*********************************************
" SELECT * FROM xxx INTO TABLE gt_itab WHERE
" yyy IN yyy
" [AND zzz IN zzz]
" .
go_dyn_report->add_line( 'START-OF-SELECTION.' ).
go_dyn_report->add_line( 'SELECT * FROM' ).
go_dyn_report->add_line( p_table ).
go_dyn_report->add_line( 'INTO TABLE gt_itab' ).
IF gt_marked_comp IS INITIAL.
go_dyn_report->add_line( '.' ).
EXIT.
ENDIF.
go_dyn_report->add_line( ' WHERE ' ).
LOOP AT gt_marked_comp INTO ge_marked_comp.
CASE sy-tabix.
WHEN 1.
CONCATENATE ge_marked_comp ' IN ' ge_marked_comp
INTO ge_source_line RESPECTING BLANKS.
WHEN OTHERS.
CONCATENATE 'AND ' ge_marked_comp ' IN ' ge_marked_comp
INTO ge_source_line RESPECTING BLANKS.
ENDCASE.
go_dyn_report->add_line( ge_source_line ).
ENDLOOP.
go_dyn_report->add_line( '.' ).
ENDMETHOD. "add_data_selection
*&---------------------------------------------------------------------*
*& METHOD add_select_options
*&---------------------------------------------------------------------*
* add SELECT-OPTIONS to the report.
* This is basically the part, where the selection screen is
* generated.
*----------------------------------------------------------------------*
METHOD add_select_options.
"*********************************************
" add select option for selection screen
" 'SELECT-OPTIONS xxx FOR gs_wa-xxx'
LOOP AT gt_marked_comp INTO ge_marked_comp.
CONCATENATE
'SELECT-OPTIONS ' ge_marked_comp ' FOR '
'gs_wa-' ge_marked_comp '.'
INTO ge_source_line RESPECTING BLANKS.
go_dyn_report->add_line( ge_source_line ).
ENDLOOP.
ENDMETHOD. "add_select_options
*&---------------------------------------------------------------------*
*& METHOD add_salv_display
*&---------------------------------------------------------------------*
* show alv using class cl_salv_table
*----------------------------------------------------------------------*
METHOD add_salv_display.
"*********************************************
go_dyn_report->add_line( 'cl_salv_table=>factory(' ).
go_dyn_report->add_line( 'IMPORTING r_salv_table = go_salv').
go_dyn_report->add_line( 'CHANGING t_table = gt_itab ).').
go_dyn_report->add_line( 'go_salv->display( ).').
ENDMETHOD. "add_salv_display
ENDCLASS. "lcl_abapobj_ma_rttc IMPLEMENTATION