Working with Java

Java is a cross-platform programming language. A special set of external libraries has been developed to provide work with Java in Foresight Analytics Platform. These libraries allow for working with Foresight Analytics Platform core. Java does not support working with Fore forms, components and dialog boxes available in Foresight Analytics Platform. To create user interface, use Java resources.

Connecting Assemblies

To connect an external assembly, enter the structure of the Java project to be developed, go to the Modules tab, click the button and select the Library > Java item. In the dialog box that opens go to the folder with installed Foresight Analytics Platform and select the ..\Interops\java\ JAR file with the external assembly that corresponds with the required Fore assembly. Confirm the selection in the assembly configuration dialog box. In the project structure dialog box select the checkboxes next to the added assembly and click the Apply button. After this the resources of the added external assembly can be used in the Java code.

If multiple assemblies are to be used, you can add the path to the libraries folder instead of adding each JAR file separately. To do this, click the button, select the JARs or Directories item and specify the path to the folder with JAR files, for example, C:\Program Files\Foresight\Foresight Analytics Platform 10.x\Interops\java\.

In the code enter the string with type import from the external assembly in one of the following formats:

import foresight.<assembly name>.*; //Import specified assembly contents
import foresight.<assembly name>.<type name>.*; //Import specified class/interface contents

NOTE. If development is to be executed in the development environments that do not support specifying of the path to external JAR files, then specify this path in the Path system variable.

Converting Code

On creating external builds to be used in Java, system classes, interfaces, enumerations and other types are specifically transformed. Therefore, take into account the following changes when creating a Java code using Foresight Analytics Platform resources:

  1. All methods have two call options:

  2. Properties have corresponding methods in the following formats:

  3. Classes names are added with C, for example: the Fore MetabaseManagerFactory class corresponds to the Java CMetabaseManagerFactory class.

  4. To create an object of the required class, use the structure: <Class name>.Create() or <Class name>.Create<interface name>() without the new keyword.

  5. The enumeration elements are called in a standard way: <Enumeration name>.<Enumeration element name>. If it is required to cast the enumeration element to integer type, use the swigValue() method.

  6. To cast types, create an object of the required type via "new" and pass the object as a parameter, for example:

ICredentials creds;
...
IPasswordCredentials pswdCreds = new IPasswordCredentials(creds);

Example

The example of connecting to repository in the Java code:

import foresight.metabase.*; //Import entire assembly contents
 
public class Main {
    public static void main(String[] args) {
        IMetabaseManager mbMan = CMetabaseManagerFactory.Create().getActive();
        IMetabaseDefinitions mbDefs = mbMan.getDefinitions();
        mbDefs.ReadFromRegistry();
        IMetabaseDefinition mbDef = mbDefs.doFindById("FPRepository");
        ISecurityPackage pack = mbDef.Packs.FindById(mbDef.get_SecurityPackage()).Package;
        ICredentials creds = pack.doCreateCredentials(AuthenticationMode.amPassword);
        IPasswordCredentials pswdCreds = new IPasswordCredentials(creds);
        pswdCreds.put_UserName("user");
        pswdCreds.put_Password("password");
        IMetabase mb = mbDef.doOpenDefault(pswdCreds);
        // Display repository name
        System.out.println(mb.getName());
    }
}

See also:

Using Foresight Analytics Platform Resources in Third-Party Applications