Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. The easiest, no-code approach is to make use of JSON Tool plugin itself. The JSON Tool itself is a Process Tool & Post Form Submission Processing Plugin. This means that we can invoke it from within a process flow or from submission of form.
  2. We can also write Bean Shell code. Here's a quick sample code to make http get call.

    Code Block
    languagejava
    titleBean Shell code to make restful API calls sample
    linenumberstrue
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpRequestBase;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import java.io.IOException;
    import org.joget.commons.util.LogUtil;
    
    try{
    	String jsonUrl = "http://sample.joget.com/apilocalhost:8080/jw/web/json/workflow/assignment/list/count?packageId=crm";
    	String name = "header1";
    	String value = "value1";
    
    	CloseableHttpClient client = null;
    	CloseableHttpClient client = HttpClients.createDefault();
    
    	HttpRequestBase request = null;
    	request = new HttpGet(jsonUrl);
    	request.setHeader(name, value);
    
    	HttpResponse response = client.execute(request);
    
    } catch (Exception ex) {
        LogUtil.error(getClass().getName(), ex, "");
    } finally {
        try {
            if (request != null) {
                request.releaseConnection();
            }
            if (client != null) {
                client.close();
            }
        } catch (IOException ex) {
            LogUtil.error(getClass().getName(), ex, "");
        }
    }

    We can execute this piece of code from various plugin types giving us the flexibility on where/when we want to invoke it. The only disadvantage compared to the former is that we need to maintain the custom coding ourselves instead of configuring through a plugin. These are the plugin types relevant to our solution to call the code from:-

    1. Bean Shell for Process Tool
    2. Bean Shell Validator
    3. Bean Shell Form Binder 
  3. We can only combine the both methods together by triggering the JSON process tool plugin in a beanshell code too. Here's a quick sample code written to be used in Bean Shell for Process Tool. Note that each plugin type would inject different variables for the Bean Shell code to consume. 

    Code Block
    languagejava
    titleCall JSON Tool plugin from Bean Shell code
    linenumberstrue
    import java.util.Map;
    import java.util.HashMap;
    import javax.servlet.http.HttpServletRequest;
    import org.joget.apps.app.model.AppDefinition;
    import org.joget.apps.app.service.AppPluginUtil;
    import org.joget.apps.app.service.AppUtil;
    import org.joget.plugin.base.ApplicationPlugin;
    import org.joget.plugin.base.Plugin;
    import org.joget.plugin.base.PluginManager;
    import org.joget.plugin.property.model.PropertyEditable;
    import org.joget.workflow.model.WorkflowAssignment;
      
    public Object execute(AppDefinition appDef, HttpServletRequest request, WorkflowAssignment workflowAssignment) {
        String jsonUrl = "http://localhost:8080/jw/web/json/workflow/assignment/list/count?packageId=crm";
         
        //Reuse Email Tool to send separated email to a list of users;
        Plugin plugin = pluginManager.getPlugin("org.joget.apps.app.lib.JsonTool");
        ApplicationPlugin jsonTool = (ApplicationPlugin) plugin;
    
        Map propertiesMap = new HashMap();
        propertiesMap.put("pluginManager", pluginManager);
        propertiesMap.put("appDef", appDef);
        propertiesMap.put("request", request);
        propertiesMap.put("workflowAssignment", workflowAssignment);
        
        //configure json tool plugin
        propertiesMap.put("jsonUrl", jsonUrl);
        propertiesMap.put("requestType", ""); //empty is for GET call
        propertiesMap.put("multirowBaseObject", "");
        propertiesMap.put("debugMode", "");
        propertiesMap.put("formDefId", "request");
        propertiesMap.put("headers", new Object[]{});
    
        List fieldMappings = new ArrayList();
    
        Map fieldMapping = new HashMap();
        fieldMapping.put("jsonObjectName", "total");
        fieldMapping.put("field", "day");
        fieldMappings.add(fieldMapping);
    
        //repeat this code to add more row
        // fieldMapping = new HashMap();
        // fieldMapping.put("jsonObjectName", "jsonAttrName");
        // fieldMapping.put("field", "formFieldId");
        // fieldMappings.add(fieldMapping);
    
        propertiesMap.put("fieldMapping", fieldMappings.toArray());
             
        //set properties and execute the tool
        ((PropertyEditable) jsonTool).setProperties(propertiesMap);
        jsonTool.execute(propertiesMap);
         
        return null;
    }
      
    //call execute method with injected variable
    return execute(appDef, request, workflowAssignment);

...