Creating APIs‎ > ‎Logic‎ > ‎

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. Logic libraries are JavaScript files of re-usable solutions for patterns. There is no limit to the number of libraries an API can use.

For more information about how to make a system library that API Creator includes available to JavaScript event programming and use it in JavaScript logic, see API Properties

Add a User Library to an API Project

Best Practice: There is cost in CPU and memory associated with each library you make available. Select only those libraries your API will use.

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.

For more information about how to add a JavaScript library to your API, see API Properties.

  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 User Library to 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 Live API Creator based on Jetty) The caliveapicreator/lib/ext directory.
  • (Tomcat) The tomcat/apache-tomcat-<version>/lib directory.

For more information about Tomcat installations, including how to leverage JAR files in APIs, see Install on Apache Tomcat.

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: Authentication Provider JavaScript Wrapper Function Example

The following JavaScript code snippet shows how to create an authentication provider in Live API Creator. This JavaScript wrapper function can call out to Java or other services:

function sendEmail(){ 
var result = {};
var msg = "error";
var configSetup = {
to  : "to" ,
from : "from",
title : "title",
text: "text"
};
result.configure = function configure(myconfig){
configSetup.to    = myconfig.to || "to";
configSetup.from  = myconfig.from || "from";
configSetup.title = myconfig.title || "title";
configSetup.text  = myconfig.text || "text";
 };
 
 result.send = function send(){
   try{
    //call my mail interface here
msg = "Send email (stub) to: " + configSetup.to + ", from: " + configSetup.from + " body text: "+ configSetup.text; 
} catch(e) {
return e;
}
return msg;
};
return result;
}

For more information about selecting an API-specific authentication providers, see Authentication Providers.

Call from an Event Rule

In the Demo API, the following is a commit event on the PurchaseOrder object, for the Insert operation:

var mail = sendEmail();
var config = {
to: 'user@gmail.com',
from: 'sales@acme.com',
title: 'This is an email test',
text: 'This is the email text body'
};
mail.configure(config);
log.debug(mail.send());

In the Log event viewer, the following message response displays:

Send email (stub) to....

For more information about how to call reusable solutions from rules, see the Business to Business Example.

Pattern: Library Initialization

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.

In this example, the moment.js library is used, as follows:

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 the 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.