Logic Libraries

JavaScript invocations typically provide the row and logic context for your programmatic access. When you write rules in API Creator using JavaScript, you will often benefit from using libraries. Libraries are JavaScript or Java files of re-usable solutions for patterns. For example, libraries can include functions, such as date routines.
lac42
JavaScript invocations typically provide the row and logic context for your programmatic access. When you write rules in API Creator using JavaScript, you will often benefit from using libraries. Libraries are JavaScript or Java files of re-usable solutions for patterns. For example, libraries can include functions, such as date routines.
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.
Follow these steps:
 
  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 that are defined within the library.
The JavaScript library is added to your API.
Add Java Libraries
You can add a Java library to your API by instantiating Java objects inside JavaScript. 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
To use a library, you must define the library as available to JavaScript event programming and used 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 references as available.
System libraries are the standard set of logic libraries that API Creator includes. Your APIs share the system libraries. Your libraries are available on the Your libraries page.
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.
Export JavaScript Libraries
You can export your JavaScript libraries so that you can import them and can reference them in other APIs.
Follow these steps:
 
  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.
Reload Updated Libraries
If you update your library code, reload the updated library into your API.
Follow these steps:
 
  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 that 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.
Call Libraries from Event Rules: Commit Event Example
The following code snippet shows a commit event rule in the Business to Business (B2B) sample. This commit event rule 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 initialize your library from a commit even rule. For more information about how to call reusable solutions from rules, see B2B API Sample.
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.
Logic Libraries Best Practices
Define libraries as available to JavaScript event programming and used in JavaScript logic only for those that your API uses. There is CPU cost and memory associated with each library you mark as 
Used
.