Monday, March 19, 2018

ABAP - Save SmartForms OTF-output as GOS PDF-attachment

function z_gos_pdf_attachment_create.
*"----------------------------------------------------------------------
*"*"Lokale interface:
*"  IMPORTING
*"     VALUE(IC_OBJTYP) TYPE  SWO_OBJTYP
*"     VALUE(IC_TYPEID) TYPE  SWO_TYPEID
*"     VALUE(ITB_OTFDATA) TYPE  TSFOTF
*"     VALUE(IC_FILENAME) TYPE  LOCALFILE
*"  EXPORTING
*"     VALUE(ES_RETURN) TYPE  BAPIRET2
*"----------------------------------------------------------------------
 *&---------------------------------------------------------------------*
*& Function Z_GOS_PDF_ATTACHMENT                                       *
*&---------------------------------------------------------------------*
*& Purpose     : Saving the OTF-output of a SmartForm as a             *
*&               PDF-attachment using Generic Object Services          *
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*    data:
    ltb_otfdata           type tsfotf,
    ltb_objcont           type soli_tab.
   data:
    ls_folder_id          type soodk,
    ls_attach_id          type soodk.
   data:
    lc_objtyp             type swo_objtyp,
    lc_typeid             type swo_typeid,
    lc_objlen             type so_obj_len,
    lc_filename           type localfile.
   clear es_return.
 * Importparameters to local variables
  ltb_otfdata[] = itb_otfdata[].
  lc_objtyp     = ic_objtyp.
  lc_typeid     = ic_typeid.
  lc_filename   = ic_filename.
 * Convert OTF to PDF in correct format
  perform convert_otf
          using    ltb_otfdata
          changing ltb_objcont
                   lc_objlen
                   es_return.
   check es_return is initial.
 * Get folder ID
  perform get_folder_id
          changing ls_folder_id
                   es_return.
   check es_return is initial.
 * Create attachment (in database)
  perform create_attachment
          using    ls_folder_id
                   ltb_objcont
                   lc_objlen
                   lc_filename
          changing ls_attach_id
                   es_return.
   check es_return is initial.
 * Create link between object and attachment
  perform create_link
          using    ls_folder_id
                   ls_attach_id
                   lc_objtyp
                   lc_typeid
          changing es_return.
   if es_return is initial.
    es_return-type   = 'S'.
    es_return-id     = 'SO'.
    es_return-number = '182'.
  endif.
 
endfunction.function-pool z_gos_pdf_attachment.         "MESSAGE-ID ..
 *-----------------------------------------------------------------------

* Constants*-----------------------------------------------------------------------
constants:
  cc_textpool_repid       type syrepid value 'SAPFSSO3',
  cc_id_textsymbol        type textpoolid value 'I',
  cc_key_message          type textpoolky value '001',
  cc_objtyp_message       type swo_objtyp value 'MESSAGE',
  cc_doctp_ext            type so_doc_tp value 'EXT',
  cc_relationtype_atta    type binreltyp value 'ATTA'.
 *-----------------------------------------------------------------------

* Internal tables
*-----------------------------------------------------------------------
data:
gtb_textpool            type standard table of textpool.
* ----------------------------------------------------------------------
***INCLUDE LZ_GOS_PDF_ATTACHMENTF01 .
*----------------------------------------------------------------------
*&---------------------------------------------------------------------
*&     Form convert_otf
*&---------------------------------------------------------------------
*     -->ITB_OTF
*     <--ETB_OBJCONT
*     <--EC_OBJLEN
*     <--ES_RETURN
*----------------------------------------------------------------------
form convert_otf
    using   itb_otf    type tsfotf
    changing etb_objcont type soli_tab
             ec_objlen  type so_obj_len
             es_return  type bapiret2.
  data:
   ltb_pdf              type tsftext.
  data:
   ls_string            type string.
  data:
   li_bin_filesize      type i.
  refresh etb_objcont.
 clear: ec_objlen,
        es_return.
 * Convert OTF format (smartforms output) to PDF
 call function 'CONVERT_OTF'
      exporting
           format               = 'PDF'
      importing
           bin_filesize         = li_bin_filesize
      tables
           otf                  = itb_otf
           lines                = ltb_pdf
      exceptions
           err_max_linewidth    = 1
           err_format           = 2
           err_conv_not_possible = 3
           others               = 4.
  if sy-subrc ne 0.
   es_return-type  = 'E'.
   es_return-id    = 'SO'.
   es_return-number = '754'.
   exit.
 endif.
  ec_objlen = li_bin_filesize.
 * Convert OTF format to String
 call function 'SWA_STRING_FROM_TABLE'
      exporting
           character_table           = ltb_pdf
      importing
           character_string          = ls_string
      exceptions
           no_flat_charlike_structure = 1
           others                    = 2.
  if sy-subrc ne 0.
   es_return-type  = 'E'.
   es_return-id    = 'SO'.
   es_return-number = '754'.
   exit.
 endif.
 * Convert String to Sap Office table
 call function 'SWA_STRING_TO_TABLE'
      exporting
           character_string          = ls_string
      importing
           character_table           = etb_objcont
      exceptions
           no_flat_charlike_structure = 1
           others                    = 2.
  if sy-subrc ne 0.
   es_return-type  = 'E'.
   es_return-id    = 'SO'.
   es_return-number = '754'.
   exit.
 endif.
 endform.                   " convert_otf
 &---------------------------------------------------------------------
*&     Form get_folder_id
&---------------------------------------------------------------------
*     <--ES_FOLDER_ID
*     <--ES_RETURN
*----------------------------------------------------------------------
form get_folder_id
    changing es_folder_id    type soodk
             es_return  type bapiret2.
  clear: es_folder_id,
        es_return.
  call function 'SO_FOLDER_ROOT_ID_GET'
      exporting
           region   = 'B'
      importing
           folder_id = es_folder_id
      exceptions
           others   = 1.
  if sy-subrc ne 0.
   es_return-type  = 'E'.
   es_return-id    = 'SO'.
   es_return-number = '005'.
   exit.
 endif.
 endform.                   " get_folder_id
* &---------------------------------------------------------------------
*&     Form create_attachment
*&---------------------------------------------------------------------
*     -->IS_FOLDER_ID
*     -->ITB_OBJCONT
*     -->IC_OBJLEN
*     -->IC_FILENAME
*     <--ES_ATTACH_ID
*     <--ES_RETURN
*----------------------------------------------------------------------
form create_attachment
    using   is_folder_id    type soodk
             itb_objcont type soli_tab
             ic_objlen  type so_obj_len
             ic_filename type localfile
    changing es_attach_id    type soodk
             es_return  type bapiret2.
  data:
   ltb_objhead          type soli_tab.
  data:
   ls_object_id         type soodk,
   ls_object_hd_change  type sood1.
  data:
   lc_attach_type       type so_obj_tp value cc_doctp_ext,
   lc_owner             type so_usr_nam.
  clear es_attach_id.
  ls_object_id = is_folder_id.
  ls_object_hd_change-objlen = ic_objlen.
 ls_object_hd_change-objla = sy-langu.
 * Extract object description and extention from filename
 perform extract_object_description
         using   ic_filename
         changing ls_object_hd_change-objdes
                  ls_object_hd_change-file_ext.
 * Get objectname
 perform get_objectname
         changing ls_object_hd_change-objnam.
  lc_owner = sy-uname.
  call function 'SO_ATTACHMENT_INSERT'
      exporting
           object_id                 = ls_object_id
           object_hd_change          = ls_object_hd_change
           attach_type               = lc_attach_type
           owner                     = lc_owner
      importing
           attach_id                 = es_attach_id
      tables
           objcont                   = itb_objcont
           objhead                   = ltb_objhead
      exceptions
           active_user_not_exist     = 1
           object_type_not_exist     = 2
           operation_no_authorization = 3
           owner_not_exist           = 4
           parameter_error           = 5
           substitute_not_active     = 6
           substitute_not_defined    = 7
           x_error                   = 8
           system_failure            = 9
           communication_failure     = 10
           others                    = 11.
  if sy-subrc ne 0.
   es_return-type      = 'E'.
   es_return-id        = 'SO'.
   es_return-number    = '897'.
   es_return-message_v1 = ls_object_hd_change-objdes.
   exit.
  else.
   commit work and wait.
  endif.
 endform.                   " create_attachment
*&---------------------------------------------------------------------
*&     Form extract_object_description
*&---------------------------------------------------------------------
*     -->IC_FILENAME
*     <--EC_OBJDES
*     <--EC_FILE_EXT
*----------------------------------------------------------------------
form extract_object_description
    using   ic_filename type localfile
    changing ec_objdes  type so_obj_des
             ec_file_ext type so_fileext.
  data:
   li_length            type i,
   li_start_position    type i,
   lc_filename          type localfile,
   lc_stripped_name     type localfile.
  clear: ec_objdes,
        ec_file_ext.
  lc_filename = ic_filename.
  call function 'SO_SPLIT_FILE_AND_PATH'
      exporting
           full_name    = lc_filename
      importing
           stripped_name = lc_stripped_name
      exceptions
           x_error      = 1
           others       = 2.
  if sy-subrc eq 0.
   split lc_stripped_name
      at '.'
    into ec_objdes
         ec_file_ext.
    set locale language sy-langu.
   translate ec_file_ext to upper case.
   set locale language space.
  else.
   ec_objdes  = ic_filename.
   ec_file_ext = 'PDF'.
  endif.
 endform.                   " extract_object_description
* &---------------------------------------------------------------------
*&     Form get_objectname
*&---------------------------------------------------------------------
*     <--EC_OBJNAM
*----------------------------------------------------------------------
form get_objectname
    changing ec_objnam  type so_obj_nam.
  data:
   ls_textpool          type textpool.
  field-symbols:
   <fs_textpool>        type textpool.
  if gtb_textpool[] is initial.
   perform read_textpool.
 endif.
  read table gtb_textpool
      assigning <fs_textpool>
      with key id = cc_id_textsymbol
               key = cc_key_message
      binary search.
  if sy-subrc eq 0.
   ec_objnam = <fs_textpool>-entry(<fs_textpool>-length).
    set locale language sy-langu.
   translate ec_objnam to upper case.
   set locale language space.
   endif.
 endform.                   " get_objectname
* &---------------------------------------------------------------------
*&     Form read_textpool
*&---------------------------------------------------------------------
form read_textpool.
  refresh gtb_textpool.
 * Read textpool from program SAPFSSO3
 read textpool cc_textpool_repid
      into    gtb_textpool
      language sy-langu.
  sort gtb_textpool by id key.
 endform.                   " read_textpool
* &---------------------------------------------------------------------
*&     Form create_link
*&---------------------------------------------------------------------
*     -->IS_FOLDER_ID
*     -->IS_ATTACH_ID
*     -->IC_OBJTYP
*     -->IC_TYPEID
*     <--ES_RETURN
*----------------------------------------------------------------------
form create_link
    using   is_folder_id    type soodk
             is_attach_id    type soodk
             ic_objtyp  type swo_objtyp
             ic_typeid  type swo_typeid
    changing es_return  type bapiret2.
  data:
   ls_obj_rolea           type borident,
   ls_obj_roleb           type borident.
  data:
   lc_relationtype        type binreltyp value cc_relationtype_atta.
  concatenate is_folder_id
             is_attach_id
        into ls_obj_roleb-objkey.
  ls_obj_roleb-objtype = cc_objtyp_message.
  ls_obj_rolea-objkey = ic_typeid.
 ls_obj_rolea-objtype = ic_objtyp.
  call function 'BINARY_RELATION_CREATE'
      exporting
           obj_rolea   = ls_obj_rolea
           obj_roleb   = ls_obj_roleb
           relationtype = lc_relationtype
      exceptions
           others      = 1.
  if sy-subrc ne 0.
   es_return-type      = 'E'.
   es_return-id        = 'SO'.
   es_return-number    = '897'.
   es_return-message_v1 = ls_obj_roleb-objkey.
   exit.
  else.
   commit work and wait.
  endif.
 endform.                   " create_link

No comments:

Post a Comment