In this tutorial, we will be following the guideline for 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. How 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.
- Datasource: Using custom datasource or Joget default datasource
- Custom JDBC Driver: The JDBC driver for custom datasource
- Custom JDBC URL: The JDBC connection URL for custom datasource
- Custom JDBC Username: The username for custom datasource
- Custom JDBC Password: The password for custom datasource
- SQL Query: The query to populate options.
- 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.
- SELECT id, name from app_fd_sample where group = ?
- 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 not using AJAX for cascading drop-down list.
5. Are there any resources/API that can be reused?
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 built by following this guideline.
The following tutorial is prepared with a Macbook Pro and Joget Source Code version 5.0.0. Please refer to the Guideline for developing a plugin article for other platform commands.
Let's say our folder directory is as follows.
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 is stored.
Run the following command to create a maven project in "plugins" directory.
Then, the shell script will ask us to key in a version number for the plugin and request for a confirmation before generating the maven project.
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 be using AppPluginUtil.getMessage method to support i18n and using constant variable MESSAGE_PATH for message resource bundle directory.
Then, we have to create a UI for admin user to provide inputs for our plugin. In getPropertyOptions method, we have already specify that our Plugin Properties Options definition file is located at "/properties/jdbcOptionsBinder.json". Let us create a directory "resources/properties" under "jdbc_options_binder/src/main" directory. After creating 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 will use "@@message.key@@" syntax to support i18n in our properties options.
In the Properties Options, we added a button for testing connection when using a custom datasource. This button will call a JSON API to do the test. So, our plugin will need to implement org.joget.plugin.base.PluginWebSupport interface to make it as a Web Service Plugin as well. Let's implement the webService method as follows, to test the JDBC connection.
Once we are done with the properties option to collect input and the web service to test the connection, we can work on the main method of the plugin which is the loadAjaxOptions method.
c. Manage the dependency libraries of your plugin
Our plugin is using dbcp, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse class, so we will need to add jsp-api and commons-dbcp library to our POM file.
d. Make your plugin internationalization (i18n) ready
We are using i18n message key in getLabel and getDescription method. We also used i18n message key in our properties options definition as well. So, we will need to create a message resource bundle properties file for our plugin.
Create directory "resources/messages" under "jdbc_options_binder/src/main" directory. Then, create a "JdbcOptionsBinder.properties" file in the folder. In the properties file, let us add all the message keys and labels as below.
e. Register your plugin to the Felix Framework
We will have to register our plugin class in Activator class (Auto generated in the same class package) to tell the Felix Framework that this is a plugin.
f. Build it and test
Let build our plugin. Once the building process is done, we will find that a "jdbc_options_binder-5.0.0.jar" file is created under "jdbc_options_binder/target" directory.
Then, let us upload the plugin jar to Manage Plugins. After uploading the jar file, double check that the plugin is uploaded and activated correctly.
Then, let us create an AJAX Cascading Drop-Down List in a form to test it. Let's create our test form as follows.
Then, configure our select box and JDBC binder.
In the query, we will use the following query to get the user list based on group id.
Configure the dependency to "group". Then, test the result.
The user select box options changed based on the selected values of group select box.
Now, let's change the query to the following to test the Cascading Drop-Down List without using AJAX.
Remember to un-tick the "Use AJAX for cascade options?" option to make it not use AJAX.
Yes, it works as well. Then, we can test the custom configuration and the test connection button.
8. Take a step further, share it or sell it
You can download the source code from jdbc_options_binder_src.zip.
To download the ready-to-use plugin jar, please find it in http://marketplace.joget.org/.