Messages, Exceptions and Web Dynpro ABAP
This
chapter shows how to work with exceptions and messages within the Web
Dynpro ABAP applications. For each of the two variants (messages and
exceptions),we present two possible implementation scenarios. Therefore,
we start with the modality to create the texts of messages and
exceptions,and we conclude with the modality of using them for the
created applications.
A
good user interface catches exceptions, describes the errors that occur
and displays messages that contain information for the application end
user.
As
we have already seen,Web Dynpro offers the interface, through the
IF_WD_MESSAGE_MANAGER, as a manager for messages.This interface has many
methods we can choose to create messages.For example:
- REPORT_T100_MESSAGE–reports a message by using a T100 input
- REPORT_ERROR_MESSAGE–reports a Web Dynpro message with optional parameters
- REPORT_ATTRIBUTE_T100_MESSAGE–reports a Web Dynpro exception for a Context Attribute
- REPORT_ATTRIBUTE_ERROR_MESSAGE – reports a Web Dynpro exception for a Context Attribute
- REPORT_SUCCESS–reports a success message
- REPORT_WARNING–reports a warning message
- REPORT_EXCEPTION–reports a Web Dynpro exception
Until
now,we have created only simple messages that contained text introduced
in coding.This art of programming isn’t recommended for many
reasons,e.g.the message text inserted in coding cannot be translated
without re-coding.In this chapter,we will create messages by using
assistance class,T100 table,exception class and message class.
Message Handling
The messages that we display in Web Dynpro applications can be created in many ways.For example,we can create messages:
- As dynamic texts stored in the text pool of the assistance class
- As texts from database table T100
Itis not recommendable to enter specific language text in coding.
Dynamic Texts Stored in the Text Pool of the Assistance Class
One of the most important functions of the ssistance classes is the management of the dynamic texts.
In the Web Dynpro ABAP'the CL_WD_COMPONENT_ASSISTANCE class provides central functions through which a WD component can access text symbols of the assistance class.
In the Web Dynpro ABAP'the CL_WD_COMPONENT_ASSISTANCE class provides central functions through which a WD component can access text symbols of the assistance class.
We create a WD component,a registration form for a user.
WD component structure
The
user has to fill a registration form with two mandatory fields:name and
date of birth.If the user fills the form with the requested data,we
show an information message to inform him that the data are saved.If the
user doesn’t fill a mandatory field,we show an error message.The layout
and the context node are presented.
To
be able to work with dynamic texts stored in the text pool of the
assistance class,we have to firstly create an assistance class. We can
use class builder, Transaction SE24,to create a usual ABAP
class.An Assistance class is ausual ABAP class that inherits from the
super-class CL_WD_COMPONENT_ASSISTANCE. This super-class provides to our
YCL_ASSISTANCECLASS class two additional methods.
The Layout and the context node
Creation of an assistance class
To maintain text symbols in the assistance class,we choose from the Assistance Class Maintenance menu Goto->Text Elements .Each new text symbol has an identification symbol(a three-digit ID)and a corresponding text.
Text symbols
To
be able to have access to the text symbols of the assistance class by
using the instance attribute WD_ASSIST,we have to assign the assistance
class to our component.For each Web Dynpro component,we can create only a
uniquely assigned assistance class.
Assistance class assigned to a WD component
When the user presses the REGISTRATION button,the Framework triggers the event handler method onactionregistration.
Coding of event handler method
METHOD onactionregistration. DATA: text_success TYPE string, text_error TYPE string. text_error = wd_assist->if_wd_component_assistance~get_text( key = '001'). text_success = wd_assist->if_wd_component_assistance~get_text( key = '002'). DATA: lr_node TYPE REF TO if_wd_context_node, lv_name TYPE string, lv_dateofbirth TYPE ydateofbirth. lr_node = wd_context->get_child_node('PERSON'). lr_node->get_attribute(EXPORTING name ='NAME' IMPORTING value = lv_name). lr_node->get_attribute(EXPORTING name ='DATEOFBIRTH' IMPORTING value = lv_dateofbirth). DATA lr_api_controller TYPE REF TO if_wd_controller. DATA lr_message_manager TYPE REF TO if_wd_message_manager. lr_api_controller ?= wd_this->wd_get_api( ). lr_message_manager = lr_api_controller->get_message_manager( ). IF lv_name IS INITIAL OR lv_dateofbirth IS INITIAL. lr_message_manager->report_error_message(message_text = text_error). ELSE. lr_message_manager->report_success(message_text =text_success). ENDIF. ENDMETHOD.
To
have access to the text symbols of the assistance class,we have to
create a local variable of STRING type(text_success TYPE string).At
runtime,when the method if_wd_component_assistance get_text is
called,the ID is passed to the KEY parameter,and the corresponding text
symbol is stored into the local variable of STRING type.
DATA:text_success TYPE string. text_success =
wd_assist->if_wd_component_assistance get_text(key =‘002').To access
the text from an assistance class,we can use the Web Dynpro Code Wizard.
Web Dynpro code wizard
If
the attributes name or dateofbirth are empty,we show an error message
by using the report_error_message method.If not,we show an information
message by using the report_success method.
Application result
Text from Database Table T100
We can create messages by using message maintenance –transaction SE91.
Message class YCL_T100_MESSAGE
The
Messages created here are stored in the database table T100.This table
has the following columns: message number MSGNR, short text
TEXT,language key SPRSL and message classARBGB.
The structure of the T100 table
We
can search in this table to find all the messages with number
000,language English and all the texts that begin with the word
“The”.After this search,we find also our message,too.
Searching for a message in T100 table
We create the same WD component, but in this case we use messages with text from the database table T100.
When
the user presses the REGISTRATION button,the Framework triggers the
event handler method onactionregistration. If the attributes NAME or
DATEFOFBIRTH are empty,we show an error message by using the
report_t100_message method.Msgty represents the message type
E–error,msgid represents the name of the message class and msgno
represents the message ID(a three-digit number).
Coding of event handler method
METHOD onactionregistration. DATA: lr_node TYPE REF TO if_wd_context_node, lv_name TYPE string, lv_dateofbirth TYPE ydateofbirth. lr_node = wd_context->get_child_node('PERSON'). lr_node->get_attribute(EXPORTING name ='NAME' IMPORTING value = lv_name). lr_node->get_attribute(EXPORTING name ='DATEOFBIRTH' IMPORTING value = lv_dateofbirth). DATA lr_api_controller TYPE REF TO if_wd_controller. DATA lr_message_manager TYPE REF TO if_wd_message_manager. lr_api_controller ?= wd_this->wd_get_api( ). lr_message_manager = lr_api_controller->get_message_manager( ). IF lv_name IS INITIAL OR lv_dateofbirth IS INITIAL. lr_message_manager->report_t100_message( msgid ='YCL_T100_MESSAGE' msgno ='000' msgty ='E'). ELSE. lr_message_manager->report_t100_message( msgid ='YCL_T100_MESSAGE' msgno ='001' msgty ='I'). ENDIF. ENDMETHOD.
If
the attributes name or dateofbirth are not empty,we show an information
message type I.At runtime,the User Interface is the same as for the
latest example.The only difference is the way we have created these
messages.
Exceptions
can be raised by the ABAP runtime environment,or explicitly in coding.
Exception situations have many causes(e.g. cardinality violation for a
context node).The exceptions are treatable or untreatable;all the
unhandled exceptions cause a runtime error,a so-called short dump.
We can find all the short dumps from the system with transaction ST22,ABAPrun
time error. We can find the runtime error from today and yesterday by
using the two buttons“Today” and “Yesterday”,or we can search for a
specific runtime error by using our selected parameters.
ABAP runtime error
Web
Dynpro ABAP offers support to work with the exception classes through
the methods of the Message Manager.Each exception has an explanatory
text assigned to it that describes the exception.It can be an Online
Text Repository OTR or a text from a message class.
We
can create our own exception classes as global classes by using Class
Builder or the local classes in our programms. Class-based exception
handling is based on the exception classes; we have a notation
specification for an exception class: its name has to begin with cx,zcx
or ycx. As basic exception classes,we have CX_STATIC_CHECK,CX_DYNAMIC_
CHECK and CX_NO_CHECK.This classes are derived from the superclass
CX_ROOT.
Exception Class with OTR Text
We create a WD component with the structure presented.
WD component structure
By
using the view V_VIEW,we implement a search mask.The user enters the
concurrent ID and receives more information about him.All the data about
candidates are stored inthe database table YPERSON we have created in
the ABAP Dictionary.When we don’t find in the database a concurrent with
the searched ID,we raise an exception and we show a proper message.The
view layout is presented.
View layout
Context structure
We
have defined a node Search(Singleton,Cardinality 1...1)that holds the
child node PERSON and the attribute ID_PERSON. The attribute ID_PERSON
has the type yperson-id_person,Input help mode Automatic.For this table
field,we have assigned in the ABAP Dictionary the search help
YSH_ID_PERSON.We use this attribute to hold the concurrent ID entered by
the user in the search mask.
The
child node PERSON (Singleton,Cardinality 0...1)has the dictionary
structure YPERSON,and as attributes we have quickly almost the entire
table fields(without id_person).We use this context node to hold all the
concurrent data with the searched ID.To show the result information,we
have created a data binding between this node and the Table UI Element.
The
user enters an ID and presses the button SEARCH.If the concurrent with
this ID exists in the database table YPERSON,we show in the UI Element
table the proper information,otherwise we raise an exception and the
corresponding error message is shown.Before working with exception
messages,we have to create an exception class.
Creating an exception class
As
can be seen,we create an exception class without message class.In this
case,the OTR is used as a central storage area for the exception
texts.In the Text tab,we create an exception text The candidate with
ID:&id_candidate& doesn’t exist,with the ID:YCX_NO_CANDIDATE.
Defining exception texts
Each
exception text we create in the Texts tab consists of an ID and a
corresponding text.The placeholder &id_candidate&is replaced at
runtime by the content of the attribute with the same name defined in
the Attributes tab.
Defining attributes
When the user presses the SEARCH button,the Framework triggers the event handler method onactionsearch.
Coding of event handler method
METHOD onactionsearch . DATA obj TYPE REF TO ycx_exception_otr_txt. DATA: lt_candidate TYPE TABLE OF yperson, lr_node TYPE REF TO if_wd_context_node, ls_data TYPE if_v_view=>element_search, lv_id_person TYPE yperson-id_person. lr_node = wd_context->get_child_node('SEARCH'). lr_node->get_attribute(EXPORTING name ='ID_PERSON' IMPORTING value = lv_id_person). TRY. SELECT * FROM yperson INTO TABLE lt_candidate WHERE id_person = lv_id_person. IF sy-subrc = 0. DATA lr_subnode TYPE REF TO if_wd_context_node. lr_subnode = lr_node->get_child_node('PERSON'). lr_subnode->bind_table( new_items = lt_candidate). ELSE. RAISE EXCEPTION TYPE ycx_exception_otr_txt EXPORTING textid = ycx_exception_otr_txt=>ycx_no_candidate id_candidate = lv_id_person. ENDIF. CATCH ycx_exception_otr_txt INTO obj. DATA lr_api_controller TYPE REF TO if_wd_controller. DATA lr_message_manager TYPE REF TO if_wd_message_manager. lr_api_controller ?= wd_this->wd_get_api( ). lr_message_manager = lr_api_controller->get_message_manager( ). lr_message_manager->report_exception(message_object = obj). ENDTRY. ENDMETHOD.
After
defining the data,we pass the value of the attribute id_person in the
local variable lv_id_person.By using the TRY ENDTRY control structure,we
handle a possible exception that can occur.We use open SQL to select
all the data from the database table YPERSON into the internal table
lt_candidate,where the field id_person is the same with our searched ID.
If
we find this record in our database table,we populate the child node
PERSON,because no exception occurs in the TRY block andthe system
continues the processing after ENDTRY. Otherwise,a class-based exception
occurs in the TRY block and the system searches for an exception
handler.With RAISE EXCEPTION,we can raise an exception explicitly from
our WD method.
RAISE EXCEPTION TYPE ycx_exception_otr_txt
EXPORTING textid = ycx_exception_otr_txt=>ycx_no_candidate id_candidate = lv_id_person.
EXPORTING textid = ycx_exception_otr_txt=>ycx_no_candidate id_candidate = lv_id_person.
This
exception is caught in the CATCH block of the same TRY control
structure:CATCH ycx_exception_otr_txt INTO obj.Because the additional
INTO is specified,a reference to the exception object is stored in
obj,where obj is the object reference variable.
After this,we use the method REPORT_EXCEPTION of the
IF_WD_MESSAGE_MANAGER interface to report a WD exception.
We present the execution result.The first picture represents the runtime where the exception occurs in the TRY-ENDTRY control structure,and the second picture represents the case when no exception occurs.In the second scenario,we can show,in the same time,the result of the Search help created in the ABAP Dictionary and attached to the id_person field of our database table YPERSON.
IF_WD_MESSAGE_MANAGER interface to report a WD exception.
We present the execution result.The first picture represents the runtime where the exception occurs in the TRY-ENDTRY control structure,and the second picture represents the case when no exception occurs.In the second scenario,we can show,in the same time,the result of the Search help created in the ABAP Dictionary and attached to the id_person field of our database table YPERSON.
Application result
Exception Class with Text from a Message Class
We
create the same WD component, but in this case we use an exception
class with text from a message class. In this case,the interface
IF_T100_MESSAGE is automatically included in the class definition.
Figure shows the creation of an exception class by using Message Class.
Creating an exception class
Message class
In the “Properties”tab of the exception class,we have to enter the name of the message class we use.
Message class assignment
Class attributes
Each
exception ID can be mapped to a message class and message ID.To do
this,we have to use the Button Message Text. We assign the exception
class attribute ID_CANDIDATE to the message class YCL_T100_MSG,message
number 000.The placeholder and is replaced at runtime by the content of
the ID_CANDIDATE attribute.
After
activation,we can use this exception class in our WD
component.Exception texts based on message classes are basically used in
the same way as the exception texts thatuse OTR texts. When the user
presses the SEARCH button,the Framework triggers the event handler
method onactionsearch that has the same structureas the Listing.The only
modification we have to do is to change the name of the exception class
raised when an exception occurs.
Assigning attributes of an exception class to a message
Listing Exception handling
DATA obj TYPE REF TO ycx_exception_t100_txt. …………. RAISE EXCEPTION TYPE ycx_exception_t100_txt EXPORTING textid = ycx_exception_t100_txt=>ycx_no_candidate id_candidate = lv_id_person. ………. CATCH ycx_exception_t100_txt INTO obj. ………
No comments:
Post a Comment