Assistance Class as a Model for a Multicomponent Application SAP ABAP Web Dynpro

Essentially,each ABAP class can be used as a model. The particularity of an assistance classes is that we can assign it to a Web Dynpro component and it is automatically instantiated when the proper component is called.

We create an assistance class named YCL_AC_MODEL.

Assistance class

In this assistance class,we create three methods namedSEARCH_CANDIDAT,SELECT_CANDIDATE and INSERT_CANDIDATE. Here, we store the coding required within the WD components to look for a candidate,to select all the candidates and to insert information about a candidate into or from the database table YPERSON.

Methods for the assistance class

Before implementing our methods,we created the lowest level of coding that abstracts the data access.In this case,we choose to implement the simplest data access method by using the classical ABAP tools,respective through the Function Modules,but we can also use other tools,e.g.the Persistent Object Classes,required to benefit of the object oriented programming(OOP).In case we want to use Persistent Objects to access the data from the database table in the Object Oriented mode,we can use for our Web Dynpro project the same structure (by taking the Assistance class as a model). But,in this case,we have to replace the Function Modules with Persistent Classes.tableOne of the created Function Modules will be used later,as the end point for a BAPI.It will be also used to create a Web Service.

A Function Module (FM)is a procedure with public interface, which can be created within a Function Group. To create a Function Group, we select Create--> Function Group from the context menu of our package name. We create a Function Group named YFG_DBACCESS.Here,we implement three FMs,named.

YFM_SEARCH_ CANDIDATE,
YFM_SELECT_CANDIDATE and YFM_ INSERT_CANDIDATE.

Listing shows the implementation of YFM_SEARCH_CANDIDATE.

The exception class YCX_EXCEPTION_T100_TXT was defined hereinbefore,when we have seen how we can work with the exception classes in WD.

Shows the implementation of YFM_SEARCH_CANDIDATE

FUNCTION YFM_SEARCH_CANDIDATE .
*"-------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(ID_CANDIDATE)TYPE YPERSON-ID_PERSON
*" EXPORTING
*" VALUE(ITAB)TYPE YTABLE_TYPE
*" RAISING
*" YCX_EXCEPTION_T100_TXT
*"--------------------------------------------------
TRY.
SELECT *
FROM yperson
INTO TABLE itab WHERE id_person = id_candidate.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE ycx_exception_t100_txt
EXPORTING
textid = ycx_exception_t100_txt=>ycx_no_candidate
id_candidate = id_candidate.
ENDIF.
ENDTRY.
ENDFUNCTION.

Listing Implementation of YFM_SELECT_CANDIDATE

FUNCTION YFM_SELECT_CANDIDATE.
*"------------------------------------------------
*"*"Local Interface:
*" EXPORTING
*" VALUE(ITAB) TYPE YTABLE_TYPE
*" RAISING
*" YCX_EXCEPTION_T100_TXT
*"-------------------------------------------------
TRY.
SELECT *
FROM yperson
INTO TABLE itab.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE ycx_exception_t100_txt
EXPORTING
textid = ycx_exception_t100_txt=>ycx_select_no_posible.
ENDIF.
ENDTRY.
ENDFUNCTION.

This FM selects the information about all the candidates. In this case,we create a new message ID–001“Data cannot be selected!”and a new exception ID YCX_ SELECT_NO_POSSIBLE with the text from the message class – message ID 001

Listing shows the implementation of YFM_INSERT_CANDIDATE

FUNCTION YFM_INSERT_CANDIDATE .
*"------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(ID_CANDIDATE)TYPE YPERSON-ID_PERSON
*" VALUE(FIRSTNAME)TYPE YPERSON-FIRSTNAME
*" VALUE(LASTNAME)TYPE YPERSON-LASTNAME
*" VALUE(AGE) TYPE YPERSON-AGE
*" VALUE(TELEPHONE)TYPE YPERSON-TELEPHONE
*" VALUE(ID_COUNTRY)TYPE YPERSON-ID_COUNTRY
*" RAISING
*" YCX_EXCEPTION_T100_TXT
*"------------------------------------------
DATA wa_person TYPE yperson.
wa_person-mandt = sy-mandt.
wa_person-id_person =id_candidate.
wa_person-firstname =firstname.
wa_person-lastname =lastname.
wa_person-age = age.
wa_person-telephone =telephone.
wa_person-id_country =id_country.
TRY.
INSERT into yperson values wa_person.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE ycx_exception_t100_txt
EXPORTING textid = ycx_exception_t100_txt=>ycx_insert_no_possible.
ENDIF.
ENDTRY.
ENDFUNCTION.

This FM inserts in the database table YPERSON the information about a candidate.In this case,we create a new message ID–002 “Candidate insert cannot be done!”and a new exception ID YCX_INSERT_NO_POSSIBLE with the text from the message class – message ID 002.

A FM can be tested by using the Test/Execute Button .In this way,we can check if our created FM works correctly.Then,we implement our first method from the assistance class.

Implementation of the SEARCH_CANDIDATE method

Pattern

Implementation of the SELECT_CANDIDATE method

Implementation of the INSERT_CANDIDATE method

The Class Builder provides a test environment for classes.We can test our class by using the same test/execute Button.

Testing the assistance class

In this way,we have autonomous units that can be independently tested before we integrate them into bigger applications. If we look backwards to the example that refers to the Exception class with OTR text,we can see that the entire coding was integrated in a method of the view controller.So,we didn’t separate the application layer from the business layer and from the presentation layer.

We create the presentation layer through a WD application. We create a main component named Y_MAIN,in which we will use three secondary elements:

Y_SEARCH,Y_SELECT and Y_INSERT.

WD component’s structure

All the components defined the same assistance class,the YCL_AC_MODEL.Any changes made to the model class in the Framework are available for any other component that has this model class defined as assistance class.In this way,we can share data among components.

As we have mentioned above,each ABAP class can be used as a model class,but the services offered by the Web Dynpro Framework in this respect are available only for an assistance class that inherits the abstract classCL_WD_COMPONENT_ASSISTANCE.

The Y_MAIN component represented the start point of this application;it is the only component that has a Web Dynpro application defined.We assign our assistance class YCL_AC_MODEL for our components.

Assistance class assigned to our components

The V_MAIN layout

By using the ViewContainerUIElement,we show one of the views:
V_SELECT,V_INSERT or V_SEARCH.

To be able to navigate among the three used Views,we have to define three outbound plugs.Figure shows the defined outbound plugs and the window structure.

Outbound plugs

Before being able to create the instance of the used components,e have to create usages at the V_MAIN View level.

When the user presses the linkToAction UI element–Insert–the Framework triggers the event handler method onactioninsert.

Event handler method

METHOD onactioninsert .
DATA: lr_cmp_usage TYPE REF TO if_wd_component_usage.
lr_cmp_usage = wd_this->wd_cpuse_insert( ).
IF lr_cmp_usage->has_active_component( ) IS INITIAL.
lr_cmp_usage->create_component(assistance_class = me->wd_assist).
ENDIF.
Wd_this-fire_op_to_v_insert_plg( ).
ENDMETHOD.

As we can see,through the statement
lr_cmp_usage--> create_component (assistance_class = me



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAP ABAP Web Dynpro Topics