ODATA动态生成entity一般是entity过多或者entity的字段名不确定,会随着业务情况的不通而发生变化,这时候会选择动态生成。
动态生成entity的本质就是循环执行*MPC*方法中define创建部分的代码,我们前台SEGW创建的ENTITY在*MPC*中,所以重定义*MPC_EXT*方法时为了保证entity的一致需要继承一下它的超类*MPC*
data:
lo_annotation type ref to /iwbep/if_mgw_odata_annotation, "#EC NEEDED
lo_entity_type type ref to /iwbep/if_mgw_odata_entity_typ, "#EC NEEDED
lo_complex_type type ref to /iwbep/if_mgw_odata_cmplx_type, "#EC NEEDED
lo_property type ref to /iwbep/if_mgw_odata_property, "#EC NEEDED
lo_entity_set type ref to /iwbep/if_mgw_odata_entity_set. "#EC NEEDED
***********************************************************************************************************************************
* ENTITY - zresult
***********************************************************************************************************************************
lo_entity_type = model->create_entity_type( iv_entity_type_name = 'zresult' iv_def_entity_set = abap_false ). "#EC NOTEXT
***********************************************************************************************************************************
*Properties
***********************************************************************************************************************************
lo_property = lo_entity_type->create_property( iv_property_name = 'MimeType' iv_abap_fieldname = 'MIMETYPE' ). "#EC NOTEXT
lo_property->set_type_edm_string( ).
lo_property->set_creatable( abap_true ).
lo_property->set_updatable( abap_true ).
lo_property->set_sortable( abap_true ).
lo_property->set_nullable( abap_false ).
lo_property->set_filterable( abap_true ).
lo_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' )->add(
EXPORTING
iv_key = 'unicode'
iv_value = 'false' ).
lo_property = lo_entity_type->create_property( iv_property_name = 'ID' iv_abap_fieldname = 'ID' ). "#EC NOTEXT
lo_property->set_is_key( ).
lo_property->set_type_edm_string( ).
lo_property->set_maxlength( iv_max_length = 30 ). "#EC NOTEXT
lo_property->set_creatable( abap_true ).
lo_property->set_updatable( abap_true ).
lo_property->set_sortable( abap_true ).
lo_property->set_nullable( abap_false ).
lo_property->set_filterable( abap_true ).
lo_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' )->add(
EXPORTING
iv_key = 'unicode'
iv_value = 'false' ).
lo_property = lo_entity_type->create_property( iv_property_name = 'URI' iv_abap_fieldname = 'URI' ). "#EC NOTEXT
lo_property->set_is_key( ).
lo_property->set_type_edm_string( ).
lo_property->set_creatable( abap_true ).
lo_property->set_updatable( abap_true ).
lo_property->set_sortable( abap_false ).
lo_property->set_nullable( abap_false ).
lo_property->set_filterable( abap_true ).
lo_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' )->add(
EXPORTING
iv_key = 'unicode'
iv_value = 'false' ).
lo_entity_type->bind_structure( iv_structure_name = 'ZCL_ZOTFOR_REPORT_MPC=>TS_ZRESULT' ). "#EC NOTEXT
***********************************************************************************************************************************
* ENTITY SETS
***********************************************************************************************************************************
lo_entity_set = lo_entity_type->create_entity_set( 'zresultSet' ). "#EC NOTEXT
lo_entity_set->set_creatable( abap_false ).
lo_entity_set->set_updatable( abap_false ).
lo_entity_set->set_deletable( abap_false ).
lo_entity_set->set_pageable( abap_false ).
lo_entity_set->set_addressable( abap_false ).
lo_entity_set->set_has_ftxt_search( abap_false ).
lo_entity_set->set_subscribable( abap_false ).
lo_entity_set->set_filter_required( abap_false ).
data:
lo_annotation type ref to /iwbep/if_mgw_odata_annotation, "#EC NEEDED
lo_entity_type type ref to /iwbep/if_mgw_odata_entity_typ, "#EC NEEDED
lo_association type ref to /iwbep/if_mgw_odata_assoc, "#EC NEEDED
lo_ref_constraint type ref to /iwbep/if_mgw_odata_ref_constr, "#EC NEEDED
lo_assoc_set type ref to /iwbep/if_mgw_odata_assoc_set, "#EC NEEDED
lo_nav_property type ref to /iwbep/if_mgw_odata_nav_prop. "#EC NEEDED
***********************************************************************************************************************************
* ASSOCIATIONS
***********************************************************************************************************************************
lo_association = model->create_association(
iv_association_name = 'SalesOrderToItems' "#EC NOTEXT
iv_left_type = 'SaleslLists' "#EC NOTEXT
iv_right_type = 'SalesOrderItems' "#EC NOTEXT
iv_right_card = 'N' "#EC NOTEXT
iv_left_card = '1' "#EC NOTEXT
iv_def_assoc_set = abap_false ). "#EC NOTEXT
* Referential constraint for association - SalesOrderToItems
lo_ref_constraint = lo_association->create_ref_constraint( ).
lo_ref_constraint->add_property( iv_principal_property = 'Vbeln' iv_dependent_property = 'Vbeln' ). "#EC NOTEXT
lo_assoc_set = model->create_association_set( iv_association_set_name = 'SalesOrderToItemsSet' "#EC NOTEXT
iv_left_entity_set_name = 'SaleslListsSet' "#EC NOTEXT
iv_right_entity_set_name = 'SalesOrderItemsSet' "#EC NOTEXT
iv_association_name = 'SalesOrderToItems' ). "#EC NOTEXT
***********************************************************************************************************************************
* NAVIGATION PROPERTIES
***********************************************************************************************************************************
* Navigation Properties for entity - SaleslLists
lo_entity_type = model->get_entity_type( iv_entity_name = 'SaleslLists' ). "#EC NOTEXT
lo_nav_property = lo_entity_type->create_navigation_property( iv_property_name = 'SalesOrderItemsSet' "#EC NOTEXT
iv_abap_fieldname = 'SALESORDERITEMSSET' "#EC NOTEXT
iv_association_name = 'SalesOrderToItems' ). "#EC NOTEXT