Logic Libraries

When you write rules using JavaScript, you will often benefit from using pre-existing libraries. A pre-existing library can be a JavaScript or Java library. 
lac311
When you write rules using JavaScript, you will often benefit from using pre-existing libraries. A pre-existing library can be a JavaScript or Java library. 
Logic libraries are JavaScript files of re-usable solutions for patterns. System libraries are the standard set of logic libraries that API Creator includes. Your API can reference these libraries. You can view the system libraries on the System libraries page.
In this article:
Add JavaScript Libraries to APIs
You can add only JavaScript libraries to your API using API Creator. After you have added a library to your API, you can reference its facilities in your rules by invoking classes and methods directly from JavaScript.
Best Practice:
There is cost in CPU and memory associated with each library you make available. Select only those libraries your API references.
  1.  With your API open, in the Create section, click
    API Properties
    .
    The Details tab appears by default.
  2. Click the 
    Libraries
    tab.
    The Your libraries page appears by default. This page displays a list of your logic libraries.
  3. Click 
    Create New Library
    .
    The Logic library window opens.
  4. Complete the following fields and then save your changes:
    Name
    Enter a name for your library.
    Code
    Find your JavaScript library by clicking 
    Choose File
    , finding and selecting your JavaScript library, and then clicking 
    Open
    .
    The Logic library window closes.
  5. Define the library as available to JavaScript event programming and used in JavaScript logic by selecting the 
    Used
     checkbox and then save your changes.
    The 
    Used
     checkbox must be selected so that you can reference the functions defined within the library.
The JavaScript library is added to your API.
Export JavaScript Libraries
You can export your JavaScript libraries so that you can import them and reference them in other APIs.
  1. On the Your Libraries page, click 
    Edit
    for the library you want to export.
    The code editor expands to reveal more options.
  2. Click 
    Export Code
    .
  3. Save your changes.
The JavaScript library is exported as a JavaScript text file.
Add Java Libraries
You can use Java JAR files in your APIs. Before you start API Server, copy your Java JAR files into the following directory:
  • (The self-contained, single user version of
    CA Live API Creator
    based on Jetty) The
    caliveapicreator/lib/ext
     directory.
  • (Tomcat) The 
    tomcat/apache-tomcat-<version>/lib
     directory.
If you have installed 
CA Live API Creator
 as a Docker container, see the information about how to load libraries at container startup in Install as a Docker Container.
For more information:
Define the Available Libraries
You can define a library as available to JavaScript event programming and use it in JavaScript logic. For example, many APIs use date arithmetic. The 
Moment.js
 library is a JavaScript date library for parsing, validating, manipulating, and formatting dates. You can make this library available inside your API.
Best Practice:
 There is cost in CPU and memory associated with each library you make available. Define only those libraries your API will reference as available.
Do one of the following:
  • On the Your libraries page, select the 
    Used
     checkbox for the library you want to define as available and then save your changes.
  • To define a system library as available, complete the following:
    1. Click the 
      System libraries
       tab.
      The System libraries page appears, displaying the JavaScript libraries for your API.
      The first several libraries that are listed are selected and available by default. You cannot clear the 
      Used
       checkbox for these libraries.
    2. Select the 
      Used
       checkbox for the library you want to define as available and then save your changes.
The library is defined as available.
Reload Updated Libraries
If you update your library code, reload the updated library into your API.
  1. With your API open, in the Create section, click
    API Properties
    .
    The Details tab displays by default.
  2. Click the 
    Libraries
     tab.
    The Your libraries page appears by default.
  3. Click
    Change
     for the library you have updated.The Logic library dialog opens.
  4. In the Code field, click 
    Choose File
    , select the updated JavaScript file, and then save your changes.
The updated library is reloaded into your API.
Load Libraries: B2B sendToWebHook Function Example
You can view the JavaScript code in the Business to Business (B2B) sample B2B Northwind API. The
TransformToWebHook
logic library includes the 
sendToWebHook
 JavaScript wrapper function. This function can call out to Java or other services.
For more information about this function and library, see Business to Business Example.
Call Libraries from Event Rules
The following is a commit event in the B2B sample. This commit event is on the Orders object for the INSERT operation:
var salesRep = row.EmployeeID; // object model (row) provides accessors to related objects
if (salesRep !== null) {
  var mail = B2B.sendEmail(); // this is a loadable library
  var config = {
    to: 'Andrew.Fuller@acme.com', // better, find this from DB
    from: 'sales@acme.com',
    title: 'Congratulations on your new Order',
    text: 'An order was placed for you, totaling ' + row.AmountTotal
  };
  mail.configure(config);
  var msg = mail.send();
  logicContext.logDebug('Sending Mail - msg: ' + msg);
}
You can avoid the
configure
call using a simpler approach. This useful pattern for initializing libraries is illustrated in the Business to Business example.
For more information about how to call reusable solutions from rules, see Business to Business Example.
Load Libraries: moment.js Library Example
In this example, you have an Orders table with the 
order_date
 and 
shipping_date
 date attributes. You would like the 
processing_delay
 computed attribute to show the time that is elapsed between these two dates. You can use the 
moment.js
 library–a JavaScript date library for parsing, validating, manipulating, and formatting dates–or use Java objects.
The following example uses the 
moment.js
 library:
var orderDate = currentBean.order_date.getTime();
var shippingDate = currentBean.shipping_date.getTime();
currentBean.processing_delay = moment(orderData).from(shippingData, true);
For more information about the
moment.js
library, see the moment.js site.
Use Library Facilities in your Rules
Prerequisite:
You have added a library to your API.
You can use library facilities in your rules by invoking methods inside classes from your JavaScript.
Example:
The following 
myClass
 Java class contains the 
add()
 method:
public class MyClass {
  public static int add(int a, int b) {
    return a+b;
  }
}
You can invoke the 
add()
 method by adding the following code snippet to your JavaScript with the 
Java.type
 keyword:
var MyClassObject = Java.type("MyClass");
var myResult = MyClassObject.add(1,2);
If your Java class is part of a package, for example
foo.bar.MyClass
, then provide the full name of the class while accessing the 
Class
 object.
For more information about using Java inside JavaScript, see JavaScript.