Start of Content Area

Background documentation Generating a Persistent Object  Locate the document in its SAP Library structure

report DEMO_CREATE_PERSISTENT.

selection-screen begin of screen 400 title TEXT-400.
parameters DELETE as checkbox.
selection-screen end of screen 400.

selection-screen begin of screen 500 title TEXT-500.
parameters COMMIT as checkbox.
selection-screen end of screen 500.

data WA_SPFLI type SPFLI.

data: CONNECTION type ref to CL_SPFLI_PERSISTENT,
      AGENT      type ref to CA_SPFLI_PERSISTENT.

data: EXC type ref to CX_ROOT,
      TEXT type STRING.

WA_SPFLI-CARRID     = 'LH'.
WA_SPFLI-CONNID     = '123'.
WA_SPFLI-COUNTRYFR  = 'DE'.
WA_SPFLI-CITYFROM   = 'FRANKFURT'.
WA_SPFLI-AIRPFROM   =  'FRA'.
WA_SPFLI-COUNTRYTO  = 'SG'.
WA_SPFLI-CITYTO     = 'SINGAPORE'.
WA_SPFLI-AIRPTO     = 'SIN'.
WA_SPFLI-FLTIME     = '740'.
WA_SPFLI-DEPTIME    = '234500'.
WA_SPFLI-ARRTIME    = '180000'.
WA_SPFLI-DISTANCE   = '10000'.
WA_SPFLI-DISTID     = 'KM'.
WA_SPFLI-FLTYPE     = ' '.
WA_SPFLI-PERIOD     = '1'.

    AGENT = CA_SPFLI_PERSISTENT=>AGENT.

try.
    CONNECTION = AGENT->GET_PERSISTENT(  I_CARRID = WA_SPFLI-CARRID
                                        I_CONNID = WA_SPFLI-CONNID ).
    message 'Connection already exists' type 'I'.
    call selection-screen 400 starting at 10 10.
    if DELETE = 'X'.
      try.
          AGENT->DELETE_PERSISTENT( I_CARRID = WA_SPFLI-CARRID
                                   I_CONNID = WA_SPFLI-CONNID ).
          commit work.
        catch CX_ROOT into EXC.
          TEXT = EXC->GET_TEXT( ).
          message TEXT type 'I'.
      endtry.
    endif.
  catch CX_ROOT into EXC.
    TEXT = EXC->GET_TEXT( ).
    message TEXT type 'I'.
    try.
        CONNECTION = AGENT->CREATE_PERSISTENT(
                       I_CARRID = WA_SPFLI-CARRID
                       I_CONNID = WA_SPFLI-CONNID
                       I_COUNTRYFR = WA_SPFLI-COUNTRYFR
                       I_CITYFROM = WA_SPFLI-CITYFROM
                       I_AIRPFROM = WA_SPFLI-AIRPFROM
                       I_COUNTRYTO = WA_SPFLI-COUNTRYTO
                       I_CITYTO = WA_SPFLI-CITYTO
                       I_AIRPTO = WA_SPFLI-AIRPTO
                       I_FLTIME = WA_SPFLI-FLTIME
                       I_DEPTIME = WA_SPFLI-DEPTIME
                       I_ARRTIME = WA_SPFLI-ARRTIME
                       I_DISTANCE = WA_SPFLI-DISTANCE
                       I_DISTID = WA_SPFLI-DISTID
                       I_FLTYPE = WA_SPFLI-FLTYPE
                       I_PERIOD = WA_SPFLI-PERIOD            ).
        call selection-screen 500 starting at 10 10.
        if COMMIT = 'X'.
          commit work.
        endif.
        message 'Connection created' type 'I'.
      catch CX_ROOT into EXC.
        TEXT = EXC->GET_TEXT( ).
        message TEXT type 'I'.
    endtry.
endtry.

A reference to the class actor of the persistent class CL_SPFLI_PERSISTENT is assigned to the reference variable AGENT. The static constructor of the class CA_SPFLI_PERSISTENT generates this reference once only. The GET_PERSISTENT method checks whether or not there is a persistent object with this key in the database already. If so, it can be deleted using DELETE_PERSISTENT. If not, the system raises and catches the exception CX_OS_OBJECT_NOT_FOUND. Then it tries to create the object in the relevant CATCH block using CREATE_PERSISTENT. Note that the object is created in the database only when the COMMIT WORK statement is executed. If there is no COMMIT WORK statement, the program retains only the managed object, which is deleted from the database after the program ends. (See Transaction Mode).

 

End of Content Area