Samples / Stack / Stateful Sessions / Locks
Stateful Sessions — Locks
ENQUEUE_E_TABLE and ENQUEUE_READ, end and restart the session
- Class
z2ui5_cl_smps_app_485- Repository
- abap2UI5/samples-stack
- UI5 libraries
- sap.m, sap.tnt, sap.ui.core
- Lowest UI5 release
- 1.71
- In the browser
- Needs a system - opens in the playground for reading
- Controls it builds
- sap.m.Button, sap.m.HBox, sap.m.Label, sap.m.MessageStrip, sap.m.Page, sap.m.Shell, sap.m.VBox, sap.tnt.InfoLabel, sap.ui.core.mvc.View
The class
" @keywords stateful session lock enqueue dequeue set_session_stateful
" @summary ENQUEUE_E_TABLE and ENQUEUE_READ, end and restart the session
CLASS z2ui5_cl_smps_app_485 DEFINITION PUBLIC.
PUBLIC SECTION.
INTERFACES z2ui5_if_app.
DATA session_text TYPE string READ-ONLY.
DATA lock_text TYPE string READ-ONLY.
DATA:
BEGIN OF error READ-ONLY,
text TYPE string,
flag TYPE abap_bool,
END OF error.
PROTECTED SECTION.
DATA lock_counter TYPE i.
DATA session_is_stateful TYPE abap_bool.
METHODS initialize_view
IMPORTING
client TYPE REF TO z2ui5_if_client.
METHODS on_event
IMPORTING
client TYPE REF TO z2ui5_if_client.
METHODS set_session_stateful
IMPORTING
client TYPE REF TO z2ui5_if_client
stateful TYPE abap_bool.
METHODS update_lock_counter.
PRIVATE SECTION.
ENDCLASS.
CLASS z2ui5_cl_smps_app_485 IMPLEMENTATION.
METHOD initialize_view.
DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
)->ele( n = `View` ns = `mvc`
)->a( n = `displayBlock` v = `true`
)->a( n = `height` v = `100%`
)->a( n = `xmlns` v = `sap.m`
)->a( n = `xmlns:mvc` v = `sap.ui.core.mvc`
)->a( n = `xmlns:tnt` v = `sap.tnt` ).
DATA(page) = view->ele( `Shell`
)->ele( `Page`
)->a( n = `title` v = `abap2UI5 - Sample: Sticky Session with Locks (ABAP Standard Only)`
)->a( n = `showNavButton` b = client->check_app_prev_stack( )
)->a( n = `navButtonPress` v = client->_event( `BACK` ) ).
page->tag( `MessageStrip`
)->a( n = `text` v = client->_bind( error-text )
)->a( n = `type` v = `Error`
)->a( n = `visible` v = client->_bind( error-flag ) ).
page->tag( `MessageStrip`
)->a( n = `text` v = `Press Lock, then Refresh lock status - SM12 holds the lock now. End session and refresh ` &&
`again: the lock went away with the session that held it, so start the session again ` &&
`before the next Lock. Rollback Work releases it while the session goes on.`
)->a( n = `type` v = `Information`
)->a( n = `showIcon` v = `true`
)->a( n = `class` v = `sapUiSmallMarginBottom` ).
DATA(vbox) = page->ele( `VBox` ).
DATA(hbox) = vbox->ele( `HBox`
)->a( n = `alignItems` v = `Center` ).
hbox->ele( n = `InfoLabel` ns = `tnt`
)->a( n = `text` v = client->_bind( session_text ) ).
hbox->tag( `Button`
)->a( n = `press` v = client->_event( `END_SESSION` )
)->a( n = `text` v = `End session` ).
hbox->tag( `Button`
)->a( n = `press` v = client->_event( `START_SESSION` )
)->a( n = `text` v = `Start session again` ).
hbox = vbox->ele( `HBox`
)->a( n = `alignItems` v = `Center` ).
hbox->tag( `Label`
)->a( n = `text` v = `press button to create lock entry (SM12) in backend session`
)->a( n = `class` v = `sapUiTinyMarginEnd` ).
hbox->tag( `Button`
)->a( n = `press` v = client->_event( `LOCK` )
)->a( n = `text` v = `Lock`
)->a( n = `type` v = `Emphasized` ).
hbox = vbox->ele( `HBox` ).
hbox->tag( `Button`
)->a( n = `press` v = client->_event( `REFRESH` )
)->a( n = `text` v = `Refresh lock status` ).
hbox->tag( `Button`
)->a( n = `press` v = client->_event( `ROLLBACK` )
)->a( n = `text` v = `Rollback Work` ).
vbox->ele( `HBox`
)->ele( n = `InfoLabel` ns = `tnt`
)->a( n = `text` v = client->_bind( lock_text ) ).
client->view_display( view->stringify( ) ).
ENDMETHOD.
METHOD on_event.
CASE client->get_event( ).
WHEN `BACK`.
set_session_stateful( client = client stateful = abap_false ).
client->nav_app_leave( ).
WHEN `LOCK`.
lcl_locking=>acquire_lock( ).
client->message_toast_display( `Lock acquired. Press 'Refresh lock status'` ).
WHEN `END_SESSION`.
set_session_stateful( client = client stateful = abap_false ).
WHEN `START_SESSION`.
set_session_stateful( client = client stateful = abap_true ).
WHEN `REFRESH`.
update_lock_counter( ).
WHEN `ROLLBACK`.
" read before and after, so the toast says what the ROLLBACK
" released and not what the last Refresh happened to show
DATA(locks_before) = lcl_locking=>get_lock_counter( ).
ROLLBACK WORK.
update_lock_counter( ).
client->message_toast_display( COND #( WHEN locks_before > 0 AND lock_counter = 0
THEN `ROLLBACK WORK done, the lock is released`
WHEN lock_counter > 0
THEN `ROLLBACK WORK done, the lock is still held`
ELSE `ROLLBACK WORK done, there was no lock to release` ) ).
ENDCASE.
ENDMETHOD.
METHOD set_session_stateful.
client->set_session_stateful( stateful ).
session_is_stateful = stateful.
IF stateful = abap_true.
session_text = `Session ON (stateful)`.
ELSE.
session_text = `Session OFF (stateless)`.
ENDIF.
ENDMETHOD.
METHOD z2ui5_if_app~main.
TRY.
error = VALUE #( ).
IF client->check_on_init( ).
" the view first: the counter reads the lock table through a
" function module that can fail, and the bound text reaches the
" browser either way - after the view, a failure leaves a screen
" under the error box instead of none
set_session_stateful( client = client stateful = abap_true ).
initialize_view( client ).
update_lock_counter( ).
ELSEIF client->check_on_navigated( ).
" the session as the user left it - switching it back on here
" would undo an "End session" on every navigation
set_session_stateful( client = client stateful = session_is_stateful ).
initialize_view( client ).
ELSEIF client->check_on_event( ).
TRY.
on_event( client ).
" a lock that could not be taken is the outcome this sample is
" about, so it is shown in the MessageStrip of the view rather
" than in a popup - see lcx_error in the local implementations
CATCH lcx_error INTO DATA(x_error).
error-text = x_error->get_text( ).
error-flag = abap_true.
ENDTRY.
ENDIF.
CATCH cx_root INTO DATA(lx).
client->message_box_display( lx->get_text( ) ).
ENDTRY.
ENDMETHOD.
METHOD update_lock_counter.
" the counter belongs to the ONE lock entry of this sample: every Lock
" the session presses again on the key it already holds counts it up,
" SM12 still shows a single entry
lock_counter = lcl_locking=>get_lock_counter( ).
lock_text = COND #( WHEN lock_counter = 0
THEN |No lock on { lcl_locking=>lock_key } in SM12|
ELSE |SM12 holds the lock on { lcl_locking=>lock_key } - requested { lock_counter } time(s)| ).
ENDMETHOD.
ENDCLASS.