SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Generic object services (GOS) - in Background



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SAP Business Workflow
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Tue Mar 04, 2008 6:25 pm    Post subject: Generic object services (GOS) - in Background Reply with quote

Generic object services (GOS) - in Background
Author: Ram Manohar Tiwari
Original: https: //www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/2519

Just kidding Wink

I saw a few un-answered questions on SDN on how to attach documents and URLs with the Business Objects, in background and then it should appear in GOS attachment list. So I am trying to understand the basics inside GOS.

[Those un-answered question are years old so I am not sure if GOS is obsolete & now it is only used in Waldorf Wink ]
However, I did manage to write a program using the inside- logic of GOS, to attach Notes and URLs in background. [ Wow...finally I discovered that the earth is flat & it's standstill Wink...any doubts.. ]

Anyway ...here we go..
Attaching documents to Business Objects in background / providing a custom GOS like functionality for BSPs:
[ Actually, I pretend that I know BSPs just because I have some experience with JSPs/HTML and..ABAP is...well no... It’s not my mother tongue Wink. So I confidently used to pass suggestions about BSPs as long as I don't have to write it by myself. ]

The Problem:
Since GOS can only be used with SAPGui Front-end ( OK..may be with ITS as well) and only in foreground, mass-attachment of documents (in background) can’t be handled with GOS. However, the basic applications (Classes & Methods), used in GOS, can be utilized to create a custom program for this purpose.

Use: Since this new program will have the ability to run in background, irrespective of front-end, the same code can also be utilized to provide a GOS like facility in BSPs or while developing an upload program for attaching documents/URLs to Business Objects, in background.

Inside GOS:
Basically, inside the application, main business object and attachment, both are treated as Business Objects and then a link is maintained between both the object instances. The relationship type, while maintaining the link, describes whether the attached object is a URL or a file attachment, note and so on.

However, while the main business object, to which you are trying to attach the document / URL is already known e.g. for Purchase order the Object type ('BUS2012’ )is known and instance (?) exists in database but the instance for the attachment has to be created first before the linking.

Attachment can be a URL / a Note / a File and so you need to first upload (data as well in case of file) the attahcment info, and in the process also get a business object instance generated of BO type ‘MESSAGE’.

The program
The program-processing will have following steps:


Upload the File to be attached or in case of URL just get the URL name. In case of mass upload, the input can be read from a data-file on application server, having info (e.g. URL / File Path ) against the Business Object key ( e.g. Purchase Order Number ).
Create an instance of BO type ‘MESSAGE’ using BO Method MESSAGE.Create. In case you are not comfortable with BO macros, simply call the main FM used within. The important part here is to pass the document type e.g. URL, EXT (for external files), and contents of file as well the file type ( e.g. TXT, PDF ) in case of File attachments. Here the document type can be derived from the relationship type of the link.
Now, the attachment is created in database as a ‘MESSAGE’ and instance is known. We’ll refer to this as object_b and main Business Object as objet_a.
Now, maintain the link.

Check the attached documents through GOS toolbar Attachment List, using main business objects’ transaction e.g. ME23N for Purchase Orders. Sample Code

Code:
*---------------------------------------------------------------------*
* Report  Z_RMTIWARI_ATTACH_DOC_TO_BO
*---------------------------------------------------------------------*
* Written By : Ram Manohar Tiwari
* Function   : We need to maintain links between Business Object and
*              the attachment.Attachment document is basiclally a
*              business object of type 'MESSAGE'.In order to maintain
*              links, first the attachment will be crated as Business
*              Object of type 'MESSAGE' using Message.Create method.
*              Need to check if we can also use FM
*              'SO_DOC_INSERT_WITH_ORIG_API1' or SO_OBJECT_INSERT rather
*              than using Message.Create method.
*---------------------------------------------------------------------*

REPORT  Z_RMTIWARI_ATTACH_DOC_TO_BO             .
* Include for BO macros
  INCLUDE : <cntn01>.
* Load class.
  CLASS CL_BINARY_RELATION definition load.
  CLASS CL_OBL_OBJECT      definition load.


PARAMETERS:
*  Object_a
   P_BOTYPE LIKE obl_s_pbor-typeid DEFAULT 'ZFRIENDS', " e.g. 'BUS2012'
   P_BO_ID  LIKE OBL_S_PBOR-INSTID DEFAULT '00007',    " Key e.g. PO No.

*  Object_b
   P_DOCTY  LIKE obl_s_pbor-typeid DEFAULT 'MESSAGE' NO-DISPLAY,
   P_MSGTYP LIKE SOFM-DOCTP        DEFAULT 'URL'     NO-DISPLAY,

*  Relationship
   P_RELTYP  LIKE mdoblrel-reltype DEFAULT 'URL'.

  types: BEGIN OF TY_MESSAGE_KEY,
          FOLTP   TYPE SO_FOL_TP,
          FOLYR   TYPE SO_FOL_YR,
          FOLNO   TYPE SO_FOL_NO,
          DOCTP   TYPE SO_DOC_TP,
          DOCYR   TYPE SO_DOC_YR,
          DOCNO   TYPE SO_DOC_NO,
          FORTP   TYPE SO_FOR_TP,
          FORYR   TYPE SO_FOR_YR,
          FORNO   TYPE SO_FOR_NO,
         END OF TY_MESSAGE_KEY.

  DATA : LV_MESSAGE_KEY type TY_MESSAGE_KEY.
  DATA : LO_MESSAGE type SWC_OBJECT.
  DATA : LT_DOC_CONTENT type standard table of SOLI-LINE with header
line.

* First derive the Attachment's ( MESSAGE )document type.
  P_DOCTY = 'MESSAGE'.
  CASE P_RELTYP.
*   In case of URls
    WHEN 'URL'.
       P_MSGTYP = 'URL'.
*   In case of Notes / Private Notes
    WHEN 'NOTE' OR 'PNOT'.
       P_MSGTYP = 'RAW'.
    WHEN 'ATTA'.
       P_MSGTYP = 'EXT'.
*   Not implemented as yet...exit
     EXIT.
    WHEN OTHERS.
*    ....exit
     EXIT.
    ENDCASE.
*----------------------------------------------------------------*

* Create an initial instance of BO 'MESSAGE' - to call the
* instance-independent method 'Create'.
  swc_create_object LO_MESSAGE 'MESSAGE' LV_MESSAGE_KEY.

* define container to pass the parameter values to the method call
* in next step.
  swc_container LT_MESSAGE_CONTAINER.
* Populate container with parameters for method
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTITLE' 'Title'.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTLANGU' 'E'.
  swc_set_element LT_MESSAGE_CONTAINER 'NO_DIALOG'     'X'.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTNAME' P_DOCTY.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTYPE'   P_MSGTYP.

* 'DocumentContent' is a multi-line element ( itab ).

* In case of URLs..it should be concatenated with &KEY& in the begining.
  CASE P_MSGTYP.
    WHEN 'URL'.
      LT_DOC_CONTENT = '&KEY&http://www.rmtiwari.com' .
      append LT_DOC_CONTENT.

* In case of Notes or Private Notes, get the data from files on appl
* server or from wherever(? - remember background).
     WHEN 'RAW'.
       LT_DOC_CONTENT = 'Hi How r u?' .
       append LT_DOC_CONTENT.

* In case of File attachments
     WHEN 'EXT'.
*       Upload the file contents using open dataset in lt_doc_content .
*       Some conversion ( Compress ) might be required.
*       Not sure at this point
  ENDCASE.

  swc_set_element LT_MESSAGE_CONTAINER 'DocumentContent' LT_DOC_CONTENT.

  swc_call_method LO_MESSAGE 'CREATE' LT_MESSAGE_CONTAINER.

* Refresh to get the reference of create 'MESSAGE' object for attachment
  swc_refresh_object LO_MESSAGE.

* Get Key of new object
  swc_get_object_key LO_MESSAGE LV_MESSAGE_KEY.

* Now we have attachment as a business object instance. We can now
* attach it to our main business object instance.

* Create main BO object_a
  data: LO_IS_OBJECT_A type SIBFLPORB.

  LO_IS_OBJECT_A-INSTID = P_BO_ID.
  LO_IS_OBJECT_A-TYPEID = P_BOTYPE.
  LO_IS_OBJECT_A-CATID  = 'BO'.

* Create attachment BO object_b
  data: LO_IS_OBJECT_B type SIBFLPORB.

  LO_IS_OBJECT_B-INSTID = LV_MESSAGE_KEY.
  LO_IS_OBJECT_B-TYPEID = P_DOCTY.
  LO_IS_OBJECT_B-CATID  = 'BO'.

*TRY.
CALL METHOD CL_BINARY_RELATION=>CREATE_LINK
  EXPORTING
    IS_OBJECT_A            = LO_IS_OBJECT_A
*    IP_LOGSYS_A            =
    IS_OBJECT_B            = LO_IS_OBJECT_B
*    IP_LOGSYS_B            =
    IP_RELTYPE             = P_RELTYP
*    IP_PROPNAM             =
*    I_PROPERTY             =
*  IMPORTING
*    EP_LINK_ID             =
*    EO_PROPERTY            =
    .
*CATCH CX_OBL_PARAMETER_ERROR .
*CATCH CX_OBL_MODEL_ERROR .
*CATCH CX_OBL_INTERNAL_ERROR .
*ENDTRY.

* Check if everything OK...who cares!!
  commit work.


I tried to clarify a few points about attaching URL, Notes & documents in background using GOS basics in my earlier weblog. However, I received a few queries from the readers who wanted the code for attaching documents in background [ Also for 4.6C ].

The code for it, might well have been discussed in the forums since my earlier weblog was published but I would like to present the code here for attaching documents like DOC, PDF, GIF etc. Last time I cheated my readers with 'only funda but no code' for document attachments.

The code from my earlier weblog was converted to suit 4.6C by one of my weblog readers [ Madeleine Kempka / Kempka Madeleine - I am not sure about the sequence of first & last name Smile ] and then I corrected a few problems with it for attaching PDF and other documents.

Points to remember:
  • For file attachments: Pass the file path on application server and also choose correct message type [ P_MSGTYP ] as per the file extension[e.g. PDF,DOC etc]. P_RELTYP should be 'ATTA'. Also file size should be calculated and passed while creating the message.
  • For URLs: P_RELTYP should be 'URL' & P_MSGTYP will be derived as 'URL'.
  • For Notes: P_RELTYP should be 'NOTE' & P_MSGTYP will be derived as 'RAW'.

Sample Code:
--------------------------------------------------------------------------------
In case you find bugs in the code below...rememeber, it's just a sample...real one contains too many Wink
--------------------------------------------------------------------------------

Code:
REPORT  Z_RMTIWARI_ATTACH_DOC_TO_BO_46          .
*---------------------------------------------------------------------*
* Function   : We need to maintain links between Business Object and
*              the attachment.Attachment document is basiclally a
*              business object of type 'MESSAGE'.In order to maintain
*              links, first the attachment will be crated as Business
*              Object of type 'MESSAGE' using Message.Create method.
*
*              This program can be used to attach PC documents eg. PDF
*              ,DOC,TXT,GIF etc. to a SAP Business Object e.g Purchase
*              Order etc. You should pass the correct message type
*              [ P_MSGTYP ] as per the file extension[e.g. PDF,DOC etc].
*
*              The code below is suitable for 4.6C+, though you might
*              like to use the commented code, instead, for 4.7+
*---------------------------------------------------------------------*
* Include for BO macros
  INCLUDE : <cntn01>.

* Load class.
*  CLASS CL_BINARY_RELATION definition load.

  PARAMETERS:
*  Object_a
*   P_BOTYPE LIKE obl_s_pbor-typeid DEFAULT 'BUS2031', " e.g. 'BUS2012'
*   P_BO_ID  LIKE OBL_S_PBOR-INSTID DEFAULT '0002029816',
   P_BOTYPE LIKE borident-OBJTYPE DEFAULT 'BUS2031', " e.g. 'BUS2012'
   P_BO_ID  LIKE borident-OBJKEY DEFAULT '0002029816',
                                                    " Key e.g. PO No.

*  Object_b
   P_MSGTYP LIKE SOFM-DOCTP  DEFAULT 'PDF',
   P_DOCTY  LIKE borident-OBJTYPE DEFAULT 'MESSAGE',

*  Relationship
   P_RELTYP  LIKE BRELTYP-RELTYPE DEFAULT 'ATTA',

*  File Name
   P_FNAME like rlgrap-filename Default '/usr/data/Test.pdf'.

  types: BEGIN OF TY_MESSAGE_KEY,
        FOLTP TYPE SO_FOL_TP,
        FOLYR TYPE SO_FOL_YR,
        FOLNO TYPE SO_FOL_NO,
        DOCTP TYPE SO_DOC_TP,
        DOCYR TYPE SO_DOC_YR,
        DOCNO TYPE SO_DOC_NO,
        FORTP TYPE SO_FOR_TP,
        FORYR TYPE SO_FOR_YR,
        FORNO TYPE SO_FOR_NO,
       END OF TY_MESSAGE_KEY.
  DATA : LV_MESSAGE_KEY type TY_MESSAGE_KEY.
  DATA : LO_MESSAGE type SWC_OBJECT.
  DATA : LT_DOC_CONTENT type standard table of SOLI-LINE
                             with header line.

*----------------------------------------------------------------------*

* First derive the Attachment's ( MESSAGE )document type.
  P_DOCTY = 'MESSAGE'.
  CASE P_RELTYP.
*   In case of URls
    WHEN 'URL'.
      P_MSGTYP = 'URL'.
*   In case of Notes / Private Notes
    WHEN 'NOTE' OR 'PNOT'.
      P_MSGTYP = 'RAW'.
    WHEN 'ATTA'.
*     Take given parameter e.g. 'DOC', 'PDF' etc.
*     P_MSGTYP = 'EXT'.
    WHEN OTHERS.
*    ....exit
    EXIT.
  ENDCASE.
*----------------------------------------------------------------*

* Create an initial instance of BO 'MESSAGE' - to call the
* instance-independent method 'Create'.
  swc_create_object LO_MESSAGE 'MESSAGE' LV_MESSAGE_KEY.

* define container to pass the parameter values to the method call
* in next step.
  swc_container LT_MESSAGE_CONTAINER.

* Populate container with parameters for method
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTITLE' 'TestDocument'.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTLANGU' 'E'.
  swc_set_element LT_MESSAGE_CONTAINER 'NO_DIALOG'     'X'.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTNAME'   P_DOCTY.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTYPE'   P_MSGTYP.


* In case of URLs..it should be concatenated with &KEY& in the begining.
  CASE P_MSGTYP.
  WHEN 'URL'.
    LT_DOC_CONTENT = '&KEY&http://www.rmtiwari.com' .
    append LT_DOC_CONTENT.
* In case of Notes or Private Notes, get the data from files on appl
* server or from wherever(? - remember background).
  WHEN 'RAW'.
    LT_DOC_CONTENT = 'Hi How r u?' .
    append LT_DOC_CONTENT.

* In case of PC File attachments
  WHEN OTHERS.
    OPEN DATASET P_FNAME FOR INPUT.
    IF SY-subrc EQ 0.
      DO.
        READ DATASET P_FNAME INTO LT_DOC_CONTENT.
        IF SY-subrc EQ 0.
          append LT_DOC_CONTENT.
        ELSE.
          EXIT.
        ENDIF.
      ENDDO.
      CLOSE DATASET P_FNAME.
    ENDIF.
  ENDCASE.


* 'DocumentContent' is a multi-line element ( itab ).
  swc_set_table LT_MESSAGE_CONTAINER 'DocumentContent' LT_DOC_CONTENT.

* Size is required in case of File attachments
  data : LV_DOC_SIZE type i.
  data : L_FILE_LINES type i.

  DESCRIBE TABLE LT_DOC_CONTENT LINES L_FILE_LINES.

  READ TABLE LT_DOC_CONTENT INDEX L_FILE_LINES.

  LV_DOC_SIZE = ( 255 * ( L_FILE_LINES - 1 ) ) +
              STRLEN( LT_DOC_CONTENT ).

  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTSIZE'   LV_DOC_SIZE .

* Refresh to get the reference of create 'MESSAGE' object for attachment
  swc_refresh_object LO_MESSAGE.
  swc_call_method LO_MESSAGE 'CREATE' LT_MESSAGE_CONTAINER.

* Get Key of new object
  swc_get_object_key LO_MESSAGE LV_MESSAGE_KEY.

* Now we have attachment as a business object instance. We can now
* attach it to our main business object instance.

* Create main BO object_a
* data: LO_IS_OBJECT_A type SIBFLPORB.  "type SIBFLPORB is unknown, so I
  data: LO_IS_OBJECT_A type BORIDENT.

  LO_IS_OBJECT_A-OBJKEY = P_BO_ID.
  LO_IS_OBJECT_A-OBJTYPE = P_BOTYPE.
*  LO_IS_OBJECT_A-CATID  = 'BO'.

* Create attachment BO object_b
* data: LO_IS_OBJECT_B type SIBFLPORB.    "type SIBFLPORB is unknown
  data: LO_IS_OBJECT_B type BORIDENT.

  LO_IS_OBJECT_B-OBJKEY = LV_MESSAGE_KEY.
  LO_IS_OBJECT_B-OBJTYPE = P_DOCTY.
*  LO_IS_OBJECT_B-CATID  = 'BO'.

*TRY.
*CALL METHOD CL_BINARY_RELATION=>CREATE_LINK
*  EXPORTING
*    IS_OBJECT_A            = LO_IS_OBJECT_A
*    IS_OBJECT_B            = LO_IS_OBJECT_B
*    IP_RELTYPE             = P_RELTYP.

call function 'BINARY_RELATION_CREATE'
  EXPORTING
    obj_rolea    = LO_IS_OBJECT_A
    obj_roleb    = LO_IS_OBJECT_B
    relationtype = P_RELTYP
  EXCEPTIONS
    others       = 1.

* Check if everything OK...who cares!!
commit work.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SAP Business Workflow All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.