Persistent Objects: Using Business Key Identity

...Previous

Source Code
*&---------------------------------------------------------------------*
*& Report  Z_GET_PERSISTENT
*& Published @ SAPTechnical.com
*&---------------------------------------------------------------------*
*&Author : Abdul Hakim
*&Development Language: ABAP
*&System Release: SAP Netweaver 2004
*&Title: Persistent Object using Business Key Object Identity!!
*&---------------------------------------------------------------------*
REPORT  Z_GET_PERSISTENT.
selection-screen begin of block blk1 with frame title tit1.
parameters: sno   like zstudent01-sno obligatory,
            sname like zstudent01-sname obligatory,
            mark1 like zstudent01-mark1 obligatory,
            mark2 like zstudent01-mark2 obligatory.
selection-screen end of block blk1.
selection-screen begin of block blk2 with frame title tit2.
parameters: r1 type c radiobutton group rad1,
            r2 type c radiobutton group rad1,
            r3 type c radiobutton group rad1.
selection-screen end of block blk2.
*---------------------------------------------------------------------*
*       CLASS lcl_class1 DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
class lcl_class1 definition.
  public section.
    data: agent     type ref to zca_persist_01,
          students  type ref to zcl_persist_01.
    data result1 type ref to zcl_persist_01.
    methods: fetch_persistent importing im_sno like sno
                                        im_sname like sname,
             create_persistent importing im_sno like sno
                                         im_sname like sname
                                         im_mark1 like mark1
                                         im_mark2 like mark2,
             delete_persistent importing im_sno like sno
                                         im_sname like sname,
             output.
  private section.
    data: sno type zstudent01-sno,
          sname type zstudent01-sname,
          mark1 type zstudent01-mark1,
          mark2 type zstudent01-mark2.
endclass.                    "lcl_class1 DEFINITION
*---------------------------------------------------------------------*
*       CLASS lcl_class1 IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
class lcl_class1 implementation.
  method fetch_persistent.
    agent = zca_persist_01=>agent.
    try.
        agent->get_persistent( exporting i_sno     = im_sno
                                         i_sname   = im_sname
                               receiving result  = students ).
        .
        sname = students->get_sname( ).
        sno   = students->get_sno( ).
        mark1 = students->get_mark1( ).
        mark2 = students->get_mark2( ).
        if r1 eq 'X'.
          output( ).
        endif.
      CATCH CX_OS_OBJECT_NOT_FOUND .
      MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    endtry.
  endmethod.                    "fetch_persistent
  method output.
    write:/ sno,
            sname,
            mark1,
            mark2.
  endmethod.                    "output
  method create_persistent.
    fetch_persistent( exporting im_sname = im_sname
                                im_sno = im_sno ).
    try.
        agent->create_persistent( exporting i_mark1 = im_mark1
                                            i_mark2 = im_mark2
                                            i_sname = im_sname
                                            i_sno   = im_sno
                                  receiving result = students ).
        commit work.
        write 'Object Created'.
      CATCH CX_OS_OBJECT_EXISTING .
      MESSAGE 'Object already exists' TYPE 'I' DISPLAY LIKE 'E'.
    endtry.
  endmethod.                    "create_persistent
  method delete_persistent.
    fetch_persistent( exporting im_sname = im_sname
                                im_sno = im_sno ).
    try.
        agent->delete_persistent( exporting i_sname = im_sname
                                            i_sno   = im_sno ).
        commit work.
        write 'Object Deleted'.
      CATCH CX_OS_OBJECT_NOT_EXISTING .
      MESSAGE 'Object doesn''t exists' TYPE 'I' DISPLAY LIKE 'E'.
    endtry.
  endmethod.                    "delete_persistent
endclass.                    "lcl_class1 IMPLEMENTATION
data ref_class1 type ref to lcl_class1.
*---------------------------------------------------------------------*
*       Load-of-Program
*---------------------------------------------------------------------*
load-of-program.
  tit1 = text-001.
  tit2 = text-001.
*---------------------------------------------------------------------*
*       Start-of-Selection
*---------------------------------------------------------------------*
start-of-selection.
  create object ref_class1.
  if r1 eq 'X'.
    ref_class1->fetch_persistent( exporting im_sno = sno
                                            im_sname = sname ).
  elseif r2 eq 'X'.
    ref_class1->create_persistent( exporting im_sno = sno
                                             im_sname = sname
                                             im_mark1 = mark1
                                             im_mark2 = mark2 ).
  else.
    ref_class1->delete_persistent( exporting im_sno = sno
                                             im_sname = sname ).
  endif.

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image