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. API Creator includes a standard set of libraries. Logic libraries are JavaScript files of re-usable solutions for patterns. There is no limit to the number of libraries an API can use.
lac31
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. API Creator includes a standard set of libraries. Logic libraries are JavaScript files of re-usable solutions for patterns. There is no limit to the number of libraries an API can use.
Add a JavaScript Library to an API
You can add only JavaScript libraries to your API using API Creator. After you have added a library to your API, you can use 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 that you make available. Select only those libraries your API will use.
  1. With your API open, go to the Create, API Properties, Libraries, Your libraries tab, and click 
    Create New Library
    .
    The Logic library window opens.
  2. 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
    , find and select your JavaScript library, and then clicking 
    Open
    .
    The Logic library window closes.
  3. Enable/activate the library inside your API by selecting the 
    Used
     checkbox and then save your changes.
The library is added to your API.
Add a Java Library to
CA Live API Creator
You can leverage 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 
    t
    omcat/apache-tomcat-<version>/
    lib
     directory.
    For more information about Tomcat installations, including how to leverage JAR files in APIs, see Install on Apache Tomcat
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.
Define the Available Libraries
The JavaScript libraries for your API are displayed on the Create, API Properties, Libraries, System libraries tab. The first few libraries listed on this tab are selected and available by default. You cannot clear the 
Used
 checkbox for those libraries. You can make the other libraries listed available to JavaScript event programming and use them in JavaScript logic by selecting the 
Used
 checkbox for those libraries. 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. Select only those libraries your API will use.
Reload an Updated Library
If you update your library code, you must reload it.
  1. With your API open, go to the Create, API Properties, Libraries, Your libraries tab, and click
    Change
     for the revised library.The Logic Library dialog opens.
  2. Click
    Choose File
    , select your altered code, and save your changes.
Load Libraries: B2B sendEmail Function Example
The following JavaScript code snippet in the B2B Derby NW API shows the
sendEmail
 JavaScript wrapper function. This function can call out to Java or other services:
Screen Shot 2016-11-21 at 7.07.11 PM.png
Call Libraries from Event Rules
The following is a commit event in the Business to Business example. 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: '[email protected]', // better, find this from DB
    from: '[email protected]',
    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);
}
For more information about how to call reusable solutions from rules, see the Business to Business Example.
 
You can avoid the configure call using a simpler approach. This useful pattern for initializing libraries is illustrated in the Business to Business example.
Load Libraries: moment.js Library Example
In this example, you have a table called Orders, with two attributes:
order_date
and
shipping_date
. You would like to have a computed attribute called
processing_delay
showing the time elapsed between these two dates. You can use the
moment.js
library or use Java objects. The
moment.js
library is a JavaScript date library for parsing, validating, manipulating, and formatting dates.
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 a library's facilities in your rules by invoking methods inside classes directly from 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.