将草稿功能添加到被管理业务对象
ABAP RESTful 应用程序编程模型 › 开发 › 开发应用程序 › 使用独占草稿功能开发事务应用 › 草稿 - 启用被管理业务对象 › 将草稿功能添加到被管理业务对象
对于具有草稿的管理场景,将草稿行为添加到行为定义。
上下文和过程
在本开发指南的前几部分中,您已开发具有托管业务对象的运行业务服务。通过将语法元素 with draft 添加到行为定义,将草稿功能包含在业务服务中。在行为定义中使用此关键字元素后,工具支持会立即指导您完成行为定义中的后续步骤。语法警告和快速修复可用于根据草稿前提条件调整被管理业务对象。

备注
实施类型为
unmanaged的业务对象以相同方式启用草稿。
行为定义中的调整
按照以下步骤草稿启用差旅业务对象:
将语法元素 with draft 添加到行为定义的标头。
备注
您会收到语法错误,草稿业务对象没有可用的草稿持久性。
使用行为定义中的快速修复为 Travel 实体创建草稿数据库表:
在行为定义中使用关键字
draft table定义草稿数据库表,并指定草稿数据库表的名称。在示例场景中,我们使用 /DMO/D_TRAVEL_D。
备注
ABAP 航班参考场景中的草稿场景使用后缀
_D。对于草稿数据库表,我们使用前缀D_指示草稿持久性。有关详细信息,请参阅 开发对象的命名规则 。使用快速修复创建具有指定名称的数据库表。

随即打开创建数据库表的向导。完成数据库表生成向导后,ADT 工具支持会自动生成与活动持久性相对应的表元素。此外,草稿管理结构包括
%admin包含在数据库表中。有关草稿数据库表的详细信息,请参阅草稿数据库表。
将注释
@AbapCatalog.anonymizedWhenDelivered : true添加到存储用户信息的元素。(仅与差旅草稿表相关。)展开以下代码示例以查看数据库表
/DMO/D_TRAVEL_D的源代码。示例代码
@EndUserText.label : 'Draft table for entity /DMO/R_Travel_D' @AbapCatalog.enhancement.category : #EXTENSIBLE_ANY @AbapCatalog.tableCategory : #TRANSPARENT @AbapCatalog.deliveryClass : #A @AbapCatalog.dataMaintenance : #RESTRICTED define table /DMO/D_TRAVEL_D { key mandt : mandt not null; key traveluuid : sysuuid_x16 not null; travelid : /dmo/travel_id; agencyid : /dmo/agency_id; customerid : /dmo/customer_id; begindate : /dmo/begin_date; enddate : /dmo/end_date; @Semantics.amount.currencyCode : '/dmo/d_travel_d.currencycode' bookingfee : /dmo/booking_fee; @Semantics.amount.currencyCode : '/dmo/d_travel_d.currencycode' totalprice : /dmo/total_price; currencycode : /dmo/currency_code; description : /dmo/description; overallstatus : /dmo/overall_status; @AbapCatalog.anonymizedWhenDelivered : true localcreatedby : abp_creation_user; localcreatedat : abp_creation_tstmpl; @AbapCatalog.anonymizedWhenDelivered : true locallastchangedby : abp_locinst_lastchange_user; locallastchangedat : abp_locinst_lastchange_tstmpl; "%admin" : include sych_bdl_draft_admin_inc; }
重复为预订实体和 BookingSupplement 实体创建草稿数据库表。
在示例场景中,我们使用名称
/DMO/D_BOOKING_D和/DMO/D_BKSUPPL_D。为草稿业务对象定义总计 ETag 字段。
在从草稿到活动数据的转换期间,使用总计 ETag 启用乐观并发检查。
将总计 ETag 字段添加到差旅实体的持久数据库表。
展开以下代码示例以查看数据库表
/DMO/A_TRAVEL_D的源代码。示例代码
@EndUserText.label : 'Active Travel Persistence for Draft Reference Scenario' @AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE @AbapCatalog.tableCategory : #TRANSPARENT @AbapCatalog.deliveryClass : #A @AbapCatalog.dataMaintenance : #RESTRICTED define table /DMO/A_TRAVEL_D { key client : abap.clnt not null; key travel_uuid : sysuuid_x16 not null; travel_id : /dmo/travel_id not null; ... @AbapCatalog.anonymizedWhenDelivered : true local_created_by : abp_creation_user; local_created_at : abp_creation_tstmpl; @AbapCatalog.anonymizedWhenDelivered : true local_last_changed_by : abp_locinst_lastchange_user; local_last_changed_at : abp_locinst_lastchange_tstmpl; last_changed_at : abp_lastchange_tstmpl; }
在 CDS 视图实体
/DMO/R_Travel_D中添加元素并使用注释@Semantics.systemDateTime.lastChangedAt: true进行注释,以允许 RAP 运行时框架自动更新。展开以下代码示例以查看 CDS 视图实体
/DMO/R_Travel_D的源代码。示例代码
@EndUserText.label: 'Travel View Entity for Draft RefScen' @AccessControl.authorizationCheck: #NOT_REQUIRED define root view entity /DMO/R_Travel_D as select from /DMO/A_TRAVEL_D composition [0..*] of /DMO/R_Booking_D as _Booking association [0..1] to /DMO/I_Agency as _Agency on $projection.AgencyID = _Agency.AgencyID association [0..1] to /DMO/I_Customer as _Customer on $projection.CustomerID = _Customer.CustomerID association [0..1] to I_Currency as _Currency on $projection.CurrencyCode = _Currency.Currency { ///DMO/A_TRAVEL_D key travel_uuid as TravelUUID, travel_id as TravelID, agency_id as AgencyID, customer_id as CustomerID, begin_date as BeginDate, end_date as EndDate, @Semantics.amount.currencyCode: 'CurrencyCode' booking_fee as BookingFee, @Semantics.amount.currencyCode: 'CurrencyCode' total_price as TotalPrice, currency_code as CurrencyCode, description as Description, overall_status as OverallStatus, @Semantics.user.createdBy: true local_created_by as LocalCreatedBy, @Semantics.systemDateTime.createdAt: true local_created_at as LocalCreatedAt, @Semantics.user.localInstanceLastChangedBy: true local_last_changed_by as LocalLastChangedBy, //local ETag field --> OData ETag @Semantics.systemDateTime.localInstanceLastChangedAt: true local_last_changed_at as LocalLastChangedAt, //total ETag field @Semantics.systemDateTime.lastChangedAt: true last_changed_at as LastChangedAt, //Associations _Booking, _Agency, _Customer, _Currency }
备注
总计 ETag 字段不需要包含在投影视图中。ETag 仅用于内部处理草稿数据,不需要为业务服务公开。
将总计 ETag 字段添加到“差旅”实体的行为定义中的映射处方。
展开以下代码示例以查看行为定义的源代码
/DMO/R_Travel_D。示例代码
define behavior for /DMO/R_Travel_D alias Travel implementation in class /DMO/BP_TRAVEL_D unique ... mapping for /DMO/A_TRAVEL_D { AgencyID = agency_id; BeginDate = begin_date; BookingFee = booking_fee; CurrencyCode = currency_code; CustomerID = customer_id; Description = description; EndDate = end_date; LastChangedAt = last_changed_at; LocalCreatedAt = local_created_at; LocalCreatedBy = local_created_by; LocalLastChangedAt = local_last_changed_at; LocalLastChangedBy = local_last_changed_by; OverallStatus = overall_status; TotalPrice = total_price; TravelID = travel_id; TravelUUID = travel_uuid; } }
重新生成草稿数据库表
/DMO/D_TRAVEL_D以包括总计 ETag 字段。行为定义提供了重新生成的快速修复。
使用关键字
LastChangedAt在行为定义中将元素total etag定义为总计 ETag。备注
只能在行为定义中的锁主数据定义之后直接定义总计 ETag。
备注
建议使用行为定义中的字段特征只读将 ETag 字段指定为只读。
示例代码
managed; strict( 2 ); with draft; define behavior for /DMO/R_Travel_D alias Travel implementation in class /DMO/BP_I_TRAVEL_D unique persistent table /DMO/A_TRAVEL_D draft table /DMO/D_TRAVEL_D lock master total etag LastChangedAt ... { ... field ( readonly ) ...LastChangedAt; ... }
显式启用草稿业务对象中的成分。
将
with draft添加到行为定义来起草业务对象后,所有业务对象内部关联将自动启用草稿。为使此行为明确,行为会提示您使用with draft指定草稿业务对象中的成分。有关详细信息,请参阅启用草稿的关联。
定义草稿确定操作的验证和确定
Prepare。Prepare操作中定义的确定和验证在Activate操作之前和期间调用,以将草稿传输到活动实例。有关详细信息,请参阅准备待激活的草稿实例。Activate 操作在其优化变式中使用,通过跳过不需要的确定和验证执行来提高性能。请注意,要使用此优化变式,需要考虑确定和验证建模的最佳实践。
在行为定义中定义
Prepare操作。包括为
Prepare中的差旅业务对象定义的所有验证。备注
通常不需要在
Prepare操作中包含所有验证,而只需包含在从草稿转换为活动期间要检查的验证。示例代码
managed; strict( 2 ); with draft; define behavior for /DMO/R_Travel_D alias Travel ... draft determine action Prepare { validation validateAgency; validation validateCustomer; validation validateDates; validation validateCurrencyCode; validation validateBookingFee; validation Booking~validateCustomer; validation Booking~validateConnection; validation Booking~validateCurrencyCode; validation Booking~validateFlightPrice; validation Booking~validateStatus; validation Bookingsupplement~validateSupplement; validation Bookingsupplement~validateCurrencyCode; validation Bookingsupplement~validatePrice; }
展开以下代码示例以查看整个行为定义的源代码
/DMO/R_Travel_D。
示例代码
managed;
strict( 2 );
with draft;
define behavior for /DMO/R_Travel_D alias Travel
implementation in class /DMO/BP_TRAVEL_D unique
persistent table /DMO/A_TRAVEL_D
draft table /DMO/D_TRAVEL_D
lock master
total etag LastChangedAt
//authorization master ( instance )
etag master LocalLastChangedAt
{
create;
update;
delete;
association _Booking { create ( features : instance ); with draft; }
field ( numbering : managed, readonly ) TravelUUID;
field ( readonly ) TravelID, OverallStatus, TotalPrice, LocalCreatedAt, LocalCreatedBy, LocalLastChangedAt, LocalLastChangedBy;
field ( mandatory ) CustomerID, AgencyID, BeginDate, EndDate;
field ( features : instance ) BookingFee;
action ( features : instance ) acceptTravel result [1] $self;
action ( features : instance ) rejectTravel result [1] $self;
action ( features : instance, authorization : update ) deductDiscount parameter /DMO/A_TRAVEL_DISCOUNT result [1] $self { default function GetDefaultsFordeductDiscount ; }
internal action reCalcTotalPrice;
determination setTravelNumber on save { create; }
determination setStatusToNew on modify { create; }
determination calculateTotalPrice on modify { create; field BookingFee, CurrencyCode; }
validation validateCustomer on save { create; field CustomerID; }
validation validateAgency on save { create; field AgencyID; }
validation validateDates on save { create; field BeginDate, EndDate; }
determine action validationAgency { validation validateAgency; }
determine action validationCustomer { validation validateCustomer; }
determine action validationDates { validation validateDates; }
side effects
{ field BookingFee affects field TotalPrice;
determine action validationAgency executed on field AgencyID affects messages;
determine action validationCustomer executed on field CustomerID affects messages;
determine action validationDates executed on field BeginDate, field EndDate affects messages; }
draft action resume;
draft action Edit;
draft action Activate optimized;
draft action Discard;
draft determine action Prepare
{
validation validateAgency;
validation validateCustomer;
validation validateDates;
validation Booking ~ validateCustomer;
validation Booking ~ validateConnection;
validation Bookingsupplement ~ validateSupplement; }
mapping for /DMO/A_TRAVEL_D
{ AgencyID = agency_id;
BeginDate = begin_date;
BookingFee = booking_fee;
CurrencyCode = currency_code;
CustomerID = customer_id;
Description = description;
EndDate = end_date;
LastChangedAt = last_changed_at;
LocalCreatedAt = local_created_at;
LocalCreatedBy = local_created_by;
LocalLastChangedAt = local_last_changed_at;
LocalLastChangedBy = local_last_changed_by;
OverallStatus = overall_status;
TotalPrice = total_price;
TravelID = travel_id;
TravelUUID = travel_uuid; }
}
define behavior for /DMO/R_Booking_D alias Booking
implementation in class /DMO/BP_BOOKING_D unique
persistent table /DMO/A_BOOKING_D
draft table /DMO/D_BOOKING_D
lock dependent by _Travel
//authorization dependent by <association>
etag master LocalLastChangedAt
{
update;
delete;
association _BookingSupplement { create ; with draft; }
association _Travel { with draft; }
field ( numbering : managed, readonly ) BookingUUID;
field ( readonly ) TravelUUID, BookingID, BookingDate, LocalLastChangedAt;
field ( mandatory ) CustomerID, AirlineID, ConnectionID, FlightDate;
determination setBookingNumber on save { create; }
determination setBookingDate on save { create; }
determination calculateTotalPrice on modify { create; field FlightPrice, CurrencyCode; }
validation validateCustomer on save { create; field CustomerID; }
validation validateConnection on save { create; field AirlineID, ConnectionID, FlightDate; }
side effects
{ field FlightPrice affects field _Travel.TotalPrice; }
mapping for /DMO/A_BOOKING_D
{ AirlineID = carrier_id;
BookingDate = booking_date;
BookingID = booking_id;
BookingStatus = booking_status;
BookingUUID = booking_uuid;
ConnectionID = connection_id;
CurrencyCode = currency_code;
CustomerID = customer_id;
FlightDate = flight_date;
FlightPrice = flight_price;
LocalLastChangedAt = local_last_changed_at;
TravelUUID = parent_uuid; }
}
define behavior for /DMO/R_BookingSupplement_D alias BookingSupplement
implementation in class /DMO/BP_BOOKINGSUPPLEMENT_D unique
persistent table /DMO/A_BKSUPPL_D
draft table /DMO/D_BKSUPPL_D
lock dependent by _Travel
//authorization dependent by <association>
etag master LocalLastChangedAt
{
update;
delete;
association _Travel { with draft; }
association _Booking { with draft; }
field ( numbering : managed, readonly ) BookSupplUUID;
field ( readonly ) TravelUUID, BookingUUID, BookingSupplementID, LocalLastChangedAt;
field ( mandatory ) SupplementID;
determination setBookSupplNumber on save { create; }
determination calculateTotalPrice on modify { create; field CurrencyCode, BookSupplPrice; }
validation validateSupplement on save { create; field SupplementID; }
side effects
{ field BookSupplPrice affects entity _Travel; }
mapping for /DMO/A_BKSUPPL_D
{ BookSupplPrice = price;
BookSupplUUID = booksuppl_uuid;
BookingSupplementID = booking_supplement_id;
BookingUUID = parent_uuid;
CurrencyCode = currency_code;
LocalLastChangedAt = local_last_changed_at;
SupplementID = supplement_id;
TravelUUID = root_uuid; }
}
备注
启用草稿的业务对象需要在行为池中使用功能控制方法。如果未在业务逻辑中使用动态功能控制,则包括方法声明和空方法实施。在草稿业务对象中,为每个请求调用功能控制,以确定是请求草稿实例还是活动实例。
行为池中的调整
如果随后使用 %tky 来参考 实例的关键字段,则通常不必在行为实施中进行更改。
对于权限,权限有所不同。启用业务对象草稿时,草稿操作将隐式添加到业务对象行为中。有两个草稿操作需要在权限实施中考虑,因为它们也会在其运行时调用权限方法,请参阅运行时编辑操作和运行时激活操作。
在此演示示例中,操作的权限与更新操作的处理方式完全相同。这也适用于草稿操作 Edit 和 Activate.
在行为实施中,将相关草稿操作添加到操作映射以进行全局和实例授权。
展开以下代码示例以查看方法 get_global_authorizations 的源代码。
示例代码
METHOD get_global_authorizations.
IF requested_authorizations-%create EQ if_abap_behv=>mk-on.
IF is_create_granted( ) = abap_true.
result-%create = if_abap_behv=>auth-allowed.
ELSE.
result-%create = if_abap_behv=>auth-unauthorized.
APPEND VALUE #( %msg = NEW /dmo/cm_flight_messages(
textid = /dmo/cm_flight_messages=>not_authorized
severity = if_abap_behv_message=>severity-error )
%global = if_abap_behv=>mk-on ) TO reported-travel.
ENDIF.
ENDIF.
"Actions are treated like update
IF requested_authorizations-%update = if_abap_behv=>mk-on OR
requested_authorizations-%action-Edit = if_abap_behv=>mk-on OR
requested_authorizations-%action-Prepare = if_abap_behv=>mk-on OR
requested_authorizations-%action-acceptTravel = if_abap_behv=>mk-on OR
requested_authorizations-%action-rejectTravel = if_abap_behv=>mk-on OR
requested_authorizations-%action-deductDiscount = if_abap_behv=>mk-on.
IF is_update_granted( ) = abap_true.
result-%update = if_abap_behv=>auth-allowed.
result-%action-Edit = if_abap_behv=>auth-allowed.
result-%action-Prepare = if_abap_behv=>auth-allowed.
result-%action-acceptTravel = if_abap_behv=>auth-allowed.
result-%action-rejectTravel = if_abap_behv=>auth-allowed.
result-%action-deductDiscount = if_abap_behv=>auth-allowed.
ELSE.
result-%update = if_abap_behv=>auth-unauthorized.
result-%action-Edit = if_abap_behv=>auth-unauthorized.
result-%action-Prepare = if_abap_behv=>auth-unauthorized.
result-%action-acceptTravel = if_abap_behv=>auth-unauthorized.
result-%action-rejectTravel = if_abap_behv=>auth-unauthorized.
result-%action-deductDiscount = if_abap_behv=>auth-unauthorized.
APPEND VALUE #( %msg = NEW /dmo/cm_flight_messages(
textid = /dmo/cm_flight_messages=>not_authorized
severity = if_abap_behv_message=>severity-error )
%global = if_abap_behv=>mk-on ) TO reported-travel.
ENDIF.
ENDIF.
IF requested_authorizations-%delete = if_abap_behv=>mk-on.
IF is_delete_granted( ) = abap_true.
result-%delete = if_abap_behv=>auth-allowed.
ELSE.
result-%delete = if_abap_behv=>auth-unauthorized.
APPEND VALUE #( %msg = NEW /dmo/cm_flight_messages(
textid = /dmo/cm_flight_messages=>not_authorized
severity = if_abap_behv_message=>severity-error )
%global = if_abap_behv=>mk-on ) TO reported-travel.
ENDIF.
ENDIF.
ENDMETHOD.
展开以下代码示例以查看方法 get_instance_authorizations 的源代码。
示例代码
METHOD get_instance_authorizations.
DATA: update_requested TYPE abap_bool,
delete_requested TYPE abap_bool,
update_granted TYPE abap_bool,
delete_granted TYPE abap_bool.
READ ENTITIES OF /DMO/R_Travel_D IN LOCAL MODE
ENTITY Travel
FIELDS ( AgencyID )
WITH CORRESPONDING #( keys )
RESULT DATA(travels)
FAILED failed.
CHECK travels IS NOT INITIAL.
"Select country_code and agency of corresponding persistent travel instance
"authorization only checked against instance that have active persistence
SELECT FROM /DMO/A_TRAVEL_D AS travel INNER JOIN /DMO/AGENCY AS agency
ON travel~agency_id = agency~agency_id
FIELDS travel~travel_uuid , travel~agency_id, agency~country_code
FOR ALL ENTRIES IN @travels WHERE travel_uuid EQ @travels-TravelUUID
INTO TABLE @DATA(travel_agency_country).
"all actions are treated like update
update_requested = COND #( WHEN requested_authorizations-%update = if_abap_behv=>mk-on OR
requested_authorizations-%assoc-_Booking = if_abap_behv=>mk-on OR
requested_authorizations-%action-Edit = if_abap_behv=>mk-on OR
requested_authorizations-%action-Prepare = if_abap_behv=>mk-on OR
requested_authorizations-%action-acceptTravel = if_abap_behv=>mk-on OR
requested_authorizations-%action-deductDiscount = if_abap_behv=>mk-on OR
requested_authorizations-%action-rejectTravel = if_abap_behv=>mk-on
THEN abap_true ELSE abap_false ).
delete_requested = COND #( WHEN requested_authorizations-%delete = if_abap_behv=>mk-on
THEN abap_true ELSE abap_false ).
LOOP AT travels INTO DATA(travel).
"get country_code of agency in corresponding instance on persistent table
READ TABLE travel_agency_country WITH KEY travel_uuid = travel-TravelUUID
ASSIGNING FIELD-SYMBOL(<travel_agency_country_code>).
"Auth check for active instances that have before image on persistent table
IF sy-subrc = 0.
"check auth for update
IF update_requested = abap_true.
update_granted = is_update_granted( <travel_agency_country_code>-country_code ).
IF update_granted = abap_false.
APPEND VALUE #( %tky = travel-%tky
%msg = NEW /dmo/cm_flight_messages(
textid = /dmo/cm_flight_messages=>not_authorized_for_agencyid
agency_id = travel-AgencyID
severity = if_abap_behv_message=>severity-error )
%element-AgencyID = if_abap_behv=>mk-on ) TO reported-travel.
ENDIF.
ENDIF.
"check auth for delete
IF delete_requested = abap_true.
delete_granted = is_delete_granted( <travel_agency_country_code>-country_code ).
IF delete_granted = abap_false.
APPEND VALUE #( %tky = travel-%tky
%msg = NEW /dmo/cm_flight_messages(
textid = /dmo/cm_flight_messages=>not_authorized_for_agencyid
agency_id = travel-AgencyID
severity = if_abap_behv_message=>severity-error )
%element-AgencyID = if_abap_behv=>mk-on ) TO reported-travel.
ENDIF.
ENDIF.
" operations on draft instances and on active instances that have no persistent before image (eg Update on newly created instance)
" create authorization is checked, for newly created instances
ELSE.
update_granted = delete_granted = is_create_granted( ).
IF update_granted = abap_false.
APPEND VALUE #( %tky = travel-%tky
%msg = NEW /dmo/cm_flight_messages(
textid = /dmo/cm_flight_messages=>not_authorized
severity = if_abap_behv_message=>severity-error )
%element-AgencyID = if_abap_behv=>mk-on ) TO reported-travel.
ENDIF.
ENDIF.
APPEND VALUE #( LET upd_auth = COND #( WHEN update_granted = abap_true THEN if_abap_behv=>auth-allowed
ELSE if_abap_behv=>auth-unauthorized )
del_auth = COND #( WHEN delete_granted = abap_true THEN if_abap_behv=>auth-allowed
ELSE if_abap_behv=>auth-unauthorized )
IN
%tky = travel-%tky
%update = upd_auth
%assoc-_Booking = upd_auth
%action-Edit = upd_auth
%action-Prepare = upd_auth
%action-acceptTravel = upd_auth
%action-deductDiscount = upd_auth
%action-rejectTravel = upd_auth
%delete = del_auth
) TO result.
ENDLOOP.
ENDMETHOD.
结果
激活行为定义后,业务对象将立即启用草稿。通常,这意味着:
事务请求由草稿运行时处理,
行为池中所有方法的导入结构都会收到附加组件,即草稿标识
%is_draft。特别是,事务代码%tky确定操作是在草稿上执行还是在活动实例上执行。草稿操作在草稿生命周期内隐式可用并应用。
只要映射行为定义中不包括草稿功能,就不会更改现有 UI 服务。其行为仍类似于非草稿业务服务。基础草稿业务对象处理草稿标识
is_draft设置为假的每个请求。
有关详细信息,请参阅草稿运行时。
有时,转换为草稿业务对象需要业务逻辑中现有实施的后续活动。通常,彻底扫描现有代码,以检测需要区分活动处理和草稿处理的情况。
如果事务代码 %tky 用于参考关键组件,则无需对这些实施进行调整。%is_draft 作为关键组件自动添加到 %tky 结构。