Versions Compared

Key

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

Table of Contents

 

In this tutorial, we will following the guideline of developing a plugin to develop our JDBC Options Binder plugin. Please also refer to the very first tutorial How to develop a Bean Shell Hash Variable for more details steps.

1. What is the problem?

Sometime, we may need to write some custom query to populate the options for our multi options field.

2. What is your idea to solve the problem?

Joget Workflow has provided a plugin type called Form Options Binder Plugin. We will develop one to support JDBC connection and custom query.

3. What is the input needed for your plugin?

To develop a JDBC Options binder, we will need the JDBC connection setting and also the custom query to populate the options.

  1. Datasource: Using custom datasource or Joget default datasource
  2. Custom JDBC Driver: The JDBC driver for custom datasource
  3. Custom JDBC URL: The JDBC connection URL for custom datasource
  4. Custom JDBC Username: The username for custom datasource
  5. Custom JDBC Password: The password for custom datasource
  6. SQL Query: The query to populate options. 
  7. Use Ajax: A checkbox to decide whether or not it is using AJAX to load options. (For AJAX Cascading Drop-Down List)

The query should also support a syntax to inject dependency values when using AJAX.

Example:

  1. SELECT id, name from app_fd_sample where group = ?
  2. SELECT id, name from app_fd_sample where group in (?)

 

4. What is the output and expected outcome of your plugin?

The first column of returned JDBC result will be the value of the option and second column is the label of the option. There will be another optional third column for grouping when no using AJAX for cascading drop-down list.

 

5. Is there any resources/API that can be reuse?

We can refer to the implementation of other available Form Options Binder plugins. Joget default datasource can be retrieve with AppUtil.getApplicationContext().getBean("setupDataSource").

 

6. Prepare your development environment

 

We need to always have our Joget Workflow Source Code ready and builded by following this guideline

 

The following of this tutorial is prepared with a Macbook Pro and Joget Source Code version 5.0.0. Please refer to Guideline of developing a plugin for other platform command.

 

Let said our folder directory as following. 

 

Code Block
- Home
  - joget
    - plugins
    - jw-community
      -5.0.0

 

The "plugins" directory is the folder we will create and store all our plugins and the "jw-community" directory is where the Joget Workflow Source code stored.

 

Run the following command to create a maven project in "plugins" directory.

 

Code Block
languagebash
cd joget/plugins/
~/joget/jw-community/5.0.0/wflow-plugin-archetype/create-plugin.sh org.joget.tutorial jdbc_options_binder 5.0.0

 

Then, the shell script will ask us to key in a version for your plugin and ask us for confirmation before generate the maven project.

 

Code Block
languagebash
Define value for property 'version':  1.0-SNAPSHOT: : 5.0.0
[INFO] Using property: package = org.joget.tutorial
Confirm properties configuration:
groupId: org.joget.tutorial
artifactId: jdbc_options_binder
version: 5.0.0
package: org.joget.tutorial
Y: : y

 

We should get "BUILD SUCCESS" message shown in our terminal and a "jdbc_options_binder" folder created in "plugins" folder.

 

Open the maven project with your favour IDE. I will be using NetBeans.  

 

7. Just code it!

 

a. Extending the abstract class of a plugin type

 

Create a "JdbcOptionsBinder" class under "org.joget.tutorial" package. Then, extend the class with org.joget.apps.form.model.FormBinder abstract class. To make it work as a Form Options Binder, we will need to implement org.joget.apps.form.model.FormLoadOptionsBinder interface. We would like to support AJAX Cascading Drop-Down List as well, so we need to implement org.joget.apps.form.model.FormAjaxOptionsBinder interface also. Please refer to Form Options Binder Plugin.

 

b. Implement all the abstract methods

 

As usual, we have to implement all the abstract methods. We will using AppPluginUtil.getMessage method to support i18n and using constant variable MESSAGE_PATH for message resource bundle directory.

 

Code Block
languagejava
titleImplementation of all basic abstract methods
collapsetrue
package org.joget.tutorial;
 
import org.joget.apps.app.service.AppPluginUtil;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormAjaxOptionsBinder;
import org.joget.apps.form.model.FormBinder;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormLoadOptionsBinder;
import org.joget.apps.form.model.FormRowSet;
 
public class JdbcOptionsBinder extends FormBinder implements FormLoadOptionsBinder, FormAjaxOptionsBinder {
    
    private final static String MESSAGE_PATH = "messages/JdbcOptionsBinder";
    
    public String getName() {
        return "JDBC Option Binder";
    }
 
    public String getVersion() {
        return "5.0.0";
    }
    
    public String getClassName() {
        return getClass().getName();
    }
 
    public String getLabel() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.tutorial.JdbcOptionsBinder.pluginLabel", getClassName(), MESSAGE_PATH);
    }
    
    public String getDescription() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.tutorial.JdbcOptionsBinder.pluginDesc", getClassName(), MESSAGE_PATH);
    }
 
    public String getPropertyOptions() {
        return AppUtil.readPluginResource(getClassName(), "/properties/jdbcOptionsBinder.json", null, true, MESSAGE_PATH);
    }

    public FormRowSet load(Element element, String primaryKey, FormData formData) {
        return loadAjaxOptions(null); // reuse loadAjaxOptions method
    }
 
    public boolean useAjax() {
        return "true".equalsIgnoreCase(getPropertyString("useAjax")); // let user to decide whether or not to use ajax for dependency field
    }
 
    public FormRowSet loadAjaxOptions(String[] dependencyValues) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

 

Then, we have to do a UI for admin user to provide inputs for our plugin. In getPropertyOptions method, we already specify our Plugin Properties Options definition file is locate at "/properties/jdbcOptionsBinder.json". Let us create a directory "resources/properties" under "jdbc_options_binder/src/main" directory. After create the directory, create a file named "jdbcOptionsBinder.json" in the "properties" folder.

 

In the properties definition options file, we will need to provide options as below. Please note that we can use "@@message.key@@" syntax to support i18n in our properties options.