Invoke JSON Tool Plugin Programmatically with Bean Shell

Introduction

In the Bean Shell plugin, you can programmatically call other Joget plugins to perform a specific task without the need to rewrite specific functionality that existing plugins may have already addressed. This allows you to automate processes and integrate with external systems efficiently.

How does it work?

This guide will teach you how to configure and invoke the JSON Tool plugin in Joget programmatically. The steps involve obtaining the necessary attributes, setting up the configuration, and executing the plugin using Bean Shell scripting.

In this Bean Shell code, you will use the JSON API Tool.

Before moving on, you inspect how the plugin is configured.

You will need to obtain the attributes of the configured plugin to programmatically configure it with coding.

One quick way to learn the necessary attributes is to observe the plugin's properties using the Process Builder's Advanced Tools > JSON Definition.

Plugin Properties
{"jsonUrl":"http:\/\/localhost:8080\/jw\/test","requestType":"","headers":[],"noResponse":"","debugMode":"","formDefId":"","multirowBaseObject":"","fieldMapping":[],"wfVariableMapping":[]}

You can also check out the plugin's property options source code at https://github.com/jogetworkflow/jw-community/blob/8.0-SNAPSHOT/wflow-core/src/main/resources/properties/app/jsonTool.json or by inspecting the DOM elements in the JSON Tool configuration screen.

With this information, you can now configure the plugin through coding. Configurations are made in the propertiesMap variable in the sample code below.

Sample code:

import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.joget.apps.app.service.AppUtil;
import org.joget.plugin.base.Plugin;
import org.joget.plugin.base.PluginManager;
import org.joget.workflow.util.WorkflowUtil;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppPluginUtil;
import org.joget.plugin.property.model.PropertyEditable;
import org.joget.workflow.model.service.WorkflowManager;
import org.joget.workflow.model.WorkflowProcess;
import org.joget.apps.app.service.AppUtil;
import org.joget.workflow.model.WorkflowProcessLink;
import org.joget.workflow.model.WorkflowProcessResult;
import org.joget.workflow.model.WorkflowActivity;
import org.joget.workflow.model.WorkflowVariable;
import org.joget.commons.util.LogUtil;
import org.joget.apps.app.lib.JsonTool;
 
public void callPlugin(){
    try {   
        PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
         
        //plugin to call
        String pluginName = "org.joget.apps.app.lib.JsonTool";
        Plugin plugin = pluginManager.getPlugin(pluginName);
        AppDefinition appDef = AppUtil.getCurrentAppDefinition();
         
        //JSON API to call to start a new process
        String jsonURL = "http://localhost:8080/jw/web/json/workflow/process/start/expenseclaim:latest:process1";
         
        //prepare workflow variables for the process
        List params = new ArrayList();
         
        Map param = new HashMap();
        param.put("name", "var_approval");
        param.put("value", "New");
        params.add(param);
 
        param = new HashMap();
        param.put("name", "var_SelectApprover");
        param.put("value", "clark");
        params.add(param);
         
        Object[] paramsArray = params.toArray();
         
        //prepare propertiesMap mapped specifically for JSON tool
        Map propertiesMap = new HashMap();
        propertiesMap.put("jsonUrl", jsonURL);
        propertiesMap.put("requestType", "post");
        propertiesMap.put("debugMode", "true");
        propertiesMap.put("headers", new Object[]{});
        //propertiesMap.put("params", new Object[]{ params });
        propertiesMap.put("params", paramsArray );
         
        //obtain default properties set, if any
        propertiesMap = AppPluginUtil.getDefaultProperties(plugin, propertiesMap, appDef, null);
 
        //set properties into the JSON tool programmatically
        if (plugin instanceof PropertyEditable) {
            ((PropertyEditable) plugin).setProperties(propertiesMap);
            LogUtil.info("migrateProcess", "set properties");
        }
         
        //invoke the JSON plugin
        plugin.execute(propertiesMap);
         
        LogUtil.info("callPlugin", "execution finished");
    } catch (Exception ex) {
        LogUtil.error("callPlugin", ex, "Error");
    }
}
 
callPlugin();

Once the script is configured, you can run it to invoke the JSON Tool plugin programmatically. The plugin will process the data as per the configured properties.

Created by Julieth Last modified by Aadrian on Dec 13, 2024