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.- With your API open, in the Create section, clickAPI Properties.The Details tab appears by default.
- Click theLibrariestab.The Your libraries page appears by default. This page displays a list of your logic libraries.
- ClickCreate New Library.The Logic library window opens.
- Complete the following fields and then save your changes:NameEnter a name for your library.CodeThe Logic library window closes.Find your JavaScript library by clickingChoose File, finding and selecting your JavaScript library, and then clickingOpen.
- Define the library as available to JavaScript event programming and used in JavaScript logic by selecting theUsedcheckbox and then save your changes.TheUsedcheckbox 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.
- On the Your Libraries page, clickEditfor the library you want to export.The code editor expands to reveal more options.
- ClickExport Code.
- 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 ofCA Live API Creatorbased on Jetty) Thecaliveapicreator/lib/extdirectory.
- (Tomcat) Thetomcat/apache-tomcat-<version>/libdirectory.
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:
- About how to start API Server, see:
- (The self-contained, single user version ofCA Live API Creatorbased on Jetty) Install the Single-User Demonstration Package.
- (Tomcat) Install on Apache Tomcat.
- About how to add a Java library to your API by instantiating Java objects inside JavaScript, see JavaScript.
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 theUsedcheckbox for the library you want to define as available and then save your changes.
- To define a system library as available, complete the following:
- Click theSystem librariestab.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 theUsedcheckbox for these libraries.
- Select theUsedcheckbox 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.
- With your API open, in the Create section, clickAPI Properties.The Details tab displays by default.
- Click theLibrariestab.The Your libraries page appears by default.
- ClickChangefor the library you have updated.The Logic library dialog opens.
- In the Code field, clickChoose 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 objectsif (salesRep !== null) { var mail = B2B.sendEmail(); // this is a loadable library var config = { to: '[email protected]', // better, find this from DB from: '[email protected]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
call using a simpler approach. This useful pattern for initializing libraries is illustrated in the Business to Business example.configure
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.