前言
上一篇讲了通过ABAP程序去自动生成BW的ADSO模型,这一篇讲讲信息对象的自动生成,
一、实现效果
对于BW顾问来说,模型建完之后,就要开始选择用字段还是用信息对象进行建模,如果信息对象不存在呢,那么就要一个一个新建了,如果有一个快速批量创建信息对象的,那么这个效率就提高了不少。
那么下面直接看效果图:
信息对象有其对应技术名,描述,类型,长度等等技术信息,这些信息也是通过文件的方式去记录,上面截图可以看出,根据文件的模版填入需要的信息,然后导入到该程序中,进行批量生成。
有需要该文件模板的朋友们,可以关注私聊我进行领取。
下面看看最终实现的效果图:
二、实现代码
代码如下:
*&---------------------------------------------------------------------*
*& Report ZBW_IO_CREATE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zbw_io_create.
DATA: BEGIN OF data_final OCCURS 0.
INCLUDE STRUCTURE bapi6108.
DATA: END OF data_final.
DATA: BEGIN OF ret_mess OCCURS 0.
INCLUDE STRUCTURE bapiret2.
DATA: END OF ret_mess.
*data : la_conv_tab like line of lt_conv_tab.
DATA: BEGIN OF itab OCCURS 0,
f1(10000) TYPE c,
END OF itab.
DATA: BEGIN OF data_iobj OCCURS 0.
INCLUDE STRUCTURE bapi6108io.
DATA: END OF data_iobj.
DATA: BEGIN OF io_err OCCURS 0.
INCLUDE STRUCTURE bapi6108io.
DATA: END OF io_err.
PARAMETERS: lp_file TYPE rlgrap-filename OBLIGATORY. " To get the file location
DATA : temp_ch TYPE string,
names TYPE c LENGTH 1000.
DATA : str1 TYPE string, str2 TYPE string, str3 TYPE string, str4 TYPE string,
str5 TYPE string, str6 TYPE string, str7 TYPE string, str8 TYPE string,
str9 TYPE string, str10 TYPE string,str11 TYPE string,str12 TYPE string,
data_final_1 TYPE STANDARD TABLE OF bapi6108 WITH HEADER LINE.
DATA: lt_iolist TYPE TABLE OF bapi6108l.
*---------------------------------------------------------------------
* AT SELECTION SCREEN ON VALUE REQUEST
*---------------------------------------------------------------------
AT SELECTION-SCREEN ON VALUE-REQUEST FOR lp_file.
PERFORM help_local_file USING lp_file.
START-OF-SELECTION.
temp_ch = lp_file.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = temp_ch
filetype = 'ASC'
* HAS_FIELD_SEPARATOR = ' '
header_length = 0
read_by_line = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
data_tab = itab
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
* OTHERS = 17
.
IF sy-subrc = 1.
MESSAGE 'FILE IS OPEN; PLEASE CHECK THE FILE.' TYPE 'I'.
*WITH "FILE IS OPEN; PLEASE CHECK THE FILE.".
EXIT.
ELSEIF sy-subrc = 2.
MESSAGE 'ERROR WHILE READING THE FILE' TYPE 'I'.
EXIT.
ELSEIF sy-subrc = 13.
MESSAGE 'ACCESS DENIED.' TYPE 'I'.
EXIT.
ENDIF.
LOOP AT itab FROM 2.
names = itab-f1.
" if there are many columns to be split then make use of the FM 'ALSM_EXCEL_TO_INTERNAL_TABLE'
SPLIT names AT ',' INTO str1 str2 str3 str4 str5 str6 str7 str8 str9 str10 str11 str12.
IF str3 = 'CHA'. "信息对象类型(CHA;KYF;UNI;XXL)
data_final-infoarea = str1.
data_final-infoobject = str2.
data_iobj-infoobject = str2.
data_final-type = str3. "信息对象类型
data_final-datatp = str4. "ABAP数据类型
data_final-textshort = str5.
data_final-textlong = str6.
data_final-intlen = str7.
data_final-outputlen = str8.
data_final-lowercase = str9.
data_final-mastauthfl = str10.
data_final-txttabfl = '1'. "存在文本表
data_final-attribfl = 'X'.
data_final-novalfl = ''. "是否启用主数据属性:''为真 'X'为假
data_final-hietabfl = 'X'. "是否启用层次结构
data_final-nolangu = ''. "是否跟语言相关:''为真 'X'为假
data_final-txtshfl = 'X'. "短文本存在标志
data_final-txtmdfl = 'X'. "中文本存在标志
data_final-txtlgfl = 'X'. "长文本存在标志
ELSE.
data_final-infoarea = str1.
data_final-infoobject = str2.
data_iobj-infoobject = str2.
data_final-type = str3.
data_final-kyftp = str4. "ABAP数据类型 KYFTP(AMO/QUA/NUM/INT/FLO/DAT/TIM)
data_final-textshort = str5.
data_final-textlong = str6.
data_final-fixcuky = str11. "固定货币
data_final-uninm = str12."单位信息对象(货币/单位)
ENDIF.
APPEND data_final TO data_final.
ENDLOOP.
"剔除空行
DELETE data_final WHERE infoarea IS INITIAL OR infoobject IS INITIAL.
"检查系统是否已有
CALL FUNCTION 'BAPI_IOBJ_GETLIST'
EXPORTING
version = rs_c_objvers-active
TABLES
infoobjectlist = lt_iolist.
"删除重复创建的IO
LOOP AT data_final.
IF line_exists( lt_iolist[ infoobject = data_final-infoobject ] ).
DELETE data_final.
ENDIF.
ENDLOOP.
IF data_final[] IS INITIAL.
MESSAGE '请检查模板是否为空或IO已存在于系统!' TYPE 'I'.
ELSE.
LOOP AT data_final INTO data_final.
CLEAR : data_final_1[], data_final_1.
MOVE-CORRESPONDING data_final TO data_final_1.
APPEND data_final_1 TO data_final_1[].
CALL FUNCTION 'BAPI_IOBJ_CREATE'
EXPORTING
details = data_final_1
IMPORTING
return = ret_mess.
CLEAR : data_iobj[].
data_iobj-infoobject = data_final-infoobject.
APPEND data_iobj TO data_iobj[].
CALL FUNCTION 'BAPI_IOBJ_ACTIVATE_MULTIPLE'
TABLES
infoobjects = data_iobj[]
infoobjects_error = io_err.
ENDLOOP.
"添加成功消息
IF io_err[] IS INITIAL.
MESSAGE '信息对象创建成功!' TYPE 'I'.
ELSE.
MESSAGE '信息对象创建失败,请检查模板!' TYPE 'I'.
ENDIF.
ENDIF.
FORM help_local_file USING filename TYPE dxfile-filename.
DATA: lt_file_table TYPE filetable,
la_file_table LIKE LINE OF lt_file_table,
l_rc TYPE i,
l_pcdsn TYPE cffile-filename.
REFRESH lt_file_table.
CLEAR la_file_table.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
CHANGING
file_table = lt_file_table
rc = l_rc.
READ TABLE lt_file_table INTO la_file_table INDEX 1.
l_pcdsn = la_file_table-filename.
MOVE l_pcdsn TO filename.
ENDFORM.