学习新东西从来都是一件快乐的事情!!
前言
今天我讲一个很好玩的东西,那就是通过ABAP程序去自动生成BW的ADSO模型。我相信做过BW建模的小伙伴们都知道,BW模型建模是最基础且核心的部分,一旦设计出错,将会影响后续的一系列开发。
一、实现效果
对于BW顾问来说,模型建模虽然简单,但也会花费不少时间,如果有一个程序快速自动的一步到位,是不是就可以省下很多的时间,比如此刻写写文章,喝喝茶呢
那么下面直接看效果图:
由于模型是由字段或者信息对象所组成的一组结构,进而会衍生出不同类型的模型。由此可见上图中的三个单选,为不同类型的模型,标准的ADSO模型有其1、2、3表,而直接更新ADSO只有其2表,最后的Planing式的对比标准的少了其3表,关于模型的更多知识点我这篇先不描述了。
在该程序中,通过文件上载的方式可以知道指定模型的结构是什么,是用字段创建呢?还是信息对象创建?指定什么类型的模型?以及对应的长度,类型等等信息,有需要该文件模板的朋友们,可以关注私聊我进行领取。
下面看看最终实现的效果图:
二、实现代码
代码如下:
*&---------------------------------------------------------------------*
*& Report ZBW_TEST_DEMO3
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zbw_test_demo3.
TYPES:
BEGIN OF tn_s_object,
key TYPE abap_bool,
fieldname TYPE rsfieldnm, " => field
iobjnm TYPE rsiobjnm, " => infoObject (do not specify fieldname or asso_iobjnm)
asso_iobjnm TYPE rsiobjnm, " => field with assoziation (fieldname must be specified, too)
aggregation TYPE rsdaggrgen,
datatp TYPE datatype_d,
length TYPE rsdigits, "char / raw : # of characters, numbers: # of places
decimals TYPE decimals, "# of decimal places
sid_determination_mode TYPE rsdsiddetmode,
dimension TYPE rsdimension,
txtsh TYPE rsoadsodescr,
conversion_routine TYPE convexit,
uni_curr_for_fields TYPE rsfieldnm,
fix_unit_for_fields TYPE rsfixunit,
fix_cuky_for_fields TYPE rsfixcuky,
navigational_attr_on TYPE rs_bool,
is_sid_keyfigure TYPE rs_bool,
END OF tn_s_object .
DATA: lt_file_table TYPE filetable,
lv_rc TYPE i,
lv_escape_char TYPE char1,
lv_separator_char TYPE char1,
lt_output TYPE table_of_strings,
lt_key TYPE cl_rso_adso_api=>tn_t_key,
lv_hex TYPE xstring,
lt_dimension TYPE cl_rso_adso_api=>tn_t_dimension,
lv_prev_fieldname TYPE string,
ls_adso_fields TYPE tn_s_object,
lt_adso_fields TYPE TABLE OF tn_s_object.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS : p_rad1 TYPE c RADIOBUTTON GROUP rg1 DEFAULT 'X' USER-COMMAND uc,
p_rad2 TYPE c RADIOBUTTON GROUP rg1,
p_rad3 TYPE c RADIOBUTTON GROUP rg1.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK part1 WITH FRAME TITLE TEXT-b01.
PARAMETERS: pa_path TYPE string LOWER CASE OBLIGATORY MODIF ID ty1,
pa_line TYPE i OBLIGATORY MODIF ID ty1.
PARAMETERS: pa_ane TYPE char10 OBLIGATORY MODIF ID ty1,
pa_desc TYPE char50 MODIF ID ty1,
pa_area TYPE rsinfoarea OBLIGATORY MODIF ID ty1.
SELECTION-SCREEN END OF BLOCK part1.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR pa_path.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
file_filter = '*.CSV'
CHANGING
file_table = lt_file_table
rc = lv_rc
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
IF lt_file_table IS NOT INITIAL.
pa_path = lt_file_table[ 1 ].
ENDIF.
cl_progress_indicator=>progress_indicate(
i_text = 'Uploading in progress'
i_output_immediately = abap_true ).
START-OF-SELECTION.
cl_gui_frontend_services=>gui_upload(
EXPORTING
filename = pa_path
CHANGING
data_tab = lt_output
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19 ).
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
DATA lo_csv TYPE REF TO cl_rsda_csv_converter.
CALL METHOD cl_rsda_csv_converter=>create
RECEIVING
r_r_conv = lo_csv.
pa_line = pa_line + 1.
LOOP AT lt_output INTO DATA(ls_output) FROM pa_line.
CALL METHOD lo_csv->csv_to_structure
EXPORTING
i_data = ls_output
IMPORTING
e_s_data = ls_adso_fields.
APPEND ls_adso_fields TO lt_adso_fields.
ENDLOOP.
IF p_rad1 = 'X' OR p_rad2 = 'X'.
LOOP AT lt_adso_fields INTO ls_adso_fields WHERE key = abap_true.
IF ls_adso_fields-fieldname IS NOT INITIAL.
APPEND ls_adso_fields-fieldname TO lt_key.
ELSE.
APPEND ls_adso_fields-iobjnm TO lt_key.
ENDIF.
ENDLOOP.
IF lines( lt_key ) = 0 .
MESSAGE 'at least one key is required'
TYPE 'S' DISPLAY LIKE 'W'.
EXIT.
ENDIF.
ENDIF.
IF p_rad1 = 'X' .
DATA(ls_flags) = VALUE cl_rso_adso_api=>tn_s_adsoflags( activate_data = abap_true
write_changelog = abap_true ).
ELSEIF p_rad2 = 'X' .
ls_flags = VALUE cl_rso_adso_api=>tn_s_adsoflags( direct_update = abap_true ).
ELSEIF p_rad3 = 'X' .
ls_flags = VALUE cl_rso_adso_api=>tn_s_adsoflags( activate_data = abap_true
planning_mode = abap_true
cubedeltaonly = abap_true ).
ENDIF.
DATA(lv_adso_exists) = cl_rso_adso_api=>exist( i_adsonm = CONV #( pa_ane ) ).
IF lv_adso_exists = abap_true.
MESSAGE 'ADSO already exist, please go back and change the name'
TYPE 'S' DISPLAY LIKE 'W'.
EXIT.
ENDIF.
TRY.
cl_rso_adso_api=>create(
EXPORTING
i_adsonm = CONV #( pa_ane )
i_text = CONV #( pa_desc )
i_infoarea = pa_area
i_s_adsoflags = ls_flags
i_t_object = CORRESPONDING #( lt_adso_fields )
i_t_dimension = lt_dimension
i_t_key = lt_key
IMPORTING
e_t_msg = DATA(lt_msg)
).
CATCH cx_rs_all_msg INTO DATA(lr_msg).
cl_demo_output=>display(
EXPORTING
data = lr_msg->get_longtext( )
name = 'Error' ).
ENDTRY.