Alan Varghese
Alan Varghese

Reputation: 3

Sample Code to Call Odata Service from Java for onprem SAP

    /**
 * Calls {@code GET API_SALES_ORDER_SRV/A_SalesOrder} through
 * {@link FluentHelperRead} to get the SalesOrder events expanded to sales
 * order items and filtered by {@code keys} list
 *
 * @param keys
 *          the list of sales orders IDs to be fetched
 * @return the list of sales orders or an empty list if {@code keys} is empty
 *
 * @throws ODataException
 *           in case the request was not successful
 * @throws IllegalArgumentException
 *           in case {@code keys} is null
 *
 * @see //ProcessSalesOrderService#getAllSalesOrder()
 * @see <a href=
 *    "https://api.sap.com/shell/discover/contentpackage/SAPS4HANACloud/api/API_SALES_ORDER_SRV?resource=A_SalesOrder&operation=get_A_SalesOrder">SAP
 *    API Business Hub</a> for details of
 *    {@code GET API_SALES_ORDER_SRV/A_SalesOrder} endpoint
 */
public List<SalesOrderHeader> getByKeys(@NonNull Collection<String> keys) throws IllegalArgumentException, ODataException {
    if (keys.size() == 0) {
        return Collections.emptyList();
    }
    
    
    // create OData $filter with all keys 
    final ExpressionFluentHelper<SalesOrderHeader> filter = keys.stream()
            .map(key -> SalesOrderHeader.SALES_ORDER.eq(key))
            .reduce(ExpressionFluentHelper::or)
            .get();

    try {
        HttpDestinationProperties destinationprop = null;
        return salesOrderService.getAllSalesOrder()
            .select(SalesOrderHeader.ALL_FIELDS, SalesOrderHeader.TO_ITEM)
            .filter(filter)
            .execute(destinationprop );
    } catch (com.sap.cloud.sdk.odatav2.connectivity.ODataException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

I found the above sample code but since I am new to SAP and my system is an OnPrem version deployed on AWS. I am not sure how to pass the HttpDestinationProperties destinationprop Further that method seems to be deprecated. I am looking for some sample code to call the Sales order Odata Service using the code I generated using the instructions provided. I generated the code using a maven plugin. https://sap.github.io/cloud-sdk/docs/java/features/odata/generate-typed-odata-v2-and-v4-client-for-java I am using the odata V2 Plugin

Upvotes: 0

Views: 1796

Answers (1)

Alexander D&#252;mont
Alexander D&#252;mont

Reputation: 938

Usually you would use the SAP BTP Cloud Foundry Destination Service and Connectivity Service. Make sure to also have a Cloud Connector connected to your sub-account, it will act as reverse-proxy to the On-Premise system. You bind the destination service (e.g. plan lite) as well as the connectivity service (e.g. plan lite) to your application.

Once the prerequisites are met, you can use the SAP Cloud SDK code to resolve destinations:

// General API usage:
Destination destination = DestinationAccessor.getDestination("my-destination");

// Cast to HTTP destination:
HttpDestination httpDestination = destination.asHttp(); 

// Apply dedicated ERP logic for SAP S/4HANA
HttpDestination erpHttpDestination = httpDestination.decorate(DefaultErpHttpDestination::new);

(Of course you can boil it down to a one-liner.)

The OData call itself can be called like the following without try-catch:

return salesOrderService.getAllSalesOrder()
  .select(SalesOrderHeader.ALL_FIELDS, SalesOrderHeader.TO_ITEM)
  .filter(filter)
  .executeRequest( erpHttpDestination );

That's it!

PS.: If you want to define your destination in static code, that would be possible with the DefaultErpHttpDestination.builder("http://your-proxy-host:44300") API. However this is obviously not best-practice.

Upvotes: 2

Related Questions