transformCurrentRow Method

transformCurrentRow Method
lac42
The 
logicContext
 object's 
transformCurrentRow()
 method transforms a table row to a resource instance as a JSON string. You can insert this method in the JavaScript code for your table event or event rule. This method provides transformation logic to select/rename attributes and related objects.
The
transformCurrentRow()
method plays a central role in a typical business-to-business interaction. This article illustrates the concepts of using this object using the Business to Business (B2B) sample.
For more information about the B2B sample, see B2B API Sample.
In this article:
2
The following diagram illustrates:
  1. Partner order.
     A partner receives an order.
  2. Business logic.
     
    CA Live API Creator
     processes the order by invoking business logic.
  3. ShipperAPIDef.
     
    CA Live API Creator
     sends a message to a shipper by way of the ShipperAPIDef resource.
B2B.png
Key Background
The following sections show how to transform a table row to a resource instance as a JSON string with a couple of resources and a few lines of code. The following are the underlying concepts that make this possible:
  • Row processing, particularly resource/object mapping, and logic.
For more information about row processing, see Row Objects.
  • The
    transformCurrentRow()
    method, which creates a resource instance (string) from a table row.
    The following image shows the workflow for the 
    transformCurrentRow()
     method:
Partner Order: "Catch" Webhook
You can view the
PartnerOrder
 resource in API Creator. The
PartnerOrder
resource contains multiple tables. The columns that we want are chosen/renamed. In addition to the choose/rename logic, there is additional interface logic: automatic key generation and lookup processing.
For more information about how to explicitly define resources using API Creator, see Customize your API.
Automatic Key Generation
The associated Order requires OrderIDs as foreign keys. But Partners cannot
 
provide OrderIDs. OrderIDs are internal implementation and generated by the database management system (DBMS) on INSERT.
CA Live API Creator
 provides automatic key generation services to obtain the OrderID and cascade the value into the Items objects. This process is automatic and based on schema information about keys and the resource join definition.
For more information about how 
CA Live API Creator
returns the key in update responses, see Automatic Key Generation.
Lookup Processing
Analogously, ProductIDs link Items to Products. But Partners should not need to know the internal implementation details. Partners can provide product names but not ProductIDs.
CA Live API Creator
 provides lookup processing to provide ProductIDs, given the product names. But even that does not go far enough. It is unreasonable to ask Partners to insert 
@metadata
tags. Instead, you can define request events that insert
@metadata
 tags in a re-usable fashion.
For more information:
Business Logic
As a part of row processing,
CA Live API Creator
 finds the relevant logic. For this transaction, the logic includes the
Process Shipper WebHook
commit event rule, as shown in the following image (of the
B2B Northwind
API sample):
Business Logic: "Throw" Webhook
You can view the
Process Shipper WebHook
 commit event rule in API Creator.
  1. With the
    B2B Northwind
     API sample open, in the Manage section, click
    Rules
    .
    A list of rules display on the Rules page.
  2. Click the
    Process Shipper WebHook
    commit event rule.
    The following image shows the commit event rule details:
    var shipper = row.Shippers; // object model (row) provides accessors to related objects
    if (shipper !== null) { // resource defines transform: cols, aliases, related objects
       var transform = logicContext.transformCurrentRow("ShipperAPIDef"); // find with code completion
       B2B.sendToWebHook(transform, shipper.webHookURL); // loadable library (API Properties > Libraries)
    }
ShipperAPIDef
In the code shown in the previous image, the
ShipperAPIDef
resource is yet another resource. Similar to the other resource, it defines what tables we want, what attributes, and how they are renamed. This is the "shape" of the POST we want to send to our shipper.
The
transformCurrentRow()
 method takes the current Order row (contained in the 
logicContext
object) and creates an instance of it per the ShipperAPIDef resource. So, as for the incoming PartnerOrder, resources define the mapping and transformation logic. The next line sends it to the address maintained in the shipper row.
transformCurrentRow operation
CA Live API Creator
 has access to the (table) row by way of the 
logicContext
 object's 
transformCurrentRow()
method. It uses this (and associated metadata) to retrieve the value of a specified resource using the
getResource()
 method.
The input row is passed by way of
logicContext
. It is a
table
row and 
not
the resource row from the incoming request. That ensures that
CA Live API Creator
can reuse your business logic over all resources defined over that table, and regardless of the attributes they contain.
CA Live API Creator
 retrieves the value by supplying the primary key from the row. The
getResource()
method performs the mapping and transformation logic. There may be little or no correlation between table rows and the resource definition.
For more information: