FUNCTION Z02HR_GET_ATTACHMENT
.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IS_OBJECT) TYPE SIBFLPORB
*" EXPORTING
*" REFERENCE(E_FLAG) TYPE C
*" TABLES
*" ET_ATTA STRUCTURE Z02HR_ATTA
*"----------------------------------------------------------------------
DATA : LS_OBJECT TYPE SIBFLPORB ,
LS_OPTION TYPE OBL_S_RELT ,
LT_OPTIONS TYPE OBL_T_RELT ,
LT_LINKS TYPE OBL_T_LINK ,
LS_LINK TYPE OBL_S_LINK .
*--- read attachment
DATA : LT_OBJECT_HEADER TYPE STANDARD TABLE OF SOLISTI1 ,
LT_OBJECT_CONTENT TYPE STANDARD TABLE OF SOLISTI1 ,
LT_CONTENTS_HEX TYPE STANDARD TABLE OF SOLIX ,
L_BLOB_LENGTH TYPE I .
FIELD-SYMBOLS <FS_BOJECT_HEADER> TYPE SOLISTI1 .
DATA : L_DOCMENT_ID TYPE SOFOLENTI1 -DOC_ID ,
LS_DOCUMENT_DATA TYPE SOFOLENTI1 ,
L_HANDLE TYPE I ,
L_PATH ( 60 ) TYPE C VALUE '/OA/SAP-OA/' ,
L_FNAME ( 60 ) TYPE C .
DATA : L_TEXT1 TYPE STRING ,
L_TEXT2 TYPE STRING .
DATA : L_FLAG TYPE C .
*--- attachment infomation
DATA : ES_ATTA TYPE Z02HR_ATTA .
DATA : L_SPACE TYPE C .
DATA : L_OA ( 3 ) TYPE C .
CLEAR : E_FLAG ,
ES_ATTA ,
ET_ATTA[] .
LS_OBJECT = IS_OBJECT .
IF LS_OBJECT -CATID IS INITIAL .
LS_OBJECT -CATID = 'BO' .
ENDIF .
*--- option
CLEAR LS_OPTION .
LS_OPTION - SIGN = 'I' .
LS_OPTION -OPTION = 'EQ' .
LS_OPTION -LOW = 'ATTA' .
APPEND LS_OPTION TO LT_OPTIONS .
*--- get attachment list.
TRY .
CALL METHOD CL_BINARY_RELATION =>READ_LINKS_OF_BINRELS
EXPORTING
IS_OBJECT = LS_OBJECT
IT_RELATION_OPTIONS = LT_OPTIONS
IP_ROLE = 'GOSAPPLOBJ'
"ip_no_buffer = gp_refresh
IMPORTING
ET_LINKS = LT_LINKS .
CATCH CX_OBL_PARAMETER_ERROR .
CATCH CX_OBL_INTERNAL_ERROR .
CATCH CX_OBL_MODEL_ERROR .
ENDTRY .
CHECK LT_LINKS IS NOT INITIAL .
*--- connect FTP
PERFORM FRM_CONNECT_FTP CHANGING L_HANDLE .
*--- attachment exit.
LOOP AT LT_LINKS INTO LS_LINK .
CLEAR ES_ATTA .
CLEAR L_DOCMENT_ID .
L_DOCMENT_ID = LS_LINK -INSTID_B .
CALL FUNCTION 'SO_DOCUMENT_READ_API1'
EXPORTING
DOCUMENT_ID = L_DOCMENT_ID
IMPORTING
DOCUMENT_DATA = LS_DOCUMENT_DATA
TABLES
OBJECT_HEADER = LT_OBJECT_HEADER
OBJECT_CONTENT = LT_OBJECT_CONTENT
CONTENTS_HEX = LT_CONTENTS_HEX
EXCEPTIONS
DOCUMENT_ID_NOT_EXIST = 1
OPERATION_NO_AUTHORIZATION = 2
X_ERROR = 3
OTHERS = 4 .
IF SY -SUBRC <> 0 .
L_FLAG = 'X' .
ENDIF .
**--- Create file on FTP server
* call function 'SAPGUI_PROGRESS_INDICATOR'
* exporting
* text = 'Create file on FTP Server'.
*--- create file name
READ TABLE LT_OBJECT_HEADER INDEX 1 ASSIGNING <FS_BOJECT_HEADER> .
IF SY -SUBRC = 0 .
SPLIT <FS_BOJECT_HEADER> - LINE AT '=' INTO L_TEXT1 L_TEXT2 .
IF STRLEN ( L_TEXT2 ) >= 3 .
L_OA = L_TEXT2+0 ( 3 ) .
TRANSLATE L_OA TO UPPER CASE .
IF L_OA <> 'OA_' .
CONTINUE .
ENDIF .
ELSE .
CONTINUE .
ENDIF .
CONCATENATE L_PATH SY -DATUM SY -UZEIT '_' L_TEXT2 INTO L_FNAME .
ES_ATTA -NAME = L_TEXT2 .
ENDIF .
*--- file tpye
CLEAR : L_TEXT1 , L_TEXT2 .
READ TABLE LT_OBJECT_HEADER INDEX 2 ASSIGNING <FS_BOJECT_HEADER> .
IF SY -SUBRC = 0 .
SPLIT <FS_BOJECT_HEADER> - LINE AT '=' INTO L_TEXT1 L_TEXT2 .
*--- transfer data to FTP
IF L_TEXT2 = 'ASC' .
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
HANDLE = L_HANDLE
FNAME = L_FNAME
CHARACTER_MODE = 'X'
TABLES
TEXT = LT_OBJECT_CONTENT .
ELSEIF L_TEXT2 = 'BIN' .
L_BLOB_LENGTH = LS_DOCUMENT_DATA -DOC_SIZE .
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
HANDLE = L_HANDLE
FNAME = L_FNAME
BLOB_LENGTH = L_BLOB_LENGTH
TABLES
BLOB = LT_CONTENTS_HEX .
ENDIF .
ENDIF .
ES_ATTA -PERNR = IS_OBJECT -INSTID .
ES_ATTA -PATH = L_FNAME .
ES_ATTA -DOCSIZE = LS_DOCUMENT_DATA -DOC_SIZE .
ES_ATTA -UPDAT = SY -UZEIT .
SHIFT ES_ATTA -UPDAT RIGHT BY 1 PLACES .
CONCATENATE SY -DATUM ES_ATTA -UPDAT INTO ES_ATTA -UPDAT .
"es_atta-UPDAT = sy-datum.
"es_atta-uptim = sy-uzeit.
APPEND ES_ATTA TO ET_ATTA .
ENDLOOP .
*-- Disconnect
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
HANDLE = L_HANDLE .
*-- connect close
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
DESTINATION = 'SAPFTP'
EXCEPTIONS
OTHERS = 1 .
IF L_FLAG = 'X' .
E_FLAG = 'E' .
ELSE .
E_FLAG = 'S' .
ENDIF .
ENDFUNCTION .
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IS_OBJECT) TYPE SIBFLPORB
*" EXPORTING
*" REFERENCE(E_FLAG) TYPE C
*" TABLES
*" ET_ATTA STRUCTURE Z02HR_ATTA
*"----------------------------------------------------------------------
DATA : LS_OBJECT TYPE SIBFLPORB ,
LS_OPTION TYPE OBL_S_RELT ,
LT_OPTIONS TYPE OBL_T_RELT ,
LT_LINKS TYPE OBL_T_LINK ,
LS_LINK TYPE OBL_S_LINK .
*--- read attachment
DATA : LT_OBJECT_HEADER TYPE STANDARD TABLE OF SOLISTI1 ,
LT_OBJECT_CONTENT TYPE STANDARD TABLE OF SOLISTI1 ,
LT_CONTENTS_HEX TYPE STANDARD TABLE OF SOLIX ,
L_BLOB_LENGTH TYPE I .
FIELD-SYMBOLS <FS_BOJECT_HEADER> TYPE SOLISTI1 .
DATA : L_DOCMENT_ID TYPE SOFOLENTI1 -DOC_ID ,
LS_DOCUMENT_DATA TYPE SOFOLENTI1 ,
L_HANDLE TYPE I ,
L_PATH ( 60 ) TYPE C VALUE '/OA/SAP-OA/' ,
L_FNAME ( 60 ) TYPE C .
DATA : L_TEXT1 TYPE STRING ,
L_TEXT2 TYPE STRING .
DATA : L_FLAG TYPE C .
*--- attachment infomation
DATA : ES_ATTA TYPE Z02HR_ATTA .
DATA : L_SPACE TYPE C .
DATA : L_OA ( 3 ) TYPE C .
CLEAR : E_FLAG ,
ES_ATTA ,
ET_ATTA[] .
LS_OBJECT = IS_OBJECT .
IF LS_OBJECT -CATID IS INITIAL .
LS_OBJECT -CATID = 'BO' .
ENDIF .
*--- option
CLEAR LS_OPTION .
LS_OPTION - SIGN = 'I' .
LS_OPTION -OPTION = 'EQ' .
LS_OPTION -LOW = 'ATTA' .
APPEND LS_OPTION TO LT_OPTIONS .
*--- get attachment list.
TRY .
CALL METHOD CL_BINARY_RELATION =>READ_LINKS_OF_BINRELS
EXPORTING
IS_OBJECT = LS_OBJECT
IT_RELATION_OPTIONS = LT_OPTIONS
IP_ROLE = 'GOSAPPLOBJ'
"ip_no_buffer = gp_refresh
IMPORTING
ET_LINKS = LT_LINKS .
CATCH CX_OBL_PARAMETER_ERROR .
CATCH CX_OBL_INTERNAL_ERROR .
CATCH CX_OBL_MODEL_ERROR .
ENDTRY .
CHECK LT_LINKS IS NOT INITIAL .
*--- connect FTP
PERFORM FRM_CONNECT_FTP CHANGING L_HANDLE .
*--- attachment exit.
LOOP AT LT_LINKS INTO LS_LINK .
CLEAR ES_ATTA .
CLEAR L_DOCMENT_ID .
L_DOCMENT_ID = LS_LINK -INSTID_B .
CALL FUNCTION 'SO_DOCUMENT_READ_API1'
EXPORTING
DOCUMENT_ID = L_DOCMENT_ID
IMPORTING
DOCUMENT_DATA = LS_DOCUMENT_DATA
TABLES
OBJECT_HEADER = LT_OBJECT_HEADER
OBJECT_CONTENT = LT_OBJECT_CONTENT
CONTENTS_HEX = LT_CONTENTS_HEX
EXCEPTIONS
DOCUMENT_ID_NOT_EXIST = 1
OPERATION_NO_AUTHORIZATION = 2
X_ERROR = 3
OTHERS = 4 .
IF SY -SUBRC <> 0 .
L_FLAG = 'X' .
ENDIF .
**--- Create file on FTP server
* call function 'SAPGUI_PROGRESS_INDICATOR'
* exporting
* text = 'Create file on FTP Server'.
*--- create file name
READ TABLE LT_OBJECT_HEADER INDEX 1 ASSIGNING <FS_BOJECT_HEADER> .
IF SY -SUBRC = 0 .
SPLIT <FS_BOJECT_HEADER> - LINE AT '=' INTO L_TEXT1 L_TEXT2 .
IF STRLEN ( L_TEXT2 ) >= 3 .
L_OA = L_TEXT2+0 ( 3 ) .
TRANSLATE L_OA TO UPPER CASE .
IF L_OA <> 'OA_' .
CONTINUE .
ENDIF .
ELSE .
CONTINUE .
ENDIF .
CONCATENATE L_PATH SY -DATUM SY -UZEIT '_' L_TEXT2 INTO L_FNAME .
ES_ATTA -NAME = L_TEXT2 .
ENDIF .
*--- file tpye
CLEAR : L_TEXT1 , L_TEXT2 .
READ TABLE LT_OBJECT_HEADER INDEX 2 ASSIGNING <FS_BOJECT_HEADER> .
IF SY -SUBRC = 0 .
SPLIT <FS_BOJECT_HEADER> - LINE AT '=' INTO L_TEXT1 L_TEXT2 .
*--- transfer data to FTP
IF L_TEXT2 = 'ASC' .
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
HANDLE = L_HANDLE
FNAME = L_FNAME
CHARACTER_MODE = 'X'
TABLES
TEXT = LT_OBJECT_CONTENT .
ELSEIF L_TEXT2 = 'BIN' .
L_BLOB_LENGTH = LS_DOCUMENT_DATA -DOC_SIZE .
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
HANDLE = L_HANDLE
FNAME = L_FNAME
BLOB_LENGTH = L_BLOB_LENGTH
TABLES
BLOB = LT_CONTENTS_HEX .
ENDIF .
ENDIF .
ES_ATTA -PERNR = IS_OBJECT -INSTID .
ES_ATTA -PATH = L_FNAME .
ES_ATTA -DOCSIZE = LS_DOCUMENT_DATA -DOC_SIZE .
ES_ATTA -UPDAT = SY -UZEIT .
SHIFT ES_ATTA -UPDAT RIGHT BY 1 PLACES .
CONCATENATE SY -DATUM ES_ATTA -UPDAT INTO ES_ATTA -UPDAT .
"es_atta-UPDAT = sy-datum.
"es_atta-uptim = sy-uzeit.
APPEND ES_ATTA TO ET_ATTA .
ENDLOOP .
*-- Disconnect
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
HANDLE = L_HANDLE .
*-- connect close
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
DESTINATION = 'SAPFTP'
EXCEPTIONS
OTHERS = 1 .
IF L_FLAG = 'X' .
E_FLAG = 'E' .
ELSE .
E_FLAG = 'S' .
ENDIF .
ENDFUNCTION .