Samples / Stack / RAP with Draft
Change and Save a Draft
UPDATE ... %is_draft = mk-on
- Class
z2ui5_cl_smps_app_008- Repository
- abap2UI5/samples-stack
- UI5 libraries
- sap.m, 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.Column, sap.m.ColumnListItem, sap.m.Input, sap.m.MessageStrip, sap.m.Page, sap.m.Shell, sap.m.Table, sap.m.Text, sap.m.Title, sap.m.Toolbar, sap.m.ToolbarSpacer, sap.ui.core.mvc.View
The class
" @keywords eml rap draft change save modify
" @summary UPDATE ... %is_draft = mk-on
"! <p class="shorttext synchronized">RAP with Draft - Change and Save a Draft</p>
"! An ordinary UPDATE. The only thing that makes it a draft update is
"! %is_draft = mk-on in the key.
"!
"! MODIFY ENTITIES OF z2ui5_r_smps_trd
"! ENTITY travel
"! UPDATE FIELDS ( description )
"! WITH VALUE #( ( %tky = VALUE #( traveluuid = uuid
"! %is_draft = if_abap_behv=>mk-on )
"! description = s_draft-description ) )
"! FAILED DATA(s_failed)
"! REPORTED DATA(s_reported).
"!
"! Worth knowing: the COMMIT writes the draft table, not the application table,
"! and the active instance stays untouched until someone activates the draft
"! (sample 09). Validations do not run yet either - a draft may be
"! incomplete.
CLASS z2ui5_cl_smps_app_008 DEFINITION PUBLIC.
PUBLIC SECTION.
INTERFACES z2ui5_if_app.
TYPES:
BEGIN OF ty_s_draft,
travel_uuid TYPE string,
travel_id TYPE string,
description TYPE string,
END OF ty_s_draft.
DATA t_drafts TYPE STANDARD TABLE OF ty_s_draft WITH EMPTY KEY.
PROTECTED SECTION.
DATA client TYPE REF TO z2ui5_if_client.
METHODS data_read.
METHODS draft_save.
METHODS view_display.
PRIVATE SECTION.
ENDCLASS.
CLASS z2ui5_cl_smps_app_008 IMPLEMENTATION.
METHOD z2ui5_if_app~main.
me->client = client.
IF client->check_on_init( ).
data_read( ).
view_display( ).
ELSEIF client->check_on_navigated( ).
view_display( ).
ELSEIF client->check_on_event( `SAVE` ).
draft_save( ).
ELSEIF client->check_on_event( `REFRESH` ).
" a draft the Enter Draft Mode sample created in a tab of its own
data_read( ).
ENDIF.
ENDMETHOD.
METHOD draft_save.
" OPTIONAL: the uuid comes from the client, and the row it names may be
" gone from the list by now - a table expression without it raises
" CX_SY_ITAB_LINE_NOT_FOUND and dumps
DATA(uuid) = client->get_event_arg( ).
DATA(s_draft) = VALUE #( t_drafts[ travel_uuid = uuid ] OPTIONAL ).
IF s_draft IS INITIAL.
client->message_toast_display( `Draft not found - it may have been activated or discarded meanwhile` ).
RETURN.
ENDIF.
" An ordinary UPDATE - the only thing that makes it a draft update is
" %is_draft = mk-on in the key. The active instance stays untouched.
MODIFY ENTITIES OF z2ui5_r_smps_trd
ENTITY travel
UPDATE FIELDS ( description )
WITH VALUE #( ( %tky = VALUE #( traveluuid = uuid
%is_draft = if_abap_behv=>mk-on )
description = s_draft-description ) )
FAILED DATA(s_failed)
REPORTED DATA(s_reported).
IF s_failed-travel IS NOT INITIAL.
ROLLBACK ENTITIES.
z2ui5_cl_smps_context=>msg_display( client = client val = s_reported-travel ).
RETURN.
ENDIF.
" The COMMIT writes the draft into the draft table z2ui5_d_smps_trd.
" Note what does NOT happen here: no validation runs. A draft may be
" incomplete or plainly wrong and still be saved - it survives the
" session and even a logoff. The validations wait for Activate.
COMMIT ENTITIES RESPONSE OF z2ui5_r_smps_trd
FAILED DATA(s_failed_commit)
REPORTED DATA(s_reported_commit).
IF s_failed_commit IS NOT INITIAL.
ROLLBACK ENTITIES.
z2ui5_cl_smps_context=>msg_display( client = client val = s_reported_commit-travel ).
RETURN.
ENDIF.
data_read( ).
client->message_toast_display( |Draft of travel { s_draft-travel_id } saved - it survives a logoff| ).
ENDMETHOD.
METHOD data_read.
SELECT FROM z2ui5_r_smps_trd "#EC CI_NOWHERE
FIELDS TravelUuid
ORDER BY TravelId
INTO TABLE @DATA(t_result) UP TO 20 ROWS.
" read the DRAFT instances, not the active ones - so the form below
" shows what is currently in the draft, which is what gets changed
READ ENTITIES OF z2ui5_r_smps_trd
ENTITY travel
FIELDS ( travelid description ) WITH VALUE #( FOR s_row IN t_result
( %tky = VALUE #( traveluuid = s_row-traveluuid
%is_draft = if_abap_behv=>mk-on ) ) )
RESULT DATA(t_result_drafts).
t_drafts = VALUE #( FOR s_result IN t_result_drafts
( travel_uuid = |{ s_result-traveluuid }|
travel_id = |{ s_result-travelid ALPHA = OUT }|
description = |{ s_result-description }| ) ).
ENDMETHOD.
METHOD view_display.
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` ).
DATA(page) = view->ele( `Shell`
)->ele( `Page`
)->a( n = `title` v = `abap2UI5 - EML - 08 Change and Save a Draft`
)->a( n = `showNavButton` b = client->check_app_prev_stack( )
)->a( n = `navButtonPress` v = client->_event_nav_app_leave( ) ).
page->tag( `MessageStrip`
)->a( n = `text` v = `Only travels that already have a draft show up here - create one with the Enter Draft Mode app.`
)->a( n = `type` v = `Information` ).
DATA(table) = page->ele( `Table`
)->a( n = `items` v = client->_bind( t_drafts )
)->a( n = `noDataText` v = `No drafts yet - press Edit in the Enter Draft Mode app, then press refresh here` ).
table->ele( `headerToolbar`
)->ele( `Toolbar`
)->tag( `Title`
)->a( n = `text` v = `UPDATE ... WITH %is_draft = mk-on`
)->tag( `ToolbarSpacer`
)->tag( `Button`
)->a( n = `press` v = client->_event( `REFRESH` )
)->a( n = `icon` v = `sap-icon://refresh`
)->a( n = `tooltip` v = `Refresh` ).
table->ele( `columns`
)->ele( `Column`
)->tag( `Text`
)->a( n = `text` v = `ID`
)->end(
)->ele( `Column`
)->tag( `Text`
)->a( n = `text` v = `Description (draft)`
)->end(
)->ele( `Column`
)->tag( `Text`
)->a( n = `text` v = `` ).
table->ele( `items`
)->ele( `ColumnListItem`
)->ele( `cells`
)->tag( `Text`
)->a( n = `text` v = `{TRAVEL_ID}`
)->tag( `Input`
)->a( n = `value` v = `{DESCRIPTION}`
)->tag( `Button`
)->a( n = `press` v = client->_event( val = `SAVE`
arg = `${TRAVEL_UUID}` )
)->a( n = `text` v = `Save Draft` ).
client->view_display( view->stringify( ) ).
ENDMETHOD.
ENDCLASS.