Search code examples
odatasapui5abapsap-gateway

Create deep entity without deep structure


I want to pass an amount of storage units in one step. I need all storage units at once in the CREATE_ENTITY method in my OData service implementation (CREATE_ENTITY, or CREATE_DEEP_ENTITY, or...?), because I have to make a comparison of some values.

I tried a batch request. This doesn't work, because the CREATE_ENTITY method was called for each storage unit. So I could access only one storage unit in each call.

I also searched for tutorials concerning deep_entities. But I only find some with deep structures (head - items). But I have a flat structure (key: storage Unit) and want to pass this as table/array to my CREATE_ENTITY method. It should be possible to do this in SAPUI5.

As workaround I could pass all storage units into a string and pass this to the CREATE_ENTITY method. But that seems quite amateurish to me.

Here is how I invoke CREATE method:

onStartVert: function () {
    this.oContext = this.getModel().createEntry("/LenumIPunktSet", {
        success: this._successSave.bind(this),
        error: this._errorSave.bind(this)
    });
    var oBindingPath = {
        path: this.oContext.getPath()
    };
    this.getView().bindObject(oBindingPath);
    var sLenum;
    for (var i = 0; i < this._data.LePool.length; i++) {
        sLenum = this._data.LePool[i].lenum;
        this.getModel().create("/LenumIPunktSet", {
            lenum: sLenum
        });
    }
    this.getModel().submitChanges();
    this.getRouter().navTo("iPunkt02");
},

The signature for the CHANGESET_PROCESS method is:

CT_CHANGESET_DATA   TYPE /IWBEP/IF_MGW_CORE_SRV_RUNTIME=>TY_T_CHANGESET_DATA    
/IWBEP/CX_MGW_BUSI_EXCEPTION        Business Exception
/IWBEP/CX_MGW_TECH_EXCEPTION        Technical Exception

So, by now there is no table IT_CHANGESET_REQUEST available. My entity type has only this one field (lenum) I need as key.


Solution

  • The key is to implement a changeset.

    In your DPC_EXT redefine the following three methods:


    The method CHANGESET_BEGIN will activate the batch processing.

    METHOD /iwbep/if_mgw_appl_srv_runtime~changeset_begin.
        cv_defer_mode = 'X'.
    ENDMETHOD.
    
    METHOD /iwbep/if_mgw_appl_srv_runtime~changeset_end.
    * empty
    ENDMETHOD. 
    

    The method changeset_process will contain the logic. it_changeset_request contains all entities which are part of this batch request.

    METHOD /iwbep/if_mgw_appl_srv_runtime~changeset_process.
        LOOP AT it_changeset_request ASSIGNING FIELD-SYMBOL(<fs_changeset_request>).
            " <fs_changeset_request>-request_context->get_request_details( ) << which entity is it?
            " <fs_changeset_request>-operation_type << is it CREATE, UPDATE or DELETE?
            " <fs_changeset_request>-entry_provider->read_entry_data( ... ) << read entity into structure
        ENDLOOP.
    ENDMETHOD.
    
    

    See this blog for some details.