Skip to Content

In odata – > soap, delta token operation is supported provided there is a web service operation which can return the delta token and tombstone values. The operation must take an input parameter and return all values added or removed after this value

Steps from Design Time tool

  1. Create an OData Service Implementation Project
  2. Create an OData Model for getProduct operation, which can have properties ProductID, ProductName, ProductDescription,,SupplierId,isDeleted

Img1.jpg

    3. Right click on odatasvc and choose Select data source

    4. Select the entity set and choose the query CRUD operation. Select the data source SOAP Service.

Img2.jpg

    5. Specify the wsdl file and choose the getProducts operation from the list of soap operations and click on finish

Img3.jpg

    6. Right click on Query and select Define Custom Code

Img4.jpg

    7. Select the script type as either javascript or groovy script

Img5.jpg

     8. The delta token value has to be fetched from the odata request uri and this value must be sent to the web service operation as an input parameter. A hashmap must be created in processRequestData function with key being key of entity set and value being delta token value passed in uri. This must be set to message body.


function processRequestData(message) {
  importPackage(com.sap.gateway.ip.core.customdev.util);
  importPackage(java.util);
  importPackage(com.sap.gateway.core.ip.component.commons);
  var odataMethod = message.getHeaders().get(ODataExchangeHeaderProperty.ODataMethod.toString());
  if (odataMethod == "GET_FEED") {
  map = new HashMap();
  map = getUriInfo(message).getCustomQueryOptions();
  var deltaToken = map.get("!deltatoken");
  var childMap1 = new LinkedHashMap();
  if(deltaToken!=null)
  {
  childMap1.put("ProductId", deltaToken);
  }
  else
  {
  childMap1.put("ProductId", "0");
  }
  message.setBody(childMap1);
  }
  return message;
}


function getUriInfo(message) {
  importPackage(org.apache.olingo.odata2.api.uri);
  importPackage(com.sap.gateway.core.ip.component.commons);
  var uriInfo = message.getHeaders().get(ODataExchangeHeaderProperty.UriInfo.toString());
  return uriInfo;
}

If the odata request is https://localhost:8083/gateway/odata/SAP/DELTATOKEN;v=1/ProductSet?!deltatoken=10,

Then the soap request body looks like

   <soapenv:Body>
      <tes:getProductsBasedOnDeltaToken>
         <tes:ProductId>10</tes:ProductId>
      </tes:getProductsBasedOnDeltaToken>
   </soapenv:Body>

    9. Once the web service response is returned, the response includes both added entries and deleted entries after the delta token value. This has to be split into 2 result sets one having added entries and one having deleted entries in processResponseData function. Also, the next delta token value must also be set.


function processResponseData(message) {
  importPackage(com.sap.gateway.ip.core.customdev.util);
  importPackage(org.apache.olingo.odata2.api.edm);
  var uriInfo = getUriInfo(message);
  var entitySet = uriInfo.getStartEntitySet();
  deltaTokenAndTombStoneHandling(message, entitySet);
  return message;
}

Here a column isDeleted is used to distinguish added and deleted entries. There can be different implementations possible to recognize added and deleted entries

Deleted entries must be set to message header and added entries must be set to message body. The next del token value must be set to message header.


function deltaTokenAndTombStoneHandling(message, entitySet) {
  importPackage(java.util);
  deletedItems = new ArrayList();
  resultEntities = new ArrayList();
  var columnName = "ISDELETED";
  dataSourceResponse = new ArrayList();
  dataSourceResponse = message.getBody();
  customMap = new HashMap();
  customMap = getUriInfo(message).getCustomQueryOptions();
  deltaToken = customMap.get("!deltatoken");
importPackage(com.sap.gateway.core.ip.component.commons);
  if (deltaToken != null && deltaToken.length() > 0) {
  for (var i = 0; i < dataSourceResponse.size(); i++) {
  if (dataSourceResponse.get(i).get(columnName).toString() == "true")
  deletedItems.add(dataSourceResponse.get(i));
  else
  resultEntities.add(dataSourceResponse.get(i));
  }
  importPackage(com.sap.gateway.ip.core.customdev.api);
  message.setHeader(
  ODataCamelExchangeHeaders.DELETED_ENTITIES.toString(),
  deletedItems);
  message.setHeader(
  ODataExchangeHeaderProperty.ODataResponseType.toString(),
  ODataResponseType.DeltaResponseMap);
  deltaResponse = new HashMap();
  deltaResponse.put(
  ODataExchangeBodyDeltaResponseMapProperty.Properties
  .toString(), resultEntities);
  deltaResponse
  .put(ODataExchangeBodyDeltaResponseMapProperty.DeletedEntities
  .toString(), deletedItems);
  message.setBody(deltaResponse);
  }
  importPackage(com.sap.gateway.ip.core.customdev.api);
  message.setHeader(
  ODataCamelExchangeHeaders.IS_DELTA_IMPLEMENTED.toString(), true);
  token = generateDeltaToken(dataSourceResponse, message, "Products",
  "PRODUCTID", entitySet);
  message.setHeader(ODataExchangeHeaderProperty.DeltaToken.toString(), token);
}
function generateDeltaToken(jdbcResponse, message, entitySetName, columnName, entitySet) {
  if (entitySet.getName() == entitySetName) {
  tokenBuilder = "";
  tokenBuilder = calculateDeltaToken(jdbcResponse, tokenBuilder,
  columnName);
  return tokenBuilder.toString();
  } else
  return null;
}
function calculateDeltaToken(response, token, columnName) {
  var currentTokenValue = 0;
  var highestTokenValue = 0;
  importPackage(java.util);
  for (var i = 0; i < response.size(); i++) {
  if (response.get(i).get(columnName) != null) {
  currentTokenValue = response.get(i).get(columnName);
  if (currentTokenValue > highestTokenValue)
  highestTokenValue = currentTokenValue;
  }
  }
  token += highestTokenValue.toString();
  return token;
}

     10. Right click on Query and select Define Response Mapping

Img10.jpg

    11. Do the mapping as below

Img11.jpg

    12. Right Click on Project and select Generate and Deploy Integration Content. This will deploy the bundle.

Now fire an OData Request https://localhost:8083/gateway/odata/SAP/DELTATOKEN;v=1/ProductSet?!deltatoken=10 on the browser and response will give the list of changed products (either delta token values or tombstone values) greater than productId 10 and also give the next delta token value

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply