编辑补充文本

ABAP RESTful 应用程序编程模型 › 开发 › 开发单个业务对象功能 › 编辑语言相关字段 › 编辑补充文本

通过使用 ABAP 航班参考场景的示例,本主题将指导您完成使最终用户能够以登录语言维护补充的语言相关产品描述的步骤。

本主题中的示例取自可从 GitHub 下载的 ABAP 航班参考场景,请参阅 ABAP 航班参考场景。

包 /DMO/FLIGHT_REUSE 中的补充业务对象使用 CDS 根视图实体 /DMO/I_Supplement 及其映射视图 /DMO/C_Supplement 进行建模。文本提供者视图具有构成关系,包含文本元素和语言字段。文本元素在投影视图中非规范化,有关如何编辑此非规范化的逻辑在投影行为定义的行为池中实施。

前提条件

  • 已按照在映射视图中获取语言相关文本中所述对语言相关文本元素进行建模。

  • 具有非规范化文本和文本提供者视图的视图在业务对象的父子关系中。

  • 业务对象已创建且已启用更新。

过程

  1. 在投影行为定义中,为 create 和 update 操作定义增强。

    projection implementation in class /dmo/bp_c_supplement unique;
    strict(2);
    use draft;
    
    define behavior for /DMO/C_Supplement alias Supplement
    {
      use create ( augment );
      use update ( augment );
      use delete;
    
    }
    
  2. 要行为启用非规范化文本字段,请将其作为可修改字段添加到投影行为定义中。

    projection implementation in class /dmo/bp_c_supplement unique;
    strict(2);
    use draft;
    
    define behavior for /DMO/C_Supplement alias Supplement
    {
      use create ( augment );
      use update ( augment );
      use delete;
    
      field ( modify ) SupplementDescription;
    }
    
  3. 在映射业务对象的行为池中,添加增强实施方法(导入参数为 CREATE,导入参数为,导入参数为 UPDATE)。

    备注

    如果还没有映射行为定义的实施类,您将收到警告,提示您创建一个。映射业务对象的行为池必须使用 implementation in class ProjectionBehaviorPool unique; 在映射行为定义中指明。将其添加到映射行为定义后,可以使用映射行为定义中提供的快速修复创建行为池。

    生成的行为池包括两种分别实施 CREATE 和 UPDATE 的增强逻辑的方法。为了能够在 UPDATE 逻辑中引用新创建的实例,必须在一种方法中完成 CREATE 和 UPDATE 的实施。

    CLASS lhc_Supplement DEFINITION INHERITING FROM cl_abap_behavior_handler.
      PRIVATE SECTION.
    
        METHODS augment FOR MODIFY
          IMPORTING entities_create FOR CREATE Supplement
                    entities_update FOR UPDATE Supplement.
    
    ENDCLASS.
    
    CLASS lhc_Supplement IMPLEMENTATION.
    
      METHOD augment.
      ENDMETHOD.
    
    ENDCLASS.
    
  4. 增强实施必须处理行为已启用字段的所有可能的传入请求选项:

    传入请求

    补充文本实体的增强

    包含新产品名称的新补充实例的 CREATE。

    新补充文本实例的 CREATE_BY_ASSOCIATION。

    已存在产品名称的补充实例的 UPDATE。

    现有补充文本实例的 UPDATE。

    尚未保存到数据库的新创建补充实例的 UPDATE。

    仅存在于事务缓冲区中的补充文本实例的 UPDATE。

    现有补充实例的 UPDATE 不含产品名称条目。

    现有补充实例的补充文本实例的 CREATE_BY_ASSOCIATION。

    实施逻辑以增强操作 CREATE 和 UPDATE:

    1. 分别处理 CREATE 和 UPDATE 的导入参数。

    2. 在 CREATE 的导入参数处循环以检查传入创建请求。填写相关表以进行修改增强请求,并使用补充文本 CREATE_BY_ASSOCIATION 的值创建内部表。使用 EML CREATE 语句增强传入 MODIFY AUGMENTING 请求。

    3. 如果传入请求为 UPDATE,则读取请求的补充实例的相关补充文本实例。循环要更新的字段 SupplementDescription 的所有已导入实例并处理先前描述的案例,以增强传入的 UPDATE 请求。从此处理中排除不存在的补充实例。根据用例,必须填充 CREATE BY ASSOCIATION 的内部表或 UPDATE 的内部表。使用补充和补充文本实体的 EML MODIFY AUGMENTING 语句增强传入 UPDATE 请求。

    展开以下代码示例以查看方法 augment 的源代码。

    示例代码

    METHOD augment.
    
        DATA: suppltext_for_new_suppl      TYPE TABLE FOR CREATE /DMO/I_Supplement\_SupplementText,
              suppltext_for_existing_suppl TYPE TABLE FOR CREATE /DMO/I_Supplement\_SupplementText,
              supplementtext_update        TYPE TABLE FOR UPDATE /DMO/I_SupplementText.
        DATA: relates_create TYPE abp_behv_relating_tab,
              relates_update TYPE abp_behv_relating_tab,
              relates_cba    TYPE abp_behv_relating_tab.
        DATA: suppl_text_tky_link TYPE STRUCTURE FOR READ LINK /DMO/I_Supplement\\Supplement\_SupplementText,
              suppl_text_tky      LIKE suppl_text_tky_link-target.
    
        "Handle create requests including SupplementDescription
        LOOP AT entities_create INTO DATA(supplement_create).
          "Count Table index for uniquely identifiably %cid on creating supplementtext
          APPEND sy-tabix TO relates_create.
    
          "Direct the Supplement Create to the corresponding SupplementText Create-By-Association using the current language
          APPEND VALUE #( %cid_ref           = supplement_create-%cid
                          %is_draft          = supplement_create-%is_draft
                          %key-supplementid  = supplement_create-%key-SupplementID
                          %target            = VALUE #( ( %cid              = |CREATETEXTCID{ sy-tabix }|
                                                          %is_draft         = supplement_create-%is_draft
                                                          %key-supplementid = supplement_create-SupplementID
                                                          languagecode      = sy-langu
                                                          description       = supplement_create-SupplementDescription
                                                          %control          = VALUE #( supplementid = if_abap_behv=>mk-on
                                                                                       languagecode = if_abap_behv=>mk-on
                                                                                       description  = supplement_create-%control-SupplementDescription )
                                                       ) )
                         ) TO suppltext_for_new_suppl.
        ENDLOOP.
    
        MODIFY AUGMENTING ENTITIES OF /DMO/I_Supplement
          ENTITY Supplement
            CREATE BY \_SupplementText
            FROM suppltext_for_new_suppl
            RELATING TO entities_create BY relates_create.
    
        IF entities_update IS NOT INITIAL.
    
          READ ENTITIES OF /DMO/I_Supplement
            ENTITY Supplement BY \_SupplementText
              FROM CORRESPONDING #( entities_update )
              LINK DATA(link)
            FAILED DATA(link_failed).
    
          "Handle update requests
          LOOP AT entities_update INTO DATA(supplement_update) WHERE %control-SupplementDescription = if_abap_behv=>mk-on.
    
            CHECK NOT line_exists( link_failed-supplement[ KEY draft  %tky = CORRESPONDING #( supplement_update-%tky ) ] )
              OR line_exists( suppltext_for_new_suppl[ KEY cid  %cid_ref = supplement_update-%cid_ref  %is_draft = supplement_update-%is_draft ] ).
            DATA(tabix) = sy-tabix.
    
            "Create variable for %tky for target entity instances
            suppl_text_tky = CORRESPONDING #( supplement_update-%tky )  .
            suppl_text_tky-LanguageCode = sy-langu.
    
            "If a suppl_text with sy-langu already exists, perform an update. Else perform a create-by-association.
            IF line_exists( link[ KEY draft source-%tky  = CORRESPONDING #( supplement_update-%tky )
                                            target-%tky  = CORRESPONDING #( suppl_text_tky ) ] ).
    
              APPEND tabix TO relates_update.
    
              APPEND VALUE #( %tky             = suppl_text_tky
                              %cid_ref         = supplement_update-%cid_ref
                              description      = supplement_update-SupplementDescription
                              %control         = VALUE #( description = supplement_update-%control-SupplementDescription )
                            ) TO supplementtext_update.
    
              "If suppl_text was created in the current MODIFY, perform an update based on %cid
            ELSEIF line_exists(  suppltext_for_new_suppl[ KEY cid %is_draft = supplement_update-%is_draft
                                                              %cid_ref  = supplement_update-%cid_ref ] ).
              APPEND tabix TO relates_update.
    
              APPEND VALUE #( %tky             = suppl_text_tky
                              %cid_ref         = suppltext_for_new_suppl[ %is_draft = supplement_update-%is_draft
                                                                      %cid_ref  = supplement_update-%cid_ref ]-%target[ 1 ]-%cid
                              description      = supplement_update-SupplementDescription
                              %control         = VALUE #( description = supplement_update-%control-SupplementDescription )
                            ) TO supplementtext_update.
    
              "If suppl_text with sy-langu does not exist yet for corresponding supplement
            ELSE.
              APPEND tabix TO relates_cba.
    
              "Direct the Supplement Update to the corresponding SupplementText Create-By-Association using the current language
              APPEND VALUE #( %tky     = CORRESPONDING #( supplement_update-%tky )
                              %cid_ref = supplement_update-%cid_ref
                              %target  = VALUE #( ( %cid          = |UPDATETEXTCID{ tabix }|
                                                    languagecode  = sy-langu
                                                    %is_draft     = suppl_text_tky-%is_draft
                                                    description   = supplement_update-SupplementDescription
                                                    %control      = VALUE #( languagecode = if_abap_behv=>mk-on
                                                                             description   = supplement_update-%control-SupplementDescription )
                                                 ) )
                             ) TO suppltext_for_existing_suppl.
            ENDIF.
    
          ENDLOOP.
        ENDIF.
    
        MODIFY AUGMENTING ENTITIES OF /DMO/I_Supplement
          ENTITY SupplementText
            UPDATE FROM supplementtext_update
            RELATING TO entities_update BY relates_update
          ENTITY Supplement
            CREATE BY \_SupplementText
            FROM suppltext_for_existing_suppl
            RELATING TO entities_update BY relates_cba.
    
      ENDMETHOD.
    

来源:SAP Help Portal · 2025 FPS02 (Oct 2026) · 中文为 SAP 官方机器翻译

← 编辑语言相关字段 · 使用类型和控制映射 →