Joget DX 8 Stable Released
The stable release for Joget DX 8 is now available, with a focus on UX and Governance.
In this Bean Shell code, we will make use of the JSON API Tool.
Before we move on, let's inspect how the plugin is configured first.
Figure 1: Configure JSON Tool
We will need to obtain the attributes of the configured plugn so that we can then programmatically configure it with coding.
One quick way to learn the attributes needed is to make use of the Process Builder's Advanced Tools > JSON Definition to observe the plugin's properties.
Figure 2: JSON Tool Attributes
{"jsonUrl":"http:\/\/localhost:8080\/jw\/test","requestType":"","headers":[],"noResponse":"","debugMode":"","formDefId":"","multirowBaseObject":"","fieldMapping":[],"wfVariableMapping":[]}
We can also check out the plugin property options source code of the plugin too 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.
Figure 3: Obtaining JSON Tool Attributes
With this information on hand, we 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();