Receiving data from a Business Entity in Batches

Batching is a key concept to reduce the time users will have to wait for data to be accessed. While our front end components (like the SmartBusinessEntityAdapter for GUI for .NET or the Integration with the JSDO for Mobile/Web applications) incorporate batching behavior (fetching data in batches and requesting the next batch as needed) automatically developers may need to control this manually in certain situations when processing data.

Batching with Business Entities is completely controlled through the IFetchDataRequest (or FetchDataRequest) parameter object to the ServiceAdapter:RetrieveData or ServiceInterface:FetchData calls.

With a request developers can assign the BatchSize or the request parameter object. This value controls the maximum number of records that should be returned by the back end Business Entity. 

In response to this request the Business Entity populates the NextContext property. This property is empty when no further data is available. This property contains a CHARACTER value when additional data is available on the back end. In this case, developers will need to populate the Context property of the request parameter object with the NextContext retrieved from the previous request and perform a next request. The Business Entity will then return the next batch of data. This should be repeated until no more data is available on the back end (thus NextContext is returned empty).

  File Modified

File via-serviceadapter.p

Aug 11, 2015 by Mike Fechner

File via-serviceinterface.p

Aug 11, 2015 by Mike Fechner

Sample: Batching using the Service Adapter

via-serviceadapter.p
/*------------------------------------------------------------------------
    File        : via-serviceinterface.p
    Purpose     : Batching Sample via Service Interface (Backend) 
  ----------------------------------------------------------------------*/

/* ***************************  Definitions  ************************** */

ROUTINE-LEVEL ON ERROR UNDO, THROW.

USING Consultingwerk.Framework.* FROM PROPATH.
USING Consultingwerk.OERA.*      FROM PROPATH.

{Consultingwerk/SmartComponentsDemo/OERA/Sports2000/dsCustomer.i}

DEFINE VARIABLE oRequest AS FetchDataRequest NO-UNDO . 

/* ***************************  Main Block  *************************** */

/* Prepare request parameter object with query string and batch size */
oRequest = NEW FetchDataRequest ("eCustomer", 
                                 "for each eCustomer where eCustomer.SalesRep = 'HXM' and eCustomer.CustNum < 1400", 
                                 15 /* batch size */) .

REPEAT:
    /* Perform request for a batch */
    FrameworkSettings:ServiceAdapter:RetrieveData ("", 
                                                   "Consultingwerk.SmartComponentsDemo.OERA.Sports2000.CustomerBusinessEntity",
                                                   oRequest,
                                                   OUTPUT DATASET dsCustomer) .
    
    /* Process batch data */
    FOR EACH eCustomer:
        DISPL eCustomer.CustNum 
              eCustomer.Name
              eCustomer.SalesRep .
    END.
    
    /* When a next batch is available, the NextContext contains
       the Context for the next request. Otherwise, leave here as
       there is no more data on the backend. */
    IF oRequest:NextContext > "" THEN 
        oRequest:Context = oRequest:NextContext .
    ELSE 
        LEAVE .  
END.

Sample:Batching using the Service Interface

via-serviceinterface.p
/*------------------------------------------------------------------------
    File        : via-serviceadapter.p
    Purpose     : Batching Sample via Service Adapter (Frontend)
  ----------------------------------------------------------------------*/

/* ***************************  Definitions  ************************** */

ROUTINE-LEVEL ON ERROR UNDO, THROW.

USING Consultingwerk.OERA.* FROM PROPATH.
 
{Consultingwerk/SmartComponentsDemo/OERA/Sports2000/dsCustomer.i}

DEFINE VARIABLE oRequest AS FetchDataRequest NO-UNDO . 

/* ***************************  Main Block  *************************** */

/* Prepare request parameter object with query string and batch size */
oRequest = NEW FetchDataRequest ("eCustomer", 
                                 "for each eCustomer where eCustomer.SalesRep = 'HXM' and eCustomer.CustNum < 1400", 
                                 15 /* batch size */) .

REPEAT:
    /* Perform request for a batch */
    ServiceInterface:FetchData ("Consultingwerk.SmartComponentsDemo.OERA.Sports2000.CustomerBusinessEntity",
                                oRequest,
                                OUTPUT DATASET dsCustomer) .
    
    /* Process batch data */
    FOR EACH eCustomer:
        DISPL eCustomer.CustNum 
              eCustomer.Name
              eCustomer.SalesRep .
    END.
    
    /* When a next batch is available, the NextContext contains
       the Context for the next request. Otherwise, leave here as
       there is no more data on the backend. */
    IF oRequest:NextContext > "" THEN 
        oRequest:Context = oRequest:NextContext .
    ELSE 
        LEAVE .  
END.