Friday, November 23, 2007

sap abap program for Use BDS to Store Files Like Excel JPG

BDS or MIME Repository to keep EXECL JPGs etc inside SAP
* Use SE80+Mime Repository to create a Folder ZJNC in PUBLIC with your image

* Program yjncMimeTest shows how easy it is to use Interface IF_MR_API
* The MIME way is small and crisp
* Thanks to
* https://forums.sdn.sap.com/thread.jspa?threadID=266657&tstart=15
* http://www.sapdevelopment.co.uk/graphics/add_image.htm

*================================================================================
* yjncBDStest is a Simple Maintenance of BDS
* Idea is to use BDS to store files like Excel JPG
* I prefer this now over CLUSTER & IMPORT/EXPORT
* as with BDN(Tcode OAOR) it is possible to transport BDS content

* Based on
http://smoschid.tripod.com/APPL_SAP_SOURCE_CODE/Z_BUSINESS_DOC_SERVICE.TXT
* by Stefanos Moschidis - SAP Certified Technical Consultant

* create a screen 100 with a container MCONTAINER
* GuiTitle TITLEBAR100
* GuiStatus STATUS100 weith 1 EXIT button

* SAP Desktop Office Integration Using ABAP Objects
*
http://www.intelligententerprise.com/channels/applications/feature/archive/ehre.jhtml
* SAP BDS & BDN: Integrated Document Management
*
http://www.intelligententerprise.com/channels/applications/feature/archive/schulze.jhtml

* Good ready code of BDS+DOI
* http://www.sappro.com/download03.cfm?session=

*
http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCSRVBDS/BDS_STRUCTURE.pdf

* there could be situations where you want BDS integration

*================================================================================

* Author Jayanta Narayan Choudhuri
* Flat 302
* 395 Jodhpur Park
* Kolkata 700 068
* Email
sss@cal.vsnl.net.in
* URL: http://www.geocities.com/ojnc

*================================================================================
REPORT yjncMimeTest. " the MIME way

DATA: o_mr_api TYPE REF TO if_mr_api.

DATA: BEGIN OF graphic_table OCCURS 0,
line(255) TYPE x,
END OF graphic_table.

DATA: graphic_size TYPE i.

DATA : mycontainer TYPE REF TO cl_gui_custom_container .

DATA : ok_code LIKE sy-ucomm ,
save_ok LIKE sy-ucomm .

DATA: graphic_url(255).

DATA: image TYPE REF TO cl_gui_picture.

DATA: l_graphic_xstr TYPE xstring,
l_graphic_conv TYPE i,
l_graphic_offs TYPE i.

START-OF-SELECTION.

IF o_mr_api IS INITIAL.
o_mr_api = cl_mime_repository_api=>if_mr_api~get_api( ).
ENDIF.

CALL METHOD o_mr_api->get
EXPORTING
i_url = 'sap/public/zjnc/jbln.jpg'
IMPORTING
e_content = l_graphic_xstr
EXCEPTIONS
parameter_missing = 1
error_occured = 2
not_found = 3
permission_failure = 4
OTHERS = 5.

* current will hold the image in a XSTRING var

CLEAR graphic_url.


graphic_size = XSTRLEN( l_graphic_xstr ).
CHECK graphic_size > 0.

l_graphic_conv = graphic_size.
l_graphic_offs = 0.

WHILE l_graphic_conv > 255.
graphic_table-line = l_graphic_xstr+l_graphic_offs(255).
APPEND graphic_table.
l_graphic_offs = l_graphic_offs + 255.
l_graphic_conv = l_graphic_conv - 255.
ENDWHILE.

graphic_table-line = l_graphic_xstr+l_graphic_offs(l_graphic_conv).
APPEND graphic_table.

CALL FUNCTION 'DP_CREATE_URL'
EXPORTING
type = 'IMAGE'
subtype = 'GIF'
TABLES
data = graphic_table[]
CHANGING
url = graphic_url
EXCEPTIONS
dp_invalid_parameter = 1
dp_error_put_table = 2
dp_error_general = 3
OTHERS = 4.


IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.

* PERFORM f_download_blob.

CALL SCREEN 100 .

*&;---------------------------------------------------------------------*
*&; Module STATUS_0100 OUTPUT
*&;---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STATUS100'.
SET TITLEBAR 'TITLEBAR100'.

ENDMODULE. " STATUS_0100 OUTPUT

*&;---------------------------------------------------------------------*
*&; Module prepare_controls OUTPUT
*&;---------------------------------------------------------------------*
MODULE prepare_controls OUTPUT.
IF mycontainer IS INITIAL.

CREATE OBJECT mycontainer
EXPORTING
container_name = 'MCONTAINER'
EXCEPTIONS
OTHERS = 1.

CASE sy-subrc.
WHEN 0.
WHEN OTHERS.
RAISE cntl_error.
ENDCASE.

ENDIF.

IF graphic_url IS NOT INITIAL AND image IS INITIAL.
PERFORM f_pic_display.
EXIT.
ENDIF.


ENDMODULE. " prepare_controls OUTPUT

*&;---------------------------------------------------------------------*
*&; Module USER_COMMAND_0100 INPUT
*&;---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
save_ok = ok_code .
CLEAR ok_code .
IF save_ok = 'EXIT' .
LEAVE TO SCREEN 0.
ENDIF .
ENDMODULE. " USER_COMMAND_0100

*&---------------------------------------------------------------------*
*& Form f_pic_display
*&---------------------------------------------------------------------*
FORM f_pic_display.

CREATE OBJECT image
EXPORTING
parent = mycontainer.

CALL METHOD image->load_picture_from_url
EXPORTING
url = graphic_url.

image->set_display_mode( image->display_mode_normal_center ).

cl_gui_cfw=>flush( ).

cl_gui_cfw=>update_view( ).

ENDFORM. "f_pic_display

*================================================================================
REPORT yjncbdstest. " the BDS way

TYPE-POOLS: sbdst, abap.

*
======================================================================
* DATA DECLARATIONS
*
======================================================================
DATA : viewer TYPE REF TO i_oi_document_viewer .
DATA : mycontainer TYPE REF TO cl_gui_custom_container .

DATA : edurl(2048).

CONSTANTS : buttonselected VALUE 'X' .

DATA : ok_code LIKE sy-ucomm ,
save_ok LIKE sy-ucomm .

* For uploading Files to BDS
DATA: BEGIN OF itab OCCURS 0,
raw TYPE sdok_sdatx,
END OF itab.

DATA: filesize TYPE i,
filename TYPE string,
myext TYPE string.

DATA: myclassname TYPE sbdst_classname,
myclasstype TYPE sbdst_classtype,
mymimetype TYPE bapicompon-mimetype,
mycomponents TYPE sbdst_components WITH HEADER LINE,
myfiles TYPE sbdst_files WITH HEADER LINE,
mysignature TYPE sbdst_signature WITH HEADER LINE,
excluding TYPE sbdst_excluding WITH HEADER LINE,
myquery TYPE sbdst_query WITH HEADER LINE,
myuris TYPE sbdst_uri WITH HEADER LINE.

DATA: graphic_url(255).

DATA: image TYPE REF TO cl_gui_picture.

DATA: len TYPE i,
offset TYPE i,
wlen TYPE i,
woff TYPE i.

* -------------------------------------------------------------------
* 2 KEYWORDS
* -------------------------------------------------------------------
DATA: key_1 LIKE bapibds01-key_word,
key_2 LIKE bapibds01-key_word.

*
======================================================================
* SELECTION SCREEN DEFAULT
*
======================================================================
* -------------------------------------------------------------------
* Frame of Program Options ; TEXT-002 = 'Program Options' .
* -------------------------------------------------------------------
SELECTION-SCREEN BEGIN OF BLOCK action_a WITH FRAME TITLE text-002 .

PARAMETERS: upload RADIOBUTTON GROUP navs DEFAULT 'X',
get_url RADIOBUTTON GROUP navs,
downsngl RADIOBUTTON GROUP navs,
navigato RADIOBUTTON GROUP navs,
showimg RADIOBUTTON GROUP navs.

SELECTION-SCREEN END OF BLOCK action_a .

SELECTION-SCREEN BEGIN OF BLOCK action_b WITH FRAME TITLE text-002 .

PARAMETERS: p_file TYPE rlgrap-filename,
p_mime TYPE bapicompon-mimetype,
p_class TYPE sbdst_classname.


SELECTION-SCREEN END OF BLOCK action_b.

*
======================================================================
INITIALIZATION .
*
======================================================================
* t_upload = '1 Upload File to BDS' .
* t_url = '2 Show from URL' .
* t_single = '3 Download File from BDS' .
* t_navi = '4 Show Navigator' .
* t_show = '5 Show Image' .



* At selection screen
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = 'P_FILE'
IMPORTING
file_name = p_file.

*
======================================================================
START-OF-SELECTION .
*
======================================================================

MOVE p_class TO: myclassname, key_1.

MOVE 'OT' TO myclasstype.

CLEAR myfiles.

MOVE '1' TO: myfiles-doc_count, myfiles-comp_count.

MOVE p_file TO filename.
COMPUTE len = STRLEN( filename ).

DO len TIMES.
offset = len - sy-index.
IF filename+offset(1) = '.'.
woff = offset + 1.
wlen = len - woff.
MOVE filename+woff(wlen) TO myext.
ENDIF.

IF filename+offset(1) = '\' OR filename+offset(1) = '/'.
woff = offset + 1.
wlen = len - woff.
MOVE filename+woff(wlen) TO myfiles-filename.

wlen = offset.
MOVE filename+0(wlen) TO myfiles-directory.
EXIT.
ENDIF.
ENDDO.

TRANSLATE myext TO UPPER CASE.

* http://www.w3schools.com/media/media_mimeref.asp
CASE myext.
WHEN 'XLS'.
MOVE 'application/vnd.ms-excel' TO mymimetype.
WHEN 'DOC'.
MOVE 'application/msword' TO mymimetype.
WHEN 'PPT'.
MOVE 'application/vnd.ms-powerpoint' TO mymimetype.
WHEN 'PDF'.
MOVE 'application/pdf' TO mymimetype.
WHEN 'JPG'.
MOVE 'image/jpeg' TO mymimetype.
WHEN 'GIF'.
MOVE 'image/gif' TO mymimetype.
WHEN 'BMP'.
MOVE 'image/bmp' TO mymimetype.
WHEN 'TIF'.
MOVE 'image/tiff' TO mymimetype.
WHEN 'ZIP'.
MOVE 'application/zip' TO mymimetype.
WHEN 'RTF'.
MOVE 'application/rtf' TO mymimetype.
WHEN 'TXT'.
MOVE 'text/plain' TO mymimetype.
ENDCASE.

IF mymimetype IS INITIAL.
MOVE p_mime TO mymimetype.
ENDIF.

MOVE mymimetype TO: myfiles-mimetype, key_2.

IF myfiles-filename IS NOT INITIAL.
IF myfiles-directory IS INITIAL.
MOVE '/' TO myfiles-directory.
ENDIF.
APPEND myfiles.
ENDIF.

CASE buttonselected .

* -------> UPLOAD DOCUMENT IN BUSINESS DOCUMENT NAVIGATOR
<------------
WHEN upload.
PERFORM build_keyword_criteria .

IF myfiles-filename IS INITIAL.
MESSAGE e999(zmsgs) WITH 'File Name is Mandatory'.
ENDIF.

IF mymimetype IS INITIAL.
MESSAGE e999(zmsgs) WITH 'Mime Type is Mandatory'.
ENDIF.

CALL METHOD cl_bds_document_set=>create_with_files
EXPORTING
classname = myclassname
classtype = myclasstype
CHANGING
files = myfiles[]
signature = mysignature[]
EXCEPTIONS
internal_error = 1
error_kpro = 2
parameter_error = 3
not_allowed = 4
not_authorized = 5
nothing_found = 6
OTHERS = 7.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

MESSAGE i999(zmsgs) WITH 'Upload Successful'.

* -------> Display Image ACCORDING TO KEYWORD CRITERIA <----------
WHEN downsngl .
PERFORM build_keyword_criteria .

IF myfiles-filename IS INITIAL.
MESSAGE e999(zmsgs) WITH 'File Name is Mandatory'.
ENDIF.

CALL METHOD cl_bds_document_set=>get_with_url
EXPORTING
classname = myclassname
classtype = myclasstype
url_lifetime = 'T'
CHANGING
uris = myuris[]
signature = mysignature[].

CALL METHOD cl_bds_document_set=>get_with_files
EXPORTING
classname = myclassname
classtype = myclasstype
CHANGING
files = myfiles[]
signature = mysignature[]
EXCEPTIONS
nothing_found = 1
error_kpro = 2
internal_error = 3
parameter_error = 4
not_authorized = 5
not_allowed = 6.

IF sy-subrc NE 0 .
MESSAGE i999(zmsgs) WITH 'ERROR DOWNLOADING FILE' sy-subrc .
ENDIF .

* -------> FIND A FILE SHOW INPLACE FROM SAP R3 URL <----------
WHEN get_url .
PERFORM build_keyword_criteria.

CALL METHOD cl_bds_document_set=>get_with_url
EXPORTING
classname = myclassname
classtype = myclasstype
url_lifetime = 'T'
CHANGING
uris = myuris[]
signature = mysignature[].

CALL SCREEN 100 .

* -------> SHOW DOCUMENTS IN BUSINESS DOCUMENT NAVIGATOR
<------------
WHEN navigato .
PERFORM build_keyword_criteria.

CALL METHOD cl_bds_document_set=>call_navigator
EXPORTING
classname = myclassname
classtype = myclasstype
CHANGING
excluding = excluding[]
signature = mysignature[].

* -------> SHOW IMAGE if image/jpeg <------
WHEN showimg .
PERFORM build_keyword_criteria.

CALL METHOD cl_bds_document_set=>get_with_table
EXPORTING
classname = myclassname
classtype = myclasstype
CHANGING
content = itab[]
EXCEPTIONS
error_kpro = 1
internal_error = 2
nothing_found = 3
no_content = 4
parameter_error = 5
not_authorized = 6
not_allowed = 7
OTHERS = 8.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

PERFORM f_display_pic.

ENDCASE .

*&;---------------------------------------------------------------------*
*&; Form BUILD_KEYWORD_CRITERIA
*&;---------------------------------------------------------------------*
FORM build_keyword_criteria.

MOVE '1' TO: mysignature-doc_count, mysignature-comp_count.
MOVE: 'BDS_KEYWORD' TO mysignature-prop_name,
key_1 TO mysignature-prop_value.
APPEND mysignature.

MOVE '1' TO: mysignature-doc_count, mysignature-comp_count.
MOVE: 'BDS_KEYWORD' TO mysignature-prop_name,
key_2 TO mysignature-prop_value.
APPEND mysignature.

ENDFORM. " BUILD_KEYWORD_CRITERIA

*&;---------------------------------------------------------------------*
*&; Module STATUS_0100 OUTPUT
*&;---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STATUS100'.
SET TITLEBAR 'TITLEBAR100'.

ENDMODULE. " STATUS_0100 OUTPUT

*&;---------------------------------------------------------------------*
*&; Module prepare_controls OUTPUT
*&;---------------------------------------------------------------------*
MODULE prepare_controls OUTPUT.
IF mycontainer IS INITIAL.

CREATE OBJECT mycontainer
EXPORTING
container_name = 'MCONTAINER'
EXCEPTIONS
OTHERS = 1.

CASE sy-subrc.
WHEN 0.
WHEN OTHERS.
RAISE cntl_error.
ENDCASE.

ENDIF.

IF graphic_url IS NOT INITIAL AND image IS INITIAL.
PERFORM f_pic_display.
EXIT.
ENDIF.

IF viewer IS INITIAL.
CALL METHOD c_oi_container_control_creator=>get_document_viewer
IMPORTING
viewer = viewer
EXCEPTIONS
unsupported_platform = 1.

IF sy-subrc NE 0.
MESSAGE i999(zmsg) WITH 'Unknown error'.
ENDIF.

CALL METHOD viewer->init_viewer
EXPORTING
parent = mycontainer
EXCEPTIONS
cntl_error = 1
cntl_install_error = 2
dp_install_error = 3
dp_error = 4.

IF sy-subrc NE 0.
MESSAGE i999(zmsgs) WITH 'Unknown error'.
ENDIF.
ENDIF.

CLEAR edurl.

SORT myuris BY uri_count DESCENDING .

READ TABLE myuris INDEX 1 .
IF sy-subrc = 0 .
edurl = myuris-uri .
ENDIF .

IF NOT edurl IS INITIAL.
CALL METHOD viewer->view_document_from_url
EXPORTING
document_url = edurl
show_inplace = 'X'
EXCEPTIONS
cntl_error = 1
not_initialized = 2
dp_error_general = 3
invalid_parameter = 4.

IF sy-subrc NE 0.
MESSAGE i999(zmsgs) WITH 'Unknown error'.
ENDIF.
ENDIF.


ENDMODULE. " prepare_controls OUTPUT

*&;---------------------------------------------------------------------*
*&; Module USER_COMMAND_0100 INPUT
*&;---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
save_ok = ok_code .
CLEAR ok_code .
IF save_ok = 'EXIT' .
LEAVE TO SCREEN 0.
ENDIF .
ENDMODULE. " USER_COMMAND_0100

*&---------------------------------------------------------------------*
*& Form F_upload_blob
*&---------------------------------------------------------------------*
FORM f_upload_blob.

MOVE p_file TO filename.

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filetype = 'BIN'
filename = filename
IMPORTING
filelength = filesize
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 <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDFORM. " F_upload_blob

*&---------------------------------------------------------------------*
*& Form F_download_blob
*&---------------------------------------------------------------------*
FORM f_download_blob.

MOVE p_file TO filename.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filetype = 'BIN'
filename = filename
IMPORTING
filelength = filesize
TABLES
data_tab = itab
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 22.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDFORM. " F_download_blob

*&---------------------------------------------------------------------*
*& Form f_display_pic
*&---------------------------------------------------------------------*
FORM f_display_pic.

CLEAR graphic_url.

CALL FUNCTION 'DP_CREATE_URL'
EXPORTING
type = 'IMAGE'
subtype = 'GIF'
TABLES
data = itab[]
CHANGING
url = graphic_url
EXCEPTIONS
dp_invalid_parameter = 1
dp_error_put_table = 2
dp_error_general = 3
OTHERS = 4.


IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.

* PERFORM f_download_blob.

CALL SCREEN 100 .

ENDFORM. "f_display_pic

*&---------------------------------------------------------------------*
*& Form f_pic_display
*&---------------------------------------------------------------------*
FORM f_pic_display.

CREATE OBJECT image
EXPORTING
parent = mycontainer.

CALL METHOD image->load_picture_from_url
EXPORTING
url = graphic_url.

image->set_display_mode( image->display_mode_normal_center ).

cl_gui_cfw=>flush( ).

cl_gui_cfw=>update_view( ).

ENDFORM. "f_pic_display


* CREATE FROM TABLE
*==================
* PERFORM build_keyword_criteria .
*
* PERFORM f_upload_blob.
*
* MOVE 1 TO mycomponents-doc_count.
* MOVE 1 TO mycomponents-comp_count.
* MOVE mymimetype TO mycomponents-mimetype.
* APPEND mycomponents.
*
* CALL METHOD cl_bds_document_set=>create_with_table
* EXPORTING
* classname = myclassname
* classtype = myclasstype
* components = mycomponents[]
* content = itab[]
* CHANGING
* signature = mysignature[]
* EXCEPTIONS
* internal_error = 1
* error_kpro = 2
* parameter_error = 3
* not_authorized = 4
* not_allowed = 5
* nothing_found = 6
* OTHERS = 7.
*
* IF sy-subrc <> 0.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
* ENDIF.

No comments:

Blog Archive