Thursday, 18 December 2014

Widening and Narrowing casting

*================================ Casting ======================================*
CLASS lc_super DEFINITION.
  PUBLIC SECTION.
    METHODS : addition IMPORTING f1 TYPE i
                                 f2 TYPE i
                       EXPORTING f3 TYPE i.
    METHODS : subtract CHANGING  f3 TYPE i.

    METHODS : divide   CHANGING f3 TYPE i.


ENDCLASS.
CLASS lc_super IMPLEMENTATION.
  METHOD addition.
    f3 = f1 + f2.
  ENDMETHOD.
  METHOD subtract.
    f3 = f3 - 1.
  ENDMETHOD.
  METHOD divide.
    f3 = f3 / 2.
  ENDMETHOD.
ENDCLASS.
CLASS lc_sub_1 DEFINITION INHERITING FROM lc_super.
  PUBLIC SECTION.
    METHODS : addition REDEFINITION.
    METHODS : multiple CHANGING f3 TYPE i.
ENDCLASS.
CLASS lc_sub_1 IMPLEMENTATION.
  METHOD addition.
    f3 = f1 + f2 + 10.
  ENDMETHOD.
  METHOD multiple.
    f3 = f3 * 16.
  ENDMETHOD.
ENDCLASS.


PARAMETERS : p_var1 TYPE i,
             p_var2 TYPE i,
             p_var3 TYPE i,
             p_var4 TYPE i,
             p_var5 TYPE i,
             p_var6 TYPE i.

START-OF-SELECTION.

  DATA : lcl_ref TYPE REF TO lc_super,
         lcl_sub TYPE REF TO lc_sub_1.

  CREATE OBJECT lcl_sub.

  lcl_ref = lcl_sub.

  CALL METHOD lcl_ref->addition
    EXPORTING
      f1 = p_var1
      f2 = p_var2
    IMPORTING
      f3 = p_var3.

  WRITE : / 'Addition :', p_var3.

  CALL METHOD lcl_ref->subtract
    CHANGING
      f3 = p_var3.

  WRITE : / 'Subtraction :', p_var3.

  CALL METHOD lcl_ref->('MULTIPLE')
    CHANGING
      f3 = p_var3.

  WRITE : / 'Mulitplication :', p_var3.

  CALL METHOD lcl_ref->divide
    CHANGING
      f3 = p_var3.

  DATA : lcl_temp TYPE REF TO lc_sub_1.

  CLEAR : lcl_ref, lcl_sub.
  CREATE OBJECT lcl_temp.

  lcl_ref = lcl_temp.


  DATA : lo_cast_error TYPE REF TO cx_sy_move_cast_error..

  TRY.
      lcl_sub ?= lcl_ref.
    CATCH cx_sy_move_cast_error INTO lo_cast_error.
      WRITE: / 'Widening cast failed 2'.
  ENDTRY.

  IF lcl_sub IS NOT INITIAL.
    CALL METHOD lcl_sub->addition
      EXPORTING
        f1 = p_var4
        f2 = p_var5
      IMPORTING
        f3 = p_var6.
    WRITE: / p_var6.

    CALL METHOD lcl_sub->subtract
      CHANGING
        f3 = p_var6.
    WRITE: / p_var6.
    CALL METHOD lcl_sub->multiple
      CHANGING
        f3 = p_var6.
    WRITE: / p_var6.

    CALL METHOD lcl_sub->divide
      CHANGING
        f3 = p_var6.
    WRITE : p_var6.
  ENDIF.

END-OF-SELECTION.
*================================ Casting ======================================*

Use class for fetching the employee data!

DATA : lo_read   TYPE REF TO if_hrpa_paitf_read.
DATA : lo_reader TYPE REF TO cl_hrpa_paitf_read.
DATA : lo_master TYPE REF TO if_hrpa_masterdata_bl .
DATA : lt_0154   TYPE STANDARD TABLE OF p0154.

CALL METHOD cl_hrpa_masterdata_factory=>get_business_logic
  IMPORTING
    business_logic = lo_master.

CREATE OBJECT lo_reader
  EXPORTING
    masterdata_bl = lo_master.

lo_read = lo_reader.

TRY.

    CALL METHOD lo_read->read
      EXPORTING
        tclas         = 'A'
        pernr         = '15155000'
        infty         = '0154'
        subty         = '*'
        objps         = '*'
        sprps         = '*'
        begda         = sy-datum
        endda         = sy-datum
*       seqnr         =
*       massn         =
*       massg         =
        no_auth_check = abap_true
*       message_handler =
      IMPORTING
        pnnnn_tab     = lt_0154
*       pnnnn2_tab    =
*       pref_tab      =
*       text_tab      =
*       secondary_infty =
*       is_ok         =
*       object_keys   =
      .
  CATCH cx_hrpa_violated_assertion .
*    WRITE: / 'No Entries available'.
ENDTRY.

CHECK lt_0154[] IS NOT INITIAL.
WRITE : / 'HAI'.

Saturday, 17 May 2014

Singleton Class Creation!


1.       Create a class in SE24 which has instantiation property as private, this would create a constructor which is private.
Create Singleton Class
2.       Since constructor is private, to instantiate the class, we will need to create a Public Static method. In this public static method we will instantiate the class. To do this we will create an attribute of the type singleton class and store the reference in this attribute.
 Class Attribute 
Class Attribute
 Class Parameters
3.       Code snippet for the public static method is
Code Snipplets
  

IF gref_obj IS not BOUND.
    CREATE OBJECT gref_obj TYPE zash_singleton_test.
  ENDIF.
  ref_obj = gref_obj.

4.       Now create an attribute at instance level of type Public which will be used to store the value to be shared.
 Store Values Attribute
5.       Now create an instance of the class in Child component and set the values to beshared in WDDOINIT of a child Componentcontroller

  DATA: lo_obj TYPE REF TO zash_singleton_test.
  lo_obj ?= zash_singleton_test=>get_instance( ).
lo_obj->a_test_string  = 
'This value will be printed from calling Component '.

6.       Read this value from parent component's WDDOINIT of Componentcontroller
  DATA lo_cmp_usage TYPE REF TO if_wd_component_usage.

  lo_cmp_usage =   wd_this->wd_cpuse_z_sub_comp( ).
  IF lo_cmp_usage->has_active_component( ) IS INITIAL.
    lo_cmp_usage->create_component( ).
  ENDIF.

  DATA: lo_obj1 TYPE REF TO zash_singleton_test.
  DATA z_retrieved_value TYPE string.

  lo_obj1 ?= zash_singleton_test=>get_instance( ).
  IF lo_obj1 IS BOUND.
    z_retrieved_value = lo_obj1->a_test_string.

  ENDIF.

Lets use ABAP Objects for BDC


class ZCL_BC_BDC definition
  public
  final
  create private .
public section.
*"* public components of class ZCL_BC_BDC
*"* do not include other source files here!!!
  constants CO_MSG_TYPE_INFO type C value 'I'. "#EC NOTEXT
  constants CO_MSG_TYPE_SUCCESS type C value 'S'. "#EC NOTEXT
  constants CO_MSG_TYPE_ERROR type C value 'E'. "#EC NOTEXT
  constants CO_MSG_TYPE_WARNING type C value 'W'. "#EC NOTEXT
  constants CO_MSG_TYPE_ABEND type C value 'A'. "#EC NOTEXT
  class-methods S_INSTANTIATE
    importing
      !IM_BDC_TYPE type C
      !IM_TCODE type TCODE
      !IM_BDC_MODE type C default 'N'
      !IM_UPD_MODE type C default 'S'
      !IM_GROUP type APQI-GROUPID optional
      !IM_USER type APQI-USERID default SY-UNAME
      !IM_KEEP type APQI-QERASE optional
      !IM_HOLDDATE type APQI-STARTDATE optional
    returning
      value(RE_BDC) type ref to ZCL_BC_BDC .
  class-methods S_FREE_INSTANCE .
  methods ADD_FIELD
    importing
      !IM_FLD type ANY
      !IM_VAL type ANY
      !IM_CONV type CHAR1 optional .
  methods ADD_SCREEN
    importing
      !IM_REPID type ANY
      !IM_DYNNR type ANY .
  methods PROCESS
    exporting
      !EX_SUBRC type SY-SUBRC
      !EX_MESSAGES type TAB_BDCMSGCOLL
    raising
      ZCX_BATCH_INPUT_ERROR .
  methods CLEAR_BDC_DATA .
*"* protected components of class ZCL_BC_BDC
*"* do not include other source files here!!!
*"* protected components of class ZCL_BC_BDC
*"* do not include other source files here!!!
protected section.
private section.
*"* private components of class ZCL_BC_BDC
*"* do not include other source files here!!!
  class-data BDC type ref to ZCL_BC_BDC .
  data BDCDATA type BDCDATA_TAB .
  data BDC_TYPE type CHAR2 .
  data TCODE type TCODE .
  data CT_BDC_MODE type C .
  data CT_UPD_MODE type C .
  data BI_GROUP type APQI-GROUPID .
  data BI_USER type APQI-USERID .
  data BI_KEEP type APQI-QERASE .
  data BI_HOLDDATE type APQI-STARTDATE .
  methods CONSTRUCTOR
    importing
      !IM_BDC_TYPE type C
      !IM_TCODE type TCODE
      !IM_BDC_MODE type C
      !IM_UPD_MODE type C
      !IM_GROUP type APQI-GROUPID
      !IM_USER type APQI-USERID
      !IM_KEEP type APQI-QERASE
      !IM_HOLDDATE type APQI-STARTDATE .
  methods CALL_TRANSACTION
    exporting
      !EX_SUBRC type SY-SUBRC
      !EX_MESSAGES type TAB_BDCMSGCOLL .
  methods CREATE_BATCH_INPUT_SESS
    raising
      ZCX_BATCH_INPUT_ERROR .
ENDCLASS.
CLASS ZCL_BC_BDC IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_BC_BDC->ADD_FIELD
* +-------------------------------------------------------------------------------------------------+
* | [--->] IM_FLD TYPE ANY
* | [--->] IM_VAL TYPE ANY
* | [--->] IM_CONV TYPE CHAR1(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD add_field.
*----------------------------------------------------------------------*
* Insert field in BDC screen
*
* Additional functionality if IV_CONV is populated as below:-
* D - Convert date from YYYYMMDD format to output format
* S - Allow initial values to be populated in BDC
*----------------------------------------------------------------------*
  DATA:
    bdcdata LIKE LINE OF me->bdcdata.
* Initial values are skipped unless suppress 'S' is used
  IF im_val IS INITIAL AND im_conv <> 'S'.
    RETURN.
  ENDIF.
  CASE im_conv.
    WHEN 'D'.
      CALL FUNCTION 'CONVERSION_EXIT_MODAT_OUTPUT'
        EXPORTING
          input = im_val
        IMPORTING
          output = bdcdata-fval.
    WHEN OTHERS.
      bdcdata-fval = im_val.
  ENDCASE.
  bdcdata-fnam = im_fld.
  APPEND bdcdata TO me->bdcdata.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_BC_BDC->ADD_SCREEN
* +-------------------------------------------------------------------------------------------------+
* | [--->] IM_REPID TYPE ANY
* | [--->] IM_DYNNR TYPE ANY
* +--------------------------------------------------------------------------------------</SIGNATURE>
method ADD_SCREEN.
*----------------------------------------------------------------------*
* Start new BDC screen
*----------------------------------------------------------------------*
  DATA:
    bdcdata LIKE LINE OF me->bdcdata.
  bdcdata-program = im_repid.
  bdcdata-dynpro = im_dynnr.
  bdcdata-dynbegin = 'X'.
  APPEND bdcdata TO me->bdcdata.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_BC_BDC->CALL_TRANSACTION
* +-------------------------------------------------------------------------------------------------+
* | [<---] EX_SUBRC TYPE SY-SUBRC
* | [<---] EX_MESSAGES TYPE TAB_BDCMSGCOLL
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD CALL_TRANSACTION.
  DATA:
    bdc_subrc LIKE sy-subrc.
  CLEAR: ex_subrc, ex_messages.
*-----------------------------------------------------------------------
* Call BDC
*-----------------------------------------------------------------------
* Call the transaction and collect the messages
  CALL TRANSACTION me->tcode
                   USING me->bdcdata
                   MODE me->ct_bdc_mode
                   UPDATE me->ct_upd_mode
                   MESSAGES INTO ex_messages.
  bdc_subrc = sy-subrc.
*-----------------------------------------------------------------------
* Process BDC messages
*-----------------------------------------------------------------------
* Check if there are any errors
  LOOP AT ex_messages TRANSPORTING NO FIELDS
                     WHERE msgtyp = co_msg_type_error
                     OR msgtyp = co_msg_type_abend.
    EXIT. "Exit loop
  ENDLOOP.
  IF sy-subrc = 0 OR bdc_subrc <> 0.
    ex_subrc = 4.
  ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_BC_BDC->CLEAR_BDC_DATA
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD CLEAR_BDC_DATA.
  CLEAR: me->bdcdata.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_BC_BDC->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] IM_BDC_TYPE TYPE C
* | [--->] IM_TCODE TYPE TCODE
* | [--->] IM_BDC_MODE TYPE C
* | [--->] IM_UPD_MODE TYPE C
* | [--->] IM_GROUP TYPE APQI-GROUPID
* | [--->] IM_USER TYPE APQI-USERID
* | [--->] IM_KEEP TYPE APQI-QERASE
* | [--->] IM_HOLDDATE TYPE APQI-STARTDATE
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD constructor.
  me->bdc_type = im_bdc_type.
  me->tcode = im_tcode.
  me->ct_bdc_mode = im_bdc_mode.
  me->ct_upd_mode = im_upd_mode.
  me->bi_group = im_group.
  me->bi_user = im_user.
  me->bi_keep = im_keep.
  me->bi_holddate = im_holddate.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_BC_BDC->CREATE_BATCH_INPUT_SESS
* +-------------------------------------------------------------------------------------------------+
* | [!CX!] ZCX_BATCH_INPUT_ERROR
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD create_batch_input_sess.
  CALL FUNCTION 'BDC_OPEN_GROUP'
    EXPORTING
      group = me->bi_group
      holddate = me->bi_holddate
      keep = me->bi_keep
      user = me->bi_user
    EXCEPTIONS
      client_invalid = 1
      destination_invalid = 2
      group_invalid = 3
      group_is_locked = 4
      holddate_invalid = 5
      internal_error = 6
      queue_error = 7
      running = 8
      system_lock_error = 9
      user_invalid = 10
      OTHERS = 11.
  IF sy-subrc <> 0.
    RAISE EXCEPTION TYPE zcx_batch_input_error
      EXPORTING
        msgty = sy-msgty
        msgid = sy-msgid
        msgno = sy-msgno
        msgv1 = sy-msgv1
        msgv2 = sy-msgv2
        msgv3 = sy-msgv3
        msgv4 = sy-msgv4.
  ENDIF.
  CALL FUNCTION 'BDC_INSERT'
    EXPORTING
      tcode = me->tcode
    TABLES
      dynprotab = me->bdcdata
    EXCEPTIONS
      internal_error = 1
      not_open = 2
      queue_error = 3
      tcode_invalid = 4
      printing_invalid = 5
      posting_invalid = 6
      OTHERS = 7.
  IF sy-subrc <> 0.
    RAISE EXCEPTION TYPE zcx_batch_input_error
      EXPORTING
        msgty = sy-msgty
        msgid = sy-msgid
        msgno = sy-msgno
        msgv1 = sy-msgv1
        msgv2 = sy-msgv2
        msgv3 = sy-msgv3
        msgv4 = sy-msgv4.
  ENDIF.
  CALL FUNCTION 'BDC_CLOSE_GROUP'
    EXCEPTIONS
      not_open = 1
      queue_error = 2
      OTHERS = 3.
  IF sy-subrc <> 0.
    RAISE EXCEPTION TYPE zcx_batch_input_error
      EXPORTING
        msgty = sy-msgty
        msgid = sy-msgid
        msgno = sy-msgno
        msgv1 = sy-msgv1
        msgv2 = sy-msgv2
        msgv3 = sy-msgv3
        msgv4 = sy-msgv4.
  ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_BC_BDC->PROCESS
* +-------------------------------------------------------------------------------------------------+
* | [<---] EX_SUBRC TYPE SY-SUBRC
* | [<---] EX_MESSAGES TYPE TAB_BDCMSGCOLL
* | [!CX!] ZCX_BATCH_INPUT_ERROR
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD process.
  CLEAR: ex_subrc, ex_messages.
  CASE me->bdc_type.
    WHEN 'CT'.
      me->call_transaction( IMPORTING ex_subrc = ex_subrc
                                      ex_messages = ex_messages ).
    WHEN 'BI'.
      me->create_batch_input_sess( ).
  ENDCASE.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_BC_BDC=>S_FREE_INSTANCE
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD s_free_instance.
  FREE zcl_bc_bdc=>bdc.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_BC_BDC=>S_INSTANTIATE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IM_BDC_TYPE TYPE C
* | [--->] IM_TCODE TYPE TCODE
* | [--->] IM_BDC_MODE TYPE C (default ='N')
* | [--->] IM_UPD_MODE TYPE C (default ='S')
* | [--->] IM_GROUP TYPE APQI-GROUPID(optional)
* | [--->] IM_USER TYPE APQI-USERID (default =SY-UNAME)
* | [--->] IM_KEEP TYPE APQI-QERASE(optional)
* | [--->] IM_HOLDDATE TYPE APQI-STARTDATE(optional)
* | [<-()] RE_BDC TYPE REF TO ZCL_BC_BDC
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD s_instantiate.
* Create singleton so that in any single session, only
* one instance of class exists
  IF zcl_bc_bdc=>bdc IS NOT BOUND.
    ASSERT FIELDS im_tcode CONDITION im_tcode IS NOT INITIAL.
    ASSERT FIELDS im_bdc_type CONDITION im_bdc_type = 'CT' OR im_bdc_type = 'BI'.
    CREATE OBJECT zcl_bc_bdc=>bdc
      EXPORTING
        im_bdc_type = im_bdc_type
        im_tcode = im_tcode
        im_bdc_mode = im_bdc_mode
        im_upd_mode = im_upd_mode
        im_group = im_group
        im_user = im_user
        im_keep = im_keep
        im_holddate = im_holddate.
  ENDIF.
  re_bdc = zcl_bc_bdc=>bdc.
ENDMETHOD.
ENDCLASS.

Saturday, 5 April 2014

Let's learn ABAP OO Objects

Global class & Exception Class Creation


Transaction: SE24

Through this transaction code, we can able to create a global class. Basically global class are created for the use of reusability. If reusability is not not required, then we can create a local class. 

Lets class name starts with YCL_ or ZCL













Provide a description for the class and make as usual ABAP Class with Public.

Final methods cannot be redefined in subclasses. Final classes cannot have any more subclasses and constitute the final node of an inheritance tree.



Lets create a sample program using this global class, for this purpose we have created an attribute and few methods with an exception class. 

Properties Tab



Attributes Tab

Here, I have created an attribute (A_SFLIGHT), which can used for all the methods in the same class.

Methods Tab

















Here I have created a CONSTRUCTOR method, Constructors are special methods that cannot be called using CALL METHOD. Instead, they are called automatically by the system to set the initial state of a new object or class. There are two types of constructors - instance constructors and static constructors. Constructors are methods with a predefined name.

The system calls the instance constructor once for each instance of the class, directly after the object has been created in the CREATE OBJECTstatement. You can pass the input parameters of the instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS additions in the CREATE OBJECT statement.
The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR.

The static constructor has no parameters. The system calls the static constructor once for each class, before the class is accessed for the first time. The static constructor cannot therefore access the components of its own class.

In constructor method






























In above mentioned code, I have used the exception class. Creation of Exception class are explained finally.

Method VALIDATE_SFLIGHT


















Method COST_OF_FLIGHT


























Usage of global class in a report program

*---* Reference Object...

     DATA : lr_object     TYPE REF TO ycl_sample_class_01,
                lr_exception TYPE REF TO ycx_exec_sample.

    *---* Structure...

     DATA : x_sflight      TYPE sflight.

    *---* Variable Declaration...

     DATA : v_error       TYPE string,
                v_price       TYPE s_price,
                v_curre       TYPE s_currcode.

     START-OF-SELECTION.

     TRY.

      CREATE OBJECT lr_object " Constructor
        EXPORTING
          carrid = 'AB'
          connid = '17'
          fldate = '20130828'.

      lr_object->validate_sflight( IMPORTING sflight = x_sflight ). " Validate the sflight entry

      lr_object->cost_of_flight(    " Cost of the flight ticket
      IMPORTING
        price     = v_price
        currency  = v_curre ).

    CATCH ycx_exec_sample INTO lr_exception.
      v_error = lr_exception->if_message~get_text( ).
      WRITE v_error.
    
    ENDTRY.

    END-OF-SELECTION.

    WRITE : / 'Carrid :',   x_sflight-carrid.
    WRITE : / 'Price :',    v_price.
    WRITE : / 'Currency :', v_curre.
    WRITE : / v_error.

Exception Class

Again use the Transaction code SE24 for creating a exception class 

Let class name starts with YCX_ or ZCX













Follow the same procedure as per the below mentioned screenshot, to create the same. 




















In ATTRIBUTES Tab, I have added a attribute 'CARRID', to get the CARRID value while exception is raised.



















In TEXT Tab, add the custom text messsage to get displayed while exception is raised.














Use this exception class, In the constructor method of YCL_SAMPLE_CLASS_01 class.














Lets see the output

With correct CARRID Value












With Incorrect CARRID Value